<?php
  if( !hasPermission($_SESSION['user_detail']['role_id'], 'vendor_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 vendor_master WHERE id='".$_REQUEST['eid']."'";
    $select_update_res = mysqli_query($conn,$select_update);
    $update_row        = mysqli_fetch_array($select_update_res,MYSQLI_BOTH);
    $type              = $update_row['type'];
    $file_img          = $update_row['file_img'];
  }

  
  $g_act_codes         = accountCode($conn);
  $vendors = mysqli_query($conn, "SELECT * FROM `vendor_master` WHERE `status` = '0' ORDER BY name ASC");
?>
<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 VENDOR</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'], 'vendor_list') ) { ?>
          <a href="index.php?page=vendor/list.php" class="btn btn-danger capital bottom10 custome-btn">VIEW VENDOR 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="vendor/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-3">
                      <div class="form-group">
                        <label class="capital">TYPE</label><span style="color: red">*</span><br>
                        <label class="radio-inline fontradiobuttion">
                        <input type="radio" name="type" value="A" <?php if($type == 'A'){echo "checked";}?>> <span class="label-text">AGENT</span>
                        </label>
                        <label class="radio-inline fontradiobuttion">
                        <input type="radio" name="type" value="V" <?php if($type == 'V'){echo "checked";}?>
                       > <span class="label-text">VENDOR</span>
                        </label>
                      </div>
                  </div>
                </div>
                <div class="row">
                  <div class="col-md-1"></div>
                  <div class="col-md-4">
                    <div class="form-group">
                      <label class="capital">Vendor name</label><span style="color: red">*</span>
                      <input type="text" size="35" id="name" name="name" class="form-control required" placeholder="VENDOR NAME" autocomplete="off" onChange="CheckName(this.value);" value="<?php echo $update_row['name'];?>" />
                    </div>
                  </div>
                  <div class="col-md-4">
                    <div class="form-group">
                      <label class="capital">Print name</label><span style="color: red">*</span>
                      <input type="text" size="35" id="p_name" name="p_name" class="form-control required same-input" placeholder="PRINT NAME" autocomplete="off" autofocus="" value="<?php echo $update_row['p_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 class="col-md-4">
                    <div class="form-group">
                      <label class="capital">Print address</label><span style="color: red">*</span>
                      <textarea class="form-control required same-input" name="p_address" id="p_address" style="height: 35px !important;"><?php echo $update_row['p_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" 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" 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-2">
                    <div class="form-group">
                      <label class="capital">gstIN</label>
                      <input type="text" size="35" id="gstin" name="gstin" class="form-control" placeholder="GSTIN" autocomplete="off" value="<?php echo $update_row['gstin'];?>" />
                    </div>
                  </div>

                  <div class="col-md-2">
                    <div class="form-group">
                      <label class="capital">GROUP</label>
                      <select class="form-control select2" name="group_id" id="group_id">
                      <option></option>
                      <?php while ($vendor_row = mysqli_fetch_array($vendors)) { ?>
                      <option value="<?php echo $vendor_row['id'];?>" <?php if($vendor_row['id'] == $update_row['group_id']) { echo "selected='selected'"; } ?>><?php echo $vendor_row['name'];?></option>
                      <?php } ?>
                     </select>
                    </div>
                  </div>
                  <?php if($type == 'A'){ ?>
                  <div class="col-md-3">
                    <div class="form-group">
                      <label class="capital">PROPRIETOR</label>
                      <input type="text" class="form-control" id="proprietor" name="proprietor" autocomplete="off" placeholder="PROPRIETOR" value="<?php echo $update_row['proprietor'];?>"/>
                    </div>
                  </div>
                  <div class="col-md-3">
                    <div class="form-group">
                      <label class="capital">IMAGE</label>
                      <input type="file" size="35" id="image" name="image" autocomplete="off" class="image" />
                      <input type="hidden" size="35" id="old_image" name="old_image" autocomplete="off" class="image" value="<?php echo $file_img?>" />
                      <br/>
                      <?php
                      if($file_img !=''){ ?>
                        <a href="assets/upload/vendor/<?php echo $file_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>
                  <?php } ?>
                </div>
                <legend class="hitz-legend">CONTACT PERSON DETAILS</legend>
                <div class="row">
                    <div class="col-md-8">
                      <div class="col-md-12 table-responsive">
                        <table class="table table-striped table-bordered table-list">
                          <thead>
                            <tr class="hitz-tab capital">
                              <th>contact person</th>
                              <th>designation</th>
                              <th>mobile</th>
                              <th>email</th>
                            </tr>
                          </thead>
                          <tbody>
                            <tr align="center" class="capital">
                              <td><input type="text" size="35" id="contact_person" name="contact_person" class="form-control" placeholder="CONTACT PERSON" autocomplete="off" value="<?php echo $update_row['contact_person'];?>" /></td>

                              <td><input type="text" size="35" id="designation" name="designation" class="form-control" placeholder="DESIGNATION" autocomplete="off" value="<?php echo $update_row['designation'];?>"/></td>

                              <td><input type="text" size="35" id="con_per_mobile" name="con_per_mobile" class="form-control" placeholder="MOBILE" autocomplete="off" value="<?php echo $update_row['con_per_mobile'];?>"/></td>

                              <td><input type="text" size="35" id="con_per_email" name="con_per_email" class="form-control" placeholder="EMAIL" autocomplete="off" value="<?php echo $update_row['con_per_email'];?>"/></td>
                            </tr>
                            <tr align="center" class="capital">
                              <td><input type="text" size="35" id="contact_person1" name="contact_person1" class="form-control" placeholder="CONTACT PERSON" autocomplete="off" value="<?php echo $update_row['contact_person1'];?>"/></td>

                              <td><input type="text" size="35" id="designation1" name="designation1" class="form-control" placeholder="DESIGNATION" autocomplete="off" value="<?php echo $update_row['designation1'];?>"/></td>

                              <td><input type="text" size="35" id="con_per_mobile1" name="con_per_mobile1" class="form-control" placeholder="MOBILE" autocomplete="off" value="<?php echo $update_row['con_per_mobile1'];?>"/></td>

                              <td><input type="text" size="35" id="con_per_email1" name="con_per_email1" class="form-control" placeholder="EMAIL" autocomplete="off" value="<?php echo $update_row['con_per_email1'];?>"/></td>
                            </tr>
                            <tr align="center" class="capital">
                              <td><input type="text" size="35" id="contact_person2" name="contact_person2" class="form-control" placeholder="CONTACT PERSON" autocomplete="off" value="<?php echo $update_row['contact_person2'];?>"/></td>

                              <td><input type="text" size="35" id="designation2" name="designation2" class="form-control" placeholder="DESIGNATION" autocomplete="off" value="<?php echo $update_row['designation2'];?>" /></td>

                              <td><input type="text" size="35" id="con_per_mobile2" name="con_per_mobile2" class="form-control" placeholder="MOBILE" autocomplete="off" value="<?php echo $update_row['con_per_mobile2'];?>"/></td>
                              
                              <td><input type="text" size="35" id="con_per_email2" name="con_per_email2" class="form-control" placeholder="EMAIL" autocomplete="off" value="<?php echo $update_row['con_per_email2'];?>"/></td>
                            </tr>
                          </tbody>
                        </table>
                      </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" <?php if($update_row['status']=='0'){echo "checked";}?>> <span class="label-text">ACTIVE</span>
                        </label>
                        <label class="radio-inline fontradiobuttion">
                        <input type="radio" name="status" value="1" <?php if($update_row['status']=='1'){echo "checked";}?>> <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=vendor/list.php" class="btn btn-danger capital"> Cancel</a>
                </div>
              </form>
              
            </div>
         </div>
      </div>
   </div>
</section>
<script type="text/javascript">
  $( function() {
    jQuery("input[name='name']").on("input change", function(e) {
      jQuery('input[name="p_name"]').val(this.value)
    })

    jQuery("textarea[name='address']").on("textarea keyup", function(e) {
      jQuery('textarea[name="p_address"]').val(this.value)
    })

    $( "#autocomplete" ).autocomplete({
        source: function( request, response ) {
            
            $.ajax({
                url: "vendor/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: "vendor/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: "vendor/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: "vendor/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;
        }
    });
  });
  jQuery(document).ready(function() {
    $('#group_id').select2();
    jQuery('#signupForm').delegate('.type', 'ifChecked', function(event){
      $value = jQuery(this).val();
      if($value == 'A') {
        jQuery('#image_hide').show();
        jQuery('#proprietor_hide').show();
      }

      if($value == 'V') {
        jQuery('#image_hide').hide();
        jQuery('#proprietor_hide').hide();
      }
    });
  });
  function CheckName() {
    var name      = $("#name").val();
    $.ajax({
      'type': 'POST',
      'url': 'vendor/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>