<?php
  if( !hasPermission($_SESSION['user_detail']['role_id'], 'branch_add') ) {
    echo '<script>window.location.href="index.php?page=no_permission.php"</script>';die();
  }
    
  $voucher_no          = accountCode($conn);

  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);
    $company_id        = $update_row['company_id'];
    $branchs           = $update_row['branch'];
    $con_name          = $update_row['con_name'];
    $mobile            = $update_row['mobile'];
    $state             = $update_row['state'];
    $city              = $update_row['city'];
    $email             = $update_row['email'];
    $pancard           = $update_row['pancard'];
    $gst_in            = $update_row['gst_in'];
    $address           = $update_row['address'];
    $status            = $update_row['status'];
    $prifix            = $update_row['prifix'];
    $id                = $_GET['eid'];
  }
?>
<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"><?php echo ($id == 0) ? 'ADD NEW BRANCH' : 'UPDATE BRANCH'; ?></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'], 'branch_list') ) { ?>
          <a href="index.php?page=branch_mst/list.php" class="btn btn-danger capital bottom10 custome-btn">VIEW BRANCH 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="branch_mst/insert.php" method="post" enctype="multipart/form-data" class="common-frm-validation" id="signupForm">
                <div class="row">
                  <div class="col-md-2">
                    <div class="form-group">
                       <label class="capital">COMPANY</label><span style="color: red">*</span>
                       <select class="form-control select2 required" name="company_id" id="company_id" autofocus="">
                          <option></option>
                          <?php 
                            $ag         = "SELECT * FROM `company_mst` WHERE `status` = '0'";
                            $ag_res     = mysqli_query($conn,$ag);
                            $categories = array();
                            while ($ag_row = mysqli_fetch_array($ag_res)) {
                              $categories[] = $ag_row;
                            }
                            foreach ($categories as $category) {
                              echo '<option value="'. $category['id'] .'" '. ($category['id'] == $company_id ? 'selected' : '') .'>' . $category['name'] . '</option>';
                            }
                          ?>
                       </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 required" placeholder="BRANCH" value="<?php echo $branchs;?>" autocomplete = "off"/>
                    </div>
                  </div>

                  <div class="col-md-2">
                    <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 required" placeholder="CONTACT NAME" value="<?php echo $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 required" placeholder="CONTACT NUMBER" value="<?php echo $mobile;?>"/>
                    </div>
                  </div>
                  <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 $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 $city;?>"/>
                    </div>
                 </div>
                </div>
                <div class="row">
                  <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 $email;?>" autocomplete = "off"/>
                    </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 $pancard;?>" autocomplete = "off"/>
                    </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 required" placeholder="GSTIN" value="<?php echo $gst_in;?>" autocomplete = "off"/>
                    </div>
                  </div>
                  <div class="col-md-2">
                    <div class="form-group">
                      <label class="capital">ADDRESS</label>
                      <textarea class="form-control" name="address" id="address" style="height: 50px !important;"><?php echo $address;?></textarea>
                    </div>
                  </div>
                  <div class="col-md-1">
                    <div class="form-group">
                      <label class="capital">PRIFIX</label>
                      <input class="form-control" name="prifix" id="prifix" value="<?php echo $prifix;?>">
                    </div>
                  </div>
                  <div class="col-md-3">
                    <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 <?php if($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($status=='1'){echo "checked";}?>> <span class="label-text">IN-ACTIVE</span>
                      </label>
                    </div>
                  </div>
                </div>
                <input type="hidden" value="<?php echo $id;?>" name="id" id="id">
                <hr>
                <div class="row" align="center">
                  <button id="btn" class="btn btn-primary ajax-submit capital" type="submit"> <?php echo ($id == 0) ? 'Save & List' : 'Update & save'; ?></button>
                  <a href="index.php?page=branch_mst/list.php" class="btn btn-danger capital"> Cancel</a>
                </div>
              </form>
              
            </div>
         </div>
      </div>
   </div>
</section>
<script type="text/javascript">
  $(document).ready(function(){
    $('input').iCheck({
       checkboxClass: 'icheckbox_flat',
       radioClass: 'iradio_flat'
     });
    $('#company_id').select2();
  });
  function CheckBranch() {
    var branch      = $("#branch").val();
    $.ajax({
      'type': 'POST',
      'url': 'branch_mst/check-branch.php',
      'data': {
        'branch': branch
      },
      dataType: 'JSON',
      success: function (result) {
        if(result.status == 1) {
          $('#branch').val('');
          swal("Opps !!", "ALREADY BRANCH EXIST...!!!", "info")  
        }else{
          jQuery('.submit').attr('enable', true);
        }
      }
    });
  };
</script>
