<?php if ($credit_note->status == 'open') {
    $credit_remaining = $this->credit_note_model->credit_note_calculation('credit_remaining', $credit_note->credit_note_id);
    ?>
    <?php echo form_open(base_url('admin/credit_note/apply_credit_invoices/' . $credit_note->credit_note_id)); ?>
    <div class="rounded-2xl border border-slate-200 bg-white shadow-sm dark:border-slate-700 dark:bg-slate-900">
        <div class="flex items-center justify-between border-b border-slate-200 px-5 py-4 dark:border-slate-700">
            <h3 class="text-sm font-semibold text-slate-900 dark:text-slate-100">
                <?php echo lang('apply_credits_from', $credit_note->reference_no); ?>
            </h3>
        </div>
        <div class="apply_credit_invoices p-5" id="credit_apply" data-credit-remaining="<?php echo $credit_remaining; ?>">
            <?php if (!empty($all_invoices)) {
            $this->load->model('invoice_model');
            ?>
            <div class="overflow-x-auto rounded-xl border border-slate-200 dark:border-slate-700">
                <table class="DataTables min-w-full divide-y divide-slate-200 text-sm dark:divide-slate-700" id="DataTables" width="100%">
                    <thead class="bg-slate-50 text-slate-600 dark:bg-slate-800 dark:text-slate-300">
                        <tr>
                            <th class="px-4 py-3 text-left font-semibold"><?= lang('invoice') ?></th>
                            <th class="px-4 py-3 text-left font-semibold"><?= lang('due_date') ?></th>
                            <th class="px-4 py-3 text-left font-semibold"><?= lang('invoice_amount') ?></th>
                            <th class="px-4 py-3 text-left font-semibold"><?= lang('due_amount') ?></th>
                            <th class="px-4 py-3 text-left font-semibold"><?= lang('amount') . ' ' . ('to') . ' ' . ('credit') ?></th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-slate-100 dark:divide-slate-800">
                        <?php
                        foreach ($all_invoices as $v_invoices) {
                            $payment_status = $this->invoice_model->get_payment_status($v_invoices->invoices_id);
                            if ($payment_status != lang('fully_paid')) {
                        ?>
                                <tr>
                                    <td class="px-4 py-3">
                                        <a target="_blank" class="text-primary hover:underline"
                                           href="<?= base_url('admin/invoice/manage_invoice/invoice_details/' . $v_invoices->invoices_id) ?>">
                                            <?= html_escape($v_invoices->reference_no) ?>
                                        </a>
                                    </td>
                                    <td class="px-4 py-3 text-slate-700 dark:text-slate-200"><?= display_date($v_invoices->due_date) ?></td>
                                    <td class="px-4 py-3 text-slate-700 dark:text-slate-200"><?= display_money($this->invoice_model->calculate_to('invoice_cost', $v_invoices->invoices_id), client_currency($v_invoices->client_id)) ?></td>
                                    <td class="px-4 py-3 text-slate-700 dark:text-slate-200"><?= display_money($this->invoice_model->calculate_to('invoice_due', $v_invoices->invoices_id), client_currency($v_invoices->client_id)) ?></td>
                                    <td class="px-4 py-3">
                                        <?= form_input_group(
                                            'amount[' . $v_invoices->invoices_id . ']',
                                            '',
                                            '0',
                                            'number',
                                            false,
                                            array(
                                                'class' => 'amount-credit-field w-32',
                                                'data-parsley-type' => 'number',
                                                'data-invoice-due' => $this->invoice_model->calculate_to('invoice_due', $v_invoices->invoices_id)
                                            )
                                        ); ?>
                                        <div class="validate_error hidden mt-1 text-xs text-rose-600 dark:text-rose-400"><?= lang('credit_amount_bigger_then_due_amount'); ?></div>
                                    </td>
                                </tr>
                        <?php
                            }
                        }; ?>
                    </tbody>
                </table>
            </div>
            <div class="mt-5 flex justify-end">
                <div class="w-full max-w-lg rounded-xl border border-slate-200 bg-slate-50 p-4 dark:border-slate-700 dark:bg-slate-800/40">
                    <div class="space-y-3">
                        <div class="flex items-center justify-between gap-3">
                            <div class="text-sm font-semibold text-slate-700 dark:text-slate-200"><?php echo lang('added_into_payment'); ?>:</div>
                            <?= form_checkbox(
                                'added_into_payment',
                                '',
                                true,
                                false,
                                array(
                                    'id' => 'use_postmark',
                                    'value' => 'on',
                                    'class' => 'custom-checkbox'
                                )
                            ); ?>
                        </div>
                        <div class="postmark_config flex items-center justify-between gap-3">
                            <div class="text-sm font-semibold text-slate-700 dark:text-slate-200"><?php echo lang('send_email'); ?>:</div>
                            <?= form_checkbox(
                                'send_thank_you',
                                '',
                                true,
                                false,
                                array(
                                    'value' => 'on',
                                    'class' => 'custom-checkbox'
                                )
                            ); ?>
                        </div>
                        <div class="postmark_config flex items-center justify-between gap-3">
                            <div class="text-sm font-semibold text-slate-700 dark:text-slate-200"><?php echo lang('send') . ' ' . lang('sms'); ?>:</div>
                            <?= form_checkbox(
                                'send_sms',
                                '',
                                true,
                                false,
                                array(
                                    'value' => 'on',
                                    'class' => 'custom-checkbox'
                                )
                            ); ?>
                        </div>
                        <div class="postmark_config flex items-center justify-between gap-3">
                            <div class="flex items-center gap-1 text-sm font-semibold text-slate-700 dark:text-slate-200">
                                <span><?= lang('save_into_default_account') ?></span>
                                <i data-lucide="circle-help" data-toggle="tooltip" data-placement="top"
                                   title="<?= lang('will_be_added_into_deposit') ?>" class="h-4 w-4 text-slate-400 dark:text-slate-500"></i>:
                            </div>
                            <?= form_checkbox(
                                'save_into_account',
                                '',
                                true,
                                false,
                                array(
                                    'value' => 'on',
                                    'class' => 'custom-checkbox'
                                )
                            ); ?>
                        </div>
                        <div class="flex items-center justify-between border-t border-slate-200 pt-3 dark:border-slate-700">
                            <div class="text-sm font-semibold text-slate-700 dark:text-slate-200"><?php echo lang('amount') . ' ' . ('to') . ' ' . ('credit'); ?>:</div>
                            <div class="amount-credit text-sm font-semibold text-slate-900 dark:text-slate-100">0</div>
                        </div>
                        <div class="flex items-center justify-between">
                            <div class="text-sm font-semibold text-slate-700 dark:text-slate-200"><?php echo lang('remaining') . ' ' . ('credit'); ?>:</div>
                            <div class="credit_remaining text-sm font-semibold text-emerald-600 dark:text-emerald-400">
                                <?php echo $credit_remaining; ?>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php } else { ?>
                <p class="text-sm font-semibold text-slate-600 dark:text-slate-300"><?php echo lang('nothing_to_display'); ?></p>
            <?php } ?>
            <div class="mt-5 flex items-center justify-end gap-2 border-t border-slate-200 pt-4 dark:border-slate-700">
                <button type="button" data-dismiss="modal"
                        class="inline-flex items-center rounded-lg border border-slate-300 bg-white px-3 py-2 text-sm font-semibold text-slate-700 transition hover:bg-slate-50 dark:border-slate-600 dark:bg-slate-800 dark:text-slate-200 dark:hover:bg-slate-700">
                    <?php echo lang('close'); ?>
                </button>
                <?php if (!empty($all_invoices) && count($all_invoices) > 0) { ?>
                    <button type="submit" class="amount_exceed inline-flex items-center rounded-lg bg-slate-900 px-4 py-2 text-sm font-semibold text-white transition hover:bg-slate-800 dark:bg-slate-100 dark:text-slate-900 dark:hover:bg-white">
                        <?php echo lang('apply'); ?>
                    </button>
                <?php } ?>
            </div>
        </div>
        <?php echo form_close(); ?>
    </div>
<?php } ?>
<script type="text/javascript">
    $('input[id="use_postmark"]').on("click", function(){
        if (this.checked) {
            $('.postmark_config').show();
        } else {
            $('.postmark_config').hide();
        }
    });
</script>
