<?php
include "include/db.php";
include "include/employeeauth.php";
include "header.php";

if(isset($_REQUEST['cl_id'])){
    if(strlen($_REQUEST['cl_id']) > 0){
        $url_client_id = $_REQUEST['cl_id'];
        if($url_client_id != 0){
           $query_check_client = "SELECT * FROM client WHERE client_id = $url_client_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){
                $url_client_id = 0;
            }elseif(mysqli_num_rows($run_check_client)==1){
                $row_check_client = mysqli_fetch_array($run_check_client);
                $url_client_name = $row_check_client['client_name'];
            }
        }else{
            $url_client_id = 0;
        }   
           
    }else{
        $url_client_id = 0;
    }
}else{
    $url_client_id = 0;
}
if(!isset($url_client_name)){
    $url_client_name = 'MAPS';
}
if(strlen($url_client_name) == 0){
    $url_client_name = 'MAPS';
}
$maps_name = "";
$cat_name = "";
$description = "";
$photo_req = "";
$crew_pay = "";
$client_allow_price = "";
$min_bid_price = "";
$maps_uom = "";
$ref_photo = "";
if($conn){
    if(isset($_REQUEST['addgroupverify']) && isset($_REQUEST['maps_name_child']) && isset($_REQUEST['maps_name_parent'])){
        $maps_name_parent = $_REQUEST['maps_name_parent'];
        $maps_name_child = $_REQUEST['maps_name_child'];
        $time = date("Y-m-d H:i:s");
        
        $query_maps_group_add_check = "SELECT * FROM maps_group WHERE (parent_id = $maps_name_parent AND sub_id = $maps_name_child AND is_approve = 1  AND is_delete IS NULL) OR (parent_id = $maps_name_parent AND sub_id = $maps_name_child AND is_approve = 0  AND is_delete IS NULL)";   

        $run_maps_group_add_check = mysqli_query($conn, $query_maps_group_add_check);
            if(mysqli_num_rows($run_maps_group_add_check)==0){
                $query_maps_group_add = "INSERT INTO maps_group SET id = NULL, parent_id = $maps_name_parent, sub_id = $maps_name_child, is_approve = 0,  employee_id = $employee_id,create_time = '$time'";

                $run_maps_group_add = mysqli_query($conn, $query_maps_group_add);

                if(mysqli_affected_rows($conn)==1){
                    $successmessage = "MAPS Group item added";
                    $map_group_id = mysqli_insert_id($conn);

                    $query_cat_verify_alert = "INSERT INTO admin_alert SET admin_alert_id = NULL, admin_alert_time = '$time', admin_alert_link = 'MAPS-sheet.php?group=&cl_id=$url_client_id',employee_id= $employee_id, admin_alert_activity = 'new item group to $url_client_name.'";
                    $run_cat_verify_alert = mysqli_query($conn, $query_cat_verify_alert);
    
                    if(mysqli_affected_rows($conn)>0){
                        $alert_id = mysqli_insert_id($conn);
    
                        $query_cat_alert = "UPDATE maps_group SET admin_alert_id = $alert_id WHERE id = $map_group_id";

                        $run_cat_alert = mysqli_query($conn, $query_cat_alert);
                    }
                }else{
                    $message = "It couldn't be added due to technical reasons. Try Again!";
                }
            }else{
                $message = "The child item is already related to the Parent item. Check again.";
            }           
        

    }  

    if(isset($_REQUEST['addcatverify'])){ 
        if(strlen($_REQUEST['cat_name'])>2){
            $cat_name = $_REQUEST['cat_name'];
        $time = date("Y-m-d H:i:s");

        $query_cat_verify_check = "SELECT * FROM category WHERE (cat_name = '$cat_name' AND cat_is_delete IS NULL AND cat_approved = 1) OR (cat_name = '$cat_name' AND cat_is_delete IS NULL AND cat_approved = 0)";
        
        $run_cat_verify_check = mysqli_query($conn, $query_cat_verify_check);
        if(mysqli_num_rows($run_cat_verify_check)>0){
            $message = "Category Name Exists. Use a different Name";
            $addcatfail = "yes";

        }else{
            $query_cat_verify = "INSERT INTO category SET cat_id = NULL, cat_time = '$time', cat_name = '$cat_name', cat_approved = 0, employee_id = $employee_id";
            $run_cat_verify = mysqli_query($conn, $query_cat_verify);

            if(mysqli_affected_rows($conn)>0){
                $successmessage = "Category added.";
               
                $cate_id = mysqli_insert_id($conn);

                $query_cat_verify_alert = "INSERT INTO admin_alert SET admin_alert_id = NULL, admin_alert_time = '$time', admin_alert_link = 'MAPS-sheet.php?cat=&cl_id=$url_client_id',employee_id= $employee_id, admin_alert_activity = 'new Category $cat_name.'";
                $run_cat_verify_alert = mysqli_query($conn, $query_cat_verify_alert);

                if(mysqli_affected_rows($conn)>0){
                    $alert_id = mysqli_insert_id($conn);

                    $query_cat_alert = "UPDATE category SET admin_alert_id = $alert_id WHERE cat_id = $cate_id";
                    $run_cat_alert = mysqli_query($conn, $query_cat_alert);
                }
               
            }else{
                $message = "Technical Error Try Again";
                $addcatfail = "yes";
            }

        }       


        }else{
           $message = "Category Name should be more than 2 characters long";
           $addcatfail = "yes";
        }

        



    }

    if(isset($_REQUEST['addverify'])){       

        $maps_name = $_REQUEST['maps_name'];
        $cat_id = $_REQUEST['cat_name'];
      
        $description = $_REQUEST['description'];
        $client_allow_price = $_REQUEST['client_allow_price'];
        $min_bid_price = $_REQUEST['min_bid_price'];
        $crew_pay = $_REQUEST['crew_pay'];
        $photo_req = $_REQUEST['photo_req'];
        $time = date("Y-m-d H:i:s");
        if(strlen($description)== 0){
            $description=NULL;

        }

        if(strlen($client_allow_price)== 0){
            $client_allow_price = NULL;

        }
        if(strlen($min_bid_price)== 0){
            $min_bid_price=NULL;

        }

        if(strlen($crew_pay)== 0){
            $crew_pay=NULL;

        }
        if(strlen($photo_req)== 0){
            $photo_req=NULL;

        }

        if(isset($_REQUEST['maps_uom'])){
            if(strlen($_REQUEST['maps_uom'])>0){
            $maps_uom_id = $_REQUEST['maps_uom'];
            $query111 = "SELECT * FROM uom WHERE uom_id = $maps_uom_id";
             $run111 = mysqli_query($conn,$query111);
          
            
             if(mysqli_num_rows($run111)==1){

                 while($row = mysqli_fetch_array($run111)){
                    $maps_uom = $row['uom_name'];


                }

                

            }else{
                $maps_uom = "";
                $maps_uom_id = NULL;
            } 
        }
        }else{
            $maps_uom = "";
            $maps_uom_id = NULL;

        }
        if(strlen($maps_name)>0){
            $query233 = "SELECT * FROM maps WHERE maps_name= '$maps_name' AND cat_id = $cat_id AND client_id = $url_client_id AND maps_is_delete IS NULL AND (maps_approved = 1 OR maps_approved = 0)";
            $run233 = mysqli_query($conn, $query233);


            if(mysqli_num_rows($run233)==0){
                if(strlen($cat_id)>0){
                
                    $query = "SELECT * FROM category WHERE cat_id = $cat_id";
                    $run = mysqli_query($conn,$query);
    
                    $row = mysqli_fetch_array($run);
                    $cat_name = $row['cat_name']; 
                    if(strlen($_FILES["ref_photo"]["name"])==0){
                        $ref_photo = NULL;
                        
                        $query77 = "INSERT INTO maps SET maps_id = NULL, maps_approved = 0, employee_id = $employee_id, maps_name = '$maps_name', client_id = $url_client_id, maps_created_time ='$time',";
    
                    if($maps_uom_id != NULL){
                    $query771= "maps_crew_unit_uom_id = $maps_uom_id,";
                    }  else{
                        $query771= "";
                    }
                    if($description!=NULL){
                        $query772 = "maps_desc = '$description',";
                    } else{
                        $query772 = "";
    
                    }
                    if($client_allow_price != NULL){
                        $query773 = "maps_client_allow_price = $client_allow_price,";
                    } else{
                        $query773 = "";
    
                    }
                    if($min_bid_price != NULL){
                        $query774 = "maps_min_bid_price = $min_bid_price,";
                    } else{
                        $query774 = "";
    
                    }
                    if($photo_req != NULL){
                        $query775 = "maps_photo_req = '$photo_req',";
                    } else{
                        $query775 = "";
                    }
                    if($crew_pay != NULL){
                        $query776 = "maps_crew_pay = $crew_pay,";
                    }     else{
                        $query776 = "";
                    }              
                                    $query777 = "cat_id = $cat_id";
                    $query778 = $query77.$query771.$query772.$query773.$query774.$query775.$query776.$query777;
                                        $run77 = mysqli_query($conn, $query778);
                                        if(mysqli_affected_rows($conn)>0){

                                            $maps_id = mysqli_insert_id($conn);
                      
                if(isset($_REQUEST['maps_uom_val1'])){
                    
                    $maps_uom_val1 = $_REQUEST['maps_uom_val1'];

                    $query_uom_val1 = "SELECT * FROM uom_field WHERE uom_id = $maps_uom_id AND uom_field_name = 'maps_uom_val1'";  
                    $run_uom_val1 = mysqli_query($conn, $query_uom_val1);
                    $row_uom_value1 = mysqli_fetch_array($run_uom_val1);
                    $uom_field_id1 = $row_uom_value1['uom_field_id'];
                    
                    $query_uom_amt1 = "INSERT INTO maps_uom_value SET maps_uom_value_id = NULL,maps_uom_value_amount = '$maps_uom_val1' ,maps_id = $maps_id, uom_field_id = $uom_field_id1";
                    $run_uom_amt1 = mysqli_query($conn, $query_uom_amt1);

               
            }

                if(isset($_REQUEST['maps_uom_val2'])){
                    $maps_uom_val2 = $_REQUEST['maps_uom_val2'];

                    $query_uom_val2 = "SELECT * FROM uom_field WHERE uom_id = $maps_uom_id AND uom_field_name = 'maps_uom_val2'";  
                    $run_uom_val2 = mysqli_query($conn, $query_uom_val2);
                    $row_uom_value2 = mysqli_fetch_array($run_uom_val2);
                    $uom_field_id2 = $row_uom_value2['uom_field_id'];
                    
                    $query_uom_amt2 = "INSERT INTO maps_uom_value SET maps_uom_value_id = NULL, maps_uom_value_amount = '$maps_uom_val2', maps_id = $maps_id, uom_field_id = $uom_field_id2";
                    $run_uom_amt2 = mysqli_query($conn, $query_uom_amt2);
               
                }

                if(isset($_REQUEST['maps_uom_val3'])){
                    $maps_uom_val3 = $_REQUEST['maps_uom_val3'];

                    $query_uom_val3 = "SELECT * FROM uom_field WHERE uom_id = $maps_uom_id AND uom_field_name = 'maps_uom_val3'";  
                    $run_uom_val3 = mysqli_query($conn, $query_uom_val3);
                    $row_uom_value3 = mysqli_fetch_array($run_uom_val3);
                    $uom_field_id3 = $row_uom_value3['uom_field_id'];
                    
                    $query_uom_amt3 = "INSERT INTO maps_uom_value SET maps_uom_value_id = NULL, maps_uom_value_amount = '$maps_uom_val3', maps_id = $maps_id, uom_field_id = $uom_field_id3";
                    $run_uom_amt3 = mysqli_query($conn, $query_uom_amt3);

             }

                if(isset($_REQUEST['maps_uom_val4'])){
                   
                    $maps_uom_val4 = $_REQUEST['maps_uom_val4'];

                    $query_uom_val4 = "SELECT * FROM uom_field WHERE uom_id = $maps_uom_id AND uom_field_name = 'maps_uom_val4'";  
                    $run_uom_val4 = mysqli_query($conn, $query_uom_val4);
                    $row_uom_value4 = mysqli_fetch_array($run_uom_val4);
                    $uom_field_id4 = $row_uom_value4['uom_field_id'];
                    
                    $query_uom_amt4 = "INSERT INTO maps_uom_value SET maps_uom_value_id = NULL, maps_uom_value_amount = '$maps_uom_val4', maps_id = $maps_id, uom_field_id = $uom_field_id4";
                    $run_uom_amt4 = mysqli_query($conn, $query_uom_amt4);
                    
                }
                if(isset($_REQUEST['maps_uom_val5'])){
                    $maps_uom_val5 = $_REQUEST['maps_uom_val5'];

                    $query_uom_val5 = "SELECT * FROM uom_field WHERE uom_id = $maps_uom_id AND uom_field_name = 'maps_uom_val5'";  
                    $run_uom_val5 = mysqli_query($conn, $query_uom_val5);
                    $row_uom_value5 = mysqli_fetch_array($run_uom_val5);
                    $uom_field_id5 = $row_uom_value5['uom_field_id'];
                    
                    $query_uom_amt5 = "INSERT INTO maps_uom_value SET maps_uom_value_id = NULL, maps_uom_value_amount = '$maps_uom_val5', maps_id = $maps_id, uom_field_id = $uom_field_id5";
                    $run_uom_amt5 = mysqli_query($conn, $query_uom_amt5);
                    
                }

                if(isset($_REQUEST['maps_uom_val6'])){
                    $maps_uom_val6 = $_REQUEST['maps_uom_val6'];

                    $query_uom_val6 = "SELECT * FROM uom_field WHERE uom_id = $maps_uom_id AND uom_field_name = 'maps_uom_val6'";  
                    $run_uom_val6 = mysqli_query($conn, $query_uom_val6);
                    $row_uom_value6 = mysqli_fetch_array($run_uom_val6);
                    $uom_field_id6 = $row_uom_value6['uom_field_id'];
                    
                    $query_uom_amt6 = "INSERT INTO maps_uom_value SET maps_uom_value_id = NULL, maps_uom_value_amount = '$maps_uom_val6', maps_id = $maps_id, uom_field_id = $uom_field_id6";
                    $run_uom_amt6 = mysqli_query($conn, $query_uom_amt6);
                    
                }

                if(isset($_REQUEST['maps_uom_val7'])){
                    $maps_uom_val7 = $_REQUEST['maps_uom_val7'];

                    $query_uom_val7 = "SELECT * FROM uom_field WHERE uom_id = $maps_uom_id AND uom_field_name = 'maps_uom_val7'";  
                    $run_uom_val7 = mysqli_query($conn, $query_uom_val7);
                    $row_uom_value7 = mysqli_fetch_array($run_uom_val7);
                    $uom_field_id7 = $row_uom_value7['uom_field_id'];
                    
                    $query_uom_amt7 = "INSERT INTO maps_uom_value SET maps_uom_value_id = NULL, maps_uom_value_amount = '$maps_uom_val7', maps_id = $maps_id, uom_field_id = $uom_field_id7";
                    $run_uom_amt7 = mysqli_query($conn, $query_uom_amt7);
                    
                }
                if(isset($_REQUEST['maps_uom_val8'])){
                    $maps_uom_val8 = $_REQUEST['maps_uom_val8'];

                    $query_uom_val8 = "SELECT * FROM uom_field WHERE uom_id = $maps_uom_id AND uom_field_name = 'maps_uom_val8'";  
                    $run_uom_val8 = mysqli_query($conn, $query_uom_val8);
                    $row_uom_value8 = mysqli_fetch_array($run_uom_val8);
                    $uom_field_id8 = $row_uom_value8['uom_field_id'];
                    
                    $query_uom_amt8 = "INSERT INTO maps_uom_value SET maps_uom_value_id = NULL, maps_uom_value_amount = '$maps_uom_val8', maps_id = $maps_id, uom_field_id = $uom_field_id8";
                    $run_uom_amt8 = mysqli_query($conn, $query_uom_amt8);
                    
                }

                if(isset($_REQUEST['maps_uom_val9'])){
                    $maps_uom_val9 = $_REQUEST['maps_uom_val9'];

                    $query_uom_val9 = "SELECT * FROM uom_field WHERE uom_id = $maps_uom_id AND uom_field_name = 'maps_uom_val9'";  
                    $run_uom_val9 = mysqli_query($conn, $query_uom_val9);
                    $row_uom_value9 = mysqli_fetch_array($run_uom_val9);
                    $uom_field_id9 = $row_uom_value9['uom_field_id'];
                    
                    $query_uom_amt9 = "INSERT INTO maps_uom_value SET maps_uom_value_id = NULL, maps_uom_value_amount = '$maps_uom_val9', maps_id = $maps_id, uom_field_id = $uom_field_id9";
                    $run_uom_amt9 = mysqli_query($conn, $query_uom_amt9);
                    
                }
                $query_cat_verify_alert = "INSERT INTO admin_alert SET admin_alert_id = NULL, admin_alert_time = '$time',admin_alert_link = 'MAPS-sheet.php?cl_id=$url_client_id', employee_id= $employee_id, admin_alert_activity = 'new Job Item $maps_name to $url_client_name.'";
                $run_cat_verify_alert = mysqli_query($conn, $query_cat_verify_alert);
                if(mysqli_affected_rows($conn)>0){
                    $alert_id = mysqli_insert_id($conn);

                    $query_mape_alert = "UPDATE maps SET admin_alert_id = $alert_id WHERE maps_id = $maps_id";
                    $run_mape_alert = mysqli_query($conn, $query_mape_alert);

                    $successmessage = "Job Item Added";
                }else{
                    $message = "Technical Error Try Again";
                }
                
                      /* }else{
                        $message = "Some Error. Try Again111";
    
                    } */
            /*  */ 



                
                                            
                                        }else{
                                            $message = "Some Error. Try Again11";
    
                                        }
                            
                            
                            
            
            
                    }else{
                        $uniqueId= time().'-'.mt_rand();
                        $temp = explode(".", $_FILES["ref_photo"]["name"]);
                        $ref_photo = $uniqueId . '.' . end($temp);
                        
                        
                        $query77 = "INSERT INTO maps SET maps_id = NULL, maps_name = '$maps_name',maps_created_time ='$time',client_id = $url_client_id,maps_approved = 0, employee_id = $employee_id, maps_ref_pic='$ref_photo',";
    
                        if($maps_uom_id != NULL){
                        $query771= "maps_crew_unit_uom_id = $maps_uom_id,";
                        }  else{
                            $query771= "";
                        }
                        if($description!=NULL){
                            $query772 = "maps_desc = '$description',";
                        } else{
                            $query772 = "";
        
                        }
                        if($client_allow_price != NULL){
                            $query773 = "maps_client_allow_price = $client_allow_price,";
                        } else{
                            $query773 = "";
        
                        }
                        if($min_bid_price != NULL){
                            $query774 = "maps_min_bid_price = $min_bid_price,";
                        } else{
                            $query774 = "";
        
                        }
                        if($photo_req != NULL){
                            $query775 = "maps_photo_req = '$photo_req',";
                        } else{
                            $query775 = "";
                        }
                        if($crew_pay != NULL){
                            $query776 = "maps_crew_pay = $crew_pay,";
                        }     else{
                            $query776 = "";
                        }              
                                        $query777 = "cat_id = $cat_id";
                        $query778 = $query77.$query771.$query772.$query773.$query774.$query775.$query776.$query777;
                        $run77 = mysqli_query($conn, $query778);
                        if(mysqli_affected_rows($conn)>0){
                            $maps_id = mysqli_insert_id($conn);
                                    
if(isset($_REQUEST['maps_uom_val1'])){                                              
                                                                     
                                      
                                          $maps_uom_val1 = $_REQUEST['maps_uom_val1'];
                      
                                          $query_uom_val1 = "SELECT * FROM uom_field WHERE uom_id = $maps_uom_id AND uom_field_name = 'maps_uom_val1'";  
                                          $run_uom_val1 = mysqli_query($conn, $query_uom_val1);
                                          $row_uom_value1 = mysqli_fetch_array($run_uom_val1);
                                          $uom_field_id1 = $row_uom_value1['uom_field_id'];
                                          
                                          $query_uom_amt1 = "INSERT INTO maps_uom_value SET maps_uom_value_id = NULL,maps_uom_value_amount = '$maps_uom_val1' ,maps_id = $maps_id, uom_field_id = $uom_field_id1";
                                          $run_uom_amt1 = mysqli_query($conn, $query_uom_amt1);
                      
                                      }
                      
                                      if(isset($_REQUEST['maps_uom_val2'])){
                                        
                                          $maps_uom_val2 = $_REQUEST['maps_uom_val2'];
                      
                                          $query_uom_val2 = "SELECT * FROM uom_field WHERE uom_id = $maps_uom_id AND uom_field_name = 'maps_uom_val2'";  
                                          $run_uom_val2 = mysqli_query($conn, $query_uom_val2);
                                          $row_uom_value2 = mysqli_fetch_array($run_uom_val2);
                                          $uom_field_id2 = $row_uom_value2['uom_field_id'];
                                          
                                          $query_uom_amt2 = "INSERT INTO maps_uom_value SET maps_uom_value_id = NULL, maps_uom_value_amount = '$maps_uom_val2', maps_id = $maps_id, uom_field_id = $uom_field_id2";
                                          $run_uom_amt2 = mysqli_query($conn, $query_uom_amt2);
                      
                                      }
                      
                                      if(isset($_REQUEST['maps_uom_val3'])){
                                      
                                          $maps_uom_val3 = $_REQUEST['maps_uom_val3'];
                      
                                          $query_uom_val3 = "SELECT * FROM uom_field WHERE uom_id = $maps_uom_id AND uom_field_name = 'maps_uom_val3'";  
                                          $run_uom_val3 = mysqli_query($conn, $query_uom_val3);
                                          $row_uom_value3 = mysqli_fetch_array($run_uom_val3);
                                          $uom_field_id3 = $row_uom_value3['uom_field_id'];
                                          
                                          $query_uom_amt3 = "INSERT INTO maps_uom_value SET maps_uom_value_id = NULL, maps_uom_value_amount = '$maps_uom_val3', maps_id = $maps_id, uom_field_id = $uom_field_id3";
                                          $run_uom_amt3 = mysqli_query($conn, $query_uom_amt3);
                      
                                      }
                      
                                      if(isset($_REQUEST['maps_uom_val4'])){
                                        
                                          $maps_uom_val4 = $_REQUEST['maps_uom_val4'];
                      
                                          $query_uom_val4 = "SELECT * FROM uom_field WHERE uom_id = $maps_uom_id AND uom_field_name = 'maps_uom_val4'";  
                                          $run_uom_val4 = mysqli_query($conn, $query_uom_val4);
                                          $row_uom_value4 = mysqli_fetch_array($run_uom_val4);
                                          $uom_field_id4 = $row_uom_value4['uom_field_id'];
                                          
                                          $query_uom_amt4 = "INSERT INTO maps_uom_value SET maps_uom_value_id = NULL, maps_uom_value_amount = '$maps_uom_val4', maps_id = $maps_id, uom_field_id = $uom_field_id4";
                                          $run_uom_amt4 = mysqli_query($conn, $query_uom_amt4);
                                        
                                      }

                                      if(isset($_REQUEST['maps_uom_val5'])){
                                        $maps_uom_val5 = $_REQUEST['maps_uom_val5'];
                    
                                        $query_uom_val5 = "SELECT * FROM uom_field WHERE uom_id = $maps_uom_id AND uom_field_name = 'maps_uom_val5'";  
                                        $run_uom_val5 = mysqli_query($conn, $query_uom_val5);
                                        $row_uom_value5 = mysqli_fetch_array($run_uom_val5);
                                        $uom_field_id5 = $row_uom_value5['uom_field_id'];
                                        
                                        $query_uom_amt5 = "INSERT INTO maps_uom_value SET maps_uom_value_id = NULL, maps_uom_value_amount = '$maps_uom_val5', maps_id = $maps_id, uom_field_id = $uom_field_id5";
                                        $run_uom_amt5 = mysqli_query($conn, $query_uom_amt5);
                                        
                                    }

                                    if(isset($_REQUEST['maps_uom_val6'])){
                                        $maps_uom_val6 = $_REQUEST['maps_uom_val6'];
                    
                                        $query_uom_val6 = "SELECT * FROM uom_field WHERE uom_id = $maps_uom_id AND uom_field_name = 'maps_uom_val6'";  
                                        $run_uom_val6 = mysqli_query($conn, $query_uom_val6);
                                        $row_uom_value6 = mysqli_fetch_array($run_uom_val6);
                                        $uom_field_id6= $row_uom_value6['uom_field_id'];
                                        
                                        $query_uom_amt6 = "INSERT INTO maps_uom_value SET maps_uom_value_id = NULL, maps_uom_value_amount = '$maps_uom_val6', maps_id = $maps_id, uom_field_id = $uom_field_id6";
                                        $run_uom_amt6 = mysqli_query($conn, $query_uom_amt6);
                                        
                                    }

                                    if(isset($_REQUEST['maps_uom_val7'])){
                                        $maps_uom_val7 = $_REQUEST['maps_uom_val7'];
                    
                                        $query_uom_val7 = "SELECT * FROM uom_field WHERE uom_id = $maps_uom_id AND uom_field_name = 'maps_uom_val7'";  
                                        $run_uom_val7 = mysqli_query($conn, $query_uom_val7);
                                        $row_uom_value7 = mysqli_fetch_array($run_uom_val7);
                                        $uom_field_id7 = $row_uom_value7['uom_field_id'];
                                        
                                        $query_uom_amt7 = "INSERT INTO maps_uom_value SET maps_uom_value_id = NULL, maps_uom_value_amount = '$maps_uom_val7', maps_id = $maps_id, uom_field_id = $uom_field_id7";
                                        $run_uom_amt7 = mysqli_query($conn, $query_uom_amt7);
                                        
                                    }

                                    if(isset($_REQUEST['maps_uom_val8'])){
                                        $maps_uom_val8 = $_REQUEST['maps_uom_val8'];
                    
                                        $query_uom_val8 = "SELECT * FROM uom_field WHERE uom_id = $maps_uom_id AND uom_field_name = 'maps_uom_val8'";  
                                        $run_uom_val8 = mysqli_query($conn, $query_uom_val8);
                                        $row_uom_value8 = mysqli_fetch_array($run_uom_val8);
                                        $uom_field_id8= $row_uom_value4['uom_field_id'];
                                        
                                        $query_uom_amt8 = "INSERT INTO maps_uom_value SET maps_uom_value_id = NULL, maps_uom_value_amount = '$maps_uom_val8', maps_id = $maps_id, uom_field_id = $uom_field_id8";
                                        $run_uom_amt8 = mysqli_query($conn, $query_uom_amt8);
                                        
                                    }

                                    if(isset($_REQUEST['maps_uom_val9'])){
                                        $maps_uom_val9 = $_REQUEST['maps_uom_val9'];
                    
                                        $query_uom_val9 = "SELECT * FROM uom_field WHERE uom_id = $maps_uom_id AND uom_field_name = 'maps_uom_val9'";  
                                        $run_uom_val9 = mysqli_query($conn, $query_uom_val9);
                                        $row_uom_value9 = mysqli_fetch_array($run_uom_val9);
                                        $uom_field_id9 = $row_uom_value9['uom_field_id'];
                                        
                                        $query_uom_amt9 = "INSERT INTO maps_uom_value SET maps_uom_value_id = NULL, maps_uom_value_amount = '$maps_uom_val9', maps_id = $maps_id, uom_field_id = $uom_field_id9";
                                        $run_uom_amt9 = mysqli_query($conn, $query_uom_amt9);
                                        
                                    }
                                      $query_cat_verify_alert = "INSERT INTO admin_alert SET admin_alert_id = NULL, admin_alert_time = '$time',admin_alert_link = 'MAPS-sheet.php?cl_id=$url_client_id', employee_id= $employee_id, admin_alert_activity = 'new Job Item $maps_name to $url_client_name.'";
                $run_cat_verify_alert = mysqli_query($conn, $query_cat_verify_alert);
                if(mysqli_affected_rows($conn)>0){
                    $alert_id = mysqli_insert_id($conn);

                    $query_mape_alert = "UPDATE maps SET admin_alert_id = $alert_id WHERE maps_id = $maps_id";
                    $run_mape_alert = mysqli_query($conn, $query_mape_alert);

                    $successmessage = "Job Item Added";}
                
                                    
                                   /*  
                                    } */
                                      

/*                                                 $ref_photo = $_FILES["ref_photo"]["name"];
 */                                                $target_dir = getcwd().DIRECTORY_SEPARATOR. "maps_ref_image/";
                                                $target_file = $target_dir . $ref_photo;
                                                $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
                                    
                                                $check = getimagesize($_FILES["ref_photo"]["tmp_name"]);
                                                if($check !== false) {                                                  
                                                   
                                                        
                                    
                        if ($_FILES["ref_photo"]["size"] < 2024000) {
                                                            
                                                                    if($imageFileType == "jpg" || $imageFileType == "png" || $imageFileType == "jpeg") {
                                                                       
                                                                                if (move_uploaded_file($_FILES["ref_photo"]["tmp_name"], $target_file)) {
                                                                                    $successmessage = "Job Item Added";        
                                                                                } else {
                                                                                    $ref_photo = "";
                                                                                    $query_ref_photo_fail = "UPDATE maps SET maps_ref_pic = '$ref_photo' WHERE maps_id = $maps_id";
                                                                                    $run_ref_photo_fail = mysqli_query($conn, $query_ref_photo_fail);
                                                                                    $message =  "Reference Image upload has failed. Rest of details saved successfully. Upload it from edit item";
                                                                                    
                                                                               
                                                                                    }        
                                                                            
                                                                                   
                                                                    }else{
                                                                        $ref_photo = "";
                                                                        $query_ref_photo_fail = "UPDATE maps SET maps_ref_pic = '$ref_photo' WHERE maps_id = $maps_id";
                                                    $run_ref_photo_fail = mysqli_query($conn, $query_ref_photo_fail);
                                                                        $message =  "The file you upload should be JPG, JGEP or PNG";
                                                                        $addfail = "yes";
            

                                                                    }
                                                                 
                                                        }else{

                                                            $ref_photo = "";
                                                            $query_ref_photo_fail = "UPDATE maps SET maps_ref_pic = '$ref_photo' WHERE maps_id = $maps_id";
                                                    $run_ref_photo_fail = mysqli_query($conn, $query_ref_photo_fail);
                                                            $message =  "The file you have uploaded is too large. It should be smaller than 1024 KB";
                                                            $addfail = "yes";

                                                        }
                            
                                                    // Allow certain file formats
                                                   

                                    
                                                    
                                                
                                                } else {
                                                    $ref_photo = "";
                                                    $query_ref_photo_fail = "UPDATE maps SET maps_ref_pic = '$ref_photo' WHERE maps_id = $maps_id";
                                                    $run_ref_photo_fail = mysqli_query($conn, $query_ref_photo_fail);
                                                    
                                                    $message =  "The file you have uploaded is not an Image";
                                                    $addfail = "yes";
                            
                            
                                                    
                                                    
                                                 }
                                                
                                          




                                            }else{
                                                $message = "Some Error. Try Again1";
        
                                            }
                                
                                
                                
                
                }/* empty image */
    
    
                }else{
                    $cat_name = "";
                    $cat_id = NULL;
                    $message =  "Category Name cannot be empty";
                    $add_fail = "yes";
    
                }
            }else{
                
                
                $query = "SELECT * FROM category WHERE cat_id = $cat_id";
                    $run = mysqli_query($conn,$query);
    
                    $row = mysqli_fetch_array($run);
                    $cat_name = $row['cat_name']; 
                    $message =  "Job Name $maps_name already Exists in $cat_name Category";
                $maps_name = "";
                $add_fail = "yes";

            }



            
         
    }else{
        $maps_name = "";
        $message =  "Job Name cannot be empty";
        $add_fail = "yes";


    }

       

    }

}?>

<div id="page-wrapper">
<h3>Master Allowable Pricing Sheet (MAPS)</h3>

<!-- -->
<ul data-tabs>
<li><a <?php       
       
           if($url_client_id ==0){      
               
               echo "data-tabby-default";
               $client_id_tab_check = 'unset';
               
            }else{
                $client_id_tab_check = 'set';
            }    
            
            
            
            ?> href="#new00" id = "client_id_0">Main</a></li>

<?php
$query_client_find = "SELECT * FROM client WHERE is_approve = 1 AND client_is_delete IS NULL AND is_list = 1";

$run_client_find = mysqli_query($conn, $query_client_find);

if(mysqli_num_rows($run_client_find)>0){   
   
 while($row_client_find = mysqli_fetch_array($run_client_find)){
    $client_id_t =  $row_client_find['client_id'];
    $client_id_tab = "client_id_" . $client_id_t;
    $client_alias = $row_client_find['client_alias'];?>
        <li><a <?php        
        if($client_id_tab_check == 'set'){
            if($client_id_t == $url_client_id){
                echo "data-tabby-default ";
                $client_id_tab_check = 'unset';
            }else{
                $client_id_tab_check = 'set';
            }           
        }       
            ?>href="#new<?php echo $client_id_t.$client_id_t ?>" id="<?php echo $client_id_tab ?>"><?php echo $client_alias ?></a></li>
    <?php 
   }
}
?>       
 
   
</ul>

        <!--  active div (div start)-->
<?php 
if($url_client_id == 0){
    echo '<div id="new00">'; 
}else{
    echo '<div id="new00">';
    echo '<div id="old00">';
    echo "<p>Loading</p>";
    echo '</div>';
    echo '</div>';
    $query_client_find = "SELECT * FROM client WHERE client_id < $url_client_id AND is_approve = 1 AND client_is_delete IS NULL AND is_list = 1";

$run_client_find = mysqli_query($conn, $query_client_find);

if(mysqli_num_rows($run_client_find)>0){   
   
 while($row_client_find = mysqli_fetch_array($run_client_find)){
    $client_id_tab2 = $row_client_find['client_id'];

   ?>
        <div id="new<?php echo $client_id_tab2.$client_id_tab2?>">
        <div id="old<?php echo $client_id_tab2.$client_id_tab2?>">
        
        <p>Loading</p>
        </div>
        
        </div>
    <?php }
}

$query_client_find1 = "SELECT * FROM client WHERE client_id = $url_client_id AND is_approve = 1 AND client_is_delete IS NULL AND is_list = 1";

$run_client_find1 = mysqli_query($conn, $query_client_find1);

if(mysqli_num_rows($run_client_find1)>0){   
   
 while($row_client_find1 = mysqli_fetch_array($run_client_find1)){
    $client_id_tab21 = $row_client_find1['client_id'];

   ?>
        <div id="new<?php echo $client_id_tab21.$client_id_tab21?>">
       
    <?php }
}

} 
?>
<!--   -->
<br>   
<?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>';
                }
                    ?>              
    <div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                <!-- <div class="col-lg-12"> -->
                <h3>
        <?php if(isset($_REQUEST['new']) ||isset($add_fail)){
            
            echo "Add a Product to MAPS List<a style = 'float:right;' class='btn btn-warning' href='MAPS-main.php?cl_id=$url_client_id'>Cancel</a>";

        }elseif(isset($_REQUEST['newgroup']) ||isset($addgroupfail)){
            echo "Create a MAPS product group<a style = 'float:right;' class='btn btn-warning' href='MAPS-main.php?group=&cl_id=$url_client_id'>Cancel</a>";
            
        }elseif(isset($_REQUEST['newcat']) ||isset($addcatfail)){
            echo "Add a Category to MAPS<a style = 'float:right;' class='btn btn-warning' href='MAPS-main.php?cat=&cl_id=$url_client_id'>Cancel</a>";
            
        }elseif(isset($_REQUEST['cat']) || isset($_REQUEST['conf-cat-delete'])|| isset($_REQUEST['editcatverify'])|| isset($_REQUEST['addcatverify'])){
            echo "<a href='MAPS-main.php?newcat=&cl_id=$url_client_id' class='btn btn-success' role='button'>Add Category</a>&nbsp &nbsp<a href='MAPS-main.php?cl_id=$url_client_id' class='btn btn-success' role='button'>Manage Products</a>";
            
        }elseif(isset($_REQUEST['group']) || isset($_REQUEST['addgroupverify'])){
            echo "<a href='MAPS-main.php?newgroup=&cl_id=$url_client_id' class='btn btn-success' role='button'>Create Product Group</a>&nbsp &nbsp<a href='MAPS-main.php?cl_id=$url_client_id' class='btn btn-success' role='button'>Back to Products</a>";
            
        }else{
            echo "<a href='MAPS-main.php?new=&cl_id=$url_client_id' class='btn btn-success' role='button'>Add Product</a>&nbsp &nbsp<a href='MAPS-main.php?group=&cl_id=$url_client_id' class='btn btn-success' role='button'>Product Groups</a>&nbsp &nbsp<a href='MAPS-main.php?cat=&cl_id=$url_client_id' class='btn btn-success' role='button'>Manage Categories</a>";
           
        } ?> </h3>
        </div>                                                               

             

               <?php 
               if(isset($_REQUEST['edit']) || isset($editfail)|| isset($addfail)){
                   echo '<div class="panel-body">                      
                   <div class = "row">            
            
                        
      <div class="col-lg-12">';
                echo "<form action = 'MAPS-main.php?editverify=$maps_id&cl_id=$url_client_id' method = 'POST' class = 'form-group' enctype = 'multipart/form-data'>";
                echo "<input type = 'hidden' name='maps_id' id='maps_id' value='$maps_id' >";
               }else if(isset($_REQUEST['new']) || isset($add_fail)){
                echo '<div class="panel-body">                      
                   <div class = "row">                       
                        
      <div class="col-lg-12">';
                echo "<form action = 'MAPS-main.php?addverify=&cl_id=$url_client_id' method = 'POST' class = 'form-group' enctype = 'multipart/form-data'>";

               }else if(isset($_REQUEST['newcat']) || isset($addcatfail)){
                echo '<div class="panel-body">                      
                   <div class = "row">                       
                        
      <div class="col-lg-12">';
                echo "<form action = 'MAPS-main.php?addcatverify=&cl_id=$url_client_id' method = 'POST' class = 'form-group' enctype = 'multipart/form-data'>";

               }else if(isset($_REQUEST['editcat']) || isset($editcatfail)){
                echo '<div class="panel-body">                      
                   <div class = "row">                       
                        
      <div class="col-lg-12">';
                echo "<form action = 'MAPS-main.php?editcatverify=$cat_id&cl_id=$url_client_id' method = 'POST' class = 'form-group' enctype = 'multipart/form-data'>";
                echo "<input type = 'hidden' name='cat_id' id='cat_id' value='$cat_id' >";
               }else if(isset($_REQUEST['newgroup'])){
                echo '<div class="panel-body">                      
                   <div class = "row">                       
                        
      <div class="col-lg-12">';
                echo "<form action = 'MAPS-main.php?addgroupverify=&cl_id=$url_client_id' method = 'POST' class = 'form-group' enctype = 'multipart/form-data'>";

               }                    
               
               ?>


            <?php
if(isset($_REQUEST['newgroup'])){
    ?> 
    <div class="row">

<div class="form-group col-lg-6">
<label for="cat_name_parent">Choose Category of Parent Item</label>     
     
            <select name="cat_name_parent" id="cat_name_parent" onChange="getParentItemValue();" required autofocus >

 <?php              
             echo '<option value="" disabled="disabled" selected="selected">Select Category </option>';         

                 
               $query00 = "SELECT * FROM category WHERE cat_is_delete IS NULL AND cat_approved = 1";
               $run00 = mysqli_query($conn,$query00);

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

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

</div>

    </div>
<div class="row">

<div class="form-group col-lg-6">
<div id = "hiddenCatInput" style = "display:none">
<label for="cat_name_child">Choose Category of Child Item</label>     
     
            <select name="cat_name_child" id="cat_name_child" onChange="getChildItemValue();" required>

 <?php              
             echo '<option value="" disabled="disabled" selected="selected">Select Category </option>';         

                 
               $query00 = "SELECT * FROM category WHERE cat_is_delete IS NULL AND cat_approved = 1";
               $run00 = mysqli_query($conn,$query00);

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

    </select>
</div>

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

</div>

    </div>
    <div class = "row"  id = "hiddenSubmitGroup" style = "display:none">
                
                <div class = "form-group col-lg-6">
                <?php 
                 if(isset($_REQUEST['newgroup']) || isset($addgroupfail)){
                     echo '<input type="submit" name = "submit" value = "Add Item Group" class="btn btn-success">';
                   }
                   ?>
                </div>
                
                
                <div class = "form-group col-lg-6">
                </div>
     
                </div><!-- row inside form -->

    </form>
           </div><!-- col 12  -->
           </div><!-- row  -->
</div><!-- panel body -->  

<?php
}

if(isset($_REQUEST['editcat']) || isset($editcatfail) || isset($_REQUEST['newcat']) || isset($addcatfail)){
?>  
<?php  ?>       

<div class="row">

<div class="form-group col-lg-6">
<label for="maps_name">Category Name</label>

     
       <input type='text' class='form-control' name='cat_name' id='cat_name' value='<?php echo $cat_name?>' required>
</div>
<div class="form-group col-lg-6">
       
</div>

    </div><!--  -->
         
              
            <div class = "row">
                
           <div class = "form-group col-lg-6">
           <?php 
              if(isset($_REQUEST['editcat']) || isset($editcatfail)){
                  echo '<input type="submit" name = "submit" value = "Edit Category" class="btn btn-success">';

            }else if(isset($_REQUEST['newcat']) || isset($addcatfail)){
                echo '<input type="submit" name = "submit" value = "Add Category" class="btn btn-success">';
              }
              ?>
           </div>
           
           
           <div class = "form-group col-lg-6">
           </div>

           </div><!-- row inside form -->
               
           
            
           </form>
           </div><!-- col 12  -->
           </div><!-- row  -->
</div><!-- panel body -->             
           <?php  }   ?>
<!-- panel-body  -->

   <?php

if(isset($_REQUEST['edit']) || isset($editfail) || isset($_REQUEST['new']) || isset($addfail) || isset($add_fail)){
?>  
<?php  ?>
<div class="row">

      <div class="form-group col-lg-6">
      <?php 
               if(strlen($ref_photo)>3){
                echo '<label for="ref_photo">Change Reference Photo</label><br>';
                echo "<img src = 'maps_ref_image.php?i=".$ref_photo."' height = '100px' width = '150px'><br><br>";

               }else{
                echo '<label for="ref_photo">Upload Reference Photo</label>';

               }
               
        ?>

                           <!-- <input class="upload-file" data-max-size="1024" type="file" name="ref_photo" id="ref_photo"> -->
                           <br>


            <!--                <label class="btn btn-primary" for = "ref_photo">
    Browse --><input class="btn btn-success" data-max-size="1024" accept="image/*" type="file" name="ref_photo" id="ref_photo" ><!-- </label> -->
    </div>
    <div class="form-group col-lg-6">
             
    </div>
    
          </div><!--  -->

          <div class="row">

<div class="form-group col-lg-6">
<label for="cat_name">Category</label>     
     
            <select name="cat_name" id="cat_name" required autofocus>

 <?php 
               if(strlen($cat_name)>0){
                  
                echo "<option value = '$cat_id' selected='selected'>$cat_name</option>";

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

               }   
               $query00 = "SELECT * FROM category WHERE cat_is_delete IS NULL AND cat_approved = 1";
               $run00 = mysqli_query($conn,$query00);

               if(mysqli_num_rows($run00)>0){
                while($row00 = mysqli_fetch_array($run00)){
                     if(!($cat_name == $row00['cat_name'])){ 
                    echo "<option value='".$row00['cat_id']."'>". $row00['cat_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="maps_name">Job Task</label>

     
       <input type='text' class='form-control' name='maps_name' id='maps_name' value='<?php echo $maps_name?>' required>
</div>
<div class="form-group col-lg-6">
       
</div>

    </div><!--  -->
<div class="row">

      <div class="form-group col-lg-6">
      <label for="description">Job Description</label>

            <textarea rows="5" name="description" class = "form-control" id="description" placeholder=""  maxlength="5000"><?php echo $description; ?></textarea>
    </div>

    <div class="form-group col-lg-6">
    <label for="photo_req">Picture Requirement</label>
    <textarea rows="5" name="photo_req" class = "form-control" id="photo_req" placeholder=""  maxlength="5000"><?php echo $photo_req; ?></textarea>
    </div>
    
          </div><!--  -->

        <div class="row" id = "UOMsetDeleteRow">

<div class="form-group col-lg-4">
<label for="client_allow_price">Client Allowable Price</label>

     
       <input type='text' class='form-control' name='client_allow_price' id='client_allow_price' value='<?php echo $client_allow_price?>'>
</div>
<div class="form-group col-lg-4">
<label for="min_bid_price">Minimum Bid Price</label>

     
<input type='text' class='form-control' name='min_bid_price' id='min_bid_price' value='<?php echo $min_bid_price?>' >
</div>
<div class="form-group col-lg-4">
<label for="crew_pay">Crew Pay</label>

     
<input type='text' class='form-control' name='crew_pay' id='crew_pay' value='<?php echo $crew_pay?>' >

</div>
    </div><!--  -->

 <div class="row">

<div class="form-group col-lg-6">
<label for="maps_uom">UOM</label>     
     
            <select name="maps_uom" id="maps_uom" class="maps_uom" onChange="getUOMvalue();">

 <?php 
               if(strlen($maps_uom)>0){
                  
                echo "<option value = '$maps_uom_id' selected='selected'>$maps_uom</option>";
                
                
               }else{
                   echo '    <option value="" disabled="disabled" selected="selected">Select UOM </option>
                   ';               

               }   
               $query00 = "SELECT * FROM uom WHERE uom_is_delete IS NULL";
               $run00 = mysqli_query($conn,$query00);

               if(mysqli_num_rows($run00)>0){
                while($row00 = mysqli_fetch_array($run00)){
                     if(!($maps_uom == $row00['uom_name'])){ 
                    echo "<option value='".$row00['uom_id']."'>". $row00['uom_name']."</option>";
                     }
                }
               }  
               ?>
               <option value="">None </option>

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

    </div>

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

        
             
              
            <div class = "row">
                
           <div class = "form-group col-lg-6">
           <?php 
              if(isset($_REQUEST['edit']) || isset($editfail)|| isset($addfail)){
                  echo '<input type="submit" name = "submit" value = "Edit Product" class="btn btn-success">';

            }else if(isset($_REQUEST['new']) || isset($add_fail)){
                echo '<input type="submit" name = "submit" value = "Add Product" class="btn btn-success">';
              }
              ?>
           </div>
           
           
           <div class = "form-group col-lg-6">
           </div>

           </div><!-- row inside form -->
               
           
            
           </form>
           </div><!-- col 12  -->
           </div><!-- row  -->
</div><!-- panel body -->             
           <?php  }   ?>
<!-- panel-body  -->
            </div> <!-- panel panel-default -->
       </div> <!--  col lg 12-->
   </div> <!-- row -->
  
   <?php
if(isset($_REQUEST['group']) || isset($_REQUEST['newgroup'])|| isset($_REQUEST['editgroup'])|| isset($editgroupfail)|| isset($addgroupfail)||  isset($_REQUEST['editgroupverify'])|| isset($_REQUEST['addgroupverify'])){ ?>
    <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-bidwiz">
                                <thead>
                                    <tr>
                                   
                                            
                                         
                                            <th>
                                            Main Item

                                            </th>
                                            <th>
                                            Sub Items

                                            </th>
                                           
                                        
                                        </tr>
                                    </thead>
                                    <tbody>
<?php
if($conn){
   $query_maps = "SELECT * FROM maps WHERE client_id = $url_client_id AND maps_is_delete IS NULL AND maps_approved = 1 ORDER BY maps_name"; 

   $run_maps = mysqli_query($conn, $query_maps); 
   if(mysqli_num_rows($run_maps)>0){
       foreach($run_maps as $mapps){
            $maps_ID = $mapps['maps_id'];
            $maps_NAME = $mapps['maps_name'];
            $query_maps_group = "SELECT * FROM maps_group WHERE parent_id = $maps_ID AND is_delete IS NULL AND is_approve = 1"; 
            $run_maps_group = mysqli_query($conn, $query_maps_group);
            if(mysqli_num_rows($run_maps_group)>0){
                echo '<tr class="odd gradeX">';
                       
                echo '<td>' .$maps_NAME. '</td>';
                echo '<td>';                                 

                while($row = mysqli_fetch_array($run_maps_group)){
                    $sub_id = $row['sub_id'];
                    $query_maps1 = "SELECT maps_name FROM maps WHERE maps_id = $sub_id"; 
                    $run_maps1 = mysqli_query($conn, $query_maps1); 
                    if(mysqli_num_rows($run_maps1) == 1){
                        foreach($run_maps1 as $mappss){
                            $sub_NAME = $mappss['maps_name'];                           
                            echo $sub_NAME. ", ";                                   
                            
                        }
                    }                    
                }
                echo '</td>';  
                echo '</tr>';
               
            }
        }

   }


   
}
?>
                                        
                                        </tbody>                                    
                                </table>                                    
                                </div><!-- panel-body  -->
             </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-head">
        <h4>&nbsp&nbspPending Approval</h4>
        </div>
        <div class="panel-body">

                        <table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-bidwiz2">
                                <thead>
                                    <tr>
                                   
                                            <th>
                                            Main Item

                                            </th>
                                            <th>
                                            Sub Item

                                            </th>
                                           
                                        
                                           
                                        
                                        </tr>
                                    </thead>
                                    <tbody>
<?php
if($conn){
    $query_maps = "SELECT * FROM maps WHERE client_id = $url_client_id AND maps_is_delete IS NULL AND maps_approved = 1 ORDER BY maps_name"; 
 
    $run_maps = mysqli_query($conn, $query_maps); 
    if(mysqli_num_rows($run_maps)>0){
        foreach($run_maps as $mapps){
             $maps_ID = $mapps['maps_id'];
             $maps_NAME = $mapps['maps_name'];
             $query_maps_group = "SELECT * FROM maps_group WHERE parent_id = $maps_ID AND is_delete IS NULL AND is_approve = 0 AND employee_id = $employee_id"; 
             $run_maps_group = mysqli_query($conn, $query_maps_group);
             if(mysqli_num_rows($run_maps_group)>0){                                            
 
                 while($row = mysqli_fetch_array($run_maps_group)){
                     $sub_id = $row['sub_id'];
                     $maps_group_id = $row['id'];
                     $EMP_id = $row['employee_id'];
                     $query111 = "SELECT employee_fname,employee_lname FROM employee WHERE employee_id = $EMP_id";
                     $run111 = mysqli_query($conn, $query111);
                     $row111 = mysqli_fetch_array($run111);            
                     $EMP_name = $row111['employee_fname'] .' '.$row111['employee_lname'];
                     $query_maps1 = "SELECT maps_name FROM maps WHERE maps_id = $sub_id"; 
                     $run_maps1 = mysqli_query($conn, $query_maps1); 
                     if(mysqli_num_rows($run_maps1) == 1){
                         foreach($run_maps1 as $mappss){
                             $sub_NAME = $mappss['maps_name'];   
                             echo '<tr class="odd gradeX" style = "background-color:#ffbb96">';
                                          
                             echo '<td>' .$maps_NAME. '</td>';
                             echo '<td>';                                  
                             echo $sub_NAME;  
                             echo '</td>'; 
                              
                             echo '</tr>';                               
                             
                         }
                     }                    
                 }
                 
                
             }
         }
 
    }
    
 } 
?>
                                        
                                        </tbody>                                    
                                </table>                                    
                                </div><!-- panel-body  -->
             </div> <!-- panel panel-default -->
        </div> <!--  col lg 12-->
    </div> <!-- row -->  
<?php
}elseif(isset($_REQUEST['cat']) || isset($_REQUEST['newcat'])|| isset($_REQUEST['editcat'])|| isset($_REQUEST['deletecat'])|| isset($_REQUEST['conf-cat-delete'])|| isset($editcatfail)|| isset($addcatfail)|| isset($deletecatfail)|| isset($_REQUEST['editcatverify'])|| isset($_REQUEST['addcatverify'])){
?>                            
     <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-bidwiz1">
                                    <thead>
                                        <tr>                                    
                                             
                                                <th>
                                                Category Name

                                                </th>
                                            
                                               
                                            
                                            </tr>
                                        </thead>
                                        <tbody>
<?php
    if($conn){
        
       $query = "SELECT * FROM category WHERE cat_is_delete IS NULL AND cat_approved= 1 ORDER BY cat_name"; 
        $run = mysqli_query($conn, $query);
        if(mysqli_num_rows($run)>0){
            while($row = mysqli_fetch_array($run)){   
                $cat_id = $row['cat_id'];
                $cat_name = $row['cat_name'];               
                $description = $row['cat_desc']; 
                if($row['cat_approved'] == 1){
                    $cell_color = '';    
                  }else{
                    $cell_color = '#ff976e';
                  }                      
                                
                                
                
                                echo '<tr class="odd gradeX">';     
           
                             
                echo '<td>' .$cat_name. '</td>';
                                  
                echo '</tr>';
            }
        }
    }
?>
                                            
                                            </tbody>                                    
                                    </table>                                    
                                    </div><!-- panel-body  -->
                 </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-head">
                <br>
                <h4>&nbsp&nbspItems Pending Admin Approval</h4>
</div>
            <div class="panel-body">

                            <table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-bidwiz2">
                                    <thead>
                                        <tr>                                    
                                             
                                                <th>
                                                Category Name

                                                </th>
                                            
                                               
                                            
                                            </tr>
                                        </thead>
                                        <tbody>
<?php
    if($conn){
        
       $query = "SELECT * FROM category WHERE cat_is_delete IS NULL AND cat_approved= 0 AND employee_id = $employee_id ORDER BY cat_name"; 
        $run = mysqli_query($conn, $query);
        if(mysqli_num_rows($run)>0){
            while($row = mysqli_fetch_array($run)){   
                $cat_id = $row['cat_id'];
                $cat_name = $row['cat_name'];               
                $description = $row['cat_desc']; 
                if($row['cat_approved'] == 1){
                    $cell_color = '';    
                  }else{
                    $cell_color = '#ff976e';
                  }                      
                                
                                
                
                                echo '<tr class="odd gradeX" style = "background-color:#ff976e">';                 
                echo '<td>' .$cat_name. '</td>';
                                  
                echo '</tr>';
            }
        }
    }
?>
                                            
                                            </tbody>                                    
                                    </table>                                    
                                    </div><!-- panel-body  -->
                 </div> <!-- panel panel-default -->
            </div> <!--  col lg 12-->
        </div> <!-- row -->
        
        
        
        <?php  }else{   ?>                               
     <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-bidwiz-MAPS-main">
                                    <thead>
                                        <tr>
                                       
                                        
                                                <th>
                                                Reference Photo
                                                </th>
                                                <th>
                                                Category

                                                </th>
                                                <th>
                                                Job Task     
                                                </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>
                                            </tbody>
                                    
                                    </table>
                                    
                                    </div><!-- panel-body  -->
                 </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-head">
                <br>
                <h4>&nbsp&nbspItems Pending Admin Approval</h4>
</div>
            <div class="panel-body">
                            <table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-bidwiz2">
                                    <thead>
                                        <tr>
                                        <th>
                                                Job Task     
                                                </th>
                                                <th>
                                                Reference Photo
                                                </th>
                                                <th>
                                                Category

                                                </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.maps_is_delete IS NULL AND maps.client_id = $url_client_id AND maps.maps_approved = 0 AND maps.employee_id = $employee_id 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'];
                $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'];
}

}
  if($row['maps_approved'] == 1){
    $cell_color = '';    
  }else{
    $cell_color = '#ff976e';
  }                        
                
                

                echo '<tr class="odd gradeX" style = "background-color:#ff976e">';
               
               echo '<td>' .$maps_name. '</td>';
 
                echo '<td><img src = "maps_ref_image.php?i='.$ref_photo.'" height = "80px" width = "120px">'. '</td>';
                echo '<td>' .$cat_name. '</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 -->
        
        <?php  }   ?>

        <?php
if($url_client_id == 0){?>
 </div>
  <!--  active div (div end)-->
<?php
   $query_client_find3 = "SELECT * FROM client WHERE client_id > $url_client_id AND is_approve = 1 AND client_is_delete IS NULL AND is_list = 1";

   $run_client_find3 = mysqli_query($conn, $query_client_find3);

   if(mysqli_num_rows($run_client_find3)>0){   
   
       while($row_client_find3 = mysqli_fetch_array($run_client_find3)){
           $client_id_tab23 = $row_client_find3['client_id'];?>
           <div id="new<?php echo $client_id_tab23.$client_id_tab23?>">
           <div id="old<?php echo $client_id_tab23.$client_id_tab23?>">
           
           <p>Loading</p>
           </div>
           
           </div>
<?php }

   }


    }else{
        echo "</div><!--  active div (div end)-->";
        
        $query_client_find3 = "SELECT * FROM client WHERE client_id > $url_client_id AND is_approve = 1 AND client_is_delete IS NULL AND is_list = 1";

        $run_client_find3 = mysqli_query($conn, $query_client_find3);

        if(mysqli_num_rows($run_client_find3)>0){   
        
            while($row_client_find3 = mysqli_fetch_array($run_client_find3)){
                $client_id_tab23 = $row_client_find3['client_id'];?>
                <div id="new<?php echo $client_id_tab23.$client_id_tab23?>">
                <div id="old<?php echo $client_id_tab23.$client_id_tab23?>">
                
                <p>Loading</p>
                </div>
                
                </div>
    <?php }
    
        }
    } 
?> 
    </div> <!--  page wrapper -->
                              
       
        <script>
    var deleteLinks = document.querySelectorAll('.delete');

for (var i = 0; i < deleteLinks.length; i++) {
  deleteLinks[i].addEventListener('click', function(event) {
	  event.preventDefault();

	  var choice = confirm(this.getAttribute('data-confirm'));

	  if (choice) {
	    window.location.href = this.getAttribute('href');
	  }
  });
}
    </script>
    <script>
    function readURL(input) {
            if (input.files && input.files[0]) {
                var reader = new FileReader();

                reader.onload = function (e) {
                    $('#upload')
                        .attr('src', e.target.result)
                        .width(600)
                        .height(300);
                };

                reader.readAsDataURL(input.files[0]);
            }
        }
    </script>
<?php  include "footer.php";   ?>