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

<?php
$edited = can_action('39', 'edited');
$deleted = can_action('39', 'deleted');
$currency = $this->items_model->check_by(array('code' => config_item('default_currency')), 'tbl_currencies');
$invoice_view = config_item('invoice_view');

$status_key = !empty($item_info->status) ? ($item_info->status === 'completed' ? 'complete' : $item_info->status) : 'pending';
$status_label_key = ($status_key === 'complete') ? 'completed' : $status_key;
$status_class = 'bg-amber-50 text-amber-700 border-amber-200';
if ($status_key === 'complete') {
    $status_class = 'bg-emerald-50 text-emerald-700 border-emerald-200';
} elseif ($status_key === 'approved') {
    $status_class = 'bg-indigo-50 text-indigo-700 border-indigo-200';
} elseif ($status_key === 'rejected') {
    $status_class = 'bg-rose-50 text-rose-700 border-rose-200';
}

$from_warehouse = get_row('tbl_warehouse', array('warehouse_id' => $item_info->from_warehouse_id));
$to_warehouse = get_row('tbl_warehouse', array('warehouse_id' => $item_info->to_warehouse_id));
$company_name = config_item('company_name') ?: config_item('website_name');
$company_address = config_item('company_address') ?: '';
$adjustment_amount = 0;
if (isset($item_info->adjustment) && $item_info->adjustment !== null) {
    $adjustment_amount = (float) $item_info->adjustment;
} elseif (isset($item_info->shipping_cost) && $item_info->shipping_cost !== null) {
    $adjustment_amount = (float) $item_info->shipping_cost;
}
$invoice_items = $this->items_model->ordered_items_by_id($item_info->transfer_item_id);
$total_item_rows = !empty($invoice_items) ? count($invoice_items) : 0;
$total_moved_qty = 0;
if (!empty($invoice_items)) {
    foreach ($invoice_items as $row_item) {
        $total_moved_qty += (float) $row_item->quantity;
    }
}
?>

<style>
    .transfer-print-header {
        display: none;
    }

    #transfer_details .transfer-items-card {
        overflow: hidden;
        border: 1px solid hsl(var(--border));
        border-radius: 16px;
        background: hsl(var(--card));
        box-shadow: 0 1px 2px rgba(15, 23, 42, .06);
    }

    #transfer_details .transfer-items-title {
        display: flex;
        align-items: center;
        gap: 8px;
        padding: 16px 18px;
        border-bottom: 1px solid hsl(var(--border));
        color: hsl(var(--foreground));
        font-size: 15px;
        font-weight: 700;
    }

    #transfer_details .transfer-items-scroll {
        width: 100%;
        overflow-x: auto;
    }

    #transfer_details .transfer-items-table {
        width: 100%;
        min-width: 720px;
        border-collapse: collapse;
        font-size: 13px;
    }

    #transfer_details .transfer-items-table thead {
        border-bottom: 1px solid hsl(var(--border));
        background: hsl(var(--muted) / .35);
        color: hsl(var(--muted-foreground));
        text-transform: uppercase;
        letter-spacing: .04em;
        font-size: 11px;
    }

    #transfer_details .transfer-items-table th,
    #transfer_details .transfer-items-table td {
        padding: 13px 16px;
        vertical-align: top;
    }

    #transfer_details .transfer-items-table th {
        font-weight: 700;
        text-align: left;
        white-space: nowrap;
    }

    #transfer_details .transfer-items-table tbody tr + tr {
        border-top: 1px solid hsl(var(--border));
    }

    #transfer_details .transfer-items-table tbody tr:hover {
        background: hsl(var(--muted) / .2);
    }

    #transfer_details .transfer-items-table .text-right {
        text-align: right;
    }

    #transfer_details .transfer-item-name {
        display: block;
        color: hsl(var(--foreground));
        font-weight: 700;
    }

    #transfer_details .transfer-item-desc {
        margin-top: 4px;
        color: hsl(var(--muted-foreground));
        font-size: 12px;
        line-height: 1.45;
    }

    #transfer_details .transfer-item-amount {
        color: hsl(var(--foreground));
        font-weight: 700;
        white-space: nowrap;
    }

    @media (max-width: 767px) {
        #transfer_details .transfer-items-title {
            padding: 14px 16px;
        }

        #transfer_details .transfer-items-table {
            min-width: 640px;
        }

        #transfer_details .transfer-items-table th,
        #transfer_details .transfer-items-table td {
            padding: 11px 12px;
        }
    }

    @media print {
        @page {
            size: A4;
            margin: 12mm;
        }

        html,
        body {
            background: #ffffff !important;
            color: #111827 !important;
            font-size: 11px !important;
            line-height: 1.35 !important;
        }

        body * {
            visibility: hidden !important;
        }

        #transfer_details,
        #transfer_details * {
            visibility: visible !important;
        }

        #transfer_details {
            position: absolute !important;
            inset: 0 auto auto 0 !important;
            width: 100% !important;
            max-width: none !important;
            display: block !important;
            background: #ffffff !important;
            padding: 0 !important;
            margin: 0 !important;
        }

        .item-details-actions,
        #app-sidebar-shell,
        #app-header-shell,
        #app-topbar-preload,
        #transfer_details [data-lucide],
        #transfer_details svg {
            display: none !important;
        }

        .transfer-print-header {
            display: table !important;
            width: 100% !important;
            margin: 0 0 14px !important;
            padding: 0 0 12px !important;
            border-bottom: 2px solid #111827 !important;
        }

        .transfer-print-brand,
        .transfer-print-title {
            display: table-cell !important;
            vertical-align: top !important;
        }

        .transfer-print-brand strong {
            display: block !important;
            font-size: 18px !important;
            line-height: 1.2 !important;
            color: #111827 !important;
        }

        .transfer-print-brand span {
            display: block !important;
            margin-top: 3px !important;
            font-size: 10px !important;
            color: #64748b !important;
        }

        .transfer-print-title {
            text-align: right !important;
            font-size: 22px !important;
            font-weight: 800 !important;
            color: #0f766e !important;
        }

        .transfer-print-title span {
            display: block !important;
            margin-top: 5px !important;
            font-size: 10px !important;
            font-weight: 700 !important;
            color: #475569 !important;
        }

        #transfer_details .grid {
            display: grid !important;
            gap: 10px !important;
        }

        #transfer_details > .grid:first-of-type {
            grid-template-columns: 2fr 1fr !important;
            margin-bottom: 10px !important;
        }

        #transfer_details > .grid:nth-of-type(2) {
            grid-template-columns: 1fr 1fr !important;
            margin-bottom: 10px !important;
        }

        #transfer_details .lg\:col-span-2 {
            grid-column: auto !important;
        }

        #transfer_details .rounded-xl,
        #transfer_details .rounded-2xl {
            border: 1px solid #dbe3ea !important;
            border-radius: 9px !important;
            background: #ffffff !important;
            box-shadow: none !important;
            padding: 0 !important;
            break-inside: avoid;
            page-break-inside: avoid;
        }

        #transfer_details .border-b {
            border-bottom: 1px solid #dbe3ea !important;
            background: #f1f5f9 !important;
            padding: 8px 10px !important;
        }

        #transfer_details h2 {
            margin: 0 !important;
            font-size: 10px !important;
            font-weight: 800 !important;
            letter-spacing: .08em !important;
            text-transform: uppercase !important;
            color: #334155 !important;
        }

        #transfer_details .text-sm,
        #transfer_details p,
        #transfer_details span,
        #transfer_details div {
            color: #111827 !important;
        }

        #transfer_details .text-muted-foreground {
            color: #64748b !important;
        }

        #transfer_details .space-y-1,
        #transfer_details .space-y-2,
        #transfer_details .space-y-4 {
            padding: 10px !important;
        }

        #transfer_details .space-y-1 > * + *,
        #transfer_details .space-y-2 > * + *,
        #transfer_details .space-y-4 > * + * {
            margin-top: 4px !important;
        }

        #transfer_details .md\:grid-cols-2 {
            grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
            padding: 10px !important;
        }

        #transfer_details .rounded-lg {
            border-radius: 8px !important;
        }

        #transfer_details .bg-muted\/20 {
            background: #f8fafc !important;
        }

        #transfer_details .border-t {
            border-top: 1px solid #dbe3ea !important;
        }

        .transfer-print-table-wrap,
        #transfer_details .transfer-items-card {
            display: block !important;
            margin: 10px 0 !important;
            border: 1px solid #dbe3ea !important;
            border-radius: 9px !important;
            overflow: hidden !important;
            break-inside: avoid;
            page-break-inside: avoid;
        }

        .transfer-print-table-title,
        #transfer_details .transfer-items-title {
            display: block !important;
            padding: 8px 10px !important;
            background: #f1f5f9 !important;
            border-bottom: 1px solid #dbe3ea !important;
            font-size: 10px !important;
            font-weight: 800 !important;
            letter-spacing: .08em !important;
            text-transform: uppercase !important;
            color: #334155 !important;
        }

        .transfer-print-table,
        #transfer_details .transfer-items-table {
            width: 100% !important;
            min-width: 0 !important;
            border-collapse: collapse !important;
            font-size: 10px !important;
        }

        .transfer-print-table th,
        #transfer_details .transfer-items-table th {
            background: #f8fafc !important;
            color: #475569 !important;
            font-size: 9px !important;
            text-transform: uppercase !important;
            letter-spacing: .06em !important;
            text-align: left !important;
        }

        .transfer-print-table th,
        .transfer-print-table td,
        #transfer_details .transfer-items-table th,
        #transfer_details .transfer-items-table td {
            padding: 7px 8px !important;
            border-bottom: 1px solid #e5e7eb !important;
            vertical-align: top !important;
        }

        .transfer-print-table tr:last-child td,
        #transfer_details .transfer-items-table tr:last-child td {
            border-bottom: 0 !important;
        }

        .transfer-print-table .text-right,
        #transfer_details .transfer-items-table .text-right {
            text-align: right !important;
        }
    }
</style>

<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 md:flex-row md:items-center md:justify-between">
            <div>
                <p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('transferItem') ?></p>
                <h1 class="text-2xl font-semibold text-foreground"><?= html_escape($item_info->reference_no) ?></h1>
                <div class="mt-2 flex flex-wrap items-center gap-2 text-xs">
                    <span class="inline-flex items-center rounded-full border px-2 py-0.5 text-xs font-semibold <?= $status_class ?>">
                        <?= lang($status_label_key) ?>
                    </span>
                </div>
            </div>
            <div class="item-details-actions flex flex-wrap items-center gap-2">
                <?php if (!empty($edited)) { ?>
                    <a href="<?= base_url('admin/items/createTransferItem/' . $item_info->transfer_item_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="pencil" class="h-4 w-4"></i>
                        <?= lang('edit') ?>
                    </a>
                    <?= btn_modal(
                        'admin/items/clone_transfer_item/' . $item_info->transfer_itemList_id,
                        lang('clone'),
                        lang('clone') . ' ' . lang('transferItem'),
                        'md',
                        'copy',
                        array('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')
                    ) ?>
                <?php } ?>
                <a href="<?= base_url('admin/items/pdf_transferitem/' . $item_info->transfer_itemList_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('transfer_details'); return false;" 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>

    <div id="transfer_details" class="space-y-6">
        <div class="transfer-print-header">
            <div class="transfer-print-brand">
                <strong><?= html_escape($company_name ?: '-') ?></strong>
                <?php if (!empty($company_address)) { ?>
                    <span><?= html_escape($company_address) ?></span>
                <?php } ?>
                <span><?= html_escape(display_date(date('Y-m-d'))) ?></span>
            </div>
            <div class="transfer-print-title">
                <?= html_escape(lang('transferItem')) ?>
                <span><?= html_escape($item_info->reference_no) ?> | <?= html_escape(lang($status_label_key)) ?></span>
            </div>
        </div>
        <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
            <div class="lg:col-span-2 rounded-xl border border-border bg-card p-6 space-y-4">
                <div class="flex items-center gap-2 text-sm font-semibold text-foreground border-b border-border pb-3">
                    <i data-lucide="info" class="h-5 w-5"></i>
                    <h2 class="text-base font-semibold text-foreground"><?= lang('transfer') . ' ' . lang('info') ?></h2>
                </div>
                <div class="grid grid-cols-1 md:grid-cols-2 gap-4 text-sm">
                    <div>
                        <p class="text-xs text-muted-foreground"><?= lang('reference_no') ?></p>
                        <p class="font-medium text-foreground"><?= html_escape($item_info->reference_no) ?></p>
                    </div>
                    <div>
                        <p class="text-xs text-muted-foreground"><?= lang('date') ?></p>
                        <p class="font-medium text-foreground"><?= display_date($item_info->date) ?></p>
                    </div>
                    <div>
                        <p class="text-xs text-muted-foreground"><?= lang('warehouse') . ' (' . lang('TO') . ')' ?></p>
                        <p class="font-medium text-foreground"><?= html_escape($item_info->to_warehouse_name) ?></p>
                    </div>
                </div>
            </div>

            <div class="rounded-xl border border-border bg-card p-6 space-y-4">
                <div class="flex items-center gap-2 text-sm font-semibold text-foreground border-b border-border pb-3">
                    <i data-lucide="warehouse" class="h-5 w-5"></i>
                    <h2 class="text-base font-semibold text-foreground"><?= lang('Form Warehouse') ?></h2>
                </div>
                <div class="text-sm text-muted-foreground space-y-1">
                    <p class="font-semibold text-foreground"><?= !empty($from_warehouse->warehouse_code) ? html_escape($from_warehouse->warehouse_code) : '-' ?></p>
                    <p><?= !empty($from_warehouse->warehouse_name) ? html_escape($from_warehouse->warehouse_name) : '-' ?></p>
                    <p><?= !empty($from_warehouse->address) ? html_escape($from_warehouse->address) : '-' ?></p>
                    <p><?= lang('phone') ?>: <?= !empty($from_warehouse->phone) ? html_escape($from_warehouse->phone) : '-' ?></p>
                    <p><?= lang('mobile') ?>: <?= !empty($from_warehouse->mobile) ? html_escape($from_warehouse->mobile) : '-' ?></p>
                    <p><?= lang('email') ?>: <?= !empty($from_warehouse->email) ? html_escape($from_warehouse->email) : '-' ?></p>
                </div>
            </div>
        </div>

        <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
            <div class="rounded-xl border border-border bg-card p-6 space-y-4">
                <div class="flex items-center gap-2 text-sm font-semibold text-foreground border-b border-border pb-3">
                    <i data-lucide="warehouse" class="h-5 w-5"></i>
                    <h2 class="text-base font-semibold text-foreground"><?= lang('To') . ' ' . lang('Warehouse') ?></h2>
                </div>
                <div class="text-sm text-muted-foreground space-y-1">
                    <p class="font-semibold text-foreground"><?= !empty($to_warehouse->warehouse_code) ? html_escape($to_warehouse->warehouse_code) : '-' ?></p>
                    <p><?= !empty($to_warehouse->warehouse_name) ? html_escape($to_warehouse->warehouse_name) : '-' ?></p>
                    <p><?= !empty($to_warehouse->address) ? html_escape($to_warehouse->address) : '-' ?></p>
                    <p><?= lang('phone') ?>: <?= !empty($to_warehouse->phone) ? html_escape($to_warehouse->phone) : '-' ?></p>
                    <p><?= lang('mobile') ?>: <?= !empty($to_warehouse->mobile) ? html_escape($to_warehouse->mobile) : '-' ?></p>
                    <p><?= lang('email') ?>: <?= !empty($to_warehouse->email) ? html_escape($to_warehouse->email) : '-' ?></p>
                </div>
            </div>

            <div class="rounded-xl border border-border bg-card p-6 space-y-4">
                <div class="flex items-center gap-2 text-sm font-semibold text-foreground border-b border-border pb-3">
                    <i data-lucide="clipboard-list" class="h-5 w-5"></i>
                    <h2 class="text-base font-semibold text-foreground"><?= lang('summary') ?></h2>
                </div>
                <div class="space-y-2 text-sm">
                    <div class="grid grid-cols-2 gap-3 rounded-lg border border-border bg-muted/20 p-3">
                        <div>
                            <p class="text-[11px] uppercase tracking-wide text-muted-foreground">Item Rows</p>
                            <p class="text-sm font-semibold text-foreground"><?= (int) $total_item_rows ?></p>
                        </div>
                        <div>
                            <p class="text-[11px] uppercase tracking-wide text-muted-foreground"><?= lang('total_quantity') ?></p>
                            <p class="text-sm font-semibold text-foreground"><?= $total_moved_qty ?></p>
                        </div>
                    </div>
                    <div class="flex justify-between">
                        <span class="text-muted-foreground"><?= lang('sub_total') ?></span>
                        <span class="font-semibold text-foreground"><?= display_money($this->items_model->transfer_calculate_to('transfer_cost', $item_info->transfer_item_id), '', 2); ?></span>
                    </div>
                    <?php
                    $tax_info = !empty($item_info->total_tax) ? json_decode((string) $item_info->total_tax) : null;
                    $tax_total = 0;
                    if (!empty($tax_info)) {
                        $tax_name = $tax_info->tax_name;
                        $total_tax = $tax_info->total_tax;
                        if (!empty($tax_name)) {
                            foreach ($tax_name as $t_key => $v_tax_info) {
                                $tax = explode('|', $v_tax_info);
                                $tax_total += $total_tax[$t_key];
                    ?>
                    <div class="flex justify-between">
                        <span class="text-muted-foreground"><?= $tax[0] . ' (' . $tax[1] . ' %)' ?></span>
                        <span class="font-semibold text-foreground"><?= display_money($total_tax[$t_key], '', 2); ?></span>
                    </div>
                    <?php }
                        }
                    } ?>
                    <?php if ($tax_total > 0) : ?>
                    <div class="flex justify-between">
                        <span class="text-muted-foreground"><?= lang('total') . ' ' . lang('tax') ?></span>
                        <span class="font-semibold text-foreground"><?= display_money($tax_total); ?></span>
                    </div>
                    <?php endif ?>
                    <?php if ($adjustment_amount > 0) : ?>
                    <div class="flex justify-between">
                        <span class="text-muted-foreground"><?= lang('shipping_cost') ?></span>
                        <span class="font-semibold text-foreground"><?= display_money($adjustment_amount); ?></span>
                    </div>
                    <?php endif ?>
                    <div class="flex justify-between border-t border-border pt-2">
                        <span class="text-muted-foreground"><?= lang('total') ?></span>
                        <span class="font-semibold text-foreground"><?= display_money($this->items_model->transfer_calculate_to('total', $item_info->transfer_item_id), $currency->symbol); ?></span>
                    </div>
                    <?php if (config_item('amount_to_words') == 'Yes') { ?>
                    <div class="pt-2 text-xs text-muted-foreground">
                        <strong class="text-foreground"><?= lang('num_word') ?>:</strong>
                        <?= number_to_word('', $this->items_model->transfer_calculate_to('total', $item_info->transfer_item_id)); ?>
                    </div>
                    <?php } ?>
                </div>
            </div>
        </div>

        <div class="transfer-print-table-wrap transfer-items-card">
            <div class="transfer-print-table-title transfer-items-title">
                <i data-lucide="package-check" class="h-5 w-5"></i>
                <span><?= lang('items') ?></span>
            </div>
            <div class="transfer-items-scroll">
                <table class="transfer-print-table transfer-items-table">
                    <thead>
                        <tr>
                            <th><?= lang('items') ?></th>
                            <?php if (!empty($invoice_view) && $invoice_view == '2') { ?>
                                <th><?= lang('hsn_code') ?></th>
                            <?php } ?>
                            <th class="text-right"><?= lang('unit_price') ?></th>
                            <th class="text-right"><?= lang('quantity') ?></th>
                            <th class="text-right"><?= lang('tax') ?></th>
                            <th class="text-right"><?= lang('total') ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (!empty($invoice_items)) { ?>
                            <?php foreach ($invoice_items as $v_item) { ?>
                                <?php
                                $item_name = !empty($v_item->item_name) ? $v_item->item_name : strip_html_tags($v_item->item_desc);
                                $item_tax_name = !empty($v_item->item_tax_name) ? json_decode($v_item->item_tax_name) : null;
                                $tax_text = '-';
                                if (!empty($item_tax_name)) {
                                    $tax_lines = array();
                                    foreach ($item_tax_name as $v_tax_name) {
                                        $i_tax_name = explode('|', $v_tax_name);
                                        $tax_lines[] = $i_tax_name[0] . ' (' . ($i_tax_name[1] ?? 0) . '%)';
                                    }
                                    $tax_text = implode('<br>', array_map('html_escape', $tax_lines));
                                }
                                ?>
                                <tr>
                                    <td>
                                        <span class="transfer-item-name"><?= html_escape($item_name) ?></span>
                                        <?php if (!empty($v_item->item_desc)) { ?>
                                            <div class="transfer-item-desc"><?= strip_html_tags($v_item->item_desc) ?></div>
                                        <?php } ?>
                                    </td>
                                    <?php if (!empty($invoice_view) && $invoice_view == '2') { ?>
                                        <td><?= html_escape($v_item->hsn_code) ?></td>
                                    <?php } ?>
                                    <td class="text-right transfer-item-amount"><?= display_money($v_item->unit_cost) ?></td>
                                    <td class="text-right"><?= html_escape($v_item->quantity . ' ' . $v_item->unit) ?></td>
                                    <td class="text-right"><?= $tax_text ?></td>
                                    <td class="text-right transfer-item-amount"><?= display_money($v_item->total_cost) ?></td>
                                </tr>
                            <?php } ?>
                        <?php } else { ?>
                            <tr>
                                <td colspan="<?= (!empty($invoice_view) && $invoice_view == '2') ? 6 : 5 ?>" class="text-center text-muted-foreground"><?= lang('no_data') ?></td>
                            </tr>
                        <?php } ?>
                    </tbody>
                </table>
            </div>
        </div>

        <div class="rounded-2xl border border-border bg-card p-6 shadow-sm">
            <div class="flex items-center gap-2 text-sm font-semibold text-foreground border-b border-border pb-3">
                <i data-lucide="sticky-note" class="h-5 w-5"></i>
                <h2 class="text-base font-semibold text-foreground"><?= lang('notes') ?></h2>
            </div>
            <div class="mt-4 text-sm text-muted-foreground">
                <?= !empty($item_info->notes) ? $item_info->notes : '-' ?>
            </div>
        </div>
    </div>
</div>

<script>
    window.print_sales_details = function(salesId) {
        if (!document.getElementById(salesId)) return;
        window.print();
    };
</script>
