<script>
   $().ready(function() {
    $("#signupForm").validate({
      rules: {
        ac_name        : "required",
        account_group  : "required",
        mode           : "required",
        open_bal    :{
                      required:false,
                      number: true
                    }
      },
    });
  });
   function showMessage(status, client_id){
         if(status == 1){
             $title      = 'Great !!';
             $message    = 'BRANCH UPDATED SUCCESSFUL';
             $type       = 'success';
         } else {
             $title      = 'Opps...';
             $message    = 'Error in adding user. Please try again';
             $type       = 'error';
         }
    
         swal({
             title: $title,
             text: $message,
             type: $type,
             showCancelButton: false,
             confirmButtonColor: "#DD6B55",
             confirmButtonText: "OK"
         },
         function(){
             if(status == 1){
                 window.location.href = http_host + '?page=branch_mst/list.php';
             } else {
                 window.location.href = http_host + '?page=branch_mst/list.php';    
             }
         });
         
     }
</script>
<?php 
  $status = '';
  $msg = '';
  if (isset($_REQUEST['eid']) && !empty($_REQUEST['eid']) && $_REQUEST['eid']!=0){
    $select_update     = "SELECT * FROM branch_mst WHERE id='".$_REQUEST['eid']."'";
    $select_update_res = mysqli_query($conn,$select_update);
    $update_row        = mysqli_fetch_array($select_update_res,MYSQLI_BOTH);
  }

  

 if(isset($_POST['insert'])){
    $branch              = $_POST['branch'];
    $company_id          = $_POST['company_id'];
    $state               = $_POST['state'];
    $city                = $_POST['city'];
    $con_name            = $_POST['con_name'];
    $mobile              = $_POST['mobile'];
    $email               = $_POST['email'];
    $pancard             = $_POST['pancard'];
    $gst_in              = $_POST['gst_in'];
    $address             = $_POST['address'];
    $status              = $_POST['status'];
    $type                = 'update_branch';
    $user_id             = $_SESSION['user_detail']['id'];
    $data                = serialize($_POST);
    $created_datetime    = date('Y-m-d H:i:s');
    $created_id          = $_SESSION['user_detail']['id'];
    $updated_datetime    = date('Y-m-d H:i:s');
    $updated_id          = $_SESSION['user_detail']['id'];
    
    $user_query = "UPDATE branch_mst SET company_id='$company_id',branch='$branch',state='$state',city='$city',con_name='$con_name',mobile='$mobile',email='$email',pancard='$pancard',gst_in='$gst_in',address='$address',status='$status',updated_id='$updated_id',updated_datetime='$updated_datetime' WHERE id='".$_REQUEST['eid']."'";
    
    $user_result = mysqli_query($conn,$user_query) or die( mysqli_error($conn) ); 

    $activiti = "INSERT INTO `activities`(`type`, `user_id`, `data`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$type."','".$user_id."','".$data."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
    $acttivity_res = mysqli_query($conn,$activiti);
       
    if($user_result){ 
      echo "<script type='text/javascript'> showMessage(1, 1); </script>";
    } else {
      echo "<script type='text/javascript'> showMessage(0, 0); </script>";
    }

    if( !hasPermission($_SESSION['user_detail']['role_id'], 'branch_edit') ) {
      echo '<script>window.location.href="index.php?page=no_permission.php"</script>';
      die();
    }
  }
?>
<section class="content-header hitz-braed-crumb">
   <h1 class="hitz-border">EDIT BRANCH</h1>
   <ol class="breadcrumb">
      <?php if( hasPermission($_SESSION['user_detail']['role_id'], 'branch_list') ) { ?>
      <a class="btn btn-sm btn-primary btn-create btn hitz-bread-btn" href="index.php?page=account_mst/list.php">&nbsp; BRANCH LIST</a>
      <?php } ?>
    </ol>
</section>
<section class="" style="margin-top: 30px;">
   <div class="row">
      <div class="col-xs-12">
         <div class="box box-success">
            <div class="box-body">
                <form class="" id="signupForm" method="post">
                  <div class="row">
                    <div class="col-md-3">
                        <div class="form-group">
                            <label class="capital">COMPANY</label><span style="color: red">*</span>
                            <select class="form-control select2" name="company_id" id="company_id">
                             <option></option>
                             <?php 
                                $ag = "SELECT * FROM `company_mst` WHERE `status` = '0'";
                                $ag_res = mysqli_query($conn,$ag);
                                while ($ag_row=mysqli_fetch_array($ag_res)) { ?>
                                  <option  <?php if($ag_row['id'] == $update_row['company_id']) { echo "selected='selected'"; } ?>  value="<?php echo $ag_row['id'] ?>"><?php echo $ag_row['name'];?></option>
                                <?php }
                             ?>
                            </select>
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="form-group">
                           <label class="capital">BRANCH</label><span style="color: red">*</span>
                           <input type="text" name="branch" id="branch" class="form-control" placeholder="BRANCH" value="<?php echo $update_row['branch'];?>" />
                        </div>
                     </div>
                     <div class="col-md-5">
                        <div class="form-group">
                           <label class="capital">CONTACT NAME</label><span style="color: red">*</span>
                           <input type="text" name="con_name" id="con_name" class="form-control" placeholder="CONTACT NAME" value="<?php echo $update_row['con_name'];?>"/>
                        </div>
                     </div>
                     <div class="col-md-2">
                        <div class="form-group">
                           <label class="capital">MOBILE</label><span style="color: red">*</span>
                           <input type="text" name="mobile" id="mobile" class="form-control" placeholder="CONTACT NUMBER" value="<?php echo $update_row['mobile'];?>"/>
                        </div>
                     </div>
                  </div>
                  <div class="row">
                     <div class="col-md-2">
                        <div class="form-group">
                           <label class="capital">STATE</label>
                           <input type="text" name="state" id="state" class="form-control" placeholder="STATE" value="<?php echo $update_row['state'];?>" />
                        </div>
                     </div>
                     <div class="col-md-2">
                        <div class="form-group">
                           <label class="capital">CITY</label>
                           <input type="text" name="city" id="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">EMAIL</label>
                           <input type="text" name="email" id="email" class="form-control" placeholder="EMAIL" value="<?php echo $update_row['email'];?>" />
                        </div>
                     </div>
                     <div class="col-md-2">
                        <div class="form-group">
                           <label class="capital">PANCARD</label>
                           <input type="text" name="pancard" id="pancard" class="form-control" placeholder="PANCARD" value="<?php echo $update_row['pancard'];?>"/>
                        </div>
                     </div>
                     <div class="col-md-2">
                        <div class="form-group">
                           <label class="capital">GSTIN</label><span style="color: red">*</span>
                           <input type="text" name="gst_in" id="gst_in" class="form-control" placeholder="GSTIN"  value="<?php echo $update_row['gst_in'];?>"/>
                        </div>
                     </div>
                     <div class="col-md-2">
                        <div class="form-group">
                           <label class="capital">ADDRESS</label>
                           <textarea class="form-control" name="address" id="address"><?php echo $update_row['address'];?></textarea>
                        </div>
                     </div>
                  </div>
                  <div class="row">
                    <div class="col-md-12">
                      <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>
                  <div class="row">
                     <div class="col-md-12">
                        <p>Mark (*) fields are compulsory</p>
                     </div>
                  </div>
                  <hr>
                  <div class="row" align="center">
                     <button name="insert" class="btn btn-primary submit capital" type="submit"> UPDATE & LIST</button>
                     <a href="index.php?page=branch_mst/list.php" class="btn btn-danger capital"> Cancle</a>
                  </div>
                </form>
            </div>
         </div>
      </div>
   </div>
</section>
<script type="text/javascript">
  $(document).ready(function(){
    $('input').iCheck({
       checkboxClass: 'icheckbox_flat',
       radioClass: 'iradio_flat'
    });
    $('#account_group').select2();
  });
</script>
<script type="text/javascript">
  $( function() {
    $( "#get_name" ).autocomplete({
        source: function( request, response ) {
          $.ajax({
              url: "account_mst/get_name.php",
              type: 'post',
              dataType: "json",
              data: {
                  search: request.term
              },
              success: function( data ) {
                  response( data );
              }
          });
        },
        select: function (event, ui) {
            $('#get_name').val(ui.item.label);
            return false;
        }
    });
  });
</script>