<?php
  if( !hasPermission($_SESSION['user_detail']['role_id'], 'doc_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 document_mst WHERE id='".$_REQUEST['eid']."'";
    $select_update_res = mysqli_query($conn,$select_update);
    $update_row        = mysqli_fetch_array($select_update_res,MYSQLI_BOTH);
    $mod               = $update_row['module'];
    $branch            = $update_row['branch'];
    $company_id        = $update_row['company_id'];
    $start             = $update_row['start_sr_no'];
    $end               = $update_row['end_sr_no'];
    $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 DOCUMENT' : 'UPDATE DOCUMENT'; ?></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'], 'doc_list') ) { ?>
          <a href="index.php?page=document_mst/list.php" class="btn btn-danger capital bottom10 custome-btn">VIEW DOCUMENT 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="document_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">MODULE</label><span style="color: red">*</span>
                      <select class="form-control select2" name="module" id="module" autofocus="">
                        <option></option>
                        <?php foreach ($module as $modules) { 
                        
                          echo '<option value="'. $modules['title'] .'" '. ($modules['title'] == $mod ? 'selected' : '') .'>' . $modules['title'] . '</option>';

                         } ?>
                       </select>
                    </div>
                  </div>
                  <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" onchange="GetBranchData(this);">
                       <option></option>
                        <?php 
                          $company     = "SELECT * FROM `company_mst` WHERE `status` = '0'";
                          $company_res = mysqli_query($conn,$company);
                          while ($company_row=mysqli_fetch_array($company_res)) { ?>
                            <option value="<?php echo $company_row['id'];?>" <?php if($company_id == $company_row['id']) { ?> selected="selected"<?php } ?> ><?php echo $company_row['name'];?></option>
                          <?php }
                        ?>
                      </select>
                    </div>
                  </div>
                  <div class="col-md-3">
                    <div class="form-group">
                      <label class="capital">BRANCH</label><span style="color: red">*</span>
                      <select class="form-control select2" name="branch" id="branch">
                       <option></option>
                        <?php 
                          $ag = "SELECT * FROM `branch_mst` WHERE `status` = '0'";
                          $ag_res = mysqli_query($conn,$ag);
                          while ($ag_row=mysqli_fetch_array($ag_res)) { ?>
                            <option value="<?php echo $ag_row['id'];?>" <?php if($branch == $ag_row['id']) { ?> selected="selected"<?php } ?> ><?php echo $ag_row['branch'];?></option>
                          <?php }
                        ?>
                      </select>
                    </div>
                  </div>
                  <div class="col-md-2">
                    <div class="form-group">
                      <label class="capital">START SR.NO</label><span style="color: red">*</span>
                      <input type="text" name="start_sr_no" id="start_sr_no" class="form-control" placeholder="START SR.NO" value="<?php echo $start;?>"/>
                    </div>
                  </div>
                  <div class="col-md-2">
                    <div class="form-group">
                      <label class="capital">END SR.NO</label><span style="color: red">*</span>
                      <input type="text" name="end_sr_no" id="end_sr_no" class="form-control" placeholder="END SR.NO" value="<?php echo $end;?>" onchange="checkNumberSeries(this);"/>
                    </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=document_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'
    });
    $('#module').select2();
    $('#branch').select2();
    $('#company_id').select2();
  });
  function checkNumberSeries() {
    var module      = $("#module").val();
    var branch      = $("#branch").val();
    var start_sr_no = $("#start_sr_no").val();
    var end_sr_no   = $("#end_sr_no").val();
    $.ajax({
      'type': 'POST',
      'url': 'document_mst/check_number_series.php',
      'data': {
        'module': module,
        'branch': branch,
        'start_sr_no': start_sr_no,
        'end_sr_no': end_sr_no
      },
      dataType: 'JSON',
        success: function (result) {
          if(result.status == 0) {
            jQuery('.submit').attr('disabled', false);
          }else{
            jQuery('.ajax-submit').attr('disabled', true);
            swal("Opps !!", "SAME NUMBER SERIES ALLOCATED ...!!!", "info")
            setTimeout("location.reload(true);",2000);  
          }
        }
    });
  }
  
  function GetBranchData(ele) {
    var company_id      = $("#company_id").val();
    $.ajax({
      type: "POST",
      url: "document_mst/get_branch_data.php",
      data:'company_id='+company_id,
      beforeSend: function() {
        $("#branch").addClass("loader");
      },
      success: function(data){
        $("#branch").html(data);
      }
    });
  }
</script>
