<?php
$status_value = strtolower((string) ($estimates_info->status ?? ''));
$status_label = lang($estimates_info->status ?? 'estimate');
$status_class = 'bg-slate-100 text-slate-700 border-slate-200';
if (!empty($estimates_info->invoiced) && $estimates_info->invoiced === 'Yes') {
    $status_label = strtoupper(lang('invoiced'));
    $status_class = 'bg-emerald-50 text-emerald-700 border-emerald-200';
} elseif (!empty($estimates_info->emailed) && $estimates_info->emailed === 'Yes') {
    $status_label = strtoupper(lang('sent'));
    $status_class = 'bg-blue-50 text-blue-700 border-blue-200';
} elseif ($status_value === 'accepted') {
    $status_class = 'bg-emerald-50 text-emerald-700 border-emerald-200';
} elseif ($status_value === 'declined') {
    $status_class = 'bg-rose-50 text-rose-700 border-rose-200';
} elseif ($status_value === 'pending') {
    $status_class = 'bg-amber-50 text-amber-700 border-amber-200';
}

$client_info = $this->estimates_model->check_by(array('client_id' => $estimates_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->estimates_model->client_currency_symbol($estimates_info->client_id);
if (empty($header_currency)) {
    $header_currency = $this->estimates_model->check_by(array('code' => config_item('default_currency')), 'tbl_currencies');
}

$email_template = email_templates(array('email_group' => 'estimate_email'), $estimates_info->client_id);
$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_estimate') ?></p>
                <div class="flex flex-wrap items-center gap-3">
                    <h1 class="text-2xl font-semibold text-foreground">#<?= html_escape($estimates_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($estimates_info->estimate_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($estimates_info->estimate_date) ?>
                        </span>
                    <?php } ?>
                </div>
            </div>
            <div class="flex flex-wrap items-center gap-2">
                <a href="<?= base_url() ?>admin/estimates/estimates_details/<?= $estimates_info->estimates_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/estimates/create/estimates_history/<?= $estimates_info->estimates_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('estimate_history') ?>
                </a>
                <a href="<?= base_url() ?>admin/estimates/create/edit_estimates/<?= $estimates_info->estimates_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/estimates/create/edit_estimates" data-original-title="<?= lang('new_estimate') ?>" 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('estimate') ?>
                </a>
            </div>
        </div>
    </div>

    <div class="grid gap-6 xl:grid-cols-[320px_minmax(0,1fr)]">
        <aside><?= isset($estimate_sidebar_html) ? $estimate_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->estimates_model->estimate_calculation('total', $estimates_info->estimates_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_estimate') ?></h3>
                </div>
                <div class="p-5">
                    <form method="post" action="<?= base_url() ?>admin/estimates/send_estimates_email/<?= $estimates_info->estimates_id ?>" class="space-y-5">
                        <input type="hidden" name="ref" value="<?= html_escape($estimates_info->reference_no) ?>">
                        <input type="hidden" name="client_name" value="<?= html_escape($client_name_hidden) ?>">
                        <input type="hidden" name="currency" value="<?= html_escape($estimates_info->currency) ?>">
                        <input type="hidden" name="amount" value="<?= ($this->estimates_model->estimate_calculation('total', $estimates_info->estimates_id)) ?>">

                        <div class="space-y-5">
                            <div>
                                <label for="estimate-subject" class="mb-2 block text-sm font-medium text-slate-700 dark:text-slate-300"><?= lang('subject') ?></label>
                                <input
                                    id="estimate-subject"
                                    type="text"
                                    name="subject"
                                    value="<?= html_escape($subject . ' ' . $estimates_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>
