<?php
include "include/db.php";
include "include/auth.php";
if($user_type == 'admin'){
    $active_user_id = "admin_id = $admin_id,";
}elseif($user_type == 'staff'){
    $active_user_id = "employee_id = $employee_id,";
}
if((isset($_REQUEST['bid_wo_no']) && isset($_REQUEST['prop_case_num']) && isset($_REQUEST['prop_full_add']) && isset($_REQUEST['prop_state'])&& isset($_REQUEST['prop_city'])&& isset($_REQUEST['prop_zip'])&& isset($_REQUEST['new_bid_client_name'])) || (isset($_REQUEST['bid_wo_no']) && isset($_REQUEST['prop_id'])&& isset($_REQUEST['new_bid_client_name']))){

    if((strlen($_REQUEST['prop_case_num'])>0 && strlen($_REQUEST['prop_full_add'])>0 && strlen($_REQUEST['prop_state'])>0 && strlen($_REQUEST['prop_city'])>0 && strlen($_REQUEST['prop_zip'])>0)){
        $prop_case_num = trim($_REQUEST['prop_case_num']);
        $prop_loan_num = $prop_case_num;
        $prop_full_add = $_REQUEST['prop_full_add'];
        $prop_city = $_REQUEST['prop_city'];
        $prop_state = $_REQUEST['prop_state'];
        $prop_zip = $_REQUEST['prop_zip'];
        if(isset($_REQUEST['prop_county'])){
            if (strlen($_REQUEST['prop_county'])>0){
                $prop_county = $_REQUEST['prop_county'];
            }else{
                $prop_county = '';
            }        
        }else{
            $prop_county = '';
        }       

        $time = date("Y-m-d H:i:s");

        $query_check_prop = "SELECT * FROM property WHERE (prop_case_num = '$prop_case_num' AND prop_loan_num = '$prop_loan_num') OR (prop_case_num = '$prop_case_num') OR (prop_loan_num = '$prop_loan_num')";

        $run_check_prop = mysqli_query($conn, $query_check_prop);

            if(mysqli_num_rows($run_check_prop) == 0){
                $query_prop_insert = "INSERT INTO property SET prop_case_num = '$prop_case_num', prop_loan_num = '$prop_loan_num', prop_full_add = '$prop_full_add', prop_city = '$prop_city', prop_state = '$prop_state', prop_zip = '$prop_zip', prop_county = '$prop_county', create_time = '$time', is_approve = 1, $active_user_id approve_time = '$time'";

                $run_prop_insert = mysqli_query($conn, $query_prop_insert);
                if(mysqli_affected_rows($conn) > 0){
                    $prop_id = mysqli_insert_id($conn);
                    $successmessage = "$prop_case_num has been added. Use it for new Work Order";
                    if(strlen($_REQUEST['new_bid_client_name'])>0){
                        $p_client_id = $_REQUEST['new_bid_client_name'];
                   
                        $time = date("Y-m-d H:i:s");
                    
                        $p_lot_size = '';
                        
                        $p_sf = $_REQUEST['p_sf']; 
                        
                        $p_bedroom = '';
                        
                        $p_bathroom = '';

                        $check1 = "SELECT * FROM prop_assign WHERE prop_id = $property_id AND client_id = $p_client_id";
                        $run_check1 = mysqli_query($conn, $check1);
                
                        if(mysqli_num_rows($run_check1) == 0){
                            $query_insert_prop_assign = "INSERT INTO prop_assign SET client_id = $p_client_id, prop_id = $prop_id, p_lot_size = '$p_lot_size', p_sf = '$p_sf', p_bedroom = '$p_bedroom', p_bathroom = '$p_bathroom', $active_user_id is_approve = 1, approve_time = '$time', create_time = '$time'";

                            $run_insert_prop_assign = mysqli_query($conn,$query_insert_prop_assign);
                
                
                            if(mysqli_affected_rows($conn) > 0){
                
                                $successmessage = "New Client Added to this property.";
                        
                            }else{
                                $message = "Something went wrong. Try Again";                            
                
                            }     
                            
                        }else{
                            $message = "That Client Already exists in this property ID";
                            
                        }

                    }
                    

                }
            }else{
                $message = 'That Property Case or Loan Number already exists.';

        }

    }

    if(isset($_REQUEST['prop_id'])){
        if(strlen($_REQUEST['prop_id'])>0){
            $prop_id = $_REQUEST['prop_id'];
        }
    }

    if(strlen($prop_id)>0 && strlen($_REQUEST['bid_wo_no'])>0 && strlen($_REQUEST['new_bid_client_name'])>0){

        $bid_wo_no = trim($_REQUEST['bid_wo_no']);
        $new_bid_client_name = $_REQUEST['new_bid_client_name'];

        $time = date("Y-m-d H:i:s");

        $query_check_bid = "SELECT * FROM bid WHERE bid_wo_no = '$bid_wo_no'";

        $run_check_bid = mysqli_query($conn, $query_check_bid);

            if(mysqli_num_rows($run_check_bid) == 0){
                $query_bid_insert = "INSERT INTO bid SET bid_type = '1', bid_wo_no = '$bid_wo_no', client_id = $new_bid_client_name, prop_id = $prop_id, create_time = '$time', is_approve = 1, bid_amount = 0.00,$active_user_id approve_time = '$time'";

                $run_bid_insert = mysqli_query($conn, $query_bid_insert);
                if(mysqli_affected_rows($conn) > 0){
                    $wo_no = mysqli_insert_id($conn);
                    header("Location:add-items.php?state=1&wo_no=$wo_no&client_id=$new_bid_client_name&bid_wo_no=$bid_wo_no");
                }
            }else{
                $message = 'That Work order Number already exists.';
        }
    }
}
include "header.php";
?>
<div id="page-wrapper">
<h4>Fill Bid Details</h4>


                    <?php
                        if(isset($successmessage)){

                            echo '<div class="alert alert-success">
                            <strong>'. $successmessage .'</strong></div>';
                        }

                        if(isset($message)){
                            echo '<div class="alert alert-danger">
                            <strong>'. $message .'</strong>
                        </div>';
                        }
                    ?>
                      <style>
                    .autocomplete-suggestions { border: 1px solid #999; background: #FFF; overflow: auto; }
.autocomplete-suggestion { padding: 2px 5px; white-space: nowrap; overflow: hidden; }
.autocomplete-selected { background: #F0F0F0; }
.autocomplete-suggestions strong { font-weight: normal; color: #3399FF; }
.autocomplete-group { padding: 2px 5px; }
.autocomplete-group strong { display: block; border-bottom: 1px solid #000; }
                    </style>
    <div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                <!-- <div class="col-lg-12"> -->
                  <!--   <h3> -->

<form  action = "estimate.php" method = "POST" class = "form-group" enctype = "multipart/form-data">

    <div class = "row">

        <div class = "form-group col-lg-6">
            <label for="new_bid_client_name">Select Client</label>          

            <input type="text" class='form-control' name='new_bid_client_name1' id="new_bid_client_name1" autofocus required='required' placeholder="Search for Client Name">

            <input type="hidden" class='form-control' name='new_bid_client_name' id='new_bid_client_name'>
        </div>
        <div class = "form-group col-lg-6">

        </div>       

    </div>

    <div class="row">
    <div class = "form-group col-lg-6" id= "new_bid_prop_column">

        </div>
        <div class = "form-group col-lg-6">

</div>    
    
    </div>
    <div id = "new_bid_if_add">
    </div>
    <div id = "new_bid_if_set">
    </div>
    <div id = "new_bid_wo" style = "display:none">
        <div class="row">
            <div class = "form-group col-lg-6" id = "new_bid_wo_add">
                
            </div>
            <div class = "form-group col-lg-6">
            </div>


        </div>
        <div class="row">
            <div class = "form-group col-lg-6">
             <input type="submit" name = "submit" value = "Next" class="btn btn-success">
            </div>
            <div class = "form-group col-lg-6">
            </div>


        </div>

    </div>
</form>




                    <!-- </h3> -->
                </div><!-- head -->
            </div> <!--panel  -->
        </div><!--col 12  -->
    </div><!-- row -->
 </div> <!--  page wrapper -->

<?php
 include "footer.php";
?>
