<?php
$edited = can_action('153', 'edited');
$can_edit = $this->return_stock_model->can_action('tbl_return_stock', 'edit', array('return_stock_id' => $payments_info->return_stock_id));
$currency = $this->return_stock_model->check_by(array('code' => config_item('default_currency')), 'tbl_currencies');
$payments_id = !empty($payments_info) ? $payments_info->payments_id : null;
$purchase_due = $this->return_stock_model->calculate_to('return_stock_due', $payments_info->return_stock_id);
$return_stock_info = $this->return_stock_model->check_by(array('return_stock_id' => $payments_info->return_stock_id), 'tbl_return_stock');

if (is_numeric($payments_info->payment_method)) {
	$payment_method = get_row('tbl_payment_methods', array('payment_methods_id' => $payments_info->payment_method));
} else {
	$payment_method->method_name = $payments_info->payment_method;
}

$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_selected = is_numeric($payments_info->payment_method) ? $payments_info->payment_method : '0';
$payment_method_add = form_select_add_link('payment_methods_id', base_url('admin/settings/inline_payment_method'), lang('new') . ' ' . lang('payment_method'), '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('edit_payment') ?></h2>
			<p class="text-sm text-muted-foreground"><?= lang('return_stock') . ' ' . lang('payment') ?></p>
		</div>
		<a href="<?= base_url('admin/return_stock/all_payments') ?>" class="inline-flex items-center gap-2 rounded-lg border border-border bg-card px-3 py-2 text-xs font-semibold text-foreground hover:bg-muted">
			<i data-lucide="arrow-left" class="h-4 w-4"></i>
			<?= lang('back') ?>
		</a>
	</div>

	<div class="grid grid-cols-1 lg:grid-cols-12 gap-6">
		<div class="lg:col-span-5">
			<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="edit" class="h-5 w-5"></i>
					<h2 class="text-base font-semibold text-foreground"><?= lang('edit_payment') ?></h2>
				</div>
				<?php echo form_open(base_url('admin/return_stock/update_payemnt/' . $payments_id), array('class' => 'space-y-5 mt-5', 'enctype' => 'multipart/form-data', 'data-parsley-validate' => '', 'role' => 'form')); ?>
					<?= form_input_group('amount', lang('amount'), $payments_info->amount, 'text', true, array('data-parsley-type' => 'number')); ?>
					<?= form_select_group('payment_methods_id', lang('payment_method'), $payment_method_options, $payment_method_selected, true, array('class' => 'select_box', 'data-live-search' => 'true'), '', $payment_method_add); ?>
					<?= form_datepicker('payment_date', lang('payment_date'), !empty($payments_info->payment_date) ? $payments_info->payment_date : date('Y-m-d'), true, array('data-auto-close' => 'true')); ?>
					<?= form_textarea_group('notes', lang('notes'), $payments_info->notes, 3, false); ?>
					<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="save" class="h-4 w-4"></i>
							<?= lang('save_changes') ?>
						</button>
					</div>
				<?php echo form_close(); ?>
			</div>
		</div>

		<div class="lg:col-span-7">
			<div class="rounded-2xl border border-border bg-card p-6 shadow-sm space-y-6">
				<div class="flex flex-col gap-3 md:flex-row md:items-center md:justify-between">
					<div>
						<p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('payment_details') ?></p>
						<h3 class="text-lg font-semibold text-foreground"><?= $payments_info->trans_id ?></h3>
					</div>
					<?php if (!empty($can_edit) && !empty($edited)) { ?>
						<div class="flex items-center gap-2">
							<a href="<?= base_url('admin/return_stock/send_payment/' . $payments_info->payments_id . '/' . $payments_info->amount) ?>" class="inline-flex items-center gap-2 rounded-lg border border-border bg-card px-3 py-2 text-xs font-semibold text-foreground hover:bg-muted" title="<?= lang('send_email') ?>">
								<i data-lucide="mail" class="h-4 w-4"></i>
								<?= lang('send_email') ?>
							</a>
							<a href="<?= base_url('admin/return_stock/payments_pdf/' . $payments_info->payments_id) ?>" class="inline-flex items-center gap-2 rounded-lg border border-border bg-card px-3 py-2 text-xs font-semibold text-foreground hover:bg-muted" title="<?= lang('pdf') ?>">
								<i data-lucide="file-text" class="h-4 w-4"></i>
								<?= lang('pdf') ?>
							</a>
						</div>
					<?php } ?>
				</div>

				<div class="rounded-xl border border-border bg-muted/40 p-4">
					<div class="text-xs text-muted-foreground"><?= lang('amount_received') ?></div>
					<div class="text-2xl font-semibold text-foreground"><?= display_money($payments_info->amount, $currency->symbol); ?></div>
				</div>

				<dl class="grid grid-cols-1 md:grid-cols-2 gap-4 text-sm">
					<div class="space-y-1">
						<dt class="text-muted-foreground"><?= lang('payment_date') ?></dt>
						<dd class="font-medium text-foreground"><?= display_date($payments_info->payment_date); ?></dd>
					</div>
					<?php if (config_item('amount_to_words') == 'Yes') { ?>
						<div class="space-y-1">
							<dt class="text-muted-foreground"><?= lang('num_word') ?></dt>
							<dd class="font-medium text-foreground"><?= number_to_word('', $payments_info->amount); ?></dd>
						</div>
					<?php } ?>
					<div class="space-y-1">
						<dt class="text-muted-foreground"><?= lang('transaction_id') ?></dt>
						<dd class="font-medium text-foreground"><?= html_escape($payments_info->trans_id) ?></dd>
					</div>
					<div class="space-y-1">
						<dt class="text-muted-foreground"><?= lang('payment_mode') ?></dt>
						<dd class="font-medium text-foreground"><?= !empty($payment_method->method_name) ? $payment_method->method_name : '-' ?></dd>
					</div>
					<div class="space-y-1 md:col-span-2">
						<dt class="text-muted-foreground"><?= lang('notes') ?></dt>
						<dd class="font-medium text-foreground whitespace-pre-line"><?= $payments_info->notes ?></dd>
					</div>
				</dl>

				<div class="border-t border-border pt-4">
					<h4 class="text-sm font-semibold text-foreground mb-3"><?= lang('payment_for') ?></h4>
					<div class="table-responsive">
						<table class="table table-striped">
							<thead>
								<tr>
									<th><?= lang('reference_no') ?></th>
									<th class="text-right"><?= lang('return_stock_date') ?></th>
									<th class="text-right"><?= lang('return_stock') . ' ' . lang('amount') ?></th>
									<th class="text-right"><?= lang('paid_amount') ?></th>
									<?php if ($purchase_due > 0) { ?>
										<th class="text-right"><?= lang('due_amount') ?></th>
									<?php } ?>
								</tr>
							</thead>
							<tbody>
								<tr>
									<td><a href="<?= base_url('admin/return_stock/return_stock_details/' . $payments_info->return_stock_id) ?>"><?= html_escape($return_stock_info->reference_no) ?></a></td>
									<td class="text-right"><?= display_date($return_stock_info->return_stock_date) ?></td>
									<td class="text-right"><?= display_money($this->return_stock_model->calculate_to('total', $return_stock_info->return_stock_id), $currency->symbol); ?></td>
									<td class="text-right"><?= display_money($payments_info->amount, $currency->symbol); ?></td>
									<?php if ($purchase_due > 0) { ?>
										<td class="text-right text-rose-600"><?= display_money($purchase_due, $currency->symbol); ?></td>
									<?php } ?>
								</tr>
							</tbody>
						</table>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
