<?php
$enabled_quotationforms = $this->db->where(array('quotationforms_status' => 'enabled'))->get('tbl_quotationforms')->result();
$submitted_count = 0;
$completed_count = 0;
$pending_count = 0;
$quotation_form_options = array('' => lang('select_quotations_form'));

if (!empty($all_quatations)) {
    foreach ($all_quatations as $quotation_item) {
        $submitted_count++;
        if (!empty($quotation_item->quotations_status) && $quotation_item->quotations_status === 'completed') {
            $completed_count++;
        } else {
            $pending_count++;
        }
    }
}

if (!empty($enabled_quotationforms)) {
    foreach ($enabled_quotationforms as $quotation_form) {
        $quotation_form_options[(int) $quotation_form->quotationforms_id] = $quotation_form->quotationforms_title;
    }
}

?>

<?= message_box('success'); ?>
<?= message_box('error'); ?>

<div id="client-quotation-workspace" class="space-y-6">
    <section class="overflow-hidden rounded-2xl border border-border bg-card p-5 shadow-sm">
        <div class="flex items-start justify-between gap-4 pb-4">
            <div>
                <span class="inline-flex items-center rounded-full border border-primary/20 bg-primary/10 px-3 py-1.5 text-[11px] font-bold uppercase leading-none tracking-wide text-primary"><?= lang('quotations') ?></span>
                <h1 class="mb-1 mt-3 text-2xl font-bold leading-tight text-foreground md:text-3xl"><?= lang('client') . ' ' . lang('quotations') ?></h1>
                <p class="m-0 text-sm leading-6 text-muted-foreground">Track submitted quotation requests and create new quotation requirements from one client workspace.</p>
            </div>
        </div>

        <div class="grid grid-cols-1 gap-3 sm:grid-cols-2 lg:grid-cols-4">
            <article class="min-w-0 rounded-xl border border-border bg-background/85 p-4">
                <span class="block text-[11px] font-bold uppercase leading-tight tracking-wide text-muted-foreground"><?= lang('total') ?></span>
                <strong class="mt-2 block text-3xl font-bold leading-none text-foreground"><?= $submitted_count ?></strong>
                <small class="mt-2 block text-sm leading-5 text-muted-foreground"><?= lang('submitted') . ' ' . lang('quotations') ?></small>
            </article>
            <article class="min-w-0 rounded-xl border border-border bg-background/85 p-4">
                <span class="block text-[11px] font-bold uppercase leading-tight tracking-wide text-muted-foreground"><?= lang('completed') ?></span>
                <strong class="mt-2 block text-3xl font-bold leading-none text-emerald-500"><?= $completed_count ?></strong>
                <small class="mt-2 block text-sm leading-5 text-muted-foreground"><?= lang('reviewed') . ' ' . lang('requests') ?></small>
            </article>
            <article class="min-w-0 rounded-xl border border-border bg-background/85 p-4">
                <span class="block text-[11px] font-bold uppercase leading-tight tracking-wide text-muted-foreground"><?= lang('pending') ?></span>
                <strong class="mt-2 block text-3xl font-bold leading-none text-amber-500"><?= $pending_count ?></strong>
                <small class="mt-2 block text-sm leading-5 text-muted-foreground"><?= lang('awaiting') . ' ' . lang('review') ?></small>
            </article>
            <article class="min-w-0 rounded-xl border border-border bg-background/85 p-4">
                <span class="block text-[11px] font-bold uppercase leading-tight tracking-wide text-muted-foreground"><?= lang('forms') ?></span>
                <strong class="mt-2 block text-3xl font-bold leading-none text-foreground"><?= count($enabled_quotationforms) ?></strong>
                <small class="mt-2 block text-sm leading-5 text-muted-foreground"><?= lang('available') . ' ' . lang('templates') ?></small>
            </article>
        </div>
    </section>

    <div class="grid gap-6 xl:grid-cols-12">
        <section class="overflow-hidden rounded-2xl border border-border bg-card shadow-sm xl:col-span-8">
            <div class="flex flex-col items-start justify-between gap-4 border-b border-border px-5 py-4 sm:flex-row">
                <div>
                    <h2 class="m-0 text-lg font-bold leading-tight text-foreground"><?= lang('quotation_history') ?></h2>
                    <p class="m-0 mt-1 text-sm leading-6 text-muted-foreground">Review submitted quotation requests, current decision status, and approved totals.</p>
                </div>
                <span class="inline-flex shrink-0 items-center gap-2 whitespace-nowrap rounded-full border border-border bg-background px-3 py-2 text-sm font-bold text-foreground">
                    <i data-lucide="clipboard-list" class="h-4 w-4"></i>
                    <?= $submitted_count ?> <?= lang('records') ?>
                </span>
            </div>

            <div class="overflow-x-auto p-5">
                <table class="table table-striped DataTables" id="DataTables" cellspacing="0" width="100%">
                    <thead>
                    <tr>
                        <th><?= lang('title') ?></th>
                        <th><?= lang('date') ?></th>
                        <th><?= lang('amount') ?></th>
                        <th><?= lang('status') ?></th>
                    </tr>
                    </thead>
                    <tbody>
                    <script type="text/javascript">
                        $(document).ready(function () {
                            list = base_url + "client/quotations/quotationsList";
                        });
                    </script>
                    </tbody>
                </table>
            </div>
        </section>

        <aside class="overflow-hidden rounded-2xl border border-border bg-card shadow-sm xl:col-span-4">
            <div class="border-b border-border px-5 py-4">
                <div>
                    <h2 class="m-0 text-lg font-bold leading-tight text-foreground"><?= lang('request_quotation') ?></h2>
                    <p class="m-0 mt-1 text-sm leading-6 text-muted-foreground">Choose a quotation template and generate the request form instantly.</p>
                </div>
            </div>

            <div class="p-5">
                <form action="<?= base_url() ?>client/quotations/index/get_form" method="post" id="addQuotationForm" class="space-y-4" data-validate>
                    <div class="grid grid-cols-1 items-end gap-3 sm:grid-cols-[minmax(0,1fr)_auto]">
                        <div class="min-w-0">
                            <?= form_select_group(
                                'quotationforms_id',
                                lang('select_quotations_form') . ' <span class="text-red-500">*</span>',
                                $quotation_form_options,
                                '',
                                false,
                                array(
                                    'id' => 'quotationforms_id',
                                    'class' => 'min-h-[2.9rem]',
                                    'data-search' => 'true',
                                    'data-required' => 'true',
                                    'data-msg-required' => lang('select_quotations_form'),
                                )
                            ); ?>
                        </div>
                        <button type="submit" class="inline-flex min-h-[2.9rem] items-center justify-center gap-2 whitespace-nowrap rounded-xl border-0 bg-primary px-4 py-3 text-sm font-bold leading-none text-primary-foreground transition hover:-translate-y-px hover:opacity-95 sm:w-auto">
                            <i data-lucide="arrow-right" class="h-4 w-4"></i>
                            <?= lang('go') ?>
                        </button>
                    </div>
                </form>

                <div class="mt-4 rounded-xl border border-border bg-background p-4">
                    <div class="inline-flex h-10 w-10 items-center justify-center rounded-full bg-primary/10 text-primary">
                        <i data-lucide="file-pen-line" class="h-5 w-5"></i>
                    </div>
                    <h3 class="m-0 mt-3 text-base font-bold leading-tight text-foreground"><?= lang('request_quotation') ?></h3>
                    <p class="m-0 mt-1 text-sm leading-6 text-muted-foreground">Select an available quotation form to open a dedicated request page and send your requirements.</p>
                </div>
            </div>
        </aside>
    </div>
</div>
