<?= $this->session->flashdata('pesan'); ?>
<div class="card shadow-sm border-bottom-primary">
    <div class="card-header bg-white py-3">
        <div class="row">
            <div class="col">
                <h4 class="h5 align-middle m-0 font-weight-bold text-primary">
                    Data <?= $title; ?>
                </h4>
            </div>
        </div>
    </div>
    <div class="card-body">
        
        <?= form_open(); ?>

        <div class="table-responsive mb-3">
            <table class="table table-striped" id="dataTable">
                <thead>
                    <tr>
                        <th rowspan="2">No.</th>
                        <th rowspan="2">Tanggal</th>
                        <th rowspan="2">Username</th>
                        <?php if($dt_res->stts == 3) { ?>
                            <th rowspan="2">Asal Tujuan</th>
                            <th rowspan="2">Lokasi Tujuan</th>
                        <?php } else {  ?>
                            <th rowspan="2">Lokasi</th>
                        <?php } ?>
                        <th colspan="6" class="text-left">Detail Barang</th>
                        <th rowspan="2">Ket. Tambahan</th>
                    </tr>
                    <tr>
                        <th>ID Barang</th>
                        <th>Nama Barang</th>
                        <th>Jumlah</th>
                        <th>Stok Awal</th>
                        <th>Stok Akhir</th>
                        <th>Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    
                    <?php
                    $no = 1;
                    if ($dt_res) :
                        foreach ($dt_res->dt_impor as $group) : ?>
                            <?php $rowspan = count($group->dt_array); ?>
                            <?php $i = 0; ?>
                            <?php foreach ($group->dt_array as $row) : 
                                    $array_detail = base64_encode(json_encode($row));
                                ?>
                                    <tr>
                                        <?php if ($i == 0) : ?>
                                            <td rowspan="<?= $rowspan; ?>"><?= $no++; ?></td>
                                            <td rowspan="<?= $rowspan; ?>"><?= $group->tgl; ?></td>
                                            <td rowspan="<?= $rowspan; ?>"><?= $group->username; ?></td>
                                            <?php if($dt_res->stts == 3) { ?>
                                                <td rowspan="<?= $rowspan; ?>"><?= $group->asal_nm_cabang; ?></td>
                                                <td rowspan="<?= $rowspan; ?>"><?= $group->tujuan_nm_cabang; ?></td>
                                            <?php } else {  ?>
                                                <td rowspan="<?= $rowspan; ?>"><?= $group->nama_cabang; ?></td>
                                            <?php } ?>
                                        <?php endif; ?>

                                        <td><?= $row->id_barang; ?></td>
                                        <td><?= $row->nama_barang; ?></td>
                                        <td><span id="jumlah"><?= $row->jumlah; ?></span></td>
                                        <td>
                                            <span id="stok_awal">Stok Awal</span>
                                        </td>
                                        <td>
                                            <span id="stok_akhir">Stok Akhir</span>
                                        </td>
                                        <td>
                                            <input type="hidden" class="form-control" id="dt_array" name="dt_array[]" value="<?=$array_detail; ?>">


                                            <input type="hidden" class="form-control" id="user_id" name="user_id[]" value="<?= $group->id_user; ?>">
                                            <input type="hidden" class="form-control" id="tgl" name="tgl[]" value="<?= $group->tgl; ?>">


                                            <?php if($dt_res->stts == 3) { ?>
                                                <input type="hidden" class="form-control" id="asal_cabang" name="asal_cabang[]" value="<?= $group->asal_cabang; ?>">
                                                <input type="hidden" class="form-control" id="tujuan_cabang" name="tujuan_cabang[]" value="<?= $group->tujuan_cabang; ?>">
                                            <?php } else {  ?>
                                                <input type="hidden" class="form-control" id="cabang_id" name="cabang_id[]" value="<?= $group->cabang_id; ?>">
                                            <?php } ?>
                                                <input type="hidden" class="form-control" id="ket" name="ket[]" value="<?= $group->ket; ?>">


                                            <input type="hidden" class="form-control" id="id_detail_bm" name="id_detail_bm[]">
                                            <input type="hidden" class="form-control" id="user_id_pemilik" name="user_id_pemilik[]" value="<?= $row->id_user_pemilik; ?>">
                                            <input type="hidden" class="form-control" id="id_barang" name="id_barang[]" value="<?= $row->id_barang; ?>">
                                            <input type="hidden" class="form-control" id="jumlah" name="jumlah[]" value="<?= $row->jumlah; ?>">

                                            <input type="checkbox" id="tandaSet">
                                            <button type="button" 
                                                    class="btn btn-success" 
                                                    data-toggle="modal" 
                                                    data-target="#modalUpdateDetailBM" 
                                                    onclick="cariData(this)" 
                                                    data-id_user_pemilik="<?= $row->id_user_pemilik; ?>"
                                                    data-id_barang="<?= $row->id_barang; ?>"
                                                    data-tgl="<?= $group->tgl; ?>"
                                                >
                                              <i class="fas fa-plus"></i>
                                            </button>
                                        </td>

                                        <?php if ($i == 0) : ?>
                                            <td rowspan="<?= $rowspan; ?>"><?= $group->ket; ?></td>
                                        <?php endif; ?>
                                    </tr>
                                <?php $i++; ?>
                            <?php endforeach; ?>
                        <?php endforeach; ?>
                    <?php else : ?>
                        <tr>
                            <td colspan="14" class="text-center">
                                Data Kosong
                            </td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
        
        <input type="hidden" name="tgl_save" id="tgl_save" value="<?= $now; ?>">
        <div class="row form-group">
            <div class="col-md-9">
                <button type="submit" class="btn btn-primary">Proses Impor</button>
            </div>
        </div>
        <?= form_close(); ?>
    </div>
</div>




<!-- Modal -->
<div class="modal fade" id="modalUpdateDetailBM" tabindex="-1" role="dialog">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header bg-primary text-white">
        <h5 class="modal-title">Review</h5>
        <button type="button" class="close text-white" data-dismiss="modal">&times;</button>
      </div>
      <div class="modal-body">

        <!-- Tabel daftar barang -->
        <div id="tabelBarang" style="display:none;">
            <!-- Input Pencarian -->
            <input type="text" id="searchBarang" class="form-control mb-2" placeholder="Cari barang...">
            <table class="table table-bordered table-striped">
                <thead class="thead-light">
                  <tr>
                    <th>ID Barang Masuk</th>
                    <th>Tanggal Masuk</th>
                    <th>Nama Barang</th>
                    <th>Sisa Stok</th>
                    <th>Merk</th>
                    <th>Spesifikasi</th>
                    <th>Barcode</th>
                    <th>Serial Number</th>
                    <th>Aksi</th>
                  </tr>
                </thead>
                <tbody id="dataBarang"></tbody>
            </table>
        </div>

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




<script>
  // Cari data barang berdasarkan user
    let currentRow = null;

    function cariData(btn) {

      currentRow = btn.closest("tr");

      let userId = btn.getAttribute('data-id_user_pemilik');  // ambil dari data-user
      let barangId  = btn.getAttribute('data-id_barang');   // ambil dari data-tgl
      let tgl    = btn.getAttribute('data-tgl');   // ambil dari data-tgl


      let tbody = document.getElementById("dataBarang");
      tbody.innerHTML = "";

      $.ajax({
        url: '<?= base_url('barangmasuk/get_barang_masuk_by_user2') ?>', // arahkan ke controller
        type: "POST",
        data: { 
            user_id: userId,
            tgl: tgl,
            barangId: barangId,
        },
        dataType: "json",
        success: function(response) {
            
          if (response.length > 0) {
            response.forEach((b, i) => {
              let tr = `<tr>
                          <td>${b.id_barang_masuk}</td>
                          <td>${b.tanggal_masuk}</td>
                          <td>${b.nama_barang}</td>
                          <td>${b.saldo_stok}</td>
                          <td>${b.merk}</td>
                          <td>${b.spesifikasi}</td>
                          <td>${b.barcode}</td>
                          <td>${b.sn}</td>
                          <td>
                            <button class="btn btn-sm btn-primary" 
                              onclick="pilihBarang('${b.id_detail}')">
                              Pilih
                            </button>
                          </td>
                        </tr>`;
              tbody.innerHTML += tr;
            });
          } else {
            tbody.innerHTML = `<tr><td colspan="8" class="text-center">Tidak ada data barang</td></tr>`;
          }

          document.getElementById("tabelBarang").style.display = "block";
        },
        error: function(xhr, status, error) {
          console.error(error);
          alert("Gagal mengambil data barang!");
        }
      });
    }


    function pilihBarang(idDetailBm) {
        if (currentRow) {
            let input = currentRow.querySelector("#id_detail_bm");
            if (input) {
                input.value = idDetailBm;
            }

            // centang checkbox tandaSet kalau ada
            let checkbox = currentRow.querySelector("#tandaSet");
            if (checkbox) {
                checkbox.checked = true;
            }

            // tutup modal setelah pilih
            $('#modalUpdateDetailBM').modal('hide');
        }
    }
</script>