<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<?= form_hidden('_attachment_sale_id', $credit_note->id); ?>
<?= form_hidden('_attachment_sale_type', 'credit_note'); ?>
<?php
$first_applied_invoice_id = null;
if ( ! empty($credit_note->applied_credits) ) {
    $first_applied_invoice_id = $credit_note->applied_credits[0]['invoice_id'];
}
?>
<div class="col-md-12 no-padding">
    <div class="panel_s">
        <div class="panel-body">
            <div class="horizontal-scrollable-tabs preview-tabs-top panel-full-width-tabs">
                <div class="scroller arrow-left"><i class="fa fa-angle-left"></i></div>
                <div class="scroller arrow-right"><i class="fa fa-angle-right"></i></div>
                <div class="horizontal-tabs">
                    <ul class="nav nav-tabs nav-tabs-horizontal mbot15" role="tablist">
                        <li role="presentation" class="active">
                            <a href="#tab_credit_note" aria-controls="tab_credit_note" role="tab" data-toggle="tab">
                                <?= _l('credit_note'); ?>
                            </a>
                        </li>
                        <li role="presentation">
                            <a href="#invoices_credited" aria-controls="invoices_credited" role="tab" data-toggle="tab">
                                <?= _l('invoices_credited'); ?>
                                <?php if (count($credit_note->applied_credits) > 0) {
                                    echo '<span class="badge">' . count($credit_note->applied_credits) . '</span>';
                                } ?>
                            </a>
                        </li>
                        <li role="presentation">
                            <a href="#tab_refunds" aria-controls="tab_refunds" role="tab" data-toggle="tab">
                                <?= _l('refunds'); ?>
                                <?php if (count($credit_note->refunds) > 0) {
                                    echo '<span class="badge">' . count($credit_note->refunds) . '</span>';
                                } ?>
                            </a>
                        </li>
                        <li role="presentation" class="tab-separator">
                            <a href="#tab_reminders"
                                onclick="initDataTable('.table-reminders', admin_url + 'misc/get_reminders/' + <?= $credit_note->id; ?> + '/' + 'credit_note', undefined, undefined, undefined,[1,'asc']); return false;"
                                aria-controls="tab_reminders" role="tab" data-toggle="tab">
                                <?= _l('reminders'); ?>
                                <?php
$total_reminders = total_rows(
    db_prefix() . 'reminders',
    [
        'isnotified' => 0,
        'staff'      => get_staff_user_id(),
        'rel_type'   => 'credit_note',
        'rel_id'     => $credit_note->id,
    ]
);
if ($total_reminders > 0) {
    echo '<span class="badge">' . $total_reminders . '</span>';
}
?>
                            </a>
                        </li>
                        <li role="presentation" data-toggle="tooltip"
                            title="<?= _l('emails_tracking'); ?>"
                            class="tab-separator">
                            <a href="#tab_emails_tracking" aria-controls="tab_emails_tracking" role="tab"
                                data-toggle="tab">
                                <?php if (! is_mobile()) { ?>
                                <i class="fa-regular fa-envelope-open" aria-hidden="true"></i>
                                <?php } else { ?>
                                <?= _l('emails_tracking'); ?>
                                <?php } ?>
                            </a>
                        </li>
                        <li role="presentation" class="tab-separator toggle_view">
                            <a href="#" onclick="small_table_full_view(); return false;" data-placement="left"
                                data-toggle="tooltip"
                                data-title="<?= _l('toggle_full_view'); ?>">
                                <i class="fa fa-expand"></i></a>
                        </li>
                    </ul>
                </div>
            </div>
            <div class="row mtop20">
                <div class="col-md-3">
                    <?= format_credit_note_status($credit_note->status); ?>
                </div>
                <div class="col-md-9">
                    <div class="visible-xs">
                        <div class="mtop10"></div>
                    </div>
                    <div class="pull-right _buttons">
                        <?php if ($credit_note->status == 1 && ! empty($credit_note->clientid)) { ?>
                        <a href="#" data-toggle="modal" data-target="#apply_credits" class="btn btn-primary">
                            <?= _l('apply_to_invoice'); ?>
                        </a>
                        <?php } ?>
                        <?php if (staff_can('edit', 'credit_notes') && $credit_note->status != 3) { ?>
                        <a href="<?= admin_url('credit_notes/credit_note/' . $credit_note->id); ?>"
                            class="btn btn-default btn-with-tooltip sm:!tw-px-3" data-toggle="tooltip"
                            title="<?= e(_l('edit', _l('credit_note_lowercase'))); ?>"
                            data-placement="bottom">
                            <i class="fa-regular fa-pen-to-square"></i>
                        </a>
                        <?php } ?>
                        <div class="btn-group">
                            <a href="#" class="btn btn-default dropdown-toggle" data-toggle="dropdown"
                                aria-haspopup="true" aria-expanded="false"><i class="fa-regular fa-file-pdf"></i>
                                <?= is_mobile() ? ' PDF' : ''; ?>
                                <span class="caret"></span>
                            </a>
                            <ul class="dropdown-menu dropdown-menu-right">
                                <li class="hidden-xs">
                                    <a
                                        href="<?= admin_url('credit_notes/pdf/' . $credit_note->id . '?output_type=I'); ?>">
                                        <?= _l('view_pdf'); ?>
                                    </a>
                                </li>
                                <li class="hidden-xs">
                                    <a href="<?= admin_url('credit_notes/pdf/' . $credit_note->id . '?output_type=I'); ?>"
                                        target="_blank">
                                        <?= _l('view_pdf_in_new_window'); ?>
                                    </a>
                                </li>
                                <li>
                                    <a
                                        href="<?= admin_url('credit_notes/pdf/' . $credit_note->id); ?>">
                                        <?= _l('download'); ?>
                                    </a>
                                </li>
                                <li>
                                    <a href="<?= admin_url('credit_notes/pdf/' . $credit_note->id . '?print=true'); ?>"
                                        target="_blank">
                                        <?= _l('print'); ?>
                                    </a>
                                </li>
                            </ul>
                        </div>
                        <?php if ($credit_note->status != 3 && ! empty($credit_note->clientid)) { ?>
                        <a href="#" class="credit-note-send-to-client btn btn-default sm:!tw-px-3" data-toggle="modal"
                            data-target="#credit_note_send_to_client_modal">
                            <i class="fa-regular fa-envelope"></i>
                        </a>
                        <?php } ?>
                        <div class="btn-group">
                            <button type="button" class="btn btn-default pull-left dropdown-toggle"
                                data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                <?= _l('more'); ?>
                                <span class="caret"></span>
                            </button>
                            <ul class="dropdown-menu dropdown-menu-right">
                                <?php hooks()->do_action('credit_note_menu_links_start', $credit_note); ?>
                                <?php
                                   if ($credit_note->status == 1 && staff_can('edit', 'credit_notes')) { ?>
                                <li>
                                    <a href="#" onclick="refund_credit_note(); return false;" id="credit_note_refund">
                                        <?= _l('refund'); ?>
                                    </a>
                                </li>
                                <?php }
                                   // You can only mark as void, if it's not closed, not void, no credits applied, no refunds applied
if ($credit_note->status != 2 && $credit_note->status != 3 && ! $credit_note->credits_used && ! $credit_note->total_refunds && staff_can('edit', 'credit_notes')) { ?>
                                <li>
                                    <a
                                        href="<?= admin_url('credit_notes/mark_void/' . $credit_note->id); ?>">
                                        <?= _l('credit_note_status_void'); ?>
                                    </a>
                                </li>
                                <?php } elseif ($credit_note->status == 3 && staff_can('edit', 'credit_notes')) { ?>
                                <li>
                                    <a
                                        href="<?= admin_url('credit_notes/mark_open/' . $credit_note->id); ?>">
                                        <?= _l('credit_note_mark_as_open'); ?>
                                    </a>
                                </li>
                                <?php } ?>
                                <li>
                                    <a href="#" data-toggle="modal" data-target="#sales_attach_file">
                                        <?= _l('invoice_attach_file'); ?>
                                    </a>
                                </li>
                                <?php
if (staff_can('delete', 'credit_notes')) {
    $delete_tooltip = '';
    $allow_delete   = true;
    if ($credit_note->status == 2) {
        $delete_tooltip = _l('credits_applied_cant_delete_status_closed');
        $allow_delete   = false;
    } elseif ($credit_note->credits_used) {
        $delete_tooltip = _l('credits_applied_cant_delete_credit_note');
        $allow_delete   = false;
    } elseif ($credit_note->total_refunds) {
        $allow_delete   = false;
        $delete_tooltip = _l('refunds_applied_cant_delete_credit_note');
    } ?>
                                <li>
                                    <a data-toggle="tooltip"
                                        data-title="<?= e($delete_tooltip); ?>"
                                        href="<?= admin_url('credit_notes/delete/' . $credit_note->id); ?>"
                                        class="text-danger delete-text<?= $allow_delete ? ' _delete' : ''; ?>"
                                        <?= ! $allow_delete ? 'style="cursor:not-allowed;" onclick="return false;"' : ''; ?>>
                                        <?= _l('delete'); ?>
                                    </a>
                                </li>
                                <?php } ?>

                                  <li>
                                    <a data-toggle="tooltip"
                                        data-title="This function to Cancel e-invois Credit Note"
                                        href="<?= admin_url('credit_notes/cancel_document/' . $credit_note->id); ?>"
                                        class="text-danger delete-text"> Cancel Credit Note
                                    </a>
                                </li>
                            </ul>
                        </div>
                          <!--LHDN e-invoice -->
                         <a href="<?= admin_url('credit_notes/process_selected_invoices/' . $credit_note->id . '/' . $first_applied_invoice_id) ;?>"                              
                                class="mleft10 pull-right btn btn-info">
                          
                                Generate Credit Note</a>
                    </div>
                </div>
            </div>
            <div class="clearfix"></div>
            <hr class="hr-panel-separator" />

            <!-- LHDN -->


<?php 
switch ($credit_note->api_status) {
    case 'success':
        $btnClass = 'btn btn-success';
        break;
    case 'cancelled':
        $btnClass = 'btn btn-danger';
        break;
    default:
        $btnClass = 'btn btn-primary';
};?>

 <?php if (! empty($credit_note->submission_uid)): 
    
?>
  <!-- $api is now your array of results -->
   <div class="alert alert-info">

  <table class="table table-bordered" style="margin: 0px !important;">
    <thead>
      <tr>
        <th>Submission UID</th>
        <!-- <th>UUID</th> -->
        <th>Date Time Received (UTC)</th>
        <th>Overall Status</th>
        <th>Check Status</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td><?= html_escape($credit_note->submission_uid) ?></td>      
        <!-- <td><?= html_escape($credit_note->uuid) ?></td> -->
        <td><?= html_escape($credit_note->datecreated) ?></td>
        <td><div class="<?= html_escape($btnClass) ?>"><?= html_escape($credit_note->api_status) ?></div>  </td>
<td>
  <?php if (!empty($credit_note->longID) && !empty($credit_note->uuid)): ?>
    <?php
    $shareUrl = sprintf(
        myinv_portal_base() .'%s/share/%s',
        rawurlencode($credit_note->uuid),
        rawurlencode($credit_note->longID)
    );
    ?>
    <?= anchor(
         $shareUrl,
         'View Credit Note',
         [
           'target' => '_blank',
           'rel'    => 'noopener',
           'class'  => 'btn btn-success'
         ]
       ) ?>
  <?php else: ?>
    <div class="alert alert-warning">
      LongID is empty.
      <?= anchor(
           admin_url('credit_notes/check_document_status/' . $credit_note->id),
           'Click here to generate LongID',
           ['target' => '_blank','class' => 'btn btn-danger btn-sm']
         ) ?>
    </div>
  <?php endif; ?>
</td>

      </tr>
    </tbody>
  </table>


</div>
<?php else: ?>
        <?php
$mode  = strtolower((string) get_option('lhdn_mode')); // 'staging' or 'production'
$color = $mode === 'production' ? 'blue' : 'red';      // default red for non-production
?>
    <div class="alert alert-info">
 <p><strong style="color: <?= $color ?>;">Environment: <?= htmlspecialchars(ucfirst($mode ?: 'staging')) ?></strong><br>The Credit Note has not been generated yet. Kindly click on the <b>Generate Credit Note</b> button to initiate the process</p></div>

<?php endif; ?>

<?php if (empty($credit_note->applied_credits)): ?>
  <div class="alert alert-danger">
    This credit note hasn’t been applied to any invoice. Please apply it before proceeding.
  </div>
<?php endif; ?>

<?php 
$clientId = (int) $credit_note->clientid;
// Read custom fields by their IDs (7 = ID Type, 8 = ID Number in your example)
$idType = get_custom_field_value($clientId, 7, 'customers', false);
$idNo   = get_custom_field_value($clientId, 8, 'customers', false);

if ($idType === '' || $idNo === '' || $credit_note->client->tin === ''): ?>
  <div class="alert alert-danger">
    Please update client details before proceeding to <strong>Generate E-Invoice</strong>.
    Click <a href="<?= admin_url('clients/client/' . $clientId); ?>">here</a> to update.
  </div>
<?php endif; ?>
                    <!-- LHDN e-invoice -->

            <!-- LHDN -->
            <div class="tab-content">
                <div role="tabpanel" class="tab-pane ptop10 active" id="tab_credit_note">
                    <div id="credit-note-preview">
                        <div class="row">
                            <div class="col-md-6 col-sm-6">
                                <h4 class="bold">
                                    <a
                                        href="<?= admin_url('credit_notes/credit_note/' . $credit_note->id); ?>">
                                        <span id="credit-note-number">
                                            <?= e(format_credit_note_number($credit_note->id)); ?>
                                        </span>
                                    </a>
                                </h4>
                                <address class="tw-text-neutral-500">
                                    <?= format_organization_info(); ?>
                                </address>
                            </div>
                            <div class="col-sm-6 text-right">
                                <span
                                    class="bold"><?= _l('credit_note_bill_to'); ?></span>
                                <address class="tw-text-neutral-500">
                                    <?= format_customer_info($credit_note, 'credit_note', 'billing', true); ?>
                                </address>
                                <?php if ($credit_note->include_shipping == 1 && $credit_note->show_shipping_on_credit_note == 1) { ?>
                                <span
                                    class="bold"><?= _l('ship_to'); ?></span>
                                <address class="tw-text-neutral-500">
                                    <?= format_customer_info($credit_note, 'credit_note', 'shipping'); ?>
                                </address>
                                <?php } ?>
                                <p class="no-mbot">
                                    <span class="bold">
                                        <?= _l('credit_note_date'); ?>:
                                    </span>
                                    <?= _d($credit_note->date)?>
                                </p>
                                <?php if (! empty($credit_note->reference_no)) { ?>
                                <p class="no-mbot">
                                    <span
                                        class="bold"><?= _l('reference_no'); ?>:</span>
                                    <?= e($credit_note->reference_no); ?>
                                </p>
                                <?php } ?>
                                <?php if ($credit_note->project_id && get_option('show_project_on_credit_note') == '1') { ?>
                                <p class="no-mbot">
                                    <span
                                        class="bold"><?= _l('project'); ?>:</span>
                                    <?= e(get_project_name_by_id($credit_note->project_id)); ?>
                                </p>
                                <?php } ?>
                                <?php $pdf_custom_fields = get_custom_fields('credit_note', ['show_on_pdf' => 1]); ?>
                                <?php foreach ($pdf_custom_fields as $field) {
                                    $value = get_custom_field_value($credit_note->id, $field['id'], 'credit_note');
                                    if ($value == '') {
                                        continue;
                                    } ?>
                                <p class="no-mbot">
                                    <span
                                        class="bold"><?= e($field['name']); ?>:
                                    </span>
                                    <?= $value; ?>
                                </p>
                                <?php } ?>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                <div class="table-responsive">
                                    <?php $items = get_items_table_data($credit_note, 'credit_note', 'html', true); ?>
                                    <?= $items->table(); ?>
                                </div>
                            </div>
                            <div class="col-md-5 col-md-offset-7">
                                <table class="table text-right">
                                    <tbody>
                                        <tr id="subtotal">
                                            <td>
                                                <span class="tw-font-medium tw-text-neutral-700">
                                                    <?= _l('credit_note_subtotal'); ?>
                                                </span>
                                            </td>
                                            <td class="subtotal">
                                                <?= e(app_format_money($credit_note->subtotal, $credit_note->currency_name)); ?>
                                            </td>
                                        </tr>
                                        <?php if (is_sale_discount_applied($credit_note)) { ?>
                                        <tr>
                                            <td>
                                                <span class="tw-font-medium tw-text-neutral-700">
                                                    <?= _l('credit_note_discount'); ?>
                                                    <?php if (is_sale_discount($credit_note, 'percent')) { ?>
                                                    (<?= e(app_format_number($credit_note->discount_percent, true)); ?>%)
                                                    <?php } ?>
                                                </span>
                                            </td>
                                            <td class="discount">
                                                <?= e('-' . app_format_money($credit_note->discount_total, $credit_note->currency_name)); ?>
                                            </td>
                                        </tr>
                                        <?php } ?>
                                        <?php foreach ($items->taxes() as $tax) {
                                            echo '<tr class="tax-area"><td class="tw-font-medium !tw-text-neutral-700">' . e($tax['taxname']) . ' (' . e(app_format_number($tax['taxrate'])) . '%)</td><td>' . e(app_format_money($tax['total_tax'], $credit_note->currency_name)) . '</td></tr>';
                                        } ?>
                                        <?php if ((int) $credit_note->adjustment != 0) { ?>
                                        <tr>
                                            <td>
                                                <span
                                                    class="tw-font-medium tw-text-neutral-700"><?= _l('credit_note_adjustment'); ?></span>
                                            </td>
                                            <td class="adjustment">
                                                <?= e(app_format_money($credit_note->adjustment, $credit_note->currency_name)); ?>
                                            </td>
                                        </tr>
                                        <?php } ?>
                                        <tr>
                                            <td>
                                                <span
                                                    class="tw-font-medium tw-text-neutral-700"><?= _l('credit_note_total'); ?></span>
                                            </td>
                                            <td class="total">
                                                <?= e(app_format_money($credit_note->total, $credit_note->currency_name)); ?>
                                            </td>
                                        </tr>
                                        <?php if ($credit_note->credits_used) { ?>
                                        <tr>
                                            <td>
                                                <span class="tw-font-medium tw-text-neutral-700">
                                                    <?= _l('credits_used'); ?>
                                                </span>
                                            </td>
                                            <td>
                                                <?= e('-' . app_format_money($credit_note->credits_used, $credit_note->currency_name)); ?>
                                            </td>
                                        </tr>
                                        <?php } ?>
                                        <?php if ($credit_note->total_refunds) { ?>
                                        <tr>
                                            <td>
                                                <span class="tw-font-medium tw-text-neutral-700">
                                                    <?= _l('refund'); ?>
                                                </span>
                                            </td>
                                            <td>
                                                <?= e('-' . app_format_money($credit_note->total_refunds, $credit_note->currency_name)); ?>
                                            </td>
                                        </tr>
                                        <?php } ?>
                                        <tr>
                                            <td>
                                                <span class="tw-font-medium tw-text-neutral-700">
                                                    <?= _l('credits_remaining'); ?>
                                                </span>
                                            </td>
                                            <td>
                                                <?= e(app_format_money($credit_note->remaining_credits, $credit_note->currency_name)); ?>
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                            <?php if ($credit_note->clientnote != '') { ?>
                            <div class="col-md-12 mtop15">
                                <p class="tw-text-neutral-700 tw-font-medium">
                                    <?= _l('credit_note_client_note'); ?>
                                </p>
                                <div class="tw-text-neutral-500 tw-leading-relaxed">
                                    <?= process_text_content_for_display($credit_note->clientnote); ?>
                                </div>
                            </div>
                            <?php } ?>
                            <?php if ($credit_note->terms != '') { ?>
                            <div class="col-md-12 mtop15">
                                <p class="tw-text-neutral-700 tw-font-medium">
                                    <?= _l('terms_and_conditions'); ?>
                                </p>
                                <div class="tw-text-neutral-500 tw-leading-relaxed">
                                    <?= process_text_content_for_display($credit_note->terms); ?>
                                </div>
                            </div>
                            <?php } ?>
                        </div>
                        <?php
                                                          if (count($credit_note->attachments) > 0) { ?>
                        <div class="clearfix"></div>
                        <hr />
                        <p class="tw-text-neutral-700 tw-font-medium">
                            <?= _l('credit_note_files'); ?>
                        </p>
                        <?php foreach ($credit_note->attachments as $attachment) {
                            $attachment_url = site_url('download/file/sales_attachment/' . $attachment['attachment_key']);
                            if (! empty($attachment['external'])) {
                                $attachment_url = $attachment['external_link'];
                            } ?>
                        <div class="mbot15 row inline-block full-width"
                            data-attachment-id="<?= e($attachment['id']); ?>">
                            <div class="col-md-8">
                                <a href="<?= e($attachment_url); ?>"
                                    target="_blank"><?= e($attachment['file_name']); ?></a>
                                <br />
                                <small class="text-muted">
                                    <?= e($attachment['filetype']); ?></small>
                            </div>
                            <div class="col-md-4 text-right">
                                <?php if ($attachment['staffid'] == get_staff_user_id() || is_admin()) { ?>
                                <a href="#" class="text-muted"
                                    onclick="delete_credit_note_attachment(<?= e($attachment['id']); ?>); return false;">
                                    <i class="fa-regular fa-trash-can"></i>
                                </a>
                                <?php } ?>
                            </div>
                        </div>
                        <?php } ?>
                        <?php } ?>
                    </div>
                </div>
                <div role="tabpanel" class="tab-pane" id="invoices_credited">
                    <?php if (count($credit_note->applied_credits) == 0) {
                        echo '<div class="alert alert-info no-mbot">';
                        echo _l('credited_invoices_not_found');
                        echo '</div>';
                    } else { ?>
                    <table class="table table-bordered no-mtop">
                        <thead>
                            <tr>
                                <th><span
                                        class="bold"><?= _l('credit_invoice_number'); ?></span>
                                </th>
                                <th><span
                                        class="bold"><?= _l('amount_credited'); ?></span>
                                </th>
                                <th><span
                                        class="bold"><?= _l('credit_date'); ?></span>
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($credit_note->applied_credits as $credit) { ?>
                            <tr>
                                <td>
                                    <a
                                        href="<?= admin_url('invoices/list_invoices/' . $credit['invoice_id']); ?>">
                                        <?= e(format_invoice_number($credit['invoice_id'])); ?>
                                    </a>
                                </td>
                                <td>
                                    <?= e(app_format_money($credit['amount'], $credit_note->currency_name)); ?>
                                </td>
                                <td>
                                    <?= e(_d($credit['date'])); ?>
                                    <?php if (staff_can('delete', 'credit_notes')) { ?>
                                    <a href="<?= admin_url('credit_notes/delete_credit_note_applied_credit/' . $credit['id'] . '/' . $credit['credit_id'] . '/' . $credit['invoice_id']); ?>"
                                        class="pull-right text-danger _delete"><i class="fa fa-trash"></i></a>
                                    <?php } ?>
                                </td>
                            </tr>
                            <?php } ?>
                        </tbody>
                    </table>
                    <?php }  ?>
                </div>
                <div role="tabpanel" class="tab-pane ptop10" id="tab_emails_tracking">
                    <?php
                  $this->load->view(
                      'admin/includes/emails_tracking',
                      [
                          'tracked_emails' => get_tracked_emails($credit_note->id, 'credit_note'), ]
                  );
?>
                </div>
                <div role="tabpanel" class="tab-pane" id="tab_refunds">
                    <?php if (count($credit_note->refunds) == 0) {
                        echo '<div class="alert alert-info no-mbot">';
                        echo _l('not_refunds_found');
                        echo '</div>';
                    } else { ?>
                    <table class="table table-bordered no-mtop">
                        <thead>
                            <tr>
                                <th><span
                                        class="bold"><?= _l('credit_date'); ?></span>
                                </th>
                                <th><span
                                        class="bold"><?= _l('refund_amount'); ?></span>
                                </th>
                                <th><span
                                        class="bold"><?= _l('payment_mode'); ?></span>
                                </th>
                                <th><span
                                        class="bold"><?= _l('note'); ?></span>
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($credit_note->refunds as $refund) { ?>
                            <tr>
                                <td>
                                    <?= e(_d($refund['refunded_on'])); ?>
                                </td>
                                <td>
                                    <?= e(app_format_money($refund['amount'], $credit_note->currency_name)); ?>
                                </td>
                                <td>
                                    <?= e($refund['payment_mode_name']); ?>
                                </td>
                                <td>
                                    <?php if (staff_can('delete', 'credit_notes')) { ?>
                                    <a href="<?= admin_url('credit_notes/delete_refund/' . $refund['id'] . '/' . $refund['credit_note_id']); ?>"
                                        class="pull-right text-danger _delete">
                                        <i class="fa fa-trash"></i>
                                    </a>
                                    <?php } ?>
                                    <?php if (staff_can('edit', 'credit_notes')) { ?>
                                    <a href="#"
                                        onclick="edit_refund(<?= e($refund['id']); ?>); return false;"
                                        class="pull-right mright5">
                                        <i class="fa-regular fa-pen-to-square"></i>
                                    </a>
                                    <?php } ?>
                                    <?= e($refund['note']); ?>
                                </td>
                            </tr>
                            <?php } ?>
                        </tbody>
                    </table>
                    <?php }  ?>
                </div>
                <div role="tabpanel" class="tab-pane" id="tab_reminders">
                    <a href="#" class="btn btn-primary" data-toggle="modal"
                        data-target=".reminder-modal-credit_note-<?= e($credit_note->id); ?>"><i
                            class="fa-regular fa-bell"></i>
                        <?= _l('credit_note_set_reminder_title'); ?></a>
                    <hr />
                    <?php render_datatable([_l('reminder_description'), _l('reminder_date'), _l('reminder_staff'), _l('reminder_is_notified')], 'reminders'); ?>
                    <?php $this->load->view('admin/includes/modals/reminder', ['id' => $credit_note->id, 'name' => 'credit_note', 'members' => $members, 'reminder_title' => _l('credit_note_set_reminder_title')]); ?>
                </div>
            </div>
        </div>
    </div>
</div>
</div>
<?php $this->load->view('admin/credit_notes/send_to_client'); ?>
<?php $this->load->view('admin/credit_notes/apply_credits_to_invoices'); ?>
<script>
    init_items_sortable(true);
    init_btn_with_tooltips();
    init_datepicker();
    init_selectpicker();
    init_form_reminder();
    init_tabs_scrollable();

    function refund_credit_note() {
        var credit_note_id = <?= e($credit_note->id); ?> ;
        $('#credit_note').load(admin_url + 'credit_notes/refund/' + credit_note_id);
    }

    function edit_refund(refund_id) {
        var credit_note_id = <?= e($credit_note->id); ?> ;
        $('#credit_note').load(admin_url + 'credit_notes/refund/' + credit_note_id + '/' + refund_id);
    }
</script>