<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<?php init_head(); ?>

<div id="wrapper">
  <div class="content">
    <div class="panel_s">
      <div class="panel-body">

        <div class="tw-flex tw-justify-between tw-items-center">
          <h4 class="no-margin">Comprobantes QR Bolivia</h4>
          <a href="<?php echo admin_url('kaiteki_qr_payments/settings'); ?>" class="btn btn-default">
            <i class="fa fa-cog"></i> Configurar QR
          </a>
        </div>

        <hr>

        <div class="table-responsive">
          <table class="table dt-table">
            <thead>
              <tr>
                <th>ID</th>
                <th>Fecha</th>
                <th>Cliente / Estudiante</th>
                <th>Factura</th>
                <th>Monto</th>
                <th>Comprobante</th>
                <th>Estado</th>
                <th>Acción</th>
              </tr>
            </thead>

            <tbody>
              <?php foreach ($receipts as $r) { ?>

                <?php
                  $cliente = '';

                  if (!empty($r->firstname) || !empty($r->lastname)) {
                      $cliente = trim($r->firstname . ' ' . $r->lastname);
                  }

                  if (empty($cliente) && !empty($r->company)) {
                      $cliente = $r->company;
                  }

                  if (empty($cliente)) {
                      $cliente = 'Cliente #' . $r->client_id;
                  }

                  $invoice_number = '#' . $r->prefix . str_pad($r->number, get_option('number_padding_prefixes'), '0', STR_PAD_LEFT);
                ?>

                <tr>
                  <td><?php echo (int) $r->id; ?></td>

                  <td><?php echo _dt($r->created_at); ?></td>

                  <td>
                    <strong><?php echo html_escape($cliente); ?></strong><br>
                    <small class="text-muted">ID cliente: <?php echo (int) $r->client_id; ?></small>
                  </td>

                  <td>
                    <a href="<?php echo admin_url('invoices/list_invoices/' . $r->invoice_id); ?>">
                      <?php echo html_escape($invoice_number); ?>
                    </a>
                  </td>

                  <td><?php echo app_format_money($r->amount, get_base_currency()); ?></td>

                  <td>
                    <?php if (!empty($r->receipt_file)) { ?>
                      <a target="_blank" class="btn btn-default btn-xs"
                         href="<?php echo module_dir_url(KAITEKI_QR_PAYMENTS_MODULE_NAME, 'uploads/' . $r->receipt_file); ?>">
                        <i class="fa fa-file"></i> Ver archivo
                      </a>
                    <?php } else { ?>
                      <span class="text-muted">Sin archivo</span>
                    <?php } ?>
                  </td>

                  <td>
                    <?php if ($r->status == 'pending') { ?>
                      <span class="label label-warning">En revisión</span>
                    <?php } elseif ($r->status == 'approved') { ?>
                      <span class="label label-success">Aprobado</span>
                    <?php } elseif ($r->status == 'rejected') { ?>
                      <span class="label label-danger">Rechazado</span>
                    <?php } else { ?>
                      <span class="label label-default"><?php echo html_escape($r->status); ?></span>
                    <?php } ?>
                  </td>

                  <td>
                    <?php if ($r->status == 'pending') { ?>

                      <button class="btn btn-success btn-xs" data-toggle="modal" data-target="#approve_<?php echo (int) $r->id; ?>">
                        Aprobar
                      </button>

                      <button class="btn btn-danger btn-xs" data-toggle="modal" data-target="#reject_<?php echo (int) $r->id; ?>">
                        Rechazar
                      </button>

                      <div class="modal fade" id="approve_<?php echo (int) $r->id; ?>" tabindex="-1">
                        <div class="modal-dialog">
                          <div class="modal-content">
                            <?php echo form_open(admin_url('kaiteki_qr_payments/approve/' . $r->id)); ?>

                            <div class="modal-header">
                              <button type="button" class="close" data-dismiss="modal">&times;</button>
                              <h4 class="modal-title">Aprobar comprobante #<?php echo (int) $r->id; ?></h4>
                            </div>

                            <div class="modal-body">
                              <p>
                                <strong>Cliente:</strong> <?php echo html_escape($cliente); ?><br>
                                <strong>Factura:</strong> <?php echo html_escape($invoice_number); ?>
                              </p>

                              <?php echo render_input('amount', 'Monto a aplicar', $r->amount, 'number', ['step' => '0.01']); ?>
                              <?php echo render_textarea('admin_note', 'Nota interna'); ?>
                            </div>

                            <div class="modal-footer">
                              <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
                              <button type="submit" class="btn btn-success">Aprobar y registrar pago</button>
                            </div>

                            <?php echo form_close(); ?>
                          </div>
                        </div>
                      </div>

                      <div class="modal fade" id="reject_<?php echo (int) $r->id; ?>" tabindex="-1">
                        <div class="modal-dialog">
                          <div class="modal-content">
                            <?php echo form_open(admin_url('kaiteki_qr_payments/reject/' . $r->id)); ?>

                            <div class="modal-header">
                              <button type="button" class="close" data-dismiss="modal">&times;</button>
                              <h4 class="modal-title">Rechazar comprobante #<?php echo (int) $r->id; ?></h4>
                            </div>

                            <div class="modal-body">
                              <p>
                                <strong>Cliente:</strong> <?php echo html_escape($cliente); ?><br>
                                <strong>Factura:</strong> <?php echo html_escape($invoice_number); ?>
                              </p>

                              <?php echo render_textarea('admin_note', 'Motivo / nota interna'); ?>
                            </div>

                            <div class="modal-footer">
                              <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
                              <button type="submit" class="btn btn-danger">Rechazar</button>
                            </div>

                            <?php echo form_close(); ?>
                          </div>
                        </div>
                      </div>

                    <?php } else { ?>
                      -
                    <?php } ?>
                  </td>
                </tr>

              <?php } ?>
            </tbody>
          </table>
        </div>

      </div>
    </div>
  </div>
</div>

<?php init_tail(); ?>