<div class="rounded-2xl border border-slate-200 bg-white shadow-sm dark:border-slate-700 dark:bg-slate-900">
   
    <div class="modal-body wrap-modal wrap p-5">
        <form role="form" id="from_items"
              action="<?php echo base_url(); ?>admin/credit_note/add_insert_items/<?= $credit_note_id ?>" method="post"
              class="space-y-4">

            <div class="overflow-x-auto rounded-xl border border-slate-200 dark:border-slate-700">
                <table class="min-w-full text-sm">
                    <thead class="bg-slate-50 text-xs font-semibold uppercase tracking-wide text-slate-600 dark:bg-slate-800 dark:text-slate-300">
                    <tr>
                        <th class="w-12 px-3 py-3 text-left">
                            <input id="parent_present" type="checkbox" class="h-4 w-4 rounded border-slate-300 text-primary focus:ring-primary/40 dark:border-slate-600" title="<?= lang('select') . ' ' . lang('all') ?>">
                        </th>
                        <th class="px-3 py-3 text-left"><?= lang('item') ?></th>
                        <?php
                        $invoice_view = config_item('invoice_view');
                        if (!empty($invoice_view) && $invoice_view == '2') {
                            ?>
                            <th class="px-3 py-3 text-left"><?= lang('hsn_code') ?></th>
                        <?php } ?>
                        <th class="px-3 py-3 text-left"><?= lang('qty') ?></th>
                        <th class="px-3 py-3 text-left"><?= lang('unit_price') ?></th>
                        <th class="px-3 py-3 text-left"><?= lang('tax') ?></th>
                        <th class="px-3 py-3 text-left"><?= lang('total') ?></th>
                    </tr>
                    </thead>
                    <tbody class="divide-y divide-slate-200 dark:divide-slate-700">
                    <?php
                    $saved_items = $this->credit_note_model->get_all_items();
                    if (!empty($saved_items)) {
                        $saved_items = array_reverse($saved_items, true);
                        foreach ($saved_items as $group_id => $v_saved_items) {
                            if ($group_id != 0) {
                                $group_info = $this->db->where('customer_group_id', $group_id)->get('tbl_customer_group')->row();
                                $group = !empty($group_info->customer_group) ? $group_info->customer_group : '';
                            } else {
                                $group = '';
                            }
                            if (!empty($group)) {
                                ?>
                                <tr>
                                    <td colspan="<?= (!empty($invoice_view) && $invoice_view == '2') ? '7' : '6' ?>" class="bg-slate-100 px-3 py-2 text-xs font-semibold uppercase tracking-wide text-slate-600 dark:bg-slate-800/70 dark:text-slate-300"><?= html_escape($group) ?></td>
                                </tr>
                            <?php }

                            if (!empty($v_saved_items)) {
                                foreach ($v_saved_items as $v_item) { ?>
                                    <tr class="align-top">
                                        <td class="px-3 py-3">
                                            <input class="child_present h-4 w-4 rounded border-slate-300 text-primary focus:ring-primary/40 dark:border-slate-600" type="checkbox" name="saved_items_id[]"
                                                   value="<?= $v_item->saved_items_id ?>"/>
                                        </td>
                                        <td class="px-3 py-3 text-slate-700 dark:text-slate-200">
                                            <strong class="block"><?= html_escape($v_item->item_name) ?></strong>
                                            <span class="text-xs text-slate-500 dark:text-slate-400"><?= strip_html_tags(mb_substr($v_item->item_desc, 0, 200)) . '...'; ?></span>
                                        </td>
                                        <?php if (!empty($invoice_view) && $invoice_view == '2') { ?>
                                            <td class="px-3 py-3 text-slate-700 dark:text-slate-200"><?= html_escape($v_item->hsn_code) ?></td>
                                        <?php } ?>
                                        <td class="px-3 py-3 text-slate-700 dark:text-slate-200"><?= $v_item->quantity . ' ' . $v_item->unit_type ?></td>
                                        <td class="px-3 py-3 text-slate-700 dark:text-slate-200"><?= display_money($v_item->unit_cost) ?></td>
                                        <td class="px-3 py-3 text-xs text-slate-600 dark:text-slate-300"><?php
                                            if (!is_numeric($v_item->tax_rates_id)) {
                                                $tax_rates = json_decode($v_item->tax_rates_id);
                                            } else {
                                                $tax_rates = null;
                                            }
                                            if (!empty($tax_rates)) {
                                                foreach ($tax_rates as $key => $tax_id) {
                                                    $taxes_info = $this->db->where('tax_rates_id', $tax_id)->get('tbl_tax_rates')->row();
                                                    if (!empty($taxes_info)) {
                                                        echo ($key + 1) . '. ' . $taxes_info->tax_rate_name . ' (' . $taxes_info->tax_rate_percent . '%)<br>';
                                                    }
                                                }
                                            }
                                            ?></td>
                                        <td class="px-3 py-3 font-semibold text-slate-800 dark:text-slate-100"><?= display_money($v_item->total_cost) ?></td>
                                    </tr>
                                <?php }
                            }
                        }
                    } ?>
                    </tbody>
                </table>
            </div>

            <div class="flex items-center justify-end gap-2 border-t border-slate-200 pt-4 dark:border-slate-700">
                <button type="button" class="inline-flex items-center rounded-lg border border-slate-200 bg-white px-4 py-2 text-sm font-semibold text-slate-700 hover:bg-slate-100 dark:border-slate-700 dark:bg-slate-800 dark:text-slate-200 dark:hover:bg-slate-700" data-dismiss="modal"><?= lang('close') ?></button>
                <button type="submit" class="inline-flex items-center rounded-lg bg-slate-900 px-4 py-2 text-sm font-semibold text-white hover:bg-slate-800 dark:bg-slate-100 dark:text-slate-900 dark:hover:bg-white"><?= lang('update') ?></button>
            </div>
        </form>
    </div>
</div>

<script type="text/javascript">
    /*
     * Select All select
     */
    $(function () {
        $('#parent_present').on('change', function () {
            $('.child_present').prop('checked', $(this).prop('checked'));
        });
        $('.child_present').on('change', function () {
            var allChecked = $('.child_present').length > 0 && $('.child_present:checked').length === $('.child_present').length;
            $('#parent_present').prop('checked', allChecked);
        });
    });
    $(document).ready(function () {
        $("#from_items").validate({
            rules: {
                saved_items_id: {
                    required: true,
                }
            }
        });
    });</script>
<script src="<?php echo base_url(); ?>asset/js/custom-validation.js" type="text/javascript"></script>
<script src="<?php echo base_url(); ?>asset/js/jquery.validate.js" type="text/javascript"></script>
