<?= $this->extend('layouts/main') ?>
<?= $this->section('content') ?>

<style>
    .table thead th {
        background-color: #dee2e6;
        color: #495057;
    }

    .card-header {
        background: #6c757d;
    }

    .modal-header {
        background: #0d6efd;
    }

    .modal-header .modal-title {
        color: white;
    }

    .btn {
        border-radius: 6px;
    }

    .btn-sm {
        padding: 4px 10px;
    }

    @media (max-width: 768px) {
        .table-responsive {
            font-size: 14px;
        }

        .modal-dialog {
            max-width: 95%;
        }
    }
</style>

<div class="container-fluid">
    <h4 class="mb-3 text-primary">Stok Masuk</h4>

    <a href="<?= base_url('admin/product_in/create') ?>" class="btn btn-success mb-3 shadow-sm">
        <i class="fas fa-plus-circle"></i> Tambah Stok Masuk
    </a>

    <?php if (session()->getFlashdata('success')): ?>
        <div class="alert alert-success shadow-sm"><?= session()->getFlashdata('success') ?></div>
    <?php endif ?>

    <div class="card shadow-sm border-0">
        <div class="card-header text-white">Riwayat Stok Masuk</div>
        <div class="card-body table-responsive">
            <table class="table table-bordered table-striped align-middle">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Nama Barang</th>
                        <th>Qty</th>
                        <th>Tanggal Masuk</th>
                        <th>Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    <?php $no = 1; foreach ($data as $row): ?>
                        <tr>
                            <td><?= $no++ ?></td>
                            <td><?= esc($row['item_name']) ?></td>
                            <td><?= esc($row['qty']) ?></td>
                            <td><?= date('d-m-Y', strtotime($row['tanggal_masuk'])) ?></td>
                            <td>
                                <button class="btn btn-sm btn-info" data-bs-toggle="modal" data-bs-target="#detailModal<?= $row['id'] ?>" title="Detail">
                                    <i class="fas fa-eye"></i>
                                </button>
                                <a href="<?= base_url('admin/product_in/edit/' . $row['id']) ?>" class="btn btn-sm btn-warning" title="Edit">
                                    <i class="fas fa-edit"></i>
                                </a>
                                <a href="<?= base_url('admin/product_in/delete/' . $row['id']) ?>" class="btn btn-sm btn-danger" onclick="return confirm('Hapus data ini?')" title="Hapus">
                                    <i class="fas fa-trash-alt"></i>
                                </a>
                            </td>
                        </tr>
                    <?php endforeach ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<!-- === MODAL DETAIL === -->
<?php foreach ($data as $row): ?>
<div class="modal fade" id="detailModal<?= $row['id'] ?>" tabindex="-1" aria-labelledby="modalLabel<?= $row['id'] ?>" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered modal-lg">
    <div class="modal-content shadow">
      <div class="modal-header">
        <h5 class="modal-title" id="modalLabel<?= $row['id'] ?>">Detail Stok Masuk</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Tutup"></button>
      </div>
      <div class="modal-body">
        <table class="table table-borderless">
          <tr>
            <th style="width: 30%;">Kode</th>
            <td><?= esc($row['kode']) ?></td>
          </tr>
          <tr>
            <th>Nama Barang</th>
            <td><?= esc($row['item_name']) ?></td>
          </tr>
          <tr>
            <th>Barcode</th>
            <td><?= esc($row['barcode']) ?></td>
          </tr>
          <tr>
            <th>Supplier</th>
            <td><?= esc($row['nama_supplier']) ?></td>
          </tr>
          <tr>
            <th>Gudang</th>
            <td><?= esc($row['nama_gudang']) ?></td>
          </tr>
          <tr>
            <th>Qty</th>
            <td><?= esc($row['qty']) ?></td>
          </tr>
          <tr>
            <th>Tanggal Masuk</th>
            <td><?= date('d-m-Y', strtotime($row['tanggal_masuk'])) ?></td>
          </tr>
          <tr>
            <th>User Input</th>
            <td><?= esc($row['user_name'] ?? '-') ?></td>
          </tr>
        </table>
      </div>
    </div>
  </div>
</div>
<?php endforeach; ?>

<?= $this->endSection() ?>
