<?php
$currency = $this->purchase_model->check_by(array('code' => config_item('default_currency')), 'tbl_currencies');
if (empty($purchase_info)) {
	redirect('admin/purchase/manage_purchase');
}
$this->load->helper('string');
$trans_id = random_string('nozero', 6);
$amount_due = round($this->purchase_model->calculate_to('purchase_due', $purchase_info->purchase_id), 2);

$payment_methods = $this->db->order_by('payment_methods_id', 'DESC')->get('tbl_payment_methods')->result();
$payment_method_options = array('0' => lang('select_payment_method'));
if (!empty($payment_methods)) {
	foreach ($payment_methods as $p_method) {
		$payment_method_options[$p_method->payment_methods_id] = $p_method->method_name;
	}
}
$payment_method_add = form_select_add_link('payment_methods_id', base_url('admin/settings/inline_payment_method'), lang('new') . ' ' . lang('payment_method'), 'md');

$account_default = config_item('default_account');
$account_label = '';
if (!empty($account_default)) {
	$account_label = get_row('tbl_accounts', array('account_id' => $account_default), 'account_name');
}
$account_add_link = form_lookup_add_link('account_id', base_url('admin/account/new_account'), lang('new') . ' ' . lang('account'), 'md');
?>

<div class="space-y-6">
	<div class="flex flex-col gap-3 md:flex-row md:items-center md:justify-between">
		<div>
			<h2 class="text-2xl font-semibold text-foreground"><?= lang('purchase') . ' ' . lang('payment') ?></h2>
			<p class="text-sm text-muted-foreground"><?= lang('pay') . ' ' . lang('purchase') ?></p>
		</div>
	</div>

	<div class="grid grid-cols-1 lg:grid-cols-12 gap-6">
		<div class="lg:col-span-4">
			<?= isset($purchase_sidebar_html) ? $purchase_sidebar_html : '' ?>
		</div>

		<div class="lg:col-span-8 space-y-6">
			<?= message_box('error'); ?>
			<div class="rounded-2xl border border-border bg-card p-6 shadow-sm">
				<div class="flex items-center gap-2 text-sm font-semibold text-foreground border-b border-border pb-3">
					<i data-lucide="credit-card" class="h-5 w-5"></i>
					<h2 class="text-base font-semibold text-foreground"><?= lang('pay') . ' ' . lang('purchase') ?></h2>
				</div>

				<form method="post" data-parsley-validate="" novalidate="" id="purchase_payment"
					action="<?= base_url() ?>admin/purchase/get_payment/<?= $purchase_info->purchase_id ?>"
					class="space-y-5 mt-5">
					<input type="hidden" name="currency" value="<?= $currency->symbol ?>">

					<div class="grid grid-cols-1 md:grid-cols-2 gap-5">
						<?= form_input_group('trans_id', lang('trans_id'), $trans_id, 'text', true, array('readonly' => 'readonly')); ?>
						<?= form_input_group('amount', lang('amount') . ' (' . $currency->symbol . ')', $amount_due, 'text', true, array('min' => 0)); ?>
						<?= form_datepicker('payment_date', lang('payment_date'), date('Y-m-d'), true, array('data-auto-close' => 'true')); ?>
					<?= form_select_group('payment_methods_id', lang('payment_method'), $payment_method_options, '0', true, array('class' => 'select_box', 'data-live-search' => 'true'), '', $payment_method_add); ?>
					</div>

					<?= form_textarea_group('notes', lang('notes'), '', 3, false); ?>

					<div class="grid grid-cols-1 md:grid-cols-3 gap-4">
						<label class="inline-flex items-center gap-2 rounded-lg border border-border bg-card px-3 py-2 text-xs font-semibold text-foreground">
							<input type="checkbox" class="custom-checkbox" name="send_thank_you">
							<span><?= lang('send_email') ?></span>
						</label>
						<label class="inline-flex items-center gap-2 rounded-lg border border-border bg-card px-3 py-2 text-xs font-semibold text-foreground">
							<input type="checkbox" class="custom-checkbox" name="send_sms">
							<span><?= lang('send') . ' ' . lang('sms') ?></span>
						</label>
						<label class="inline-flex items-center gap-2 rounded-lg border border-border bg-card px-3 py-2 text-xs font-semibold text-foreground">
							<input type="checkbox" checked class="custom-checkbox" id="use_postmark" name="deduct_from_account">
							<span><?= lang('deduct_from_default_account') ?></span>
						</label>
					</div>

					<div id="postmark_config" class="space-y-3">
						<?php
						$account_lookup = form_lookup_group(
							'account_id',
							lang('select') . ' ' . lang('account'),
							$account_default,
							$account_label,
							false,
							array(),
							'',
							array('type' => 'account', 'append' => $account_add_link)
						);
						echo $account_lookup;
						?>
						<div class="flex items-center justify-end">
							<button type="submit" class="inline-flex items-center gap-2 rounded-lg bg-indigo-600 px-4 py-2 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500">
								<i data-lucide="check" class="h-4 w-4"></i>
								<?= lang('add_payment') ?>
							</button>
						</div>
					</div>
				</form>
			</div>

			<?php if (!empty($all_payments_history)) {
				$reference = ": <a href='" . base_url('admin/purchase/purchase_details/' . $purchase_info->purchase_id) . "'>" . $purchase_info->reference_no . "</a>";
				$purchase_due = $this->purchase_model->calculate_to('purchase_due', $purchase_info->purchase_id);
			?>
				<div class="rounded-2xl border border-border bg-card shadow-sm">
					<div class="border-b border-border px-4 py-4">
						<div class="flex items-center gap-2 text-sm font-semibold text-foreground">
							<i data-lucide="clock" class="h-5 w-5"></i>
							<h2 class="text-base font-semibold text-foreground"><?= lang('payment_history_for_this_purchase', $reference) ?></h2>
						</div>
					</div>
					<div class="table-responsive p-4">
						<table class="table table-striped" cellspacing="0" width="100%">
							<thead>
								<tr>
									<th><?= lang('trans_id') ?></th>
									<th><?= lang('payment_date') ?></th>
									<th><?= lang('paid_amount') ?></th>
									<th><?= lang('payment_method') ?></th>
									<th><?= lang('account') ?></th>
								</tr>
							</thead>
							<tbody>
								<?php foreach ($all_payments_history as $v_payment_history) {
									$currency = $this->purchase_model->check_by(array('code' => config_item('default_currency')), 'tbl_currencies');
									if (is_numeric($v_payment_history->payment_method)) {
										$payment_methods = $this->purchase_model->check_by(array('payment_methods_id' => $v_payment_history->payment_method), 'tbl_payment_methods');
									} else {
										$payment_methods->method_name = $v_payment_history->payment_method;
									}
									$account = get_row('tbl_accounts', array('account_id' => $v_payment_history->account_id), 'account_name');
								?>
									<tr>
										<td><a href="<?= base_url('admin/purchase/payments_details/' . $v_payment_history->payments_id) ?>"><?= html_escape($v_payment_history->trans_id) ?></a></td>
										<td><?= display_date($v_payment_history->payment_date) ?></td>
										<td><?= display_money($v_payment_history->amount, $currency->symbol); ?></td>
										<td><?= !empty($payment_methods->method_name) ? $payment_methods->method_name : '-'; ?></td>
										<td><?= !empty($account) ? $account : '-'; ?></td>
									</tr>
								<?php } ?>
							</tbody>
						</table>
					</div>
					<div class="border-t border-border px-4 py-4">
						<div class="grid grid-cols-1 sm:grid-cols-3 gap-4 text-sm">
							<div class="rounded-lg border border-border bg-muted/40 px-4 py-3">
								<div class="text-xs text-muted-foreground"><?= lang('total') . ' ' . lang('invoice_amount') ?></div>
								<div class="text-lg font-semibold text-foreground"><?= display_money($this->purchase_model->calculate_to('total', $purchase_info->purchase_id), $currency->symbol) ?></div>
							</div>
							<div class="rounded-lg border border-border bg-muted/40 px-4 py-3">
								<div class="text-xs text-muted-foreground"><?= lang('total') . ' ' . lang('paid_amount') ?></div>
								<div class="text-lg font-semibold text-foreground"><?= display_money($this->purchase_model->calculate_to('paid_amount', $purchase_info->purchase_id), $currency->symbol) ?></div>
							</div>
							<div class="rounded-lg border border-border bg-muted/40 px-4 py-3">
								<div class="text-xs text-muted-foreground"><?= lang('total') . ' ' . lang('due_amount') ?></div>
								<div class="text-lg font-semibold text-foreground"><?= display_money($purchase_due, $currency->symbol) ?></div>
							</div>
						</div>
					</div>
				</div>
			<?php } ?>
		</div>
	</div>
</div>

<script>
	document.addEventListener('change', function(event) {
		if (!event.target || event.target.id !== 'use_postmark') return;
		var config = document.getElementById('postmark_config');
		if (!config) return;
		config.style.display = event.target.checked ? 'block' : 'none';
	});
</script>
