<!-- 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">Journal</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="accounts">Accounts</a></li>
					<li class="breadcrumb-item"><a href="accounts/journal-list">Journal</a></li>
					<li class="breadcrumb-item active"><?php if ( count( $master ) > 0 ): ?>Edit<?php else: ?>Add New<?php endif;?> Journal</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="frmJournal">
					<div class="card card-primary">
						<div class="card-header">
							<h3 class="card-title"><?php if ( count( $master ) > 0 ): ?>Edit<?php else: ?>Add New<?php endif;?> Journal</h3>
						</div>

						<div class="card-body">
							<div class="row">
								<div class="col-md-6">
									<div class="form-group">
										<label>Journal No</label>
										<input type="text" name="journal_no" id="journal_no" class="form-control" placeholder="Enter Journal No ..." value="<?php echo $journal_no; ?>">
									</div>
									<div class="form-group">
										<label>Narration</label>
										<textarea name="journal_memo" id="journal_memo" class="form-control" rows="3" placeholder="Narration..."><?php if ( count( $master ) > 0 ): echo $master['memo'];else:echo set_value( 'memo' );endif;?></textarea>
									</div>
								</div>

								<div class="col-md-6">
									<div class="form-group">
										<label for="journal-date-div">Journal Date</label>
										<div class="input-group date datepicker" id="journal-date-div" data-target-input="nearest">
											<input type="text" name="journal_date" id="journal-date" class="form-control" data-target="#journal-date-div" value="<?php if ( count( $master ) > 0 ): echo date_to_ui( $master['journal_date'] );else:echo date( 'd/m/Y' );endif;?>" />
											<div class="input-group-append" data-target="#journal-date-div" data-toggle="datetimepicker">
												<div class="input-group-text"><i class="fa fa-calendar"></i></div>
											</div>
										</div>
									</div>
								</div>
							</div>

							<div class="row">
								<div class="col-md-6">
									<div class="form-group">
										<h3 class="box-header with-border">Debit Voucher</h3>
										<label>Select Account</label>
										<select name="debit_chart_id" id="debit_chart_id" class="form-control select2" data-placeholder="chart_id">
											<?php echo $ac_chart_tree; ?>
										</select>
									</div>
									<div class="form-group">
										<label>Debit Amount</label>
										<input type="text" name="debit_amount" id="debit_amount" class="form-control" placeholder="00.00"value="<?php echo set_value( 'debit' ); ?>" />
									</div>
									<div class="form-group">
										<input type="button" class="btn btn-primary pull-right" name="debit_add" id="debit_add" value="Add Debit">
									</div>
								</div>

								<div class="col-md-6">
									<div class="form-group">
										<h3 class="box-header with-border">Credit Voucher</h3>
										<label>Select Account</label>
										<select name="credit_chart_id" id="credit_chart_id" class="form-control select2" data-placeholder="chart_id">
											<?php echo $ac_chart_tree; ?>
										</select>
									</div>
									<div class="form-group">
										<label>Credit Amount</label>
										<input type="text" name="credit_amount"  class="form-control" id="credit_amount" placeholder="00.00" value="<?php echo set_value( 'credit' ); ?>">
									</div>
									<div class="form-group">
										<input type="button" name="credit_add" id="credit_add" class="btn btn-primary pull-right" value="Add Credit">
									</div>
								</div>
							</div>
						</div>
					</div>
				</form>
			</div>
		</div>
	</div>
</section>

<!-- List voucher -->
<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">Voucher List</h3>
						</div>

						<div class="card-body">
							<div class="row">
								<div class="col-md-6 pull-left">
									<div id="debit_details">
										<table class="table table-bordered table-striped">
											<thead>
												<tr>
													<th>Debit Account</th>
													<th>Amount</th>
													<th>Memo</th>
													<th>Action</th>
												</tr>
											</thead>
											<tbody>
												<?php
													$debit_amount = 0;
													foreach ( $details as $list ):
														if ( $list['debit'] ):
													?>
													<tr>
														<td><?php echo $list['chart_name']; ?></td>
														<td><?php echo $list['debit']; ?></td>
														<td><?php echo $list['memo']; ?></td>
														<td class="text-center">
															<input type="hidden" value="<?php echo $list['id']; ?>" /><span class="btn btn-danger debit_voucher_delete"><i class="fa fa-remove"></i> Delete</span>
														</td>
													</tr>
													<?php
															$debit_amount += $list['debit'];
														endif;
													endforeach;
												?>
											</tbody>
											<tfoot>
												<tr>
													<th class="left" colspan="4">&nbsp;</th>
												</tr>
												<tr>
													<th colspan="1">Total </th>
													<th colspan="3" class="right" id="debit_total"><?php echo number_format( $debit_amount, 2 ); ?></th>
												</tr>
											</tfoot>
										</table>
									</div>
								</div>

								<div class="col-md-6 pull-right">
									<div id="credit_details">
										<table class="table table-bordered table-striped">
											<thead>
												<tr>
													<th>Credit Account</th>
													<th>Amount</th>
													<th>Memo</th>
													<th>Action</th>
												</tr>
											</thead>
											<tbody>
												<?php
													$credit_amount = 0;
													foreach ( $details as $list ):
														if ( $list['credit'] ):
													?>
													<tr>
														<td><?php echo $list['chart_name']; ?></td>
														<td><?php echo $list['credit']; ?></td>
														<td><?php echo $list['memo']; ?></td>
														<td class="text-center">
															<input type="hidden" value="<?php echo $list['id']; ?>"><span class="btn btn-danger credit_voucher_delete"><i class="fa fa-remove"></i> Delete</span>
														</td>
													</tr>
													<?php
															$credit_amount += $list['credit'];
														endif;
													endforeach;
												?>
											</tbody>
											<tfoot>
												<tr>
													<th class="left" colspan="4">&nbsp;</th>
												</tr>
												<tr>
													<th colspan="1">Total </th>
													<th colspan="3" class="right" id="credit_total"><?php echo number_format( $credit_amount, 2 ); ?></th>
												</tr>
											</tfoot>
										</table>
									</div>
								</div>
							</div>
						</div>

						<div class="card-footer text-center">
							<input type="button" id="journal_complete" class="btn btn-success" value="Complete Journal Entry">
						</div>
					</div>
				</form>
			</div>
		</div>
	</div>
</section>

<script>
	var journal_draft = false;
	$(window).on('beforeunload', function(){
		if(journal_draft === true)
		{
			return true;
		}
	});
	$(window).on('unload', function() {
		if (journal_draft === true)
		{
			let journal_no = $('#journal_no').val();
			$.ajax({
				type: "POST",
				url: "accounts/delete_unsaved_journal",
				data: {journal_no: journal_no},
				async: false,
				success: function(msg) {
					return true;
				}
			});
		}
	});
</script>
