<div class="invoice-modern">
<?= message_box('success') ?>
<?= message_box('error') ?>
<?php
$client_info = $this->estimates_model->check_by(array('client_id' => $estimates_info->client_id), 'tbl_client');
$client_lang = !empty($client_info->language) ? $client_info->language : config_item('language');
unset($this->lang->is_loaded[5]);
$language_info = $this->lang->load('sales_lang', $client_lang, true, false, '', true);

$status_label = lang($estimates_info->status);
$status_value = strtolower((string) ($estimates_info->status ?? ''));
$status_class = 'bg-slate-100 text-slate-700 border-slate-200';
if ($status_value === 'accepted') {
    $status_class = 'bg-emerald-50 text-emerald-700 border-emerald-200';
} elseif (in_array($status_value, array('declined', 'cancelled'), true)) {
    $status_class = 'bg-rose-50 text-rose-700 border-rose-200';
} elseif ($status_value === 'expired') {
    $status_class = 'bg-amber-50 text-amber-700 border-amber-200';
} elseif ($status_value === 'sent') {
    $status_class = 'bg-blue-50 text-blue-700 border-blue-200';
} elseif ($status_value === 'pending') {
    $status_class = 'bg-amber-50 text-amber-700 border-amber-200';
}

$overdue_message = '';
if (safe_strtotime($estimates_info->due_date) < safe_strtotime(date('Y-m-d')) && $estimates_info->status == 'accepted') {
    $start = safe_strtotime(date('Y-m-d'));
    $end = safe_strtotime($estimates_info->due_date);
    $days_between = ceil(abs($end - $start) / 86400);
    $overdue_message = lang('estimate_overdue') . ' ' . lang('by') . ' ' . $days_between . ' ' . lang('days');
}

$all_items = $this->estimates_model->ordered_items_by_id($estimates_info->estimates_id);

if (!empty($client_info)) {
    $client_name = $client_info->name;
    $address = $client_info->address;
    $city = $client_info->city;
    $zipcode = $client_info->zipcode;
    $country = $client_info->country;
    $phone = $client_info->phone;
    $vat = $client_info->vat;
} else {
    $client_name = '-';
    $address = '-';
    $city = '-';
    $zipcode = '-';
    $country = '-';
    $phone = '-';
    $vat = '';
}

$custom_fields = custom_form_label(10, $estimates_info->estimates_id);
$custom_field_markup = '';
if (!empty($custom_fields)) {
    foreach ($custom_fields as $c_label => $v_fields) {
        if (!empty($v_fields)) {
            $custom_field_markup .= '<div>' . html_escape($c_label) . ': ' . html_escape($v_fields) . '</div>';
        }
    }
}

$sales_info = (object) array(
    'reference_no' => $estimates_info->reference_no,
    'status' => $estimates_info->status,
    'start_date' => $language_info['estimate_date'] . ': ' . display_date($estimates_info->estimate_date),
    'end_date' => $language_info['due_date'] . ': ' . display_date($estimates_info->due_date),
    'sales_agent' => !empty($estimates_info->user_id) ? lang('sales') . ' ' . lang('agent') . ': ' . fullname($estimates_info->user_id) : null,
    'custom_field' => $custom_field_markup,
    'client_id' => $estimates_info->client_id,
    'name' => $client_name,
    'address' => $address,
    'city' => $city,
    'zipcode' => $zipcode,
    'country' => $country,
    'phone' => $phone,
    'vat' => $vat,
    'notes' => $estimates_info->notes,
    'sub_total' => $this->estimates_model->estimate_calculation('estimate_cost', $estimates_info->estimates_id),
    'discount' => $this->estimates_model->estimate_calculation('discount', $estimates_info->estimates_id),
    'discount_percent' => $estimates_info->discount_percent,
    'total_tax' => $estimates_info->total_tax,
    'adjustment' => $estimates_info->adjustment,
    'total' => $this->estimates_model->estimate_calculation('total', $estimates_info->estimates_id),
    'overdue_days' => $overdue_message,
);
?>

<div class="space-y-6">
    <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('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>
                    <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>
                    <span class="inline-flex items-center rounded-full border border-border px-2.5 py-1 text-xs font-semibold text-muted-foreground">
                        <?= lang('due_date') ?>: <?= display_date($estimates_info->due_date) ?>
                    </span>
                </div>
                <?php if (!empty($estimates_info->project_id)) {
                    $project_info = $this->db->where('project_id', $estimates_info->project_id)->get('tbl_project')->row(); ?>
                    <div class="text-sm text-muted-foreground">
                        <?= lang('project') ?>:
                        <a href="<?= base_url() ?>client/projects/project_details/<?= $estimates_info->project_id ?>" class="text-primary hover:underline">
                            <?= html_escape($project_info->project_name) ?>
                        </a>
                    </div>
                <?php } ?>
            </div>
            <div class="flex flex-wrap items-center gap-2">
                <a href="<?= base_url() ?>client/estimates/change_status/accepted/<?= $estimates_info->estimates_id ?>" class="inline-flex items-center gap-2 rounded-lg border border-emerald-200 bg-emerald-50 px-3 py-2 text-xs font-semibold text-emerald-700 hover:bg-emerald-100">
                    <i data-lucide="check" class="h-4 w-4"></i>
                    <?= lang('accepted') ?>
                </a>
                <a href="<?= base_url() ?>client/estimates/change_status/declined/<?= $estimates_info->estimates_id ?>" class="inline-flex items-center gap-2 rounded-lg border border-rose-200 bg-rose-50 px-3 py-2 text-xs font-semibold text-rose-600 hover:bg-rose-100">
                    <i data-lucide="x" class="h-4 w-4"></i>
                    <?= lang('declined') ?>
                </a>
            </div>
        </div>
    </div>

    <div class="rounded-2xl border border-border bg-card p-6 shadow-sm">
        <div class="flex flex-col gap-3 md:flex-row md:items-center md:justify-between">
            <div class="text-sm text-muted-foreground">
                <?= lang('estimate') ?>
            </div>
            <div class="flex flex-wrap items-center gap-2">
                <a href="<?= base_url() ?>client/estimates/pdf_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="file-text" class="h-4 w-4"></i>
                    PDF
                </a>
                <a href="#" onclick="print_sales_details('sales_details')" 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="printer" class="h-4 w-4"></i>
                    <?= lang('print') ?>
                </a>
            </div>
        </div>
    </div>

    <?php
    $this->load->view('admin/common/sales_details_modern', array(
        'sales_info' => $sales_info,
        'all_items' => $all_items,
        'document_label' => lang('estimate'),
        'payment_status' => $status_label,
    ));
    ?>
</div>

<script type="text/javascript">
    function print_sales_details(print_section_id) {
        var printContents = document.getElementById(print_section_id).innerHTML;
        var originalContents = document.body.innerHTML;
        document.body.innerHTML = printContents;
        window.print();
        document.body.innerHTML = originalContents;
    }
</script>
</div>
