<?php
  if( !hasPermission($_SESSION['user_detail']['role_id'], 'agent_edit') ) {
    echo '<script>window.location.href="index.php?page=no_permission.php"</script>';die();
  }
    
  if (isset($_REQUEST['eid']) && !empty($_REQUEST['eid']) && $_REQUEST['eid']!=0){
    $select_update     = "SELECT * FROM agent WHERE id='".$_REQUEST['eid']."'";
    $select_update_res = mysqli_query($conn,$select_update);
    $update_row        = mysqli_fetch_array($select_update_res,MYSQLI_BOTH);
  }

  $voucher_no          = agentCode($conn);
  $g_act_codes         = accountCode($conn);

  $pancard_img = $update_row['pan_img'];

?>
<section class="content-header">
  <div class="row">
      <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 bottom20">
        <h3 class="capital font15 padding5 bold">UPDATE AGENT</h3>
      </div>
      <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 text-right bottom20">
        <h3>
          <?php if( hasPermission($_SESSION['user_detail']['role_id'], 'agent_list') ) { ?>
          <a href="index.php?page=agent/list.php" class="btn btn-danger capital bottom10 custome-btn">VIEW AGENT DETAILS</a>
          <?php } ?>
        </h3>
      </div>
  </div>
</section>
<hr class="bottom35"> 
<section class="hitz-top-header">
   <div class="row bottom40">
      <div class="col-xs-12">
         <div class="box box-success">
            <div class="box-body">
              <form action="agent/insert.php" method="post" enctype="multipart/form-data" class="common-frm-validation" id="signupForm">
                <div class="row">
                  <div class="col-md-1">
                    <div class="form-group">
                      <label class="capital">CODE</label><span style="color: red">*</span>
                      <input type="text" size="35" id="code" name="code" class="form-control required" autocomplete="off" readonly="" value="<?php echo $update_row['code'];?>" />
                    </div>
                  </div>
                  <div class="col-md-4">
                    <div class="form-group">
                      <label class="capital">agent name</label><span style="color: red">*</span>
                      <input type="text" size="35" id="name" name="name" class="form-control required" placeholder="AGENT NAME" autocomplete="off" onChange="CheckName(this.value);" value="<?php echo $update_row['name'];?>" />
                    </div>
                  </div>
                </div>
                <div class="row">
                  <div class="col-md-1"></div>
                  <div class="col-md-4">
                    <div class="form-group">
                      <label class="capital">address</label><span style="color: red">*</span>
                      <textarea class="form-control required" name="address" id="address" style="height: 35px !important;"><?php echo $update_row['address'];?></textarea> 
                    </div>
                  </div>
                </div>
                <div class="row">
                  <div class="col-md-1"></div>
                  <div class="col-md-2">
                    <div class="form-group">
                      <label class="capital">city</label><span style="color: red">*</span>
                        <input type="text" name="city" id="get_city" class="form-control required" placeholder="CITY" value="<?php echo $update_row['city'];?>" />
                    </div>
                  </div>
                  <div class="col-md-2">
                    <div class="form-group">
                      <label class="capital">District</label>
                      <input type="text" name="district" id="get_district" class="form-control" placeholder="DISTRICT" value="<?php echo $update_row['district'];?>" />  
                    </div>
                  </div>
                  <div class="col-md-2">
                    <div class="form-group">
                      <label class="capital">state</label><span style="color: red">*</span>
                      <input type="text" name="state" id="autocompletes" class="form-control required" placeholder="STATE" value="<?php echo $update_row['state'];?>" />
                    </div>
                  </div>
                  <div class="col-md-1">
                    <div class="form-group">
                      <label class="capital">country</label>
                      <input type="text" name="country" id="autocomplete" class="form-control" placeholder="COUNTRY" value="<?php echo $update_row['country'];?>" /> 
                    </div>
                  </div>
                </div>
                <div class="row">
                  <div class="col-md-1"></div>
                  <div class="col-md-2">
                    <div class="form-group">
                      <label class="capital">pincode</label>
                      <input type="text" size="35" id="pincode" name="pincode" class="form-control" placeholder="PINCODE" autocomplete="off" value="<?php echo $update_row['pincode'];?>" />
                    </div>
                  </div>
                  <div class="col-md-2">
                    <div class="form-group">
                      <label class="capital">phone</label>
                      <input type="text" size="35" id="contact" name="contact" class="form-control" placeholder="PHONE" autocomplete="off" value="<?php echo $update_row['contact'];?>" />
                    </div>
                  </div>
                  <div class="col-md-2">
                    <div class="form-group">
                      <label class="capital">Mobile</label>
                      <input type="text" size="35" id="mobile" name="mobile" class="form-control" placeholder="MOBILE" autocomplete="off" value="<?php echo $update_row['mobile'];?>" />
                    </div>
                  </div>
                  <div class="col-md-2">
                    <div class="form-group">
                      <label class="capital">e-mail</label>
                      <input type="text" size="35" id="email" name="email" class="form-control" placeholder="E-MAIL" autocomplete="off" value="<?php echo $update_row['email'];?>" />
                    </div>
                  </div>
                </div>
                <div class="row">
                  <div class="col-md-1"></div>
                  <div class="col-md-2">
                    <div class="form-group">
                      <label class="capital">pancard</label>
                      <input type="text" size="35" id="pan" name="pan" class="form-control" placeholder="PANCARD" autocomplete="off" value="<?php echo $update_row['pan'];?>" />
                    </div>
                  </div>
                   <div class="col-md-3">
                    <div class="form-group">
                      <label class="capital">PAN IMAGE</label>
                      <input type="file" size="35" id="pan_img" name="pan_img" autocomplete="off" class="image" />
                      <input type="hidden" name="pan_imgs" id="pan_imgs" value="<?php echo $pancard_img;?>"><br/>
                      <?php
                      if($pancard_img !=''){ ?>
                      <a href="assets/upload/pan/<?php echo $pancard_img?>" class="btn btn-info" data-toggle="tooltip" data-placement="bottom" title="VIEW IMAGE...!!" target="_blank"><em class="fa fa-file-image-o"></em></a>
                      <?php } ?>
                    </div>
                    <div class="form-group">
                      <div class="progress_pan" style="display:none;height: 0.5rem;    margin-left: 1px;">
                         <div class="progress-bar progress-bar-success mypanprogress" role="progressbar" style="width:0%; display:none; background-color:#1bb99a">0%</div>
                      </div>
                      <p style="margin-bottom:0rem;">&nbsp;</p>
                      <span id="pan_img_uplode" style="position: relative;bottom: 28px;"></span>
                    </div>
                </div>
                <div class="col-md-4">
                      <div class="form-group">
                        <label class="capital">Status</label><span style="color: red">*</span><br>
                        <label class="radio-inline fontradiobuttion">
                        <input type="radio" name="status" value="0" checked> <span class="label-text">ACTIVE</span>
                        </label>
                        <label class="radio-inline fontradiobuttion">
                        <input type="radio" name="status" value="1"> <span class="label-text">IN-ACTIVE</span>
                        </label>
                      </div>
                </div>
                </div>
                
                <input type="hidden" value="<?php echo $_GET['eid'];?>" name="id" id="id">
                <hr>
                <div class="row" align="center">
                  <button id="btn" class="btn btn-primary ajax-submit capital" type="submit"> update & save</button>
                  <a href="index.php?page=agent/list.php" class="btn btn-danger capital"> Cancel</a>
                </div>
              </form>
              
            </div>
         </div>
      </div>
   </div>
</section>
<script type="text/javascript">
  $( function() {
    $( "#autocomplete" ).autocomplete({
        source: function( request, response ) {
            
            $.ajax({
                url: "agent/get_country.php",
                type: 'post',
                dataType: "json",
                data: {
                    search: request.term
                },
                success: function( data ) {
                    response( data );
                }
            });
        },
        select: function (event, ui) {
            $('#autocomplete').val(ui.item.label);
            return false;
        }
    });
    $( "#autocompletes" ).autocomplete({
        source: function( request, response ) {
            
            $.ajax({
                url: "agent/get_state.php",
                type: 'post',
                dataType: "json",
                data: {
                    search: request.term
                },
                success: function( data ) {
                    response( data );
                }
            });
        },
        select: function (event, ui) {
            $('#autocompletes').val(ui.item.label);
            return false;
        }
    });
    $( "#get_district" ).autocomplete({
        source: function( request, response ) {
          $.ajax({
              url: "agent/get_district.php",
              type: 'post',
              dataType: "json",
              data: {
                  search: request.term
              },
              success: function( data ) {
                  response( data );
              }
          });
        },
        select: function (event, ui) {
            $('#get_district').val(ui.item.label);
            return false;
        }
    });
    $( "#get_city" ).autocomplete({
        source: function( request, response ) {
          $.ajax({
              url: "agent/get_city.php",
              type: 'post',
              dataType: "json",
              data: {
                  search: request.term
              },
              success: function( data ) {
                  response( data );
              }
          });
        },
        select: function (event, ui) {
            $('#get_city').val(ui.item.label);
            return false;
        }
    });

    // FILE UPLODE
    $(document).on('change', '#pan_img', function() {
      var name = document.getElementById("pan_img").files[0].name;
      var form_data = new FormData();
      var ext = name.split('.').pop().toLowerCase();
      if (jQuery.inArray(ext, ['png', 'jpg', 'jpeg', 'pdf']) == -1) {
         $.toast({
          heading: 'OPPS....',
          text: 'ONLY PDF, DOC, JPG, JPEG, & PNG FILE ALLOW TO UPLODE',
          icon: 'error',
          position: 'top-right',
          stack: false
        })
        $("#pan_img").val('');
      }
      var oFReader = new FileReader();
      oFReader.readAsDataURL(document.getElementById("pan_img").files[0]);
      var f = document.getElementById("pan_img").files[0];
      var fsize = f.size || f.fileSize;
      if (fsize > 5000000) {
        $.toast({
          heading: 'OPPS....',
          text: 'FILE SIZE BIG',
          icon: 'error',
          position: 'top-right',
          stack: false
        })
        $("#rc_img").val('');
      } else {
         form_data.append("file", document.getElementById('pan_img').files[0]);
         $.ajax({
            url: "truck_mst/pan_image_uplode.php",
            method: "POST",
            xhr: function() {
               var xhr = new window.XMLHttpRequest();
               xhr.upload.addEventListener("progress", function(evt) {
                  if (evt.lengthComputable) {
                      var percentComplete = evt.loaded / evt.total;
                      percentComplete = parseInt(percentComplete * 100);
                      $('.mypanprogress').text(percentComplete + '%');
                      $('.mypanprogress').css('width', percentComplete + '%');
                  }
               }, false);
               return xhr;
            },
            data: form_data,
            contentType: false,
            cache: false,
            processData: false,
            beforeSend: function() {
               $('#pan_img_uplode').html("<label class='text-success'>Uploading...</label>");
               $('.progress_pan').show();
               $('.mypanprogress').show();
               $('.submit').prop('disabled', true);
               $('.mypanprogress').css('width', '0');
            },
            success: function(data) {
               $('#pan_img_uplode').html('<button type="button" data-path="' + data + '" id="pan_remove_button" class="btn btn-danger" style="position: relative;top: 19px;"><em class="fa fa-trash"></em></button>');
               $('#pan_imgs').val(data);
               $('.submit').prop('disabled', false);
            }
         });
      }
    });
    $(document).on('click', '#pan_remove_button', function() {
      var path = $('#pan_remove_button').data("path");
      $.ajax({
         url: "truck_mst/remove_image.php",
         type: "POST",
         data: {
            path: path
         },
         success: function(data) {
            if (data != '') {
               $('.mypanprogress').css('width', '0');
               $('.mypanprogress').removeClass('progress-bar-success');
               $('.mypanprogress').addClass('progress-bar-danger');
               setTimeout(function() {
                  $('.progress_pan').hide('slow');
               }, 1500);
               $('#pan_img_uplode').html('');
               $("#pan_img").val('');
               $('#pan_imgs').val('');
            }
         }
      });
    });
  });
  function CheckName() {
    var name      = $("#name").val();
    $.ajax({
      'type': 'POST',
      'url': 'agent/check_name.php',
      'data': {
        'name': name
      },
      dataType: 'JSON',
      success: function (result) {
        if(result.status == 1) {
          jQuery('.submit').attr('disabled', true);
          swal("Opps !!", "SAME NAME DETECT...!!!", "info")
          setTimeout("location.reload(true);",800); 
        }else{
          jQuery('.submit').attr('enable', true);
        }
      }
    });
  };
</script>