<?php
$status_label = !empty($credit_note_info->status) ? lang($credit_note_info->status) : lang('credit_note');
$status_value = strtolower((string) ($credit_note_info->status ?? ''));
$status_class = 'bg-slate-100 text-slate-700 border-slate-200';
if ($status_value === 'open') {
    $status_class = 'bg-emerald-50 text-emerald-700 border-emerald-200';
} elseif ($status_value === 'refund') {
    $status_class = 'bg-blue-50 text-blue-700 border-blue-200';
} elseif ($status_value === 'void') {
    $status_class = 'bg-rose-50 text-rose-700 border-rose-200';
}

$client_info = $this->credit_note_model->check_by(array('client_id' => $credit_note_info->client_id), 'tbl_client');
$client_name_hidden = !empty($client_info->name) ? ucfirst((string) $client_info->name) : '-';
$recipient_email = !empty($client_info->email) ? $client_info->email : '-';
$header_currency = $this->credit_note_model->client_currency_symbol($credit_note_info->client_id);
if (empty($header_currency)) {
    $header_currency = $this->credit_note_model->check_by(array('code' => config_item('default_currency')), 'tbl_currencies');
}

$email_template = email_templates(array('email_group' => 'credit_note_email'));
$message = $email_template->template_body;
$subject = $email_template->subject;
?>

<div class="invoice-modern space-y-6">
    <?= message_box('success') ?>
    <?= message_box('error') ?>

    <div class="rounded-2xl border border-border bg-card p-6 shadow-sm">
        <div class="flex flex-col gap-4 lg:flex-row lg:items-center lg:justify-between">
            <div class="space-y-2">
                <p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('email_credit_note') ?></p>
                <div class="flex flex-wrap items-center gap-3">
                    <h1 class="text-2xl font-semibold text-foreground">#<?= html_escape($credit_note_info->reference_no) ?></h1>
                    <span class="inline-flex items-center rounded-full border px-3 py-1 text-xs font-semibold <?= $status_class ?>">
                        <?= html_escape($status_label) ?>
                    </span>
                    <?php if (!empty($credit_note_info->credit_note_date)) { ?>
                        <span class="inline-flex items-center rounded-full border border-border px-2.5 py-1 text-xs font-semibold text-muted-foreground">
                            <?= display_date($credit_note_info->credit_note_date) ?>
                        </span>
                    <?php } ?>
                </div>
            </div>
            <div class="flex flex-wrap items-center gap-2">
                <a href="<?= base_url() ?>admin/credit_note/credit_note_details/<?= $credit_note_info->credit_note_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">
                    <i data-lucide="file-text" class="h-4 w-4"></i>
                    <?= lang('details') ?>
                </a>
                <a href="<?= base_url() ?>admin/credit_note/index/credit_note_history/<?= $credit_note_info->credit_note_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">
                    <i data-lucide="history" class="h-4 w-4"></i>
                    <?= lang('credit_note_history') ?>
                </a>
                <a href="<?= base_url() ?>admin/credit_note/createcreditnote/edit_credit_note/<?= $credit_note_info->credit_note_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">
                    <i data-lucide="square-pen" class="h-4 w-4"></i>
                    <?= lang('edit') ?>
                </a>
                <a href="<?= base_url() ?>admin/credit_note/createcreditnote" data-original-title="<?= lang('new') . ' ' . lang('credit_note') ?>" data-toggle="tooltip" data-placement="top" class="inline-flex items-center gap-2 rounded-lg border border-primary/20 bg-primary/5 px-3 py-2 text-xs font-semibold text-primary hover:bg-primary/10">
                    <i data-lucide="plus" class="h-4 w-4"></i>
                    <?= lang('new') . ' ' . lang('credit_note') ?>
                </a>
            </div>
        </div>
    </div>

    <div class="grid gap-6 xl:grid-cols-[320px_minmax(0,1fr)]">
        <aside><?= isset($credit_note_sidebar_html) ? $credit_note_sidebar_html : '' ?></aside>

        <section class="space-y-4">
            <div class="grid gap-3 md:grid-cols-3">
                <div class="rounded-xl border border-border bg-card p-4 shadow-sm">
                    <div class="flex items-center gap-3">
                        <div class="rounded-xl bg-blue-50 p-2.5 text-blue-600 dark:bg-blue-900/30 dark:text-blue-400">
                            <i data-lucide="mail" class="h-5 w-5"></i>
                        </div>
                        <div>
                            <div class="text-sm font-semibold text-foreground"><?= html_escape($recipient_email) ?></div>
                            <div class="text-[11px] font-medium uppercase tracking-[0.16em] text-muted-foreground"><?= lang('email') ?></div>
                        </div>
                    </div>
                </div>
                <div class="rounded-xl border border-border bg-card p-4 shadow-sm">
                    <div class="flex items-center gap-3">
                        <div class="rounded-xl bg-emerald-50 p-2.5 text-emerald-600 dark:bg-emerald-900/30 dark:text-emerald-400">
                            <i data-lucide="wallet" class="h-5 w-5"></i>
                        </div>
                        <div>
                            <div class="text-2xl font-bold text-foreground">
                                <?= display_money($this->credit_note_model->credit_note_calculation('total', $credit_note_info->credit_note_id), !empty($header_currency->symbol) ? $header_currency->symbol : '$'); ?>
                            </div>
                            <div class="text-[11px] font-medium uppercase tracking-[0.16em] text-muted-foreground"><?= lang('amount') ?></div>
                        </div>
                    </div>
                </div>
                <div class="rounded-xl border border-border bg-card p-4 shadow-sm">
                    <div class="flex items-center gap-3">
                        <div class="rounded-xl bg-amber-50 p-2.5 text-amber-600 dark:bg-amber-900/30 dark:text-amber-400">
                            <i data-lucide="user" class="h-5 w-5"></i>
                        </div>
                        <div>
                            <div class="text-sm font-semibold text-foreground"><?= html_escape($client_name_hidden) ?></div>
                            <div class="text-[11px] font-medium uppercase tracking-[0.16em] text-muted-foreground"><?= lang('client') ?></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="rounded-2xl border border-slate-200 bg-white shadow-sm dark:border-slate-700 dark:bg-slate-900">
                <div class="border-b border-slate-200 px-5 py-4 dark:border-slate-700">
                    <h3 class="text-sm font-semibold text-slate-900 dark:text-slate-100"><?= lang('email_credit_note') ?></h3>
                </div>
                <div class="p-5">
                    <form method="post" action="<?= base_url() ?>admin/credit_note/send_credit_note_email/<?= $credit_note_info->credit_note_id ?>" class="space-y-5">
                        <input type="hidden" name="ref" value="<?= html_escape($credit_note_info->reference_no) ?>">
                        <input type="hidden" name="client_name" value="<?= html_escape($client_name_hidden) ?>">
                        <input type="hidden" name="currency" value="<?= html_escape($credit_note_info->currency) ?>">
                        <input type="hidden" name="amount" value="<?= ($this->credit_note_model->credit_note_calculation('total', $credit_note_info->credit_note_id)) ?>">

                        <div class="space-y-5">
                            <div>
                                <label for="credit-note-subject" class="mb-2 block text-sm font-medium text-slate-700 dark:text-slate-300"><?= lang('subject') ?></label>
                                <input
                                    id="credit-note-subject"
                                    type="text"
                                    name="subject"
                                    value="<?= html_escape($subject . ' ' . $credit_note_info->reference_no) ?>"
                                    class="w-full rounded-lg border border-slate-200 bg-white px-3 py-2 text-sm text-slate-700 shadow-sm focus:border-slate-400 focus:outline-none focus:ring-2 focus:ring-slate-200 dark:border-slate-700 dark:bg-slate-800 dark:text-slate-100 dark:focus:border-slate-500 dark:focus:ring-slate-700"
                                >
                            </div>

                            <div class="rounded-xl border border-border bg-card overflow-hidden">
                                <?= form_editor('message', lang('message'), $message, false, array('rows' => 8)); ?>
                            </div>

                            <div>
                                <button type="submit" class="submit inline-flex items-center rounded-lg bg-slate-900 px-4 py-2 text-sm font-semibold text-white transition hover:bg-slate-800 dark:bg-slate-100 dark:text-slate-900 dark:hover:bg-white">
                                    <?= lang('send') ?>
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </section>
    </div>
</div>
