<?php
  if( !hasPermission($_SESSION['user_detail']['role_id'], 'accountm_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 act_master WHERE id='".$_REQUEST['eid']."'";
    $select_update_res = mysqli_query($conn,$select_update);
    $update_row        = mysqli_fetch_array($select_update_res,MYSQLI_BOTH);
    $act_code          = $update_row['act_code'];
    $name              = $update_row['ac_name'];
    $account_group     = $update_row['account_group'];
    $mode              = $update_row['mode'];
    if($mode == 'cr'){
      $open_bal = $update_row['cr_open_bal'];
    }else{
      $open_bal = $update_row['dr_open_bal'];
    }
    $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 ACCOUNT' : 'UPDATE ACCOUNT'; ?></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'], 'accountm_list') ) { ?>
          <a href="index.php?page=account_mst/list.php" class="btn btn-danger capital bottom10 custome-btn">VIEW ACCOUNT 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="account_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">CODE</label><span style="color: red">*</span>
                      <?php 
                      if($_GET['eid'] == ''){
                        $code = $voucher_no;
                      }else{
                        $code = $act_code;
                      }
                      ?>
                      <input type="text" size="35" id="act_code" name="act_code" class="form-control required" autocomplete="off" readonly="" value="<?php echo $code;?>" />
                    </div>
                  </div>
                  <div class="col-md-4">
                    <div class="form-group">
                       <label class="capital">name</label><span style="color: red">*</span>
                       <input type="text" name="ac_name" id="get_name" class="form-control required" placeholder="NAME" value="<?php echo $name;?>" onchange="CheckAname(this.value);" autofocus=""/>
                    </div>
                  </div>
                  <div class="col-md-3">
                    <div class="form-group">
                       <label class="capital">ACCOUNT GROUP</label><span style="color: red">*</span>
                       <select class="form-control select2 required" name="account_group" id="account_group">
                         <option></option>
                         <?php 
                            $ag     = "SELECT * FROM `account_group` 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['name'] .'" '. ($category['name'] == $account_group ? 'selected' : '') .'>' . $category['name'] . '</option>';
                            }
                         ?>
                         
                       </select>
                    </div>
                  </div>
                  <div class="col-md-2">
                    <div class="form-group">
                       <label class="capital">OPENING BALANCE</label>
                       <input type="text" name="open_bal" id="open_bal" class="form-control" placeholder="OPENING BALANCE" value="<?php echo $open_bal;?>" />
                    </div>
                  </div>
                  <div class="col-md-1">
                    <div class="form-group">
                      <label class="capital">mode</label><span style="color: red">*</span>
                      <select class="form-control select2 required" name="mode" id="mode">
                        <option></option>
                        <option value="cr" <?php if($mode == 'cr') { ?> selected="selected"<?php } ?>>CR</option>
                        <option value="dr" <?php if($mode == 'dr') { ?> selected="selected"<?php } ?>>DR</option>
                      </select>
                    </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=account_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();
    $('#mode').select2();
  });
  $( 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;
      }
    });
  });
  function CheckAname() {
    var name = $("#get_name").val();
    $.ajax({
      'type': 'POST',
      'url': 'account_mst/check_name.php',
      'data': {
        'name': name
      },
      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>