<?php

include_once('../header.php');
include_once('../utility/common.php');
include_once(ROOT_PATH.'language/'.$lang_code_global.'/lang_add_bill.php');
if(!check_login()){
	header("Location: " . WEB_URL . "logout.php");
	die();
}
$success = "none";
$bill_type = '';
$bill_date = '';
$bill_month = '';
$bill_year = '';
$total_amount = '';
$deposit_bank_name = '';
$bill_details = '';
$branch_id = '';
$title = $_data['text_1'];
$button_text = $_data['save_button_text'];
$successful_msg = $_data['text_15'];
$form_url = WEB_URL . "bill/add_bill.php";
$id="";
$hdnid="0";

if(isset($_POST['ddlBillType'])){
	if(isset($_POST['hdn']) && $_POST['hdn'] == '0'){
		$sql = "INSERT INTO bills(bill_type,bill_date,bill_month,bill_year,total_amount,deposit_bank_name,bill_details,branch_id) values('$_POST[ddlBillType]','$_POST[txtBillDate]','$_POST[ddlBillMonth]','$_POST[ddlBillYear]','$_POST[txtTotalAmount]','$_POST[txtDepositBankName]','$_POST[txtBillDetails]','" . $_SESSION['objLogin']['branch_id'] . "')";
		mysqli_query($link,$sql);
		//mysqli_close($link);
		$url = WEB_URL . 'bill/bill_list.php?m=add';
		header("Location: $url");
	}else{
		$sql = "UPDATE `bills` SET `bill_type`='".$_POST['ddlBillType']."',`bill_date`='".$_POST['txtBillDate']."',`bill_month`='".$_POST['ddlBillMonth']."',`bill_year`='".$_POST['ddlBillYear']."',`total_amount`='".$_POST['txtTotalAmount']."',`deposit_bank_name`='".$_POST['txtDepositBankName']."',`bill_details`='".$_POST['txtBillDetails']."' WHERE bill_id='".$_GET['id']."'";
		mysqli_query($link,$sql);
		$url = WEB_URL . 'bill/bill_list.php?m=up';
		header("Location: $url");
	}
	$success = "block";
}

if(isset($_GET['id']) && $_GET['id'] != ''){
	$result = mysqli_query($link,"SELECT * FROM bills where bill_id = '" . $_GET['id'] . "'");
	if($row = mysqli_fetch_array($result)){
		$bill_type = $row['bill_type'];
		$bill_date = $row['bill_date'];
		$bill_month = $row['bill_month'];
		$bill_year = $row['bill_year'];
		$total_amount = $row['total_amount'];
		$deposit_bank_name = $row['deposit_bank_name'];
		$bill_details = $row['bill_details'];
		$hdnid = $_GET['id'];
		$title = $_data['text_1_1'];
		$button_text = $_data['update_button_text'];
		$successful_msg = $_data['text_16'];
		$form_url = WEB_URL . "bill/add_bill.php?id=".$_GET['id'];
	}
}
?>
<!-- Content Header (Page header) -->

<section class="content-header">
  <h1><?= $title;?></h1>
  <ol class="breadcrumb">
    <li><a href="<?= WEB_URL?>dashboard.php"><i class="fa fa-dashboard"></i><?= $_data['home_breadcam'];?></a></li>
    <li class="active"><?= $_data['text_2'];?></li>
    <li class="active"><?= $title;?></li>
  </ol>
</section>
<!-- Main content -->
<section class="content">
<!-- Full Width boxes (Stat box) -->
<div class="row">
  <div class="col-md-12">
    <div align="right" style="margin-bottom:1%;"> </div>
    <div class="box box-success">
      <div class="box-header">
        <h3 class="box-title"><?= $_data['text_4'];?></h3>
      </div>
      <form onSubmit="return validateMe();" action="<?= $form_url; ?>" method="post" enctype="multipart/form-data">
        <div class="box-body row">
          <div class="form-group col-md-6">
            <label for="ddlBillType"><span class="errorStar">*</span> <?= $_data['text_5'];?> :</label>
            <select name="ddlBillType" id="ddlBillType" class="form-control">
              <option value="">--<?= $_data['text_6'];?>--</option>
              <?php 
				  $result_unit = mysqli_query($link,"SELECT * FROM bill_types order by bt_id ASC");
				while($row_unit = mysqli_fetch_array($result_unit)){?>
              <option <?php if($bill_type == $row_unit['bt_id']){echo 'selected';}?> value="<?= $row_unit['bt_id'];?>"><?= $row_unit['bill_type'];?></option>
              <?php } ?>
            </select>
          </div>
          <div class="form-group col-md-6">
            <label for="txtBillDate"><span class="errorStar">*</span> <?= $_data['text_7'];?> :</label>
            <input type="text" name="txtBillDate" value="<?= $bill_date;?>" id="txtBillDate" class="form-control datepicker"/>
          </div>
          <div class="form-group col-md-6">
            <label for="ddlBillMonth"><span class="errorStar">*</span> <?= $_data['text_8'];?> :</label>
            <select name="ddlBillMonth" id="ddlBillMonth" class="form-control">
              <option value="">--<?= $_data['text_9'];?>--</option>
              <?php 
				  	$result_unit = mysqli_query($link,"SELECT * FROM tbl_add_month_setup order by m_id ASC");
					while($row_unit = mysqli_fetch_array($result_unit)){?>
              <option <?php if($bill_month == $row_unit['m_id']){echo 'selected';}?> value="<?= $row_unit['m_id'];?>"><?= $row_unit['month_name'];?></option>
              <?php } ?>
            </select>
          </div>
          <div class="form-group col-md-6">
            <label for="ddlBillYear"><span class="errorStar">*</span> <?= $_data['text_10'];?> :</label>
            <select name="ddlBillYear" id="ddlBillYear" class="form-control">
              <option value="">--<?= $_data['text_11'];?>--</option>
              <?php 
				  	$result_unit = mysqli_query($link,"SELECT * FROM tbl_add_year_setup order by y_id ASC");
					while($row_unit = mysqli_fetch_array($result_unit)){?>
              <option <?php if($bill_year == $row_unit['y_id']){echo 'selected';}?> value="<?= $row_unit['y_id'];?>"><?= $row_unit['xyear'];?></option>
              <?php } ?>
            </select>
          </div>
          <div class="form-group col-md-6">
            <label for="txtTotalAmount"><span class="errorStar">*</span> <?= $_data['text_12'];?> :</label>
            <div class="input-group">
              <input type="text" name="txtTotalAmount" value="<?= $total_amount;?>" id="txtTotalAmount" class="form-control"/>
              <div class="input-group-addon"><?= CURRENCY;?></div>
            </div>
          </div>
          <div class="form-group col-md-6">
            <label for="txtDepositBankName"><span class="errorStar">*</span> <?= $_data['text_13'];?> :</label>
            <input type="text" name="txtDepositBankName" value="<?= $deposit_bank_name;?>" id="txtDepositBankName" class="form-control"/>
          </div>
          <div class="form-group col-md-12">
            <label for="txtBillDetails"><span class="errorStar">*</span> <?= $_data['text_14'];?> :</label>
            <textarea name="txtBillDetails" id="txtBillDetails" class="form-control"><?= $bill_details;?></textarea>
          </div>
        </div>
		<div class="box-footer">
          <div class="form-group pull-right">
            <button type="submit" name="button" class="btn btn-success"><i class="fa fa-floppy-o"></i> <?= $button_text; ?></button>
            <a class="btn btn-warning" href="<?= WEB_URL; ?>bill/bill_list.php"><i class="fa fa-reply"></i> <?= $_data['back_text'];?></a> </div>
        </div>
        <input type="hidden" value="<?= $hdnid; ?>" name="hdn"/>
      </form>
      <!-- /.box-body -->
    </div>
    <!-- /.box -->
  </div>
</div>
<!-- /.row -->
<script type="text/javascript">
function validateMe(){
	if($("#ddlBillType").val() == ''){
		alert("<?= $_data['required_1']; ?>");
		$("#ddlBillType").focus();
		return false;
	}
	else if($("#txtBillDate").val() == ''){
		alert("<?= $_data['required_2']; ?>");
		$("#txtBillDate").focus();
		return false;
	}
	else if($("#ddlBillMonth").val() == ''){
		alert("<?= $_data['required_3']; ?>");
		$("#ddlBillMonth").focus();
		return false;
	}
	else if($("#ddlBillYear").val() == ''){
		alert("<?= $_data['required_4']; ?>");
		$("#ddlBillYear").focus();
		return false;
	}
	else if($("#txtTotalAmount").val() == ''){
		alert("<?= $_data['required_5']; ?>");
		$("#txtTotalAmount").focus();
		return false;
	}
	else if($("#txtDepositBankName").val() == ''){
		alert("<?= $_data['required_6']; ?>");
		$("#txtDepositBankName").focus();
		return false;
	}
	else if($("#txtBillDetails").val() == ''){
		alert("<?= $_data['required_7']; ?>");
		$("#txtBillDetails").focus();
		return false;
	}
	else{
		return true;
	}
}
</script>
<?php include_once('../footer.php'); ?>
