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

if(isset($_REQUEST['clr_id']) && isset($_REQUEST['cl_id']) && isset($_REQUEST['cat_id']) && isset($_REQUEST['invoice_no']) && isset($_REQUEST['in_no']) && isset($_REQUEST['state'])){
    if(strlen($_REQUEST['clr_id']) > 0 && strlen($_REQUEST['cl_id']) > 0 && strlen($_REQUEST['cat_id']) > 0 && strlen($_REQUEST['invoice_no']) > 0 && strlen($_REQUEST['in_no']) > 0 && strlen($_REQUEST['state']) > 0){
        $cl_id = $_REQUEST['cl_id'];
        $clr_id = $_REQUEST['clr_id'];
        $cat_id = $_REQUEST['cat_id'];
        $invoice_no = $_REQUEST['invoice_no']; 
        $invoice_id = $_REQUEST['in_no'];        
        $state = $_REQUEST['state'];
    }else{
      exit;
    }
}else{
    exit;
}

?>
   <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>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 = $cat_id  AND maps.client_id = $cl_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 invoice_item WHERE maps_id = $maps_id AND invoice_id = $invoice_id 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_invoice_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>';
                    $set = '';
                }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_invoice_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 -->  

        <!-- add item -->
<script type="text/javascript">
function addItemWO2(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: "in_add_item_adata.php",
                    data:'maps='+n+ '&cl_id=<?php echo $clr_id;?>&in_no=<?php echo $invoice_id;?>&invoice_no=<?php echo $invoice_no;?>&state=<?php echo $state;?>',
                    success: function(data){
                        $("#" +linker2).removeClass('hidden');
                        $("#" +linker).addClass('hidden');
                        updateCounter();
                    }
                });
                    
               
            
            }

            return false;
        }   
    }
}
</script> 

 <?php


    echo "<script>";
    $s = 1;
   
       $s = $s + 1;

        $fetch_cater = "SELECT * FROM maps WHERE cat_id = $cat_id  AND client_id = $cl_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 nmzx2'.$s.$k;

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

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

<!-- r item -->
<script type="text/javascript">
function remItemWO2(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: "in_rem_item_adata.php",
                        data:'maps='+n+ '&cl_id=<?php echo $clr_id;?>&in_no=<?php echo $invoice_id;?>&invoice_no=<?php echo $invoice_no;?>&state=<?php echo $state;?>',
                        success: function(data){
                            $("#" +linker).removeClass('hidden');
                            $("#" +linker2).addClass('hidden');
                            updateCounter();
                        }
                    });

               
            }

            return false;
        }   
    }
}
</script> 

 <?php


    echo "<script>";
    $s = 1;
   
             
        $s = $s + 1;

        $fetch_cater = "SELECT * FROM maps WHERE cat_id = $cat_id  AND client_id = $cl_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 remx2'.$s.$k;

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

                    echo 'remItemWO2(remx2' .$s. $k.');';
               
            }
        }       
   echo "</script>"; 
?> 
<!-- /  r item -->

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