<?php
include "include/db.php";
include "include/auth.php";

if(isset($_REQUEST['cl_id'])  && isset($_REQUEST['bid_wo_no']) && isset($_REQUEST['wo_no']) && isset($_REQUEST['state']) && isset($_REQUEST['clr_id'])){
    if(strlen($_REQUEST['cl_id']) > 0  && strlen($_REQUEST['bid_wo_no']) > 0 && strlen($_REQUEST['wo_no']) > 0 && strlen($_REQUEST['state']) > 0 && strlen($_REQUEST['clr_id']) > 0){
       
      $bid_wo_no = $_REQUEST['bid_wo_no']; 
      $wo_no = $_REQUEST['wo_no'];  
      $cl_id = $_REQUEST['cl_id'];
      $clr_id = $_REQUEST['clr_id'];
      $state = $_REQUEST['state'];
    }else{
      exit;
    }
  }else{
    exit;
  }
  $query_check_client = "SELECT * FROM client WHERE client_id = $cl_id AND is_approve = 1 AND client_is_delete IS NULL AND is_list = 1";
  $run_check_client = mysqli_query($conn, $query_check_client);
  if(mysqli_num_rows($run_check_client)!=1){
    if($cl_id != 0){
    exit;}
  }

  $url_client_id = $cl_id;
$query_cat_find1v = "SELECT * FROM category WHERE cat_approved = 1 AND cat_is_delete IS NULL ORDER BY cat_id ASC";
$run_cat_find1v = mysqli_query($conn, $query_cat_find1v);
$setr = 'no';
if(mysqli_num_rows($run_cat_find1v) >0){
  while($row_cat_find1v = mysqli_fetch_array($run_cat_find1v)){
    $cat_id = $row_cat_find1v['cat_id'];

    $query_maps_find1v = "SELECT * FROM maps WHERE cat_id = $cat_id AND client_id =$cl_id  AND maps_approved = 1 AND maps_is_delete IS NULL";
    $run_maps_find1v = mysqli_query($conn, $query_maps_find1v);
    if(mysqli_num_rows($run_maps_find1v) >0){
        if($setr == 'no'){
           $min_cat_id = $cat_id;
            $setr = 'yes';
        }
    }
  }
}
$url_cat_id = $min_cat_id;

?>

<ul data-tabsx<?php echo $cl_id;?>>

<?php
$query_cat_find = "SELECT * FROM category WHERE cat_approved = 1 AND cat_is_delete IS NULL";

$run_cat_find = mysqli_query($conn, $query_cat_find);
$cat_id_tab_check = 'set';
if(mysqli_num_rows($run_cat_find)>0){   
   
 while($row_cat_find = mysqli_fetch_array($run_cat_find)){
    $cat_id =  $row_cat_find['cat_id'];
    $cat_name =  $row_cat_find['cat_name'];

    $query_cat_search = "SELECT * FROM maps WHERE client_id = $url_client_id AND cat_id = $cat_id AND maps_approved = 1 AND maps_is_delete IS NULL";

    $run_cat_search = mysqli_query($conn, $query_cat_search);

    if(mysqli_num_rows($run_cat_search)>0){  
        $cat_id_tab = "catid_" . $cat_id . "-". $cl_id;?>
            <li><a <?php        
           
                if($cat_id_tab_check == 'set'){
                    echo "data-tabby-default ";
                    $cat_id_tab_check = 'unset';
                }          
           
           
    ?>href="#ncew<?php echo $cat_id.$cat_id ?><?php echo $cl_id;?>" style = "color: #0b57a4" id="<?php echo $cat_id_tab ?>"><?php echo $cat_name;  ?></a></li>  
    <?php  }
   }
}
?>       
 
    
</ul>
<!-- /cat-->
<!-- /cat cont loop-->
<?php 
if($url_cat_id == $min_cat_id){
    echo "<div id='ncew$min_cat_id$min_cat_id$cl_id'>"; 
}else{
    echo "<div id='ncew$min_cat_id$min_cat_id$cl_id'>";
    echo "<div id='ocld$min_cat_id$min_cat_id$cl_id'>";
    echo "<p>Loading</p>";
    echo '</div>';
    echo '</div>';

$query_cat_find = "SELECT * FROM category WHERE cat_id < $url_cat_id AND cat_approved = 1 AND cat_is_delete IS NULL";

$run_cat_find = mysqli_query($conn, $query_cat_find);

if(mysqli_num_rows($run_cat_find)>0){   
   
 while($row_cat_find = mysqli_fetch_array($run_cat_find)){

    $cat_id_tab2 = $row_cat_find['cat_id'];
    $searchcat_maps = "SELECT * FROM maps WHERE cat_id = $cat_id_tab2 AND client_id = $url_client_id AND maps_approved = 1 AND maps_is_delete IS NULL";
    $runsearchcat_maps = mysqli_query($conn, $searchcat_maps);

    if(mysqli_num_rows($runsearchcat_maps)>0){

   ?>
        <div id="ncew<?php echo $cat_id_tab2.$cat_id_tab2?><?php echo $cl_id;?>">
        <div id="ocld<?php echo $cat_id_tab2.$cat_id_tab2?><?php echo $cl_id;?>">
        
        <p>Loading</p>
        </div>
        
        </div>
    <?php }}
}

$query_cat_find1 = "SELECT * FROM category WHERE cat_id = $url_cat_id AND cat_approved = 1 AND cat_is_delete IS NULL";

$run_cat_find1 = mysqli_query($conn, $query_cat_find1);

if(mysqli_num_rows($run_cat_find1)>0){   
   
 while($row_cat_find1 = mysqli_fetch_array($run_cat_find1)){
    $cat_id_tab21 = $row_cat_find1['cat_id'];

   ?>
        <div id="ncew<?php echo $cat_id_tab21.$cat_id_tab21?><?php echo $cl_id;?>">
       
    <?php }//while
}//if

} 
?>
<!-- /cat cont loop -->


<br>
     <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-bidwizlist<?php 
                            $random_num = mt_rand (10,99).time();
                            echo $random_num;
                            ?>">
                                    <thead>
                                        <tr>
                                        <th>
                                                  Add / Remove
                                                </th>
                                                <th>
                                                Job Task     
                                                </th>
                                                <th>
                                                Reference Photo
                                                </th>                                 
                                               
                                                <th>
                                                Description
                                                </th>
                                                <th>
                                    Client Allowable Price                                                </th>
                                               
                                            <th>Minimum Bid Price</th>
                                            <th>Crew Pay</th>
                                            <th>UOM</th>
                                            <th>Photo Requirements</th>
                                            </tr>
                                        </thead>
                                        <tbody>
<?php
    if($conn){
        
       $query = "SELECT * FROM maps LEFT JOIN category
        ON maps.cat_id = category.cat_id  WHERE maps.cat_id = $min_cat_id  AND maps.client_id = $url_client_id AND maps.maps_is_delete IS NULL AND maps.maps_approved = 1 ORDER BY category.cat_name"; 
        $run = mysqli_query($conn, $query);

        if(mysqli_num_rows($run)>0){
            while($row = mysqli_fetch_array($run)){  
                $maps_id = $row['maps_id'];
                $check_set = "SELECT * FROM bid_item WHERE maps_id = $maps_id AND bid_id = $wo_no and is_delete IS NULL";

                $run_check_set = mysqli_query($conn, $check_set);
                if(mysqli_num_rows($run_check_set)>0){
                    $set = 'set';
                }
                $ref_photo = $row['maps_ref_pic'];
                $cat_name = $row['cat_name'];
                $maps_name = $row['maps_name'];
                $description = $row['maps_desc'];
                $client_allow_price = $row['maps_client_allow_price'];
                $min_bid_price = $row['maps_min_bid_price'];
                $crew_pay = $row['maps_crew_pay'];
                $photo_req = $row['maps_photo_req'];
                $maps_uom =$row['maps_crew_unit_uom_id'];
if($maps_uom == NULL){

}else{
    $query22 = "SELECT uom_name FROM uom WHERE uom_id = $maps_uom";
                
    $run22 = mysqli_query($conn, $query22);
if(mysqli_num_rows($run22)==1){
$row22 = mysqli_fetch_array($run22);

$maps_uom = $row22['uom_name'];
}
}                       
                
                

                echo '<tr class="odd gradeX">';
                if($set =='set'){
                    echo '<td><a href="#" class="btn btn-success hidden" id = "add-'.$maps_id.'">+</a><br><a href="#" class="btn btn-danger"  id = "rem-'.$maps_id.'">Remove</a></td>';
                }else{
                echo '<td><a href="#" class="btn btn-success" id = "add-'.$maps_id.'">+</a><br><a href="#" class="btn btn-danger hidden"  id = "rem-'.$maps_id.'">Remove</a></td>';}
                echo '<td>' .$maps_name. '</td>';
                echo '<td><img src = "maps_ref_image.php?i='.$ref_photo.'" height = "80px" width = "120px">'. '</td>';
                
             
                echo '<td>' .$description. '</td>';
                echo '<td>' .$client_allow_price. '</td>';
                echo '<td>' .$min_bid_price. '</td>';
                echo '<td>' .$crew_pay. '</td>';
                echo '<td>' .$maps_uom. '</td>';
                 echo '<td>' .$photo_req. '</td>';
           
                echo '</tr>';
            }
        }

    }

?>
                                            
                                            </tbody>
                                    
                                    </table>
                                    
                                    </div><!-- panel-body  -->
                 </div> <!-- panel panel-default -->
            </div> <!--  col lg 12-->
        </div> <!-- row -->       
 <!-- cat content close-->       
      
 <?php
if($url_cat_id == $min_cat_id){?>
 </div>
  <!--  active div (div end)-->
<?php
   $query_cat_find3qw = "SELECT * FROM category WHERE cat_id > $url_cat_id AND cat_approved = 1 AND cat_is_delete IS NULL";

   $run_cat_find3qw = mysqli_query($conn, $query_cat_find3qw);

   if(mysqli_num_rows($run_cat_find3qw)>0){   
   
       while($row_cat_find3qw = mysqli_fetch_array($run_cat_find3qw)){
          $cat_id_tab23qw = $row_cat_find3qw['cat_id'];
           $query_cat_find123qw = "SELECT * FROM maps WHERE cat_id = $cat_id_tab23qw AND client_id = $url_client_id AND maps_approved = 1 AND maps_is_delete IS NULL";
           $run_cat_find123qw = mysqli_query($conn, $query_cat_find123qw);


           if(mysqli_num_rows($run_cat_find123qw) > 0){           
           
           ?>


           <div id="ncew<?php echo $cat_id_tab23qw.$cat_id_tab23qw?><?php echo $cl_id;?>">
           <div id="ocld<?php echo $cat_id_tab23qw.$cat_id_tab23qw?><?php echo $cl_id;?>">
           
           <p>Loading</p>
           </div>
           
           </div>
<?php }
}
   }


    }else{
        echo "</div><!--  active div (div end)-->";
        
        $query_cat_find321 = "SELECT * FROM category WHERE cat_id > $url_cat_id AND cat_approved = 1 AND cat_is_delete IS NULL";

        $run_cat_find321 = mysqli_query($conn, $query_cat_find321);

        if(mysqli_num_rows($run_cat_find321)>0){   
        
            while($row_cat_find321 = mysqli_fetch_array($run_cat_find321)){
                $cat_id_tab2x3 = $row_cat_find321['cat_id'];?>
                <div id="ncew<?php echo $cat_id_tab2x3.$cat_id_tab2x3?><?php echo $cl_id;?>">
                <div id="ocld<?php echo $cat_id_tab2x3.$cat_id_tab2x3?><?php echo $cl_id;?>">
                
                <p>Loading</p>
                </div>
                
                </div>
    <?php }
    
        }
    } 

    echo "<script>";
    $s = 1;
    
        $client_id = $cl_id;        
        $s = $s + 1;
        $fetch_cater = "SELECT * FROM category WHERE cat_is_delete IS NULL AND cat_approved =1";
        $run_fetch_cater = mysqli_query($conn, $fetch_cater);
    
        if(mysqli_num_rows($run_fetch_cater)>0){   
            $k = 1;
            while($row_fetch_cater = mysqli_fetch_array($run_fetch_cater)){
                $cater_id = $row_fetch_cater['cat_id'];
                $k = $k + 1;
                $query_cater_search = "SELECT * FROM maps WHERE client_id = $client_id AND cat_id = $cater_id AND maps_approved = 1 AND maps_is_delete IS NULL";
    
                $run_cater_search = mysqli_query($conn, $query_cater_search);
            
                if(mysqli_num_rows($run_cater_search)>0){  
    
                    $c = 'var zqx'.$s.$k;
    
                    echo $c . ' = "catid_'. $cater_id .'-'.$client_id .'";';
                    
    
                    echo 'fetchClientItemCatData(zqx' .$s. $k.');';
                }
            }
        }        
    
    
    
    
    echo "</script>"; 


?>

 <!-- add item -->
 <script type="text/javascript">
function addItemWO3(d){
    var a = document.getElementById(d);
    if(typeof(a) != 'undefined' || a != null){ 
        a.onclick = function() {
            var maps = d.split('-')[1];    

            var n = maps;
        

            if(typeof(n) != 'undefined' || n != null){                
                var linker = "add-" + n;
                var linker2 = "rem-" + n; 
                $.ajax({
                    type: "POST",
                    url: "wo_add_item_adata.php",
                    data:'maps='+n+ '&cl_id=<?php echo $clr_id;?>&wo_no=<?php echo $wo_no;?>&bid_wo_no=<?php echo $bid_wo_no;?>&state=<?php echo $state;?>',
                    success: function(data){
                        $("#" +linker2).removeClass('hidden');
                        $("#" +linker).addClass('hidden');
                        updateCounter();
                    }
                });
            
            }

            return false;
        }   
    }
}
</script> 

 <?php


    echo "<script>";
    $s = 1;
   
/*         $client_id = $url_client_id;        
 */        $s = $s + 1;

        $fetch_cater = "SELECT * FROM maps WHERE cat_id = $min_cat_id  AND client_id = $url_client_id AND maps_is_delete IS NULL AND maps_approved = 1";
        $run_fetch_cater = mysqli_query($conn, $fetch_cater);

        if(mysqli_num_rows($run_fetch_cater)>0){   
            $k = 1;
            while($row_fetch_cater = mysqli_fetch_array($run_fetch_cater)){
                $maps_id = $row_fetch_cater['maps_id'];
                $k = $k + 1;
               

                    $c = 'var nmzx3'.$s.$k;

                    echo $c . ' = "add-'.$maps_id .'";';
                    

                    echo 'addItemWO3(nmzx3' .$s. $k.');';
               
            }
        }       
   echo "</script>"; 
?> 
<!-- /  add item -->

<!-- r item -->
<script type="text/javascript">
function remItemWO3(d){
    var a = document.getElementById(d);
    if(typeof(a) != 'undefined' || a != null){ 
        a.onclick = function() {
            var maps = d.split('-')[1];    

            var n = maps;
        

            if(typeof(n) != 'undefined' || n != null){                
                var linker = "add-" + n; 
                var linker2 = "rem-" + n; 
                $.ajax({
                    type: "POST",
                    url: "wo_rem_item_adata.php",
                    data:'maps='+n+ '&cl_id=<?php echo $clr_id;?>&wo_no=<?php echo $wo_no;?>&bid_wo_no=<?php echo $bid_wo_no;?>&state=<?php echo $state;?>',
                    success: function(data){
                        $("#" +linker).removeClass('hidden');
                        $("#" +linker2).addClass('hidden');
                        updateCounter();
                    }
                });

               
            }

            return false;
        }   
    }
}
</script> 

 <?php


    echo "<script>";
    $s = 1;
   
/*         $client_id = $url_client_id;        
 */        $s = $s + 1;

        $fetch_cater = "SELECT * FROM maps WHERE cat_id = $min_cat_id  AND client_id = $url_client_id AND maps_is_delete IS NULL AND maps_approved = 1";
        $run_fetch_cater = mysqli_query($conn, $fetch_cater);

        if(mysqli_num_rows($run_fetch_cater)>0){   
            $k = 1;
            while($row_fetch_cater = mysqli_fetch_array($run_fetch_cater)){
                $maps_id = $row_fetch_cater['maps_id'];
                $k = $k + 1;
               

                    $c = 'var remx3'.$s.$k;

                    echo $c . ' = "rem-'.$maps_id .'";';
                    

                    echo 'remItemWO3(remx3' .$s. $k.');';
               
            }
        }       
   echo "</script>"; 
?> 
 <script>
            

$('#dataTables-bidwizlist<?php                            
                            echo $random_num;
                            ?>').DataTable({
    responsive: true,
    "order": [[ 0, "desc" ]],
    dom: 'Bfrtip',
buttons: [
    
]
});

</script>

<!-- /  r item -->

