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

  if (isset($_REQUEST['eid']) && !empty($_REQUEST['eid']) && $_REQUEST['eid']!=0){
    $select_update     = "SELECT * FROM opening_balance WHERE id='".$_REQUEST['eid']."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."'";
    $select_update_res = mysqli_query($conn,$select_update);
    $update_row        = mysqli_fetch_array($select_update_res,MYSQLI_BOTH);
    $voucher_no        = $update_row['invoice_no'];
    $v_date            = date("d-m-Y", strtotime($update_row['v_date']));
    $account_name      = $update_row['account_name'];
    $amount            = $update_row['amount'];
    $narration         = $update_row['narration'];
    $flag              = $update_row['flag'];
    $id                = $_REQUEST['eid'];
  }
?>
<style type="text/css">
  .height60 {
    height: 60px !important;
  }
  .height29 {
    height: 29px !important;
  }
  .chosen-container-single .chosen-single {
    height: 29px !important;
    padding: 5px 15px !important;
}
</style>
<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 OPENING BALANCE' : 'UPDATE OPENING BALANCE'; ?></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'], 'ob_list') ) { ?>
          <a href="index.php?page=opening_balance/list.php" class="btn btn-danger capital bottom10 custome-btn">VIEW OPENING BALANCE 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="opening_balance/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">VOUCHER NO</label><span style="color: red">*</span>
                    <input type="text" size="35" id="voucher_no" name="voucher_no" class="form-control required" autocomplete="off" placeholder="VOUCHER NO" value="<?php echo $voucher_no?>" autofocus=""/>
                  </div>
                  </div>

                  <div class="col-md-2">
                    <div class="form-group">
                      <label class="capital">VOUCHER DATE</label><span style="color: red">*</span>
                        <div class="input-group date">
                          <div class="input-group-addon">
                             <i class="fa fa-calendar"></i>
                          </div>
                          <input type="text" size="35" id="v_date" name="v_date" class="form-control date-picker" placeholder="DATE" autocomplete="off" value="<?php echo $v_date;?>" />
                        </div>
                    </div>
                  </div>
                  <div class="col-md-3">
                    <div class="form-group">
                      <label class="capital">PARTY</label><span style="color: red">*</span>
                      <select class="form-control select2 required" name="account_name" id="account_name">
                        <option></option>
                          <?php 
                            $truck     = "SELECT * FROM `act_master`";
                            $truck_res = mysqli_query($conn,$truck);
                            $categories = array();
                            while ($truck_row = mysqli_fetch_array($truck_res)) {
                              $categories[] = $truck_row;
                            }
                            foreach ($categories as $category) {
                              echo '<option value="'. $category['id'] .'" '. ($category['act_code'] == $account_name ? 'selected' : '') .'>' . $category['ac_name'] . '</option>';
                            }  
                          ?>
                      </select>
                    </div>
                  </div>
                  <div class="col-md-2">
                    <div class="form-group">
                      <label class="capital">AMOUNT</label><span style="color: red">*</span>
                      <input type="text" size="35" id="amount" name="amount" class="form-control amount required number" autocomplete="off" placeholder="AMOUNT" value="<?php echo $amount;?>" />
                    </div>
                  </div>
                  <div class="col-md-3">
                    <div class="form-group">
                      <label class="capital">NARRATION</label>
                      <textarea class="form-control height60" autocomplete="off" placeholder="NARRATION" name="narration"><?php echo $narration;?></textarea>
                    </div>
                  </div>
                </div>
                <div class="row">
                  <div class="col-md-3">
                    <div class="form-group">
                      <label class="capital">FLAG</label>
                      <select class="form-control height29 required select2" name="flag" style="width: 100%;" data-placeholder="SELECT" aria-required="true" id="flag" onChange="ChecFlag(this.value);">
                        <option value></option>
                        <option value="LHC" <?php if($flag == 'LHC') { ?> selected="selected"<?php } ?>>LHC</option>
                        <option value="MR" <?php if($flag == 'MR') { ?> selected="selected"<?php } ?>>MR</option>
                        <option value="BILLING" <?php if($flag == 'BILLING') { ?> selected="selected"<?php } ?>>BILLING</option>
                      </select>
                    </div>
                  </div>
                  <div class="col-md-9">
                    <!-- <div id="txtHints01"></div> -->
                    <input type="hidden" value="<?php echo $id;?>" name="id" id="id">
                  </div>
                </div>
                <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=opening_balance/list.php" class="btn btn-danger capital"> Cancel</a>
                </div>
              </form>
              
            </div>
         </div>
      </div>
   </div>
</section>

<script type="text/javascript">
  jQuery(document).ready(function() {
    $('#account_name').select2();
    $('#flag').select2();
  });
  function ChecFlag() {
    var status      = $("#flag").val();
    var voucher_no  = $("#voucher_no").val();
    $.ajax({
      'type': 'POST',
      'url': 'opening_balance/check_voucher.php',
      'data': {
        'status': status,
        'voucher_no': voucher_no
      },
      dataType: 'JSON',
      success: function (result) {
        if(result.status == 1) {
          $('#signupForm')[0].reset();
          swal("Opps !!", "VOUCHER CODE ALREADY EXITS...!!!", "info")  
          setTimeout("location.reload(true);",2000);
        }else{
          jQuery('.submit').attr('enable', true);
        }
      }
    });
  };
</script>