<?php
  if( !hasPermission($_SESSION['user_detail']['role_id'], 'tax_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 tax_mst WHERE id='".$_REQUEST['eid']."'";
    $select_update_res = mysqli_query($conn,$select_update);
    $update_row        = mysqli_fetch_array($select_update_res,MYSQLI_BOTH);
    $sign              = $update_row['sign'];
    $mod               = $update_row['module'];
    $descr             = $update_row['descr'];
    $account_group     = $update_row['account_group'];
    $checkbox_array    = explode(",", $update_row['module']);
    $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 TAX' : 'UPDATE TAX'; ?></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'], 'tax_list') ) { ?>
          <a href="index.php?page=tax_mst/list.php" class="btn btn-danger capital bottom10 custome-btn">VIEW TAX 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="tax_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">SIGN</label><span style="color: red">*</span>
                       <select class="form-control select2 required" name="sign" id="sign" autofocus="">
                          <option value="">SIGN</option>
                          <option value="+" <?php if($sign == '+') { ?> selected="selected"<?php } ?>>+</option>
                          <option value="-" <?php if($sign == '-') { ?> selected="selected"<?php } ?>>-</option>
                       </select>
                    </div>
                  </div>
                  <div class="col-md-4">
                    <div class="form-group">
                      <label class="capital">Description</label><span style="color: red">*</span>
                      <input type="text" name="descr" id="descr" class="form-control required" placeholder="DESCRIPTION" value="<?php echo $descr;?>" onchange="CheckAname(this.value);" autocomplete="off">
                    </div>
                  </div>
                  <div class="col-md-3">
                    <div class="form-group">
                       <label class="capital">ACCOUNT</label><span style="color: red">*</span>
                       <select class="form-control select2 required" name="account_group" id="account_group">
                          <option></option>
                          <?php 
                            $ag = "SELECT * FROM `act_master`";
                            $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'] == $account_group ? 'selected' : '') .'>' . $category['ac_name'] . '</option>';
                            } 
                             
                          ?>
                       </select>
                    </div>
                  </div>
                  <div class="col-md-3">
                    <div class="form-group">
                      <label class="capital">MODULE</label><br/>
                      <?php foreach ($module as $modules) { ?>
                      <input type="checkbox" class="flat-red required" name="module[]" value="<?php echo $modules['title'];?>" <?php if(in_array($modules['title'], $checkbox_array)){ echo " checked=\"checked\""; } ?>>  <?php echo $modules['title'];?>
                      <?php } ?>
                    </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=tax_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'
    });
    $('#account_group').select2();
    $('#module').select2();
    $('#sign').select2();
  });
  function CheckAname() {
    var sign = $("#sign").val();
    var name = $("#descr").val();
    $.ajax({
      'type': 'POST',
      'url': 'tax_mst/check_name.php',
      'data': {
        'name': name,
        'sign': sign
      },
      dataType: 'JSON',
        success: function (result) {
          if(result.status == 1) {
             jQuery('.submit').attr('disabled', false);
          }else{
            jQuery('.submit').attr('disabled', true);
            swal("Opps !!", "SAME ACCOUNT DETECT...!!!", "info")
            setTimeout("location.reload(true);",2000);  
          }
        }
    });
  }
</script>
