<div class="space-y-6">
<?= message_box('success') ?>
<?= message_box('error') ?>

<?php
$paid_amount = $this->return_stock_model->calculate_to('paid_amount', $return_stock_info->return_stock_id);
$payment_status = $this->return_stock_model->get_payment_status($return_stock_info->return_stock_id);
$currency = $this->return_stock_model->check_by(array('code' => config_item('default_currency')), 'tbl_currencies');
$currency_symbol = !empty($currency->symbol) ? $currency->symbol : '';
$invoice_items = $this->return_stock_model->ordered_items_by_id($return_stock_info->return_stock_id);
$return_stock_total = $this->return_stock_model->calculate_to('total', $return_stock_info->return_stock_id);
$return_stock_due = $this->return_stock_model->calculate_to('return_stock_due', $return_stock_info->return_stock_id);
$sub_total = $this->return_stock_model->calculate_to('return_stock_cost', $return_stock_info->return_stock_id);
$discount_total = $this->return_stock_model->calculate_to('discount', $return_stock_info->return_stock_id);

if ($return_stock_info->module == 'client') {
    $party_info = $this->return_stock_model->check_by(array('client_id' => $return_stock_info->module_id), 'tbl_client');
} elseif ($return_stock_info->module == 'supplier') {
    $party_info = $this->return_stock_model->check_by(array('supplier_id' => $return_stock_info->module_id), 'tbl_suppliers');
} else {
    $party_info = null;
}

$party_name = !empty($party_info->name) ? $party_info->name : '-';
$party_address = !empty($party_info->address) ? $party_info->address : '-';
$party_email = !empty($party_info->email) ? $party_info->email : '-';
$party_phone = !empty($party_info->phone) ? $party_info->phone : '-';
$party_mobile = !empty($party_info->mobile) ? $party_info->mobile : '-';
$party_tax = !empty($party_info->tax) ? $party_info->tax : '';

$status_label = !empty($return_stock_info->main_status) ? lang($return_stock_info->main_status) : '-';
$payment_label = !empty($payment_status) ? lang($payment_status) : '-';

$status_class = 'bg-slate-100 text-slate-700 ring-slate-200 dark:bg-slate-800 dark:text-slate-200 dark:ring-slate-700';
if ($return_stock_info->main_status == 'accepted') {
    $status_class = 'bg-emerald-50 text-emerald-700 ring-emerald-200 dark:bg-emerald-900/30 dark:text-emerald-200 dark:ring-emerald-800';
} elseif ($return_stock_info->main_status == 'cancelled') {
    $status_class = 'bg-rose-50 text-rose-700 ring-rose-200 dark:bg-rose-900/30 dark:text-rose-200 dark:ring-rose-800';
} elseif ($return_stock_info->main_status == 'draft') {
    $status_class = 'bg-slate-100 text-slate-700 ring-slate-200 dark:bg-slate-800 dark:text-slate-200 dark:ring-slate-700';
}

$payment_class = 'bg-rose-50 text-rose-700 ring-rose-200 dark:bg-rose-900/30 dark:text-rose-200 dark:ring-rose-800';
if ($payment_status == 'fully_paid') {
    $payment_class = 'bg-emerald-50 text-emerald-700 ring-emerald-200 dark:bg-emerald-900/30 dark:text-emerald-200 dark:ring-emerald-800';
} elseif ($payment_status == 'partially_paid') {
    $payment_class = 'bg-amber-50 text-amber-700 ring-amber-200 dark:bg-amber-900/30 dark:text-amber-200 dark:ring-amber-800';
} elseif ($payment_status == 'draft') {
    $payment_class = 'bg-slate-100 text-slate-700 ring-slate-200 dark:bg-slate-800 dark:text-slate-200 dark:ring-slate-700';
}

$overdue_message = '';
if (!empty($return_stock_info->due_date) && safe_strtotime($return_stock_info->due_date) < safe_strtotime(date('Y-m-d')) && $payment_status != lang('fully_paid') && $payment_status != 'fully_paid') {
    $days_between = ceil(abs(safe_strtotime($return_stock_info->due_date) - safe_strtotime(date('Y-m-d'))) / 86400);
    $overdue_message = lang('invoice_overdue') . ' ' . lang('by') . ' ' . $days_between . ' ' . lang('days');
}

$tax_info = !empty($return_stock_info->total_tax) ? json_decode($return_stock_info->total_tax) : null;
$linked_invoice_no = '';
if (!empty($return_stock_info->invoices_id)) {
    $linked_invoice_no = get_any_field('tbl_invoices', array('invoices_id' => $return_stock_info->invoices_id), 'reference_no');
}
?>

    <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-start lg:justify-between">
            <div class="space-y-2">
                <p class="text-xs font-semibold uppercase tracking-[0.2em] text-muted-foreground"><?= lang('return_stock') ?></p>
                <div class="flex flex-wrap items-center gap-3">
                    <h1 class="text-2xl font-semibold text-foreground"><?= html_escape($return_stock_info->reference_no) ?></h1>
                    <span class="inline-flex items-center rounded-full px-3 py-1 text-xs font-semibold ring-1 ring-inset <?= $status_class ?>">
                        <?= $status_label ?>
                    </span>
                    <span class="inline-flex items-center rounded-full px-3 py-1 text-xs font-semibold ring-1 ring-inset <?= $payment_class ?>">
                        <?= lang('payment_status') ?>: <?= $payment_label ?>
                    </span>
                </div>
                <div class="flex flex-wrap gap-x-4 gap-y-2 text-sm text-muted-foreground">
                    <span><?= lang('date') ?>: <span class="font-medium text-foreground"><?= display_date($return_stock_info->return_stock_date) ?></span></span>
                    <span><?= lang('due_date') ?>: <span class="font-medium text-foreground"><?= display_date($return_stock_info->due_date) ?></span></span>
                    <?php if (!empty($return_stock_info->user_id)) { ?>
                        <span><?= lang('sales') . ' ' . lang('agent') ?>: <span class="font-medium text-foreground"><?= html_escape(fullname($return_stock_info->user_id)) ?></span></span>
                    <?php } ?>
                </div>
                <?php if (!empty($linked_invoice_no)) { ?>
                    <div class="text-sm text-muted-foreground">
                        <?= lang('invoice') . ' ' . lang('no') ?>:
                        <a href="<?= base_url('client/invoice/manage_invoice/invoice_details/' . $return_stock_info->invoices_id) ?>" class="font-medium text-primary hover:underline">
                            <?= html_escape($linked_invoice_no) ?>
                        </a>
                    </div>
                <?php } ?>
            </div>

            <div class="flex flex-wrap items-center gap-2">
                <a href="#" onclick="print_return_stock('print_return_stock'); 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>
                <a href="<?= base_url('client/invoice/pdf_return_stock/' . $return_stock_info->return_stock_id) ?>" class="inline-flex items-center gap-2 rounded-lg bg-emerald-600 px-3 py-2 text-xs font-semibold text-white shadow-sm hover:bg-emerald-500">
                    <i data-lucide="file-text" class="h-4 w-4"></i>
                    PDF
                </a>
            </div>
        </div>

        <?php if (!empty($overdue_message)) { ?>
            <div class="mt-4 rounded-xl border border-rose-200 bg-rose-50 px-4 py-3 text-sm text-rose-700 dark:border-rose-700/50 dark:bg-rose-900/30 dark:text-rose-200">
                <div class="flex items-start gap-3">
                    <i data-lucide="triangle-alert" class="mt-0.5 h-4 w-4"></i>
                    <div><?= $overdue_message ?></div>
                </div>
            </div>
        <?php } ?>
    </div>

    <div id="print_return_stock" class="space-y-6">
        <div class="grid gap-6 xl:grid-cols-[2fr_1fr]">
            <div class="rounded-2xl border border-border bg-card p-6 shadow-sm">
                <div class="flex items-center gap-4">
                    <img src="<?= base_url() . config_item('invoice_logo') ?>" alt="<?= html_escape(config_item('company_name')) ?>" class="h-14 w-14 rounded-xl object-contain">
                    <div>
                        <h2 class="text-lg font-semibold text-foreground"><?= html_escape(config_item('company_legal_name')) ?></h2>
                        <div class="text-sm text-muted-foreground"><?= html_escape(config_item('company_address')) ?></div>
                        <div class="text-sm text-muted-foreground"><?= html_escape(config_item('company_city')) ?>, <?= html_escape(config_item('company_zip_code')) ?></div>
                        <div class="text-sm text-muted-foreground"><?= html_escape(config_item('company_country')) ?></div>
                        <div class="mt-2 text-sm text-muted-foreground"><?= lang('phone') ?>: <?= html_escape(config_item('company_phone')) ?></div>
                        <div class="text-sm text-muted-foreground"><?= lang('vat_number') ?>: <?= html_escape(config_item('company_vat')) ?></div>
                    </div>
                </div>
            </div>

            <div class="rounded-2xl border border-border bg-card p-6 shadow-sm">
                <p class="text-xs font-semibold uppercase tracking-[0.2em] text-muted-foreground"><?= lang($return_stock_info->module) . ' ' . lang('info') ?></p>
                <h3 class="mt-3 text-lg font-semibold text-foreground"><?= html_escape($party_name) ?></h3>
                <div class="mt-2 space-y-1 text-sm text-muted-foreground">
                    <div><?= html_escape($party_address) ?></div>
                    <div><?= html_escape($party_email) ?></div>
                    <div><?= lang('phone') ?>: <?= html_escape($party_phone) ?></div>
                    <div><?= lang('mobile') ?>: <?= html_escape($party_mobile) ?></div>
                    <?php if (!empty($party_tax)) { ?>
                        <div><?= lang('tax') ?>: <?= html_escape($party_tax) ?></div>
                    <?php } ?>
                </div>
            </div>
        </div>

        <div class="rounded-2xl border border-border bg-card p-6 shadow-sm">
            <div class="grid gap-4 md:grid-cols-2 xl:grid-cols-4">
                <div class="rounded-xl border border-border bg-muted/20 p-4">
                    <div class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('reference_no') ?></div>
                    <div class="mt-2 text-base font-semibold text-foreground"><?= html_escape($return_stock_info->reference_no) ?></div>
                </div>
                <div class="rounded-xl border border-border bg-muted/20 p-4">
                    <div class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('date') ?></div>
                    <div class="mt-2 text-base font-semibold text-foreground"><?= display_date($return_stock_info->return_stock_date) ?></div>
                </div>
                <div class="rounded-xl border border-border bg-muted/20 p-4">
                    <div class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('due_date') ?></div>
                    <div class="mt-2 text-base font-semibold text-foreground"><?= display_date($return_stock_info->due_date) ?></div>
                </div>
                <div class="rounded-xl border border-border bg-muted/20 p-4">
                    <div class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('amount_due') ?></div>
                    <div class="mt-2 text-base font-semibold text-foreground"><?= display_money($return_stock_due, $currency_symbol) ?></div>
                </div>
            </div>
        </div>

        <div class="overflow-hidden rounded-2xl border border-border bg-card shadow-sm">
            <div class="overflow-x-auto">
                <table class="min-w-full text-sm">
                    <thead class="bg-muted/30 text-xs uppercase tracking-wide text-muted-foreground">
                        <tr>
                            <th class="px-4 py-3 text-left">#</th>
                            <th class="px-4 py-3 text-left"><?= lang('items') ?></th>
                            <?php if (!empty(config_item('invoice_view')) && config_item('invoice_view') == '2') { ?>
                                <th class="px-4 py-3 text-left"><?= lang('hsn_code') ?></th>
                            <?php } ?>
                            <th class="px-4 py-3 text-left"><?= lang('qty') ?></th>
                            <th class="px-4 py-3 text-right"><?= lang('price') ?></th>
                            <th class="px-4 py-3 text-left"><?= lang('tax') ?></th>
                            <th class="px-4 py-3 text-right"><?= lang('total') ?></th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-border">
                        <?php if (!empty($invoice_items)) { ?>
                            <?php foreach ($invoice_items as $key => $v_item) {
                                $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) : array();
                            ?>
                                <tr>
                                    <td class="px-4 py-4 text-muted-foreground"><?= $key + 1 ?></td>
                                    <td class="px-4 py-4">
                                        <div class="font-medium text-foreground"><?= html_escape($item_name) ?></div>
                                        <div class="text-xs text-muted-foreground"><?= strip_html_tags($v_item->item_desc) ?></div>
                                    </td>
                                    <?php if (!empty(config_item('invoice_view')) && config_item('invoice_view') == '2') { ?>
                                        <td class="px-4 py-4 text-muted-foreground"><?= html_escape($v_item->hsn_code) ?></td>
                                    <?php } ?>
                                    <td class="px-4 py-4 text-muted-foreground"><?= html_escape($v_item->quantity) ?> <?= html_escape($v_item->unit) ?></td>
                                    <td class="px-4 py-4 text-right font-medium text-foreground"><?= display_money($v_item->unit_cost, $currency_symbol) ?></td>
                                    <td class="px-4 py-4 text-xs text-muted-foreground">
                                        <?php if (!empty($item_tax_name)) {
                                            foreach ($item_tax_name as $v_tax_name) {
                                                $i_tax_name = explode('|', $v_tax_name);
                                                echo '<div>' . html_escape($i_tax_name[0]) . ' (' . html_escape($i_tax_name[1]) . '%) ' . display_money($v_item->total_cost / 100 * $i_tax_name[1], $currency_symbol) . '</div>';
                                            }
                                        } else {
                                            echo '-';
                                        } ?>
                                    </td>
                                    <td class="px-4 py-4 text-right font-semibold text-foreground"><?= display_money($v_item->total_cost, $currency_symbol) ?></td>
                                </tr>
                            <?php } ?>
                        <?php } else { ?>
                            <tr>
                                <td colspan="<?= !empty(config_item('invoice_view')) && config_item('invoice_view') == '2' ? '7' : '6' ?>" class="px-4 py-6 text-center text-muted-foreground"><?= lang('nothing_to_display') ?></td>
                            </tr>
                        <?php } ?>
                    </tbody>
                </table>
            </div>
        </div>

        <div class="grid gap-6 xl:grid-cols-[2fr_1fr]">
            <div class="rounded-2xl border border-border bg-card p-6 shadow-sm">
                <h3 class="text-sm font-semibold uppercase tracking-[0.2em] text-muted-foreground"><?= lang('terms_conditions') ?></h3>
                <div class="mt-4 text-sm text-muted-foreground">
                    <?= !empty($return_stock_info->notes) ? $return_stock_info->notes : lang('nothing_to_display') ?>
                </div>
            </div>

            <div class="rounded-2xl border border-border bg-card p-6 shadow-sm">
                <div class="space-y-3 text-sm">
                    <div class="flex items-center justify-between">
                        <span class="text-muted-foreground"><?= lang('sub_total') ?></span>
                        <span class="font-semibold text-foreground"><?= display_money($sub_total, $currency_symbol) ?></span>
                    </div>
                    <?php if ($return_stock_info->discount_total > 0) { ?>
                        <div class="flex items-center justify-between">
                            <span class="text-muted-foreground"><?= lang('discount') ?> (<?= html_escape($return_stock_info->discount_percent) ?>%)</span>
                            <span class="font-semibold text-rose-600">-<?= display_money($discount_total, $currency_symbol) ?></span>
                        </div>
                    <?php } ?>
                    <?php
                    $tax_total = 0;
                    if (!empty($tax_info) && !empty($tax_info->tax_name)) {
                        foreach ($tax_info->tax_name as $t_key => $v_tax_info) {
                            $tax = explode('|', $v_tax_info);
                            $tax_value = !empty($tax_info->total_tax[$t_key]) ? $tax_info->total_tax[$t_key] : 0;
                            $tax_total += $tax_value;
                    ?>
                        <div class="flex items-center justify-between">
                            <span class="text-muted-foreground"><?= html_escape($tax[0]) ?> (<?= html_escape($tax[1]) ?>%)</span>
                            <span class="font-semibold text-foreground"><?= display_money($tax_value, $currency_symbol) ?></span>
                        </div>
                    <?php
                        }
                    }
                    if ($return_stock_info->adjustment > 0) { ?>
                        <div class="flex items-center justify-between">
                            <span class="text-muted-foreground"><?= lang('adjustment') ?></span>
                            <span class="font-semibold text-foreground"><?= display_money($return_stock_info->adjustment, $currency_symbol) ?></span>
                        </div>
                    <?php } ?>
                    <div class="border-t border-border pt-3">
                        <div class="flex items-center justify-between text-base font-semibold text-foreground">
                            <span><?= lang('total') ?></span>
                            <span><?= display_money($return_stock_total, $currency_symbol) ?></span>
                        </div>
                        <?php if ($paid_amount > 0) { ?>
                            <div class="mt-2 flex items-center justify-between text-sm text-muted-foreground">
                                <span><?= lang('paid_amount') ?></span>
                                <span><?= display_money($paid_amount, $currency_symbol) ?></span>
                            </div>
                        <?php } ?>
                        <div class="mt-3 flex items-center justify-between text-sm font-semibold text-foreground">
                            <span><?= lang('total_due') ?></span>
                            <span><?= display_money($return_stock_due, $currency_symbol) ?></span>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <?= !empty(config_item('invoice_view')) && config_item('invoice_view') > 0 ? $this->gst->summary($invoice_items) : ''; ?>
    </div>

    <?php
    $all_payment_info = $this->db->where('return_stock_id', $return_stock_info->return_stock_id)->get('tbl_return_stock_payments')->result();
    if (!empty($all_payment_info)) {
    ?>
        <div class="rounded-2xl border border-border bg-card shadow-sm">
            <div class="border-b border-border px-6 py-4">
                <h2 class="text-base font-semibold text-foreground"><?= lang('payment') . ' ' . lang('details') ?></h2>
            </div>
            <div class="overflow-x-auto">
                <table class="min-w-full text-sm">
                    <thead class="bg-muted/30 text-xs uppercase tracking-wide text-muted-foreground">
                        <tr>
                            <th class="px-4 py-3 text-left"><?= lang('transaction_id') ?></th>
                            <th class="px-4 py-3 text-left"><?= lang('payment_date') ?></th>
                            <th class="px-4 py-3 text-left"><?= lang('amount') ?></th>
                            <th class="px-4 py-3 text-left"><?= lang('payment_mode') ?></th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-border">
                        <?php foreach ($all_payment_info as $v_payments_info) {
                            if (is_numeric($v_payments_info->payment_method)) {
                                $payment_methods = get_row('tbl_payment_methods', array('payment_methods_id' => $v_payments_info->payment_method));
                                $method_name = !empty($payment_methods->method_name) ? $payment_methods->method_name : '-';
                            } else {
                                $method_name = $v_payments_info->payment_method;
                            }
                        ?>
                            <tr>
                                <td class="px-4 py-4">
                                    <a href="<?= base_url('admin/return_stock/payments_details/' . $v_payments_info->payments_id) ?>" class="font-medium text-primary hover:underline">
                                        <?= html_escape($v_payments_info->trans_id) ?>
                                    </a>
                                </td>
                                <td class="px-4 py-4 text-muted-foreground"><?= display_date($v_payments_info->payment_date) ?></td>
                                <td class="px-4 py-4 font-medium text-foreground"><?= display_money($v_payments_info->amount, $currency_symbol) ?></td>
                                <td class="px-4 py-4 text-muted-foreground"><?= html_escape($method_name) ?></td>
                            </tr>
                        <?php } ?>
                    </tbody>
                </table>
            </div>
        </div>
    <?php } ?>
</div>

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