<!-- 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 Return</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-return-list">Purchase Return</a></li>
					<li class="breadcrumb-item active"><?php if ( count( $purchase_return ) > 0 ): ?>Edit<?php else: ?>Add New<?php endif;?> Purchase Return</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="frmPurchaseReturn" action="inventory/purchase-return-save" method="post">
					<div class="card card-primary">
						<div class="card-header">
							<h3 class="card-title"><?php if ( count( $purchase_return ) > 0 ): ?>Edit<?php else: ?>Add New<?php endif;?> Purchase Return</h3>
						</div>

						<div class="card-body">
							<div class="row">
								<div class="col-md-6">
									<div class="form-group">
										<label for="purchase_return_no">Purchase Return No</label>
										<input type="text" name="purchase_return_no" id="purchase_return_no" class="form-control" value="<?php if ( count( $purchase_return ) > 0 ) {echo $purchase_return['purchase_return_no'];} else {echo $purchase_return_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="Keep Blank for Default Price">
									</div>
								</div>

								<div class="col-md-6">
									<div class="form-group">
										<label for="purchase-return-date">Purchase Return Date</label>
										<div class="input-group date datepicker" id="purchase-return-date" data-target-input="nearest">
											<input type="text" name="purchase_return_date" id="purchase_return_date" class="form-control" data-target="#purchase-return-date" value="<?php if ( count( $purchase_return ) > 0 ) {echo date_to_ui( $purchase_return['purchase_return_date'] );} else {echo date( 'd/m/Y' );}?>" />
											<div class="input-group-append" data-target="#purchase-return-date" data-toggle="datetimepicker">
												<div class="input-group-text"><i class="fa fa-calendar"></i></div>
											</div>
										</div>
									</div>
									<div class="form-group">
										<label for="supplier_id">Select Supplier</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_return ) > 0 ) {if ( $supplier['id'] == $purchase_return['supplier_id'] ) {echo 'selected';}}?>><?php echo $supplier['name']; ?></option>
											<?php endforeach;?>
										</select>
									</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_return ) > 0 ) {echo $purchase_return['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_return ) > 0 ): ?>
							<input type="hidden" name="id" id="purchase_return_id" value="<?php echo $purchase_return['id']; ?>" />
						<?php endif;?>

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

<!-- List Purchase Return -->
<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 Return Item List</h3>
						</div>

						<div class="card-body">
							<div class="row">
								<div class="col-md-12" id="purchase_return_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"><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_return_complete" value="Complete Purchase Return Entry">
						</div>
					</div>
				</form>
			</div>
		</div>
	</div>
</section>

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