<?php 



  include('libraryPHP/site_config.php');

  include('libraryPHP/session_check_url.php');





?>


<!DOCTYPE html>

<html lang="en">



<head>

    <meta charset="utf-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0">

   <title><?php echo $appName?> | <?php echo $pageTitle?></title>
 <?php echo $appFaviconHtml ?>


    <!-- theme css start -->

    <?php include("commoncss.php"); ?>

    <!-- theme css end -->

    <style>

        .eye {

            position: absolute;

            right: 4%;

            transform: translate(0, -176%);

            top: auto;

            cursor: pointer;

        }



        #togglePassword1 {

            position: absolute;

            right: 52%;

            transform: translate(0, -176%);

            top: auto;

            cursor: pointer;

        }



        #togglePassword2 {

            position: absolute;

            right: 154%;

            transform: translate(0, -174%);

            top: auto;

            cursor: pointer;

        }



        #togglePassword3 {

            position: absolute;

            right: 52%;

            transform: translate(0, -176%);

            top: auto;

            cursor: pointer;

        }



        #togglePassword4 {

            position: absolute;

            right: 154%;

            transform: translate(0, -174%);

            top: auto;

            cursor: pointer;

        }



        #togglePassword5 {

            position: absolute;

            right: 52%;

            transform: translate(0, -174%);

            top: auto;

            cursor: pointer;

        }



        #togglePassword6 {

            position: absolute;

            right: 154%;

            transform: translate(0, -174%);

            top: auto;

            cursor: pointer;

        }
          


    </style>




</head>



<body>



    <!-- Main Wrapper -->

    <div class="main-wrapper">



        <!-- Header start  -->

        <?php include("header.php"); ?>

        <!-- /Header end -->



        <!-- Sidebar -->

        <?php include("sidebar.php"); ?>

        <!-- /Sidebar -->



        <!-- Page Wrapper -->

        <div class="page-wrapper">



            <div class="content container-fluid">



                <!-- Page Header -->

                <div class="page-header">

                    <div class="row">

                        <div class="col">

                            <h3 class="page-title">Edit Inspection</h3>

                            <ul class="breadcrumb">

                                <li class="breadcrumb-item"><a href="dashboard.php">Dashboard</a></li>
                                   <li class="breadcrumb-item"><a href="manage_inspection.php">Manage Inspection</a></li>

                                <li class="breadcrumb-item active">Edit Inspection</li>
                            </ul>

                        </div>

                    </div>

                </div>

                <!-- /Page Header -->



                <!-- start write start -->

                <div class="col-12">



                    <div class="card">



                        <div class="card-body">

                           



                               <form method="POST" id="add_subadmin_form" class="form-horizontal"
                                                enctype="multipart/form-data">
                                                <input required="required" type="hidden" id="action" name="action"
                                                    value="edit_inspection">

                                                      <input required="required" type="hidden" id="id" name="id"
                                                    value="<?php echo $getEditEqData['inspection_id']?>">


                      <div class="form-group row">

                            <label class="col-form-label col-md-2">Select Enginner</label>

                            <div class="col-md-10">
                             <select class="form-control" style="width: 100%;" name="eng" id="eng" >
                                <option value="" selected hidden disabled="">Choose Enginner </option> 

                                <?php 
                                $user_id    =   0; 
                                $name           =   '';
                                if(!empty($getEditData['user_ary'])){
                                    foreach($getEditData['user_ary'] as $value) {
                                        $user_id    =   $value['user_id'];
                                        $name           =   $value['name'];
                                        $mobile           =   $value['mobile'];
                                        
                                        ?>
                                        
                                        <option value="<?php echo $user_id;?>" <?php if($getEditEqData['user_id'] == $user_id){ echo 'selected';}?>><?php echo $name.'('.$mobile.')';?></option> 

                                        <?php  
                                    }
                                } 
                                ?>  

                            </select>

                        </div>

                    </div>


                                                     <div class="form-group row">

                                                        <label class="col-form-label col-md-2">Select Equipment Type</label>

                                                        <div class="col-md-10">
                                                             <select class="form-control" style="width: 100%;" name="etype" id="etype" onchange="findCategory(this)">
                                                                <option value="" selected hidden disabled="">Choose Equipment Type</option> 

                                                                <?php 
                                                                        $user_id    =   0; 
                                                                        $name           =   '';
                                                                        if(!empty($getEditData1['cat_ary'])){
                                                                        foreach($getEditData1['cat_ary'] as $value) {
                                                                        $category_id    =   $value['category_id'];
                                                                        $category_name           =   $value['category_name'];
                                                                  
                                                                ?>
                                                                 
                                                             <option value="<?php echo $category_id;?>" <?php if($getEditEqData['equipment_type_id'] == $category_id){ echo 'selected';}?>><?php echo $category_name;?></option> 

                                                                 <?php  
                                                                       }
                                                                    } 
                                                                ?>  

                                                             </select>

                                                        </div>

                                                    </div>

                                                      <div class="form-group row">

                            <label class="col-form-label col-md-2">Select Equipment </label>

                            <div class="col-md-10">
                             <select class="form-control" style="width: 100%;" name="equipment" id="equipment">
                                <option value="" selected hidden disabled="">Choose Equipment </option> 

                                                                <?php 
                                                                        $user_id    =   0; 
                                                                        $name           =   '';
                                                                        if(!empty($getEditEqData['epq_ary'])){
                                                                        foreach($getEditEqData['epq_ary'] as $value) {
                                                                        $equipment_id    =   $value['equipment_id'];
                                                                        $equipment_name           =   $value['equipment_name'];
                                                                  
                                                                ?>
                                                                 
                                                             <option value="<?php echo $equipment_id;?>" <?php if($getEditEqData['equipment_id'] == $equipment_id){ echo 'selected';}?>><?php echo $equipment_name;?></option> 

                                                             <?php  
                                    }
                                } 
                                ?>    

                            </select>

                        </div>

                    </div>

                    <div class="form-group row">

                            <label class="col-form-label col-md-2">Select Inspection Type</label>

                            <div class="col-md-10">
                             <select class="form-control" style="width: 100%;" name="instype" id="instype" >
                                <option value="" selected hidden disabled="">Choose Inspection Type</option> 

                                <?php 
                                $user_id    =   0; 
                                $name           =   '';
                                if(!empty($getEditData2['ins_ary'])){
                                    foreach($getEditData2['ins_ary'] as $value) {
                                        $inspection_type_id    =   $value['inspection_type_id'];
                                        $inspection_type           =   $value['inspection_type'];
                                        
                                        ?>
                                        
                                        <option value="<?php echo $inspection_type_id;?>" <?php if($getEditEqData['inspection_type_id'] == $inspection_type_id){ echo 'selected';}?>><?php echo $inspection_type;?></option> 

                                        <?php  
                                    }
                                } 
                                ?>  

                            </select>

                        </div>

                    </div>


                                                      <div class="form-group row">

                      
                     <div class="form-group row">

                        <label class="col-form-label col-md-2">Inspection Title</label>

                        <div class="col-md-10">

                            <input type="text" class="form-control"  placeholder="Enter Inspection Title" name="ititle" id="ititle"  value="<?php echo $getEditEqData['inspection_title']?>">

                        </div>

                    </div>


                     <div class="form-group row">

                        <label class="col-form-label col-md-2">Inspection Description</label>

                        <div class="col-md-10">
                                 <textarea rows="5" cols="5" class="form-control" placeholder="Enter description" name="desp"><?php echo $getEditEqData['description']?> </textarea>
                        </div>

                    </div>
                    <div class="form-group row">

                        <label class="col-form-label col-md-2">Inspection Date</label>

                        <div class="col-md-10">

                            <input type="date" class="form-control" placeholder="Enter Inspection Date" name="idate" id="idate"  value="<?php echo $getEditEqData['inspection_date']?>">

                        </div>

                    </div>








                         


                  

                     <div class="form-group row">

                        <label class="col-form-label col-md-2"> Inspection Due Date</label>

                        <div class="col-md-10">

                          

                                <div class="field_wrapper">
      <div class="d-flex ml-3">
        <div class="form-group ">

<!--             <textarea class="form-control" id="product_name" placeholder="Enter question"
              name="question[]" rows="5" cols="70"></textarea> -->
          
          <!-- <input type="date" class="form-control" id="product_name" placeholder="Enter next inspection date"
              name="answer[]"> -->
        </div>
        
        <div class="form-group mt-3">
          <a href="javascript:void(0);" class="add_button" title="Add field">+ Add More</a>
        </div>
      </div>
    </div>
                                  
                                
                                  

                        </div>

                    </div>

                    <div class="form-group row">

                         
                         <?php 
                                    if($getEditEqData['getInspectionDueDate'] != 'NA'){

                                    foreach ($getEditEqData['getInspectionDueDate'] as $key => $value) {?>
                                        <label class="col-form-label col-md-2"></label>

                        <div class="col-md-4">
                                <div class="form-elements d-flex ml-4">
          <div class="form-group">
              <label for="product_name"></label>

                <input type="date" class="form-control" id="product_name" placeholder="Enter next inspection date"
              name="answer[]" value="<?php echo $value['next_inspection_date']?>">
         

             
          </div>
          
          <div class="form-group mt-5">
          <a href="javascript:void(0);" class="remove_button" title="Add field" data-id="<?php echo  $value['next_inspection_date_id']?>" onclick="RemoveAnswer(this)">Remove</a>
          </div>
        </div>
    </div>
                               
                            <?php 
                             }
                           }?> 
                         
                    </div>

                    


                   

                    



                  

                    
                                                   


                                                  



                                                     



                                                     


                                                  

                                                   








                                                    <div class="form-group row">

                                                        <label class="col-form-label col-md-2"></label>

                                                        <div class="col-md-10">

                                                            <button class="btn btn-primary "  type="submit">Edit Inspection</button>

                                                        </div>

                                                    </div>





                                                </form>




                        </div>



                    </div>



                </div> <!-- end col -->



                <!-- start write end -->

            </div>

        </div>

        <!-- /Page Wrapper -->

        <!-- footer start -->

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

            <!-- footer end -->



    </div>

    <!-- /Main Wrapper -->



    <!-- sctipt start  -->

  <?php include('commonjs.php')?>

        <?php include_once('extrajs.php') ?>
    <!-- script end -->

   <script>




     $(document).ready(function() {

    
        

            var maxField = 10; // Total 5 product fields we add

            var addButton = $('.add_button'); // Add more button selector
      var wrapper = $('.field_wrapper');
            

            var fieldHTML = `<div class="form-elements d-flex ml-3">
                    <div class="form-group">
                            <label for="product_name"></label>

                             
          <input type="date" class="form-control" id="product_name" placeholder="Enter next inspection date"
              name="answer[]">
                  
                    </div>
                    
                    <div class="form-group mt-5">
                    <a href="javascript:void(0);" class="remove_button" title="Add field">Remove</a>
                    </div>
                </div>`; //New input field html 

                  var x = 1; //Initial field counter is 1


                $(addButton).click(function () {
                //Check maximum number of input fields
                if (x <= 100 ){
                    x++; //Increment field counter
                    $(wrapper).append(fieldHTML);
                }
            });


                 $(wrapper).on('click', '.remove_button', function (e) {
                e.preventDefault();
                $(this).parent().closest(".form-elements").remove();
                x--; //Decrement field counter
            });

            });

     jQuery.validator.addMethod("noSpace", function(value, element) { 
      return value == '' || value.trim().length != 0;  
    }, "No space please and don't leave it empty");


    $.validator.addMethod("customemail", 
    function(value, element) {
        return /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(value);
    }, 
    "Enter valid email"
); 
     $('#add_subadmin_form').validate({
                   rules: {
       

     ititle: {
      required: true,
      
      maxlength:50,
      noSpace: true,
      
      remote: {

        url: URLAPI+'user_controller.php',

        type: 'get',

        data: {

            'action': 'inspectiontitle_check_edit',

            'ititle': function() {

                return $("#ititle").val();

            },
            'etype': function() {

                return $("#etype").val();

            },
             'id': function() {

                return $("#id").val();

            },

            

        }

    },
    

},

idate:
{
    required:true,
},

nidate:{
    required:true,

},




etype:{
    required:true,
},





},
messages: {
   
    ititle: {
        required: "Please enter inspection title",
        
        maxlength: " Maximum 50  characters  allowed in inspection title",
        remote:"Inspection title already exists!",
    },

    idate:{
    required:"Please enter inspection date",
    
},
    

    etype:{
        required:"Please choose equipment type",
    },
    
    

    

    nidate:
    {
        required:'Please enter  inspection due date',
    },

    

    


},
                  

                    submitHandler:function(form,e){

                       
         
              

       //e.preventDefault();
      var link=URLAPI+'manage_all_controller.php';
      var data = new FormData($(form)[0]);
      var message = msgLoading.blankMessage[language];
      console.log('message',message);

      apiRequest.postFile(link,data,message).then(function(obj){
      if(obj.success === true){
        var title ='';
        var message = obj.msg[language];
        swal_alert(title,message,"manage_inspection.php","success");
        // $('#add_driver').modal('hide');
        // $('#image-holder').attr('src','');
        // getUserTable('first', 'all');
        return false;

      }else{
        var title = msgTitleOperation.StaffNotAdded[language];
        var message = obj.msg[language];
        swal_alert(title,message,'#','error');
         return false;
      }
    },function(err){

      consoleProvider.log('err',err);
      var title    = msgTitleOperation.errorOnServer[language];
      var messsage = msgOperations.errorOnServer[language];
      swal_alert(title,message,"#","warning");
      return false;

    });

  }//end of submit handler
                });


             function RemoveAnswer(event){
            var due_date_id =$(event).attr('data-id');;
            swal({
              title: '',
              text: 'Are you sure you want to delete inspection due date?',
              icon: "warning", //error, success, info
              buttons:  ["Cancel", "Ok"],
              //dangerMode: true,
            })
            .then((willDelete) => {
              if (willDelete) {
                btnUserDeleteContinue(event,due_date_id);
              } else {
                //swal("Your imaginary file is safe!");
              }
            });
        }
     function btnUserDeleteContinue(event, due_date_id){
    
        var link = URLAPI+'manage_all_controller.php';
        var data = {
            action:'delete_due_date',
            due_date_id:due_date_id,
        }
    
        var messsage = msgLoading.blankMessage[language];
        console.log('messsage',messsage);
        
        apiRequest.get(link, data, messsage).then(function(obj){
            console.log('obj',obj);
            if(obj.success === true) { 
            var title = '';
          
            var messsage = obj.msg[language];
            swal_alert(title,messsage,"edit_inspection.php?id=<?php echo $_GET['id']?>","success");
            //getUserTable('first', 'all');
            //-------------- DataTable Row Action end ---------
            return false;
                
    
            } else {
                //------------ Operation Message --------------------
                var title = msgTitleOperation.userDeleted[language];
                var messsage = obj.msg[language];
                msgSuccessErrorHtml.error(title, messsage, msgSuccessErrorHtmlSelector);
                //------------ Operation Message End--------------------
                return false;
            }
        },function(err){
            console.log('err',err); 
            //alert('Server not responde');
    
            //------------ Operation Message --------------------
            var title = msgTitleOperation.errorOnServer[language];
            var messsage = msgOperations.errorOnServer[language];
            msgSuccessErrorHtml.warning(title, messsage, msgSuccessErrorHtmlSelector);
            //------------ Operation Message End--------------------
            return false;      
        });
    }


    function findCategory(self) {
          //alert($(self).val());
        var link = URLAPI + 'manage_all_controller.php';
        var data = {
          action: 'get_equipment_name',
          type:'all',
          sel2:$(self).val(),
        }
        apiRequest.get(link, data).then(function(obj) {
              console.log('obj', obj);
              if (obj.success === true) {
                $("#equipment").html("");
                 var sub_array = obj.data.user_arr;
              
             
                $("#equipment").html("<option value=''>Select equipment</option>");
             
              var html_subcat='';
              $.each(sub_array, function(keysub, valuesub){
            //  for(var i=0;i<sub_array.length;i++){
                html_subcat=  $("#equipment").append('<option value="'+valuesub.equipment_id+'">'+valuesub.equipment_name+'</option>');
            //  }
              });
                    }
                    else {
                      //------------ Operation Message --------------------
                      var title = msgTitleOperation.userDeleted[language];
                      var messsage = obj.msg[language];
                      msgSuccessErrorHtml.error(title, messsage, msgSuccessErrorHtmlSelector);
                      //------------ Operation Message End--------------------
                      return false;
                    }
                  },
                  function(err) {
                    console.log('err', err);
                    //alert('Server not responde');
                    //------------ Operation Message --------------------
                    var title = msgTitleOperation.errorOnServer[language];
                    var messsage = msgOperations.errorOnServer[language];
                    msgSuccessErrorHtml.warning(title, messsage, msgSuccessErrorHtmlSelector);
                    //------------ Operation Message End--------------------
                    return false;
                  });
            }

       </script>
    
   


</body>



</html>