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

    $text_align = !empty($RTL) ? 'right' : 'left';
    $opposite_align = !empty($RTL) ? 'left' : 'right';
    $report_title = lang('stock') . ' ' . lang('report');
    $item_name = '';

    if (!empty($stock_id)) {
        $item_info = $this->db->where('stock_id', $stock_id)->get('tbl_stock')->row();
        $item_name = !empty($item_info->item_name) ? $item_info->item_name : '-';
    }
    ?>
    <style type="text/css">
        body {
            margin: 0;
            color: #1e293b;
            font-family: DejaVu Sans, Arial, Helvetica, sans-serif;
            font-size: 11px;
            line-height: 1.4;
            background: #ffffff;
        }

        .page {
            padding: 20px 24px 24px;
        }

        .report-header {
            border-bottom: 2px solid #2563eb;
            padding-bottom: 12px;
            margin-bottom: 14px;
        }

        .report-header table,
        .summary-table,
        .data-table {
            width: 100%;
            border-collapse: collapse;
        }

        .logo-cell {
            width: 58px;
            vertical-align: middle;
        }

        .logo {
            width: 46px;
            max-height: 46px;
        }

        .company-name {
            font-size: 20px;
            font-weight: bold;
            color: #0f172a;
            margin: 0 0 2px;
            text-align: <?= $text_align ?>;
        }

        .report-kicker {
            font-size: 11px;
            text-transform: uppercase;
            letter-spacing: 0.8px;
            color: #2563eb;
            margin: 0 0 2px;
            text-align: <?= $text_align ?>;
        }

        .report-title {
            font-size: 14px;
            font-weight: bold;
            color: #334155;
            margin: 0;
            text-align: <?= $text_align ?>;
        }

        .meta-cell {
            width: 150px;
            vertical-align: middle;
            text-align: <?= $opposite_align ?>;
        }

        .meta-box {
            display: inline-block;
            background: #f8fafc;
            padding: 7px 10px;
            text-align: <?= $text_align ?>;
        }

        .meta-label {
            color: #64748b;
            font-size: 9px;
            text-transform: uppercase;
            letter-spacing: 0.5px;
            margin: 0 0 2px;
        }

        .meta-value {
            color: #0f172a;
            font-size: 11px;
            font-weight: bold;
            margin: 0;
        }

        .summary-wrap {
            border: 1px solid #dbe4ee;
            margin-bottom: 14px;
        }

        .summary-label {
            font-size: 9px;
            text-transform: uppercase;
            letter-spacing: 0.5px;
            color: #64748b;
            padding: 10px 12px 2px;
            text-align: <?= $text_align ?>;
        }

        .summary-value {
            font-size: 12px;
            color: #0f172a;
            font-weight: bold;
            padding: 0 12px 10px;
            text-align: <?= $text_align ?>;
        }

        .summary-cell {
            width: 50%;
            border-right: 1px solid #dbe4ee;
        }

        .summary-cell-last {
            border-right: 0;
        }

        .section {
            margin-bottom: 15px;
        }

        .section-title {
            color: #0f172a;
            font-size: 13px;
            font-weight: bold;
            padding: 0 0 7px;
            border-bottom: 1px solid #dbe4ee;
            margin-bottom: 8px;
        }

        .category-title {
            font-size: 11px;
            font-weight: bold;
            color: #1d4ed8;
            background: #eff6ff;
            border: 1px solid #bfdbfe;
            border-bottom: 0;
            padding: 7px 9px;
            margin: 10px 0 0;
        }

        .data-table {
            margin-bottom: 10px;
            table-layout: fixed;
        }

        .data-table thead th {
            background: #1e293b;
            color: #ffffff;
            padding: 7px 9px;
            font-size: 11px;
            font-weight: bold;
            border: 1px solid #1e293b;
            text-align: <?= $text_align ?>;
        }

        .data-table tbody td,
        .data-table tfoot td {
            padding: 7px 9px;
            border: 1px solid #dbe4ee;
            text-align: <?= $text_align ?>;
            vertical-align: middle;
        }

        .data-table tbody tr:nth-child(even) td {
            background: #f8fafc;
        }

        .data-table tfoot td {
            background: #f8fafc;
            font-weight: bold;
            color: #0f172a;
        }

        .item-name {
            font-weight: bold;
            color: #0f172a;
        }

        .number-cell {
            text-align: center;
        }

        .qty-cell,
        .date-cell,
        .stock-cell {
            white-space: nowrap;
        }

        .index-col {
            width: 38px;
        }

        .qty-col,
        .date-col,
        .stock-col {
            width: 118px;
        }

        .empty-state {
            border: 1px solid #dbe4ee;
            background: #f8fafc;
            color: #475569;
            padding: 12px;
            font-size: 12px;
            margin-bottom: 14px;
        }

        .footer-note {
            border-top: 1px solid #e2e8f0;
            padding-top: 8px;
            margin-top: 16px;
            color: #94a3b8;
            font-size: 9px;
            text-align: <?= $opposite_align ?>;
        }
    </style>
</head>
<body>
<?php
$img = ROOTPATH . '/' . config_item('company_logo');
$a = file_exists($img);
if (empty($a)) {
    $img = base_url() . config_item('company_logo');
}
if (!file_exists($img)) {
    $img = ROOTPATH . '/' . 'uploads/default_logo.png';
}
?>
<div class="page">
    <div class="report-header">
        <table>
            <tr>
                <td class="logo-cell">
                    <img class="logo" src="<?= $img ?>" alt="<?= config_item('company_name') ?>">
                </td>
                <td>
                    <p class="report-kicker"><?= lang('stock') ?></p>
                    <p class="company-name"><?= config_item('company_name') ?></p>
                    <p class="report-title"><?= $report_title ?></p>
                </td>
                <td class="meta-cell">
                    <div class="meta-box">
                        <p class="meta-label"><?= lang('date') ?></p>
                        <p class="meta-value"><?= display_date(date('Y-m-d')) ?></p>
                    </div>
                </td>
            </tr>
        </table>
    </div>

    <div class="summary-wrap">
        <table class="summary-table">
            <tr>
                <?php if (!empty($stock_id)) { ?>
                    <td class="summary-cell summary-cell-last">
                        <div class="summary-label"><?= lang('items_name') ?></div>
                        <div class="summary-value"><?= html_escape($item_name) ?></div>
                    </td>
                <?php } else { ?>
                    <td class="summary-cell">
                        <div class="summary-label"><?= lang('report_from') ?></div>
                        <div class="summary-value"><?= display_date($start_date); ?></div>
                    </td>
                    <td class="summary-cell summary-cell-last">
                        <div class="summary-label"><?= lang('report_to') ?></div>
                        <div class="summary-value"><?= display_date($end_date); ?></div>
                    </td>
                <?php } ?>
            </tr>
        </table>
    </div>

    <div class="section">
        <div class="section-title"><?= lang('stock_list') ?></div>
        <?php if (!empty($purchase_stock)) { ?>
            <?php foreach ($purchase_stock as $item_name => $v_purchase_stoc) { ?>
                <?php $last_purchase_stock = null; ?>
                <div class="category-title"><?= html_escape($item_name) ?></div>
                <table class="data-table">
                    <thead>
                        <tr>
                            <th class="index-col"><?= lang('sl') ?></th>
                            <th><?= lang('item_name') ?></th>
                            <th class="stock-col"><?= lang('inventory') ?></th>
                            <th class="date-col"><?= lang('buying_date') ?></th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php
                    $total_inventory = 0;
                    if (!empty($v_purchase_stoc)) {
                        foreach ($v_purchase_stoc as $p_sl => $v_stock) {
                            $last_purchase_stock = $v_stock;
                            $total_inventory += $v_stock->inventory;
                            ?>
                            <tr>
                                <td class="number-cell"><?= $p_sl + 1 ?></td>
                                <td class="item-name"><?= html_escape($v_stock->item_name) ?></td>
                                <td class="stock-cell"><?= html_escape($v_stock->inventory) ?></td>
                                <td class="date-cell"><?= display_date($v_stock->purchase_date); ?></td>
                            </tr>
                        <?php }
                    } ?>
                    </tbody>
                    <?php if (!empty($last_purchase_stock)) { ?>
                        <tfoot>
                            <tr>
                                <td colspan="2"><?= lang('total') . ' ' . lang('assigned') ?></td>
                                <td class="stock-cell"><?= html_escape($total_inventory - $last_purchase_stock->total_stock) ?></td>
                                <td class="stock-cell"><?= lang('available_stock') ?>: <?= html_escape($last_purchase_stock->total_stock) ?></td>
                            </tr>
                        </tfoot>
                    <?php } ?>
                </table>
            <?php } ?>
        <?php } else { ?>
            <div class="empty-state"><?= lang('nothing_to_display') ?></div>
        <?php } ?>
    </div>

    <div class="section">
        <div class="section-title"><?= lang('assign_stock_list') ?></div>
        <?php if (!empty($assign_stock)) { ?>
            <?php foreach ($assign_stock as $item_name => $v_assign_report) { ?>
                <?php $last_assign_report = null; ?>
                <div class="category-title"><?= html_escape($item_name) ?></div>
                <table class="data-table">
                    <thead>
                        <tr>
                            <th class="index-col"><?= lang('sl') ?></th>
                            <th><?= lang('assigned_user') ?></th>
                            <th class="date-col"><?= lang('assign_date') ?></th>
                            <th class="qty-col"><?= lang('assign_quantity') ?></th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php
                    $total_assign_inventory = 0;
                    if (!empty($v_assign_report)) {
                        foreach ($v_assign_report as $ssl => $v_report) {
                            $last_assign_report = $v_report;
                            $total_assign_inventory += $v_report->assign_inventory;
                            ?>
                            <tr>
                                <td class="number-cell"><?= $ssl + 1 ?></td>
                                <td class="item-name"><?= html_escape($v_report->fullname) ?></td>
                                <td class="date-cell"><?= display_date($v_report->assign_date); ?></td>
                                <td class="qty-cell"><?= html_escape($v_report->assign_inventory) ?></td>
                            </tr>
                        <?php }
                    } ?>
                    </tbody>
                    <?php if (!empty($last_assign_report)) { ?>
                        <tfoot>
                            <tr>
                                <td colspan="2"><?= lang('total') . ' ' . html_escape($last_assign_report->item_name) ?></td>
                                <td class="stock-cell"><?= lang('available_stock') ?>: <?= html_escape($last_assign_report->total_stock) ?></td>
                                <td class="qty-cell"><?= html_escape($total_assign_inventory) ?></td>
                            </tr>
                        </tfoot>
                    <?php } ?>
                </table>
            <?php } ?>
        <?php } else { ?>
            <div class="empty-state"><?= lang('nothing_to_display') ?></div>
        <?php } ?>
    </div>

    <div class="footer-note">
        <?= config_item('company_name') ?> &middot; <?= lang('stock') . ' ' . lang('report') ?>
    </div>
</div>
</body>
</html>
