<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<?php
$currency = $this->transactions_model->check_by(array('code' => config_item('default_currency')), 'tbl_currencies');
$account_info = $this->transactions_model->check_by(array('account_id' => $expense_info->account_id), 'tbl_accounts');

$category = lang('undefined_category');
if ($expense_info->type == 'Income') {
	$category_info = $this->db->where('income_category_id', $expense_info->category_id)->get('tbl_income_category')->row();
	if (!empty($category_info)) {
		$category = $category_info->income_category;
	}
} else {
	$category_info = $this->db->where('expense_category_id', $expense_info->category_id)->get('tbl_expense_category')->row();
	if (!empty($category_info)) {
		$category = $category_info->expense_category;
	}
}

$client_name = $this->db->where('client_id', $expense_info->paid_by)->get('tbl_client')->row();
$payment_methods = !empty($expense_info->payment_methods_id)
	? $this->db->where('payment_methods_id', $expense_info->payment_methods_id)->get('tbl_payment_methods')->row()
	: null;

$actions = '';
if ($expense_info->billable == 'Yes' && $expense_info->project_id != 0 && $expense_info->invoices_id == 0) {
	$confirm_message = 'Are you sure to ' . lang('convert') . ' This ' . (!empty($expense_info->name) ? $expense_info->name : '-') . ' ?';
	$actions .= '<a onclick="return confirm(\'' . $confirm_message . '\')" href="' . base_url() . 'admin/transactions/convert/' . $expense_info->transactions_id . '" class="btn btn-primary btn-xs"><i data-lucide="copy" class="h-3 w-3"></i> ' . lang('convert_to_invoice') . '</a> ';
}
$actions .= btn_pdf('admin/transactions/download_pdf/' . $expense_info->transactions_id);

$detail_fields = array(
	array('label' => lang('deposit') . ' ' . lang('prefix'), 'value' => (!empty($expense_info->transaction_prefix) ? $expense_info->transaction_prefix : ' ')),
	array('label' => lang('name') . '/' . lang('title'), 'value' => (!empty($expense_info->name) ? $expense_info->name : '-')),
	array('label' => lang('date'), 'value' => display_date($expense_info->date)),
	array('label' => lang('account'), 'value' => (!empty($account_info->account_name) ? $account_info->account_name : '-')),
	array('label' => lang('amount'), 'value' => display_money($expense_info->amount, $currency->symbol)),
	array('label' => lang('categories'), 'value' => $category),
	array('label' => lang('paid_by'), 'value' => (!empty($client_name->name) ? $client_name->name : '-')),
	array('label' => lang('tags'), 'html' => get_tags($expense_info->tags, true)),
	array('label' => lang('payment_method'), 'value' => (!empty($payment_methods) ? $payment_methods->method_name : '-')),
);

$show_custom_fields = custom_form_label(1, $expense_info->transactions_id);
if (!empty($show_custom_fields)) {
	foreach ($show_custom_fields as $c_label => $v_fields) {
		if (!empty($v_fields)) {
			$detail_fields[] = array('label' => $c_label, 'html' => $v_fields, 'show_empty' => true);
		}
	}
}

$uploaded_file = !empty($expense_info->attachement) ? json_decode($expense_info->attachement) : array();
$attachments_html = '';
if (!empty($uploaded_file)) {
	$items = '';
	foreach ($uploaded_file as $fKey => $v_files) {
		if (empty($v_files)) {
			continue;
		}
		$icon = !empty($v_files->is_image) ? 'image' : 'file-text';
		$items .= '<div class="flex items-center justify-between gap-3 rounded-lg border border-border bg-white px-3 py-2">'
			. '<div class="flex items-center gap-2">'
			. '<i data-lucide="' . $icon . '" class="h-4 w-4 text-muted-foreground"></i>'
			. '<span class="text-sm text-foreground">' . html_escape($v_files->fileName) . '</span>'
			. '</div>'
			. '<a class="text-xs font-semibold text-primary" href="' . base_url() . 'admin/transactions/download/' . $expense_info->transactions_id . '/' . $fKey . '">' . lang('download') . '</a>'
			. '</div>';
	}
	$attachments_html = '<div class="space-y-2">' . $items . '</div>';
}
?>

<div class="rounded-2xl border border-border bg-card shadow-sm">
	<div class="flex items-center justify-between gap-3 border-b border-border px-5 py-4">
		<div>
			<p class="text-xs uppercase tracking-wide text-muted-foreground"><?= lang('deposit'); ?></p>
			<h3 class="text-lg font-semibold text-foreground"><?= html_escape($title); ?><?php if (!empty($expense_info->reference)) { echo ' #' . html_escape($expense_info->reference); } ?></h3>
		</div>
		<div class="flex flex-wrap gap-2">
			<?= $actions; ?>
		</div>
	</div>
	<div class="p-5 space-y-6">
		<?php
		echo $this->load->view('admin/common/details_grid', array(
			'title' => lang('details'),
			'fields' => $detail_fields,
		), true);
		?>

		<?php if (!empty($attachments_html)) { ?>
			<div class="rounded-xl border border-border bg-card p-4 space-y-3">
				<h4 class="text-sm font-semibold text-foreground"><?= lang('attachment'); ?></h4>
				<?= $attachments_html; ?>
			</div>
		<?php } ?>

		<?php if (!empty($expense_info->notes)) { ?>
			<div class="rounded-xl border border-border bg-card p-4 space-y-3">
				<h4 class="text-sm font-semibold text-foreground"><?= lang('notes'); ?></h4>
				<div class="text-sm text-foreground whitespace-pre-line break-words"><?= $expense_info->notes; ?></div>
			</div>
		<?php } ?>
	</div>
	<div class="flex justify-end gap-2 border-t border-border px-5 py-4">
		<button type="button" class="btn btn-default" data-dismiss="modal"><?= lang('close'); ?></button>
	</div>
</div>
