<?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,";
}
$message = "";
$successmessage = "";        
$prop_case_num = '';
$prop_full_add = '';
$prop_county = '';
$prop_zip = '';
$prop_city = '';
$prop_state = '';
$p_lot_size = '';
$p_sf = '';
$p_bedroom = '';
$p_bathroom = '';
$p_client_id = '';

if(isset($_REQUEST['mess'])){
    $message = $_REQUEST['mess'];
}
if(isset($_REQUEST['smess'])){
    $successmessage = $_REQUEST['smess'];
}

if(isset($_REQUEST['assigninsert'])){
    if(strlen($_REQUEST['client_name'])> 0 && strlen($_REQUEST['property_id'])> 0){
       $p_client_id = $_REQUEST['client_name'];
       $property_id = $_REQUEST['property_id'];
       $time = date("Y-m-d H:i:s");
    
        if(strlen($_REQUEST['p_lot_size'])> 0){

            $p_lot_size = $_REQUEST['p_lot_size'];

        }else{
            $p_lot_size = '';

        } 
        
        if(strlen($_REQUEST['p_sf'])> 0){
            $p_sf = $_REQUEST['p_sf'];
        }else{
            $p_sf = '';
        } 
        
        if(strlen($_REQUEST['p_bedroom'])> 0){
            $p_bedroom = $_REQUEST['p_bedroom'];
        }else{
            $p_bedroom = '';
        }
        
        if(strlen($_REQUEST['p_bathroom'])> 0){
        $p_bathroom = $_REQUEST['p_bathroom'];
        }else{
            $p_bathroom = '';
        }
        $check1 = "SELECT * FROM prop_assign WHERE prop_id = $property_id AND client_id = $p_client_id AND is_delete IS NULL AND is_approve = 1";
        $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 = $property_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";
                $assignnewfail = 'yes';

            }     
            
        }else{
            $message = "That Client Already exists in this property ID";
            $assignnewfail = 'yes';
        }
    }
}

if(isset($_REQUEST['assigneditinsert'])){
    if(strlen($_REQUEST['client_name'])> 0 && strlen($_REQUEST['assigneditinsert'])> 0 && strlen($_REQUEST['property_id'])> 0){
       $p_client_id = $_REQUEST['client_name'];
       $property_id = $_REQUEST['property_id'];
       $prop_assign_id = $_REQUEST['assigneditinsert'];
       $time = date("Y-m-d H:i:s");
    
        if(strlen($_REQUEST['p_lot_size'])> 0){

            $p_lot_size = $_REQUEST['p_lot_size'];

        }else{
            $p_lot_size = '';

        } 
        
        if(strlen($_REQUEST['p_sf'])> 0){
            $p_sf = $_REQUEST['p_sf'];
        }else{
            $p_sf = '';
        } 
        
        if(strlen($_REQUEST['p_bedroom'])> 0){
            $p_bedroom = $_REQUEST['p_bedroom'];
        }else{
            $p_bedroom = '';
        }
        
        if(strlen($_REQUEST['p_bathroom'])> 0){
        $p_bathroom = $_REQUEST['p_bathroom'];
        }else{
            $p_bathroom = '';
        }

        $check2 = "SELECT * FROM prop_assign WHERE propr_assign_id = $prop_assign_id AND is_delete IS NULL AND is_approve = 1";
        $run_check2 = mysqli_query($conn, $check2);
        $rowww = mysqli_fetch_array($run_check2);

        $client_id_old = $rowww['client_id'];
        $checknum = 0;
        if($client_id_old != $p_client_id){
            $check22 = "SELECT * FROM prop_assign WHERE prop_id = $property_id AND client_id = $p_client_id AND is_delete IS NULL AND is_approve = 1";
            $run_check22 = mysqli_query($conn, $check22);
            $checknum = mysqli_num_rows($run_check22);

        }


        if($checknum == 0){
            $query_insert_prop_assign = "UPDATE prop_assign SET client_id = $p_client_id, prop_id = $property_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' WHERE propr_assign_id = $prop_assign_id";
            $run_insert_prop_assign = mysqli_query($conn, $query_insert_prop_assign);


            if(mysqli_affected_rows($conn) > 0){
                $successmessage = "Client property details edited.";        
            }else{
                $message = "Something went wrong. Try Again";
                $assigneditfail = 'yes';
            }                 
        }else{
            $message = "That Client Already exists in this property ID";
            $assigneditfail = 'yes';
        }
    }
}

if(isset($_REQUEST['delete'])){
    if(strlen($_REQUEST['delete'])> 0){
       $prop_id = $_REQUEST['delete'];
       $prop_case_num = $_REQUEST['prop_case_num'];
       $query_prop_insert = "UPDATE property SET $active_user_id is_delete = 1 WHERE prop_id = $prop_id";  
    
       $run_prop_insert = mysqli_query($conn, $query_prop_insert);
       if(mysqli_affected_rows($conn) > 0){
           $successmessage = "$prop_case_num has been Disabled from further use in the system.";
   
       }else{
           $message = "Something went wrong. Try Again";
       }     
       

    }
}


if(isset($_REQUEST['assigndelete'])){
    if(strlen($_REQUEST['assigndelete'])> 0){
       $prop_id = $_REQUEST['assigndelete'];
        $query_prop_insert = "UPDATE prop_assign SET $active_user_id is_delete = 1 WHERE propr_assign_id = $prop_id";  
    
       $run_prop_insert = mysqli_query($conn, $query_prop_insert);
       if(mysqli_affected_rows($conn) > 0){
           $successmessage = "This Client Property has been Disabled from further use in the system.";
   
       }else{
           $message = "Something went wrong. Try Again";
       }     
       

    }
}

if(isset($_REQUEST['assignview']) || isset($_REQUEST['assignedit'])){
    if(strlen($_REQUEST['assignedit'])> 0){ 
        $prop_assign_id =   $_REQUEST['assignedit'];     
    }elseif(strlen($_REQUEST['assignview'])> 0){
       $prop_assign_id = $_REQUEST['assignview'];

    }else{
        header("Location:property.php");
    }
     
        $query_select_prop_assign = "SELECT * FROM prop_assign WHERE propr_assign_id = $prop_assign_id AND is_delete IS NULL AND is_approve = 1";
        $run_select_prop_assign = mysqli_query($conn, $query_select_prop_assign);

        if(mysqli_num_rows($run_select_prop_assign) == 1){
            while($row_select_prop_assign = mysqli_fetch_array($run_select_prop_assign)){
                $p_lot_size = $row_select_prop_assign['p_lot_size'];
                $p_sf = $row_select_prop_assign['p_sf'];
                $p_bedroom = $row_select_prop_assign['p_bedroom'];
                $p_bathroom = $row_select_prop_assign['p_bathroom'];
                $p_client_id = $row_select_prop_assign['client_id'];
            

            }

        }     
   
}

if(isset($_REQUEST['active'])){
    if(strlen($_REQUEST['active'])> 0){
       $prop_id = $_REQUEST['active'];
       $prop_case_num = $_REQUEST['prop_case_num'];
       $query_prop_insert = "UPDATE property SET $active_user_id is_delete = NULL WHERE prop_id = $prop_id";  
    
       $run_prop_insert = mysqli_query($conn, $query_prop_insert);
       if(mysqli_affected_rows($conn) > 0){
           $successmessage = "$prop_case_num has been Activated for use in the system.";
   
       }else{
           $message = "Something went wrong. Try Again";
       }      
       

    }
}

if(isset($_REQUEST['assignactive'])){
    if(strlen($_REQUEST['assignactive'])> 0){
       $prop_id = $_REQUEST['assignactive'];
       $query_prop_insert = "UPDATE prop_assign SET $active_user_id is_delete = NULL WHERE propr_assign_id = $prop_id";  
       $run_prop_insert = mysqli_query($conn, $query_prop_insert);
       if(mysqli_affected_rows($conn) > 0){
           $successmessage = "This client property is Activated for use in the system.";
   
       }else{
           $message = "Something went wrong. Try Again";
       }      
       

    }
}

if(isset($_REQUEST['assign'])){
    if(strlen($_REQUEST['assign'])> 0){
       $prop_id = $_REQUEST['assign'];
       $query_prop_select = "SELECT * FROM property WHERE prop_id = $prop_id";  
    
       $run_prop_select = mysqli_query($conn, $query_prop_select);
       if(mysqli_num_rows($run_prop_select) > 0){
            while($row_prop_select = mysqli_fetch_array($run_prop_select)){
                $prop_case_num = $row_prop_select['prop_case_num'];
                $prop_loan_num = $row_prop_select['prop_loan_num'];
                $prop_full_add = $row_prop_select['prop_full_add'];
                $prop_county = $row_prop_select['prop_county'];
            }              
       }     
       

    }
}

if(isset($_REQUEST['insert']) && isset($_REQUEST['prop_case_num']) && isset($_REQUEST['prop_full_add']) && isset($_REQUEST['prop_state'])&& isset($_REQUEST['prop_city']) && isset($_REQUEST['prop_zip'])){

    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_case_num =  mysqli_real_escape_string($conn, $prop_case_num);
        $prop_loan_num = $prop_case_num;

        $prop_full_add = trim($_REQUEST['prop_full_add']);
        $prop_full_add =  mysqli_real_escape_string($conn,  $prop_full_add);

        $prop_city = trim($_REQUEST['prop_city']);
        $prop_city =  mysqli_real_escape_string($conn,  $prop_city);

        $prop_zip = trim($_REQUEST['prop_zip']);
        $prop_zip =  mysqli_real_escape_string($conn, $prop_zip);

        $prop_state = trim($_REQUEST['prop_state']);
        $prop_state =  mysqli_real_escape_string($conn, $prop_state);

        if(isset($_REQUEST['prop_county'])){
            if (strlen($_REQUEST['prop_county'])>0){
                $prop_county= trim($_REQUEST['prop_county']);
                $prop_county =  mysqli_real_escape_string($conn, $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' AND is_delete IS NULL) OR (prop_case_num = '$prop_case_num' AND is_delete IS NULL) OR (prop_loan_num = '$prop_loan_num' AND is_delete IS NULL)";

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

            if(mysqli_num_rows($run_check_prop) == 0){ 
                $query_prop_insert = "INSERT INTO property SET $active_user_id 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, approve_time = '$time'";  
                
                $run_prop_insert = mysqli_query($conn, $query_prop_insert);
                if(mysqli_affected_rows($conn) > 0){
                    $successmessage = "$prop_case_num has been added.";

                }else{
                    $message = "Something went wrong. Try Again";
                    $addfail = 'yes';
                }

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

    }
}

if(isset($_REQUEST['editadd']) && isset($_REQUEST['prop_case_num']) && isset($_REQUEST['prop_full_add'])&& isset($_REQUEST['prop_city'])&& isset($_REQUEST['prop_state'])&& isset($_REQUEST['prop_zip'])){

    if(strlen($_REQUEST['editadd'])  >0 && strlen($_REQUEST['prop_case_num'])>0 && strlen($_REQUEST['prop_full_add'])  >0 && strlen($_REQUEST['prop_city'])  >0 && strlen($_REQUEST['prop_state'])  >0 && strlen($_REQUEST['prop_zip'])  >0){      
        $prop_id = trim($_REQUEST['editadd']);
        $prop_id =  mysqli_real_escape_string($conn, $prop_id);

        $prop_case_num = trim($_REQUEST['prop_case_num']);
        $prop_case_num =  mysqli_real_escape_string($conn, $prop_case_num);
        $prop_loan_num = $prop_case_num;

        $prop_full_add = trim($_REQUEST['prop_full_add']);
        $prop_full_add =  mysqli_real_escape_string($conn,  $prop_full_add);

        $prop_city = trim($_REQUEST['prop_city']);
        $prop_city =  mysqli_real_escape_string($conn,  $prop_city);

        $prop_zip = trim($_REQUEST['prop_zip']);
        $prop_zip =  mysqli_real_escape_string($conn, $prop_zip);

        $prop_state = trim($_REQUEST['prop_state']);
        $prop_state =  mysqli_real_escape_string($conn, $prop_state);
        if(isset($_REQUEST['prop_county'])){
            if (strlen($_REQUEST['prop_county'])>0){
                $prop_county = trim($_REQUEST['prop_county']);
                $prop_county =  mysqli_real_escape_string($conn, $prop_county);
            }else{
                $prop_county = '';
            }
        
        }else{
            $prop_county = '';
        }
     
        $time = date("Y-m-d H:i:s");
        
        $query_check_prop1 = "SELECT * FROM property WHERE prop_id = $prop_id";
        $run_check_prop1 = mysqli_query($conn, $query_check_prop1);
        
        $row_check_prop1 = mysqli_fetch_array($run_check_prop1);

        $prop_case_num_old =  $row_check_prop1['prop_case_num'];
        
        $check_prop = 'pass';
        if($prop_case_num != $prop_case_num_old){
            $query_check_prop = "SELECT * FROM property WHERE prop_case_num = '$prop_case_num' AND is_delete IS NULL";

            $run_check_prop = mysqli_query($conn, $query_check_prop);
            
            if(mysqli_num_rows($run_check_prop) > 0){
                $check_prop = 'fail';
            }

        }     

        if($check_prop == 'pass'){

            $query_prop_insert = "UPDATE property SET $active_user_id 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' WHERE prop_id = $prop_id";  
            
            $run_prop_insert = mysqli_query($conn, $query_prop_insert);
            if($run_prop_insert){
                $successmessage = "Details of $prop_case_num has been changed.";

            }else{
                $message = "Something went wrong. Try Again";
                $editfail = 'yes';
            }  
        
        }else{
                $message = 'That Property Case or Loan Number already exists.';
                $editfail = 'yes';
        }

    }
}

if(isset($_REQUEST['edit']) || isset($_REQUEST['view'])){
    if(isset($_REQUEST['edit'])){
        $prop_id = $_REQUEST['edit'];
    }elseif(isset($_REQUEST['view'])){
        $prop_id = $_REQUEST['view'];
    }
    if(strlen($prop_id)>0){
        $query_fetch_prop = "SELECT * FROM property WHERE prop_id = $prop_id";
        $run_fetch_prop = mysqli_query($conn, $query_fetch_prop);
        if(mysqli_num_rows($run_fetch_prop) == 1){
            $row_fetch_prop = mysqli_fetch_array($run_fetch_prop);
        }   

        $prop_case_num = $row_fetch_prop['prop_case_num'];
        $prop_full_add = $row_fetch_prop['prop_full_add'];
        $prop_county = $row_fetch_prop['prop_county'];
        $prop_city = $row_fetch_prop['prop_city'];
        $prop_zip = $row_fetch_prop['prop_zip'];
        $prop_state = $row_fetch_prop['prop_state'];


    }
}

include "header.php";
?>
<div id="page-wrapper">
<h3>Property Management</h3>


<?php                                     
                if(isset($successmessage)){
                    if(strlen($successmessage) > 0){
                        echo '<div class="alert alert-success">
                        <strong>'. $successmessage .'</strong></div>';
                    }
                } 

                if(isset($message)){
                    if(strlen($message) > 0){
                    echo '<div class="alert alert-danger">
                    <strong>'. $message .'</strong>
                  </div>';
                    }
                }
                    ?>              
  
        <?php 
        if(isset($_REQUEST['edit'])|| isset($editfail)){ ?>
  <div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                <!-- <div class="col-lg-12"> -->
                <h4>



        <?php
            echo "Edit Property Details of $prop_case_num<a style = 'float:right;' class='btn btn-warning' href='property.php'>Cancel</a>";?>

</h4>
        </div>


            <?php

        }elseif(isset($_REQUEST['assign']) && isset($_REQUEST['assignnew'])){ ?>
            <div class="row">
                  <div class="col-lg-12">
                      <div class="panel panel-default">
                          <div class="panel-heading">
                          <!-- <div class="col-lg-12"> -->
                          <h4>
          
          
          
                  <?php          
            echo "Select New Client for Property Case No.$prop_case_num <a style = 'float:right;' class='btn btn-warning' href='property.php'>Back to Property</a>";?>

            </h4>
                    </div>
            
            
                        <?php
            

        }elseif(isset($_REQUEST['assign'])){ ?>
            <div class="row">
                  <div class="col-lg-12">
                      <div class="panel panel-default">
                          <div class="panel-heading">
                          <!-- <div class="col-lg-12"> -->
                          <h4>
          
          
          
                  <?php                  
            echo "Clients Assigned to Case No. $prop_case_num: $prop_full_add, $prop_county<br><br><a class='btn btn-success' href='property.php?assignnew=$prop_id&assign=$prop_id'>Assign New Client</a> <a style = 'float:right;' class='btn btn-warning' href='property.php'>Back to Property</a>";?>

            </h4>
                    </div>
            
            
                        <?php
            

        }elseif(isset($_REQUEST['add'])|| isset($addfail)){?>
            <div class="row">
                  <div class="col-lg-12">
                      <div class="panel panel-default">
                          <div class="panel-heading">
                          <!-- <div class="col-lg-12"> -->
                          <h4>
          
          
          
                  <?php        
            echo "Add New Property <a style = 'float:right;' class='btn btn-warning' href='property.php'>Cancel</a>";?>

            </h4>
                    </div>
            
            
                        <?php
            
        }else{?>
           
           <div class="row">
        <div class="col-md-6">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <b>New Property Form </b> 
                </div> <!-- "panel-heading" -->                
                <div class="panel-body">          
          
                  <a href="property.php?add=" class="btn btn-success" role="button">Add New Property</a>
<br>
<br>
                  </div> <!-- "panel-body" -->
            </div> <!-- panel panel-default -->
           

            
        </div> <!--  col lg 6-->

         <div class="col-md-6">
         <?php if($user_type == 'admin'){ ?>
            <div class="panel panel-default">
                <div class="panel-heading">
                    <b>New Property Details Excel Upload </b> 
                </div> <!-- "panel-heading" -->                
                <div class="panel-body">
           
            <form action = 'prop-import.php?upload_xl_prop=' method = 'POST' enctype = 'multipart/form-data'>
                <div class="form-group col-md-6">
                   <br>
				    <input type="file" class="upload-file" required name="fileToUpload1" id="fileToUpload1">   
                </div>
                <div class="form-group col-md-6">
                   <input type="submit" class="btn btn-success" value = "Upload"> 
                   <a href="excels/property.xlsx" download class="btn btn-danger" style = 'float:right;'>
                       Property Excel Format
                   </a>
               </div>

                </form>
                </div> <!-- "panel-body" -->
            </div> <!-- panel panel-default -->
         <?php } ?>
        </div> <!--  col lg 6-->
      
    </div> <!-- row -->
                <div class="row">
                  <div class="col-lg-12">
                      <div class="panel panel-default">

            <?php  } 
        ?>                                                                   
              <?php  
               if(isset($_REQUEST['assignnew']) || isset($assignnewfail) || isset($_REQUEST['assignedit']) || isset($assigneditfail) || isset($_REQUEST['assignview'])){?>
               <div class="panel-body"> 
               <div class = "row">   
                    <div class="col-lg-12">  
                    <?php  
               if(isset($_REQUEST['assignnew']) || isset($assignnewfail)){?>
                    <form action = "property.php?assign=<?php echo $prop_id;?>&property_id=<?php echo $prop_id;?>&assigninsert=" method = "POST" class = "form-group" enctype = "multipart/form-data">
            <?php }elseif(isset($_REQUEST['assignedit']) || isset($assigneditfail)){?>

                        <form action = "property.php?assign=<?php echo $prop_id;?>&assigneditinsert=<?php echo $prop_assign_id;?>&property_id=<?php echo $prop_id;?>" method = "POST" class = "form-group" enctype = "multipart/form-data">

               <?php
               }elseif(isset($_REQUEST['assignview'])){                    
            ?>
             <form method = "POST" class = "form-group" enctype = "multipart/form-data">

                <?php 
                }
                ?>

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

       <label for="client_name">Choose Client: </label>     
     
            <select name="client_name" id="client_name" required autofocus >

 <?php      
         if(strlen($p_client_id) > 0){
            $query00 = "SELECT * FROM client WHERE client_id = $p_client_id AND client_is_delete IS NULL AND is_approve = 1 ORDER BY client_name";
            $run00 = mysqli_query($conn,$query00);

            if(mysqli_num_rows($run00)>0){
             while($row00 = mysqli_fetch_array($run00)){                     
                 echo "<option value='".$row00['client_id']."' selected = 'selected'>". $row00['client_name']."</option>";                
             }
            }  

         }else{
            echo '<option value="" disabled="disabled" selected="selected">Select Client </option>'; 
         }        
                    

                 
               $query00 = "SELECT * FROM client WHERE client_is_delete IS NULL AND is_approve = 1 ORDER BY client_name";
               $run00 = mysqli_query($conn,$query00);

               if(mysqli_num_rows($run00)>0){
                while($row00 = mysqli_fetch_array($run00)){                     
                    echo "<option value='".$row00['client_id']."'>". $row00['client_name']."</option>";                
                }
               }  
               ?>

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

        </div>
        </div>       

        <div class = "row">        
        <div class = "form-group col-lg-6">
        <label for="p_lot_size">Lot Size</label>          
        <input type='text' class='form-control' name='p_lot_size' <?php if(isset($_REQUEST['assignview'])){ echo 'disabled';}?> id='p_lot_size' value='<?php echo $p_lot_size ?>'> 

        </div>   

        <div class = "form-group col-lg-6">
        <label for="p_sf">Property Size Square Feet</label>
          

          <input type='text' class='form-control' name='p_sf' <?php if(isset($_REQUEST['assignview'])){ echo 'disabled';}?> id='p_sf' value='<?php echo $p_sf ?>'>                       

        </div>
        </div>  

         <div class = "row">        
        <div class = "form-group col-lg-6">
        <label for="p_bedroom">No of Bedrooms</label>          
        <input type='text' class='form-control' name='p_bedroom' <?php if(isset($_REQUEST['assignview'])){ echo 'disabled';}?> id='p_bedroom' value='<?php echo $p_bedroom ?>'> 

        </div>   
    
        <div class = "form-group col-lg-6">
        <label for="p_bathroom">No of Bathrooms</label>
          

          <input type='text' class='form-control' name='p_bathroom' <?php if(isset($_REQUEST['assignview'])){ echo 'disabled';}?> id='p_bathroom' value='<?php echo $p_bathroom ?>'>                       

        </div>
        </div>                          

    <div class = "row">
        
    <div class = "form-group col-lg-6">
    <?php if(isset($_REQUEST['assignview'])){ echo "<a href= 'property.php?assign=$prop_id&assignedit=$prop_assign_id' type='button' name = 'btn_edit'  class='btn btn-warning'>Edit Details</a>";}else{echo '    <input type="submit" name = "submit" value = "Submit" class="btn btn-success">
';}?>

    </div>


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

                                    
                                </form>
                                
                                


                    <br>
                    </div>  
            </div>

                </div>
               <?php }
              if(isset($_REQUEST['assign'])){?>
</div> <!-- panel panel-default -->
       </div> <!--  col lg 12-->
   </div> <!-- row -->
<div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
            <div class="panel-body">
                                
                <table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-bidwizzz1">
                                              <thead>
                                                  <tr>
                                                  
                                                          <th>
                                                              Client Name
                                                          </th>
                                                          <th>
                                                          Alias
                                                           </th>                                              
                                                        <th>
                                                           Edit
                                                          </th> 
                                                          <th>
                                                           View Details
                                                          </th>
                                                         
                                                      <th>Delete</th>
                                                     
                                                  </tr>
                                                  </thead>
                                                  <tbody>
          <?php
              if($conn){
                  $query = "SELECT * FROM prop_assign WHERE prop_id = $prop_id AND is_delete IS NULL AND is_approve = 1";
                  $run = mysqli_query($conn, $query);
          
                  if($run){
                      while($row = mysqli_fetch_array($run)){
                          $prop_assign_id = $row['propr_assign_id'];
                          $client_id = $row['client_id'];
                          $prop_is_delete = $row['is_delete'];

                          $query1 = "SELECT * FROM client WHERE client_id = $client_id";
                             $run1 = mysqli_query($conn, $query1);          
                                if($run1){
                                    while($row1 = mysqli_fetch_array($run1)){  
                                        $client_name = $row1['client_name'];
                                        $client_alias = $row1['client_alias'];
                                        echo '<tr class="odd gradeX">';
                                        echo '<td>' .$client_name. '</td>';
                                        echo '<td>' .$client_alias. '</td>';
                                      
                                        echo "<td><a href='property.php?assign=$prop_id&assignedit=$prop_assign_id'>Edit</a> </td>";  
                                        echo "<td><a href='property.php?assign=$prop_id&assignview=$prop_assign_id'>View</a> </td>";               
                                        
                                        echo "<td><a href='property.php?assign=$prop_id&assigndelete=$prop_assign_id&client_name=$client_name' class=\"delete\" data-confirm=\"Are you sure to delete this Property of $client_name?\">Delete</a> </td>";
                                        
                                        echo '</tr>';
                                    }
                                }
                      }
                  }
          
              }
          
          ?>
                                                      
                                                      </tbody>
                                              
                                              </table>
                                              </div><!-- panel-body  -->
                 </div> <!-- panel panel-default -->
            </div> <!--  col lg 12-->
        </div> <!-- row -->
          
          
                                         <?php }

             if(isset($_REQUEST['edit']) || isset($_REQUEST['add'])|| isset($addfail)|| isset($editfail) || isset($_REQUEST['view'])){ ?>
             <div class="panel-body">                      
             <div class = "row">   
                    <div class="col-lg-12">                    
                    <?php }                              
                                if(isset($_REQUEST['edit'])|| isset($editfail)){
                                    echo "<form action = 'property.php?editadd=$prop_id' method = 'POST' class = 'form-group' enctype = 'multipart/form-data'>";
                                }        
                                elseif(isset($_REQUEST['add'])|| isset($addfail)){
                                    echo '<form action = "property.php?insert=" method = "POST" class = "form-group" enctype = "multipart/form-data">';
                                }elseif(isset($_REQUEST['view'])){
                                    echo '<form class = "form-group" enctype = "multipart/form-data">';
                                }
                              
                                if(isset($_REQUEST['edit']) || isset($_REQUEST['add'])|| isset($addfail)|| isset($editfail) || isset($_REQUEST['view'])){
                                   
                              ?>

     <div class = "row">
        
        <div class = "form-group col-lg-6">
        <label for="prop_case_num">Case/Loan Number</label>
     <input type='text' class='form-control' name='prop_case_num' <?php if(isset($_REQUEST['view'])){ echo 'disabled';}?> id='prop_case_num' value='<?php if(strlen($prop_case_num)>0){echo $prop_case_num;}else{$prop_case_num =time().mt_rand (10,99); echo $prop_case_num;} ?>' required='required' autofocus>
        </div>
         <div class = "form-group col-lg-6">                             

        </div>
        </div>        

        <div class = "row">        
          
    
            <div class = "form-group col-lg-6">
                    <div class="row">
                    <div class="form-group col-lg-6">
                    <label for="prop_zip">Zipcode</label>
                            <input class='form-control' name='prop_zip' <?php if(isset($_REQUEST['view'])){ echo 'disabled';}?> maxlength='5' minlength='5' id='prop_zip' value='<?php echo $prop_zip ?>' required='required'>   
                               

                        </div>
                        <div class="form-group col-lg-6">
                        <label for="prop_city">City</label>
                    

                            <input type='text' class='form-control' name='prop_city' <?php if(isset($_REQUEST['view'])){ echo 'disabled';}?> id='prop_city' value='<?php echo $prop_city ?>' required='required'> 
                           
                        
                        </div>
                       
                    
                    </div>

                    <div class="row">

                        <div class="form-group col-lg-6">
                        <label for="prop_county">County</label>
                    

                    <input type='text' class='form-control' name='prop_county' <?php if(isset($_REQUEST['view'])){ echo 'disabled';}?> id='prop_county' value='<?php echo $prop_county ?>'>  
                       
                        </div>
                        <div class="form-group col-lg-6">
                        <label for="prop_state">State</label>

<select class='form-control' name='prop_state'  id='prop_state' <?php if(isset($_REQUEST['view'])){ echo 'disabled';}?> required='required'>
<?php
if(isset($prop_state)){
echo "<option value='$prop_state' selected = 'selected'>$prop_state</option>";
}else{
echo '<option value="" disabled="disabled" selected="selected">Select State</option>';
}
?>

<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
<option value="DC">District of Columbia</option>
<option value="FL">Florida</option>
<option value="GA">Georgia</option>
<option value="HI">Hawaii</option>
<option value="ID">Idaho</option>
<option value="IL">Illinois</option>
<option value="IN">Indiana</option>
<option value="IA">Iowa</option>
<option value="KS">Kansas</option>
<option value="KY">Kentucky</option>
<option value="LA">Louisiana</option>
<option value="ME">Maine</option>
<option value="MD">Maryland</option>
<option value="MA">Massachusetts</option>
<option value="MI">Michigan</option>
<option value="MN">Minnesota</option>
<option value="MS">Mississippi</option>
<option value="MO">Missouri</option>
<option value="MT">Montana</option>
<option value="NE">Nebraska</option>
<option value="NV">Nevada</option>
<option value="NH">New Hampshire</option>
<option value="NJ">New Jersey</option>
<option value="NM">New Mexico</option>
<option value="NY">New York</option>
<option value="PR">Puerto Rico</option>
<option value="NC">North Carolina</option>
<option value="ND">North Dakota</option>
<option value="OH">Ohio</option>
<option value="OK">Oklahoma</option>
<option value="OR">Oregon</option>
<option value="PA">Pennsylvania</option>
<option value="RI">Rhode Island</option>
<option value="SC">South Carolina</option>
<option value="SD">South Dakota</option>
<option value="TN">Tennessee</option>
<option value="TX">Texas</option>
<option value="UT">Utah</option>
<option value="VT">Vermont</option>
<option value="VA">Virginia</option>
<option value="WA">Washington</option>
<option value="WV">West Virginia</option>
<option value="WI">Wisconsin</option>
<option value="WY">Wyoming</option>
</select>               

                        </div>

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

            </div>  
                                     

            


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

         
        <label for="prop_full_add">Address</label>          
                <textarea rows="4" name="prop_full_add" <?php if(isset($_REQUEST['view'])){ echo 'disabled';}?> class = "form-control" id="prop_full_add" placeholder=""  maxlength="5000" required = "required"><?php echo $prop_full_add ?></textarea>
                </div>  

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

                 </div>  
        
        </div>                      

    <div class = "row">
        
    <div class = "form-group col-lg-6">
    <?php if(isset($_REQUEST['view'])){ echo "<a href= 'property.php?edit=$prop_id' type='button' name = 'btn_edit'  class='btn btn-warning'>Edit Details</a>";}else{echo '    <input type="submit" name = "submit" value = "Submit" class="btn btn-success">
';}?>
    </div>


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

                                    
                                </form>
                                
                                


                    </div>  
            </div>
                </div><!-- panel-body  --> 
            </div> <!-- panel panel-default -->
       </div> <!--  col lg 12-->
   </div> <!-- row --><?php } ?>
                            
     <div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
            <div class="panel-body">

                            <table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-bidwizzz-property">
                                    <thead>
                                        <tr>
                                        <th>
                                        Property ID
                                                </th>
                                                <th>
                                                Case/Loan Number
                                                </th>
                                                <th>
                                                Address
                                                 </th> 
                                                 <th>
                                                City
                                                 </th> 
                                                 <th>
                                                State
                                                 </th> 
                                                 <th>
                                                Zip
                                                 </th>                                            
                                                <th>
                                               County
                                                </th>                                      
                                                                                       
                                                <th>
                                                 Edit
                                                </th>
                                            <th>Delete</th>
                                            <th>
                                                Client Assignments
                                                </th>
                                           
                                        </tr>
                                        </thead>
                                        <tbody>                                 
                                            </tbody>
                                    
                                    </table>
                                    
                                    </div><!-- panel-body  -->
                 </div> <!-- panel panel-default -->
            </div> <!--  col lg 12-->
        </div> <!-- row -->
    </div> <!--  page wrapper -->                             
       
       
<?php  include "footer.php";   ?>