<!-- Content Header (Page header) -->
<section class="content-header">
	<div class="container-fluid">
		<div class="row mb-2">
			<div class="col-sm-6">
				<h1 class="m-0">Purchase</h1>
			</div>

			<div class="col-sm-6">
				<ol class="breadcrumb float-sm-right">
					<li class="breadcrumb-item"><a href="dashboard">Dashboard</a></li>
					<li class="breadcrumb-item"><a href="inventory">Inventory</a></li>
					<li class="breadcrumb-item"><a href="inventory/purchase-list">Purchase</a></li>
					<li class="breadcrumb-item active"><?php if ( count( $purchase ) > 0 ): ?>Edit<?php else: ?>Add New<?php endif;?> Purchase</li>
				</ol>
			</div>
		</div>
	</div>
</section>

<!-- Main content -->
<section class="content">
	<div class="container-fluid">
		<!-- Small boxes (Stat box) -->
		<div class="row">
			<div class="col-md-12">
				<form id="frmPurchase" action="inventory/purchase-save" method="post">
					<div class="card card-primary">
						<div class="card-header">
							<h3 class="card-title"><?php if ( count( $purchase ) > 0 ): ?>Edit<?php else: ?>Add New<?php endif;?> Purchase</h3>
						</div>

						<div class="card-body">
							<div class="row">
								<div class="col-md-6">
									<div class="form-group">
										<label for="purchase_no">Purchase No</label>
										<input type="text" name="purchase_no" id="purchase_no" class="form-control" value="<?php if ( count( $purchase ) > 0 ) {echo $purchase['purchase_no'];} else {echo $purchase_no;}?>">
									</div>
									<div class="form-group">
										<label for="item_id">Select Item</label>
										<select name="item_id" id="item_id" class="form-control change-price select2">
											<?php foreach ( $items as $item ): ?>
												<option value="<?php echo $item['id']; ?>"><?php echo $item['code'] . ' - ' . $item['name']; ?></option>
											<?php endforeach;?>
										</select>
									</div>
									<div class="form-group">
										<label for="quantity">Quantity</label>
										<input type="text" name="quantity" id="quantity" class="form-control required" placeholder="0">
									</div>
									<div class="form-group">
										<label for="price">Price per Unit</label>
										<input type="text" name="price" id="price" class="form-control" placeholder="Price per unit">
									</div>
								</div>

								<div class="col-md-6">
									<div class="form-group">
										<label for="purchase-date">Purchase Date</label>
										<div class="input-group date datepicker" id="purchase-date" data-target-input="nearest">
											<input type="text" name="purchase_date" id="purchase_date" class="form-control" data-target="#purchase-date" value="<?php if ( count( $purchase ) > 0 ) {echo date_to_ui( $purchase['purchase_date'] );} else {echo date( 'd/m/Y' );}?>" />
											<div class="input-group-append" data-target="#purchase-date" data-toggle="datetimepicker">
												<div class="input-group-text"><i class="fa fa-calendar"></i></div>
											</div>
										</div>
									</div>
									<div class="form-group">
										<div class="row">
											<div class="col-md-9">
												<label for="supplier_id">Select Supplier &nbsp;&nbsp;</label>
												<select name="supplier_id" id="supplier_id" class="form-control select2">
													<?php foreach ( $suppliers as $supplier ): ?>
														<option value="<?php echo $supplier['id']; ?>"<?php if ( count( $purchase ) > 0 ) {if ( $supplier['id'] == $purchase['supplier_id'] ) {echo 'selected';}}?>><?php echo $supplier['name']; ?></option>
													<?php endforeach;?>
												</select>
											</div>
											<div class="col-md-3">
												<label for="add-supplier-button">&nbsp;&nbsp;</label>
												<br>
												<button type="button" id="add-supplier-button" class="btn bg-gradient-primary" data-toggle="modal" data-target="#modal-supplier"><strong>Add New Supplier</strong></button>
											</div>
										</div>
									</div>
									<div class="form-group">
										<label for="notes">Notes</label>
										<textarea name="notes" id="notes" rows="3" class="form-control" placeholder="Notes"><?php if ( count( $purchase ) > 0 ) {echo $purchase['notes'];}?></textarea>
									</div>
								</div>
							</div>
						</div>

						<input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash(); ?>">
						<?php if ( count( $purchase ) > 0 ): ?>
							<input type="hidden" name="id" id="purchase_id" value="<?php echo $purchase['id']; ?>">
						<?php endif;?>

						<div class="card-footer text-center">
							<button type="button" id="purchase_add_item" class="btn bg-gradient-primary"><strong>Add Item</strong></button>
						</div>
					</div>
				</form>
			</div>
		</div>
	</div>
</section>

<!-- List Purchase -->
<section class="content">
	<div class="container-fluid">
		<!-- Small boxes (Stat box) -->
		<div class="row">
			<div class="col-md-12">
				<form id="frmJournal">
					<div class="card card-primary">
						<div class="card-header">
							<h3 class="card-title">Purchase Item List</h3>
						</div>

						<div class="card-body">
							<div class="row">
								<div class="col-md-12" id="purchase_details">
									<table id="data_table" class="table table-bordered table-striped">
										<thead>
											<tr>
												<th>Item Code</th>
												<th>Item Name</th>
												<th>Quantity</th>
												<th>Purchase Price</th>
												<th>Total Price</th>
												<th class="text-center">Action</th>
											</tr>
										</thead>
										<tbody>
											<?php
												$qty   = 0;
												$price = 0;
												foreach ( $details as $list ):
											?>
											<tr>
												<td><?php echo $list['item_code']; ?></td>
												<td><?php echo $list['item_name']; ?></td>
												<td><?php echo $list['quantity']; ?></td>
												<td><?php echo $list['purchase_price']; ?></td>
												<td><?php echo number_format( $list['quantity'] * $list['purchase_price'], 2 ); ?></td>
												<td class="text-center">
													<input type="hidden" value="<?php echo $list['id']; ?>" /><span class="btn btn-danger purchase_item_delete"><i class="icon-trash icon-white"></i>Delete</span>
												</td>
											</tr>
											<?php
												$qty += $list['quantity'];
												$price += $list['quantity'] * $list['purchase_price'];
												endforeach;
											?>
										</tbody>
										<tfoot>
											<tr>
												<th colspan="6">Order Totals</th>
											</tr>
											<tr>
												<td colspan="2">&nbsp;</td>
												<td><?php echo $qty; ?></td>
												<td></td>
												<td><?php echo number_format( $price, 2 ); ?></td>
												<td></td>
											</tr>
											<tr>
												<td colspan="4">Total Paid Amount</td>
												<td><input type="text" name="paid_amount" id="paid_amount"></td>
												<td></td>
											</tr>
										</tfoot>
									</table>
								</div>
							</div>
						</div>

						<div class="card-footer text-center">
							<input type="button" class="btn bg-gradient-success" id="purchase_complete" value="Complete Purchase Entry">
						</div>
					</div>
				</form>
			</div>
		</div>
	</div>
</section>

<div class="modal fade" id="modal-supplier">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<h4 class="modal-title">Supplier Entry Form</h4>
				<button type="button" class="close" data-dismiss="modal" aria-label="Close">
				<span aria-hidden="true">&times;</span>
				</button>
			</div>

			<div class="modal-body">
				<div class="form-group">
					<label for="code">Supplier Code</label>
					<input type="text" name="code" id="code" value="<?php echo $code; ?>" class="form-control" placeholder="Supplier Code"> Digit Only
				</div>
				<div class="form-group">
					<label for="name">Supplier Name</label>
					<input type="text" name="name" id="name" class="form-control" placeholder="Supplier Name">
				</div>
				<div class="form-group">
					<label for="address">Supplier Address</label>
					<textarea name="address" id="address" class="form-control" placeholder="Supplier Address"></textarea>
				</div>
				<div class="form-group">
					<label for="contact_person">Contact Person</label>
					<input type="text" name="contact_person" id="contact_person" class="form-control" placeholder="Contact Person">
				</div>
				<div class="form-group">
					<label for="phone_no">Phone No.</label>
					<input type="text" name="phone_no" id="phone_no" class="form-control" placeholder="Supplier's Phone No.">
				</div>
				<div class="form-group">
					<label for="notes">Notes</label>
					<textarea name="notes" id="notes" class="form-control" placeholder="Notes"></textarea>
				</div>
			</div>

			<div class="modal-footer justify-content-between">
				<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
				<button type="button" id="add_supplier" class="btn btn-primary">Save changes</button>
			</div>
		</div>
	</div>
</div>

<script>
	var purchase_id = document.getElementById("purchase_id")
	if (purchase_id == null) {
		var purchase_draft = false;
		$(window).on('beforeunload', function(){
			if(purchase_draft === true)
			{
				return true;
			}
		});
		$(window).on('unload', function(){
			if (purchase_draft === true)
			{
				let purchase_no = $('#purchase_no').val();
				$.ajax({
					type: "POST",
					url: "inventory/purchase_unsaved_delete",
					data: {purchase_no: purchase_no},
					async: false,
					success: function(msg) {
						return true;
					}
				});
			}
		});
	}
</script>