<!DOCTYPE html>
<html lang="en">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title><?= lang('transfer') ?></title>
    <?php
    $direction = $this->session->userdata('direction');
    if (!empty($direction) && $direction == 'rtl') {
        $RTL = 'on';
    } else {
        $RTL = config_item('RTL');
    }

    ?>
    <style type="text/css">
        @font-face {
            font-family: latha;
            font-style: normal;
            font-weight: 400;
            src: url('<?= ROOTPATH ?>/assets/latha.ttf') format('truetype');
        }

        * {
            font-family: 'tamil-latha', DejaVu Sans, sans-serif;
            box-sizing: border-box;
        }

        body {
            margin: 0;
            color: #0f172a;
            background: #ffffff;
            font-size: 12px;
            width: 100%;
            line-height: 1.45;
            <?php if (!empty($RTL)) { ?>text-align: right;
            <?php } ?>
        }

        .page-shell {
            padding: 18px 22px 24px;
        }

        .doc-table {
            width: 100%;
            border-collapse: collapse;
            table-layout: fixed;
        }

        .header-card {
            border: 1px solid #e2e8f0;
            border-radius: 16px;
            padding: 18px 20px;
            margin-bottom: 16px;
        }

        .logo-img {
            width: 60px;
            height: 60px;
            object-fit: contain;
            border: 1px solid #d7deea;
            padding: 6px;
            background: #ffffff;
        }

        .company-legal {
            margin: 0 0 4px 0;
            font-size: 24px;
            font-weight: 700;
            color: #0f172a;
            line-height: 1.25;
        }

        .meta-line {
            margin: 2px 0;
            color: #475569;
            font-size: 11px;
        }

        .doc-title {
            margin: 0 0 10px 0;
            font-size: 26px;
            font-weight: 700;
            letter-spacing: 0.14em;
            text-transform: uppercase;
            color: #64748b;
            <?php if (!empty($RTL)) { ?>text-align: left;
            <?php } else { ?>text-align: right;
            <?php } ?>
        }

        .doc-ref {
            padding: 4px 0;
            font-size: 12px;
            <?php if (!empty($RTL)) { ?>text-align: left;
            <?php } else { ?>text-align: right;
            <?php } ?>
        }

        .doc-ref strong {
            color: #0f172a;
        }

        .section-grid {
            width: 100%;
            border-collapse: collapse;
            border: 1px solid #d7deea;
            border-radius: 12px;
            overflow: hidden;
            margin-bottom: 14px;
        }

        .section-grid th {
            padding: 10px 12px;
            background: #f8fafc;
            color: #0f172a;
            font-size: 11px;
            font-weight: 700;
            letter-spacing: 0.08em;
            text-transform: uppercase;
            border-bottom: 1px solid #dbe2ea;
            <?php if (!empty($RTL)) { ?>text-align: right;
            <?php } else { ?>text-align: left;
            <?php } ?>
        }

        .section-grid td {
            width: 50%;
            padding: 12px;
            vertical-align: top;
            border-top: 1px solid #e2e8f0;
            <?php if (!empty($RTL)) { ?>text-align: right;
            <?php } else { ?>text-align: left;
            <?php } ?>
        }

        .section-grid td + td {
            border-left: 1px solid #e2e8f0;
        }

        .warehouse-code {
            margin: 0 0 4px 0;
            font-size: 14px;
            font-weight: 700;
            color: #0f172a;
        }

        .warehouse-name {
            margin: 0 0 6px 0;
            font-size: 13px;
            color: #1e293b;
            font-weight: 600;
        }

        .warehouse-line {
            margin: 1px 0;
            font-size: 11px;
            color: #475569;
        }

        .warehouse-email a {
            color: #0f172a;
            text-decoration: none;
        }

        table.items {
            width: 100%;
            border-collapse: collapse;
            table-layout: auto;
            border: 1px solid #d7deea;
            border-radius: 12px;
            overflow: hidden;
            margin-bottom: 10px;
        }

        table.items thead {
            display: table-header-group;
        }

        table.items thead tr,
        table.items tbody tr {
            height: auto;
        }

        table.items thead th {
            padding: 6px 8px;
            background: #f8fafc;
            color: #0f172a;
            text-transform: uppercase;
            letter-spacing: 0.08em;
            font-size: 10px;
            font-weight: 700;
            border-bottom: 1px solid #dbe2ea;
            vertical-align: middle;
            line-height: 1.2;
            <?php if (!empty($RTL)) { ?>text-align: right;
            <?php } else { ?>text-align: left;
            <?php } ?>
        }

        table.items tbody td {
            padding: 6px 8px;
            border-bottom: 1px solid #e2e8f0;
            color: #334155;
            font-size: 11px;
            vertical-align: middle;
            line-height: 1.2;
            <?php if (!empty($RTL)) { ?>text-align: right;
            <?php } else { ?>text-align: left;
            <?php } ?>
        }

        table.items tbody tr:last-child td {
            border-bottom: none;
        }

        .item-title {
            margin: 0 0 2px 0;
            font-size: 12px;
            font-weight: 700;
            color: #0f172a;
            line-height: 1.2;
        }

        .item-desc {
            margin: 0;
            color: #64748b;
            font-size: 10px;
            line-height: 1.2;
        }

        .totals-table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 10px;
            border: 1px solid #d7deea;
            border-radius: 12px;
            overflow: hidden;
            page-break-inside: avoid;
        }

        .totals-table td {
            padding: 9px 8px;
            border-top: 1px solid #d7deea;
            background: #f8fafc;
            font-size: 11px;
            <?php if (!empty($RTL)) { ?>text-align: right;
            <?php } else { ?>text-align: left;
            <?php } ?>
        }

        .totals-table tr:first-child td {
            border-top: none;
        }

        .totals-table tr:last-child td {
            border-top: 1px solid #0f172a;
            font-weight: 800;
            font-size: 13px;
            color: #0f172a;
            background: #ffffff;
        }

        .totals-table td:first-child {
            width: 78%;
            <?php if (!empty($RTL)) { ?>text-align: left;
            <?php } else { ?>text-align: right;
            <?php } ?>
        }

        .totals-table td:last-child {
            width: 22%;
        }

        .summary-table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 18px;
        }

        .summary-table td {
            vertical-align: top;
        }

        .notes-cell {
            width: 56%;
            padding-right: 14px;
        }

        .totals-cell {
            width: 44%;
        }

        .section-head {
            margin: 0 0 8px 0;
            font-size: 11px;
            font-weight: 700;
            letter-spacing: 0.08em;
            text-transform: uppercase;
            color: #64748b;
        }

        .notes-text {
            color: #64748b;
            font-size: 11px;
        }

        .totals-panel {
            border: 1px solid #d7deea;
            border-radius: 16px;
            overflow: hidden;
        }

        .num_word {
            margin: 8px 0 10px;
            font-size: 12px;
            color: #334155;
            <?php if (!empty($RTL)) { ?>text-align: left;
            <?php } else { ?>text-align: right;
            <?php } ?>
        }

        footer {
            color: #64748b;
            width: 100%;
            border-top: 1px solid #dbe2ea;
            padding-top: 8px;
            font-size: 10px;
            text-align: center;
            margin-top: 12px;
        }

        tr.total td,
        tr th.total,
        tr td.total {
            <?php if (!empty($RTL)) { ?>text-align: left;
            <?php } else { ?>text-align: right;
            <?php } ?>
        }

        @media (prefers-color-scheme: dark) {
            body {
                background: #0b1220;
                color: #e2e8f0;
            }

            .header-card,
            .section-grid,
            table.items {
                border-color: #1e293b;
                background: #0f172a;
            }

            .meta-line,
            .warehouse-line,
            .item-desc,
            footer {
                color: #94a3b8;
            }

            .company-legal,
            .doc-title,
            .warehouse-code,
            .warehouse-name,
            .item-title {
                color: #f8fafc;
            }

            table.items tbody td,
            .section-grid td {
                border-color: #1e293b;
                color: #cbd5e1;
            }

            .section-grid th,
            table.items thead th {
                background: #1e293b;
                color: #f8fafc;
                border-bottom-color: #1e293b;
            }

            .totals-table td {
                background: #111827;
                border-color: #1e293b;
                color: #e2e8f0;
            }

            .totals-table tr:last-child td {
                background: #0f172a;
                border-top-color: #f8fafc;
                color: #f8fafc;
            }
        }
    </style>
</head>

<body>

    <?php

    $currency = $this->items_model->check_by(array('code' => config_item('default_currency')), 'tbl_currencies');

    $uri = $this->uri->segment(3);
    if ($uri == 'invoice_email') {
        $img = base_url() . config_item('invoice_logo');
    } else {
        $img = ROOTPATH . '/' . config_item('invoice_logo');
        $a = file_exists($img);
        if (empty($a)) {
            $img = base_url() . config_item('invoice_logo');
        }
    }
    ?>

    <div class="page-shell">
        <div class="header-card">
            <table class="doc-table">
                <tr>
                    <td style="width:12%; vertical-align:top;">
                        <img class="logo-img" src="<?= $img ?>" alt="<?= config_item('company_name') ?>">
                    </td>
                    <td style="width:58%; vertical-align:top; padding-left:12px;">
                        <h2 class="company-legal"><?= config_item('company_name') ?></h2>
                        <p class="meta-line" style="margin-top:8px;"><?= config_item('company_legal_name') ?></p>
                        <p class="meta-line"><?= config_item('company_address') ?></p>
                        <p class="meta-line"><?= config_item('company_city') ?> <?= config_item('company_zip_code') ?></p>
                        <p class="meta-line"><?= config_item('company_country') ?></p>
                        <p class="meta-line">P: <?= config_item('company_phone') ?></p>
                        <p class="meta-line">E: <?= config_item('company_email') ?></p>
                    </td>
                    <td style="width:30%; vertical-align:top;">
                        <h3 class="doc-title"><?= lang('transfer') ?></h3>
                        <p class="doc-ref"><?= lang('reference_no') ?> : <strong><?= !empty($item_info->reference_no) ? $item_info->reference_no : '-' ?></strong></p>
                        <p class="doc-ref"><?= lang('date') ?> : <strong><?= !empty($item_info->date) ? display_date($item_info->date) : '-' ?></strong></p>
                        <p class="doc-ref"><?= lang('total') ?> : <strong><?= display_money($this->items_model->transfer_calculate_to('total', $item_info->transfer_item_id), $currency->symbol); ?></strong></p>
                    </td>
                </tr>
            </table>
        </div>

    <table class="section-grid">
        <tr>
            <th style="width: 50%;overflow: hidden">
                <h4 style="margin:0; font-size:11px;">
                    <?= lang('from') . ' ' . lang('warehouse') ?>
                </h4>
            </th>
            <th style="width: 50%">
                <h4 style="margin:0; font-size:11px;">
                    <?= lang('to') . ' ' . lang('warehouse') ?>
                </h4>
            </th>
        </tr>
        <tr style="margin-top: 0px">
            <?php
            $get_warehouse = get_row('tbl_warehouse', array('warehouse_id' => $item_info->from_warehouse_id));
            if (!empty($get_warehouse)) {
                $warehouse_code = $get_warehouse->warehouse_code;
                $client_name = $get_warehouse->warehouse_name;
                $address = $get_warehouse->address;
                $mobile = $get_warehouse->mobile;
                $phone = $get_warehouse->phone;
                $email = $get_warehouse->email;
            } else {
                $warehouse_code = '-';
                $client_name = '-';
                $address = '-';
                $mobile = '-';
                $zipcode = '-';
                $country = '-';
                $phone = '-';
                $email = '-';
            }
            ?>
            <td style="width: 50%;overflow: hidden">
                <div>
                    <h3 class="warehouse-code"><?= $warehouse_code ?></h3>
                    <p class="warehouse-name"><?= $client_name ?></p>
                    <p class="warehouse-line"><?= $address ?></p>
                    <p class="warehouse-line"><?= lang('phone') ?>: <?= $phone ?></p>
                    <p class="warehouse-line"><?= lang('mobile') ?>: <?= $mobile ?></p>
                    <p class="warehouse-line"><?= lang('email') ?>: <?= $email ?></p>

                </div>
            </td>
            <td style="width: 50%;overflow: hidden">
                <?php

                $get_warehouse = get_row('tbl_warehouse', array('warehouse_id' => $item_info->to_warehouse_id));


                if (!empty($get_warehouse)) {
                    $warehouse_code = $get_warehouse->warehouse_code;
                    $client_name = $get_warehouse->warehouse_name;
                    $address = $get_warehouse->address;
                    $mobile = $get_warehouse->mobile;
                    $phone = $get_warehouse->phone;
                    $email = $get_warehouse->email;
                } else {
                    $warehouse_code = '-';
                    $client_name = '-';
                    $address = '-';
                    $mobile = '-';
                    $zipcode = '-';
                    $country = '-';
                    $phone = '-';
                    $email = '-';
                }
                ?>

                <div>
                    <h3 class="warehouse-code"><?= $warehouse_code ?></h3>
                    <p class="warehouse-name"><?= $client_name ?></p>
                    <p class="warehouse-line"><?= $address ?></p>
                    <p class="warehouse-line"><?= $phone ?></p>
                    <p class="warehouse-line"><?= $mobile ?></p>
                    <p class="warehouse-line warehouse-email"><a href="mailto:<?= $email ?>"><?= $email ?></a></p>

                </div>
            </td>
        </tr>
    </table>

    <table class="items">
        <thead>
            <tr>
                <th><?= lang('description') ?></th>
                <?php
                $colspan = 3;
                $invoice_view = config_item('invoice_view');
                if (!empty($invoice_view) && $invoice_view == '2') {
                    $colspan = 4;
                ?>
                    <th><?= lang('hsn_code') ?></th>
                <?php } ?>
                <th><?= lang('price') ?></th>
                <th><?= lang('qty') ?></th>
                <th><?= lang('tax') ?></th>
                <th><?= lang('total') ?></th>
            </tr>
        </thead>
        <tbody>
            <?php
            $invoice_items = $this->items_model->ordered_items_by_id($item_info->transfer_item_id);

            if (!empty($invoice_items)) :
                foreach ($invoice_items as $key => $v_item) :


                    $item_name = $v_item->item_name ? $v_item->item_name : strip_html_tags($v_item->item_desc);
                    $item_tax_name = json_decode($v_item->item_tax_name);
            ?>

                    <tr>
                        <td class="unit">
                            <p class="item-title"><?= $item_name ?></p>
                            <p class="item-desc"><?= strip_html_tags($v_item->item_desc) ?></p>
                        </td>
                        <?php
                        $invoice_view = config_item('invoice_view');
                        if (!empty($invoice_view) && $invoice_view == '2') {
                        ?>
                            <td class="unit"><?= $v_item->hsn_code ?></td>
                        <?php } ?>
                        <td class="unit"><?= display_money($v_item->unit_cost) ?></td>
                        <td class="unit"><?= $v_item->quantity . '   ' . $v_item->unit ?></td>
                        <td class="unit"><?php
                                            if (!empty($item_tax_name)) {
                                                foreach ($item_tax_name as $v_tax_name) {
                                                    $i_tax_name = explode('|', $v_tax_name);
                                                    echo '<small class="pr-sm">' . $i_tax_name[0] . ' (' . $i_tax_name[1] . ' %)' . '</small>' . display_money($v_item->total_cost / 100 * $i_tax_name[1]) . ' <br>';
                                                }
                                            }
                                            ?></td>
                        <td class="unit"><?= display_money($v_item->total_cost) ?></td>
                    </tr>
                <?php endforeach; ?>
            <?php endif ?>


        </tbody>
    </table>
    <?php
    $tax_info = json_decode($item_info->total_tax);
    $tax_total = 0;
    ?>
    <table class="summary-table">
        <tr>
            <td class="notes-cell">
                <div class="section-head"><?= lang('notes') ?></div>
                <div class="notes-text"><?= !empty($item_info->notes) ? $item_info->notes : '-' ?></div>
            </td>
            <td class="totals-cell">
                <div class="totals-panel">
                    <table class="totals-table">
                        <tbody>
                        <tr class="total">
                            <td><?= lang('sub_total') ?></td>
                            <td><?= display_money($this->items_model->transfer_calculate_to('transfer_cost', $item_info->transfer_item_id)); ?></td>
                        </tr>
                        <?php
                        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];
                                    ?>
                                    <tr class="total">
                                        <td><?= $tax[0] . ' (' . $tax[1] . ' %)' ?></td>
                                        <td><?= display_money($total_tax[$t_key]); ?></td>
                                    </tr>
                                <?php
                                }
                            }
                        }
                        ?>
                        <?php if ($tax_total > 0) : ?>
                            <tr class="total">
                                <td><?= lang('total') . ' ' . lang('tax') ?></td>
                                <td><?= display_money($tax_total); ?></td>
                            </tr>
                        <?php endif; ?>
                        <?php if ($item_info->adjustment > 0) : ?>
                            <tr class="total">
                                <td><?= lang('shipping_cost') ?></td>
                                <td><?= display_money($item_info->adjustment); ?></td>
                            </tr>
                        <?php endif; ?>
                        <tr class="total">
                            <td><?= lang('total') ?></td>
                            <td><?= display_money($this->items_model->transfer_calculate_to('total', $item_info->transfer_item_id), $currency->symbol); ?></td>
                        </tr>
                        </tbody>
                    </table>
                </div>
                <?php if (config_item('amount_to_words') == 'Yes') { ?>
                    <p class="num_word"><strong><?= lang('num_word') ?>
                            : </strong> <?= number_to_word('', $this->items_model->transfer_calculate_to('total', $item_info->transfer_item_id)); ?></p>
                <?php } ?>
            </td>
        </tr>
    </table>

    <footer><?= config_item('invoice_footer') ?></footer>
</div>
</body>

</html>
