<?php include_once __DIR__ . '/../layouts/head.php'; ?>

<div class="container py-4" style="max-width: 1350px; width: 100%;">
  <div class="row">
    <div class="col-lg-12">
      <div class="shadow-sm bg-white rounded overflow-hidden border">
        <div class="p-0">
          <div class="row px-0 m-0">

            <?php include_once __DIR__ . '/../layouts/menu_body.php'; ?>

            <div class="col-12 col-md-9 px-0">
              <div class="tab-content" id="v-pills-tabContent">

                <div class="tab-pane fade show active" id="v-pills-home" role="tabpanel">
                  <div class="p-4">
                    <div class="mb-4">
                      <h5 class="mb-0 fw-bold">Upload Token</h5>
                    </div>

                    <?php if (!empty($_SESSION['pesan'])): ?>
                      <div class="alert alert-info" id="flash-message"><?= htmlspecialchars($_SESSION['pesan']) ?></div>
                      <script>
                        setTimeout(function () {
                          var msg = document.getElementById('flash-message');
                          if (msg) msg.style.display = 'none';
                        }, 10000);
                      </script>
                      <?php $_SESSION['pesan'] = ''; ?>
                    <?php endif; ?>

                    <div class="mb-3">

                        <form method="post" action="/dashboard/pegawai-uploadstaftoken" enctype="multipart/form-data">
                          <input type="file" name="excelFile" required class="form-control"/>
                          </br>
                          <button type="submit" name="uploadstaftoken" class="btn btn-warning">Upload</button>
                          <a href="/download_format?file=Format_Upload_Token.xlsx" class="text-danger text-decoration-none ms-3">
                            <i class="bi bi-file-earmark-bar-graph"></i> Download Format
                          </a>
                        </form>
                    </div>

                    <hr />
                    <!--
                    <h6 class="fw-bold mt-4">Daftar Token</h6>
                    <div class="table-responsive">
                      <table class="table table-striped table-bordered">
                        <thead class="table-dark">
                          <tr>
                            <th>No</th>
                            <th>Token</th>
                            <th>Kegiatan</th>
                          </tr>
                        </thead>
                        <tbody>
                          <?php
                          $stmt = $this->db->query("SELECT * FROM tb_token ORDER BY id_token DESC");
                          $tokens = $stmt->fetchAll(PDO::FETCH_ASSOC);
                          if (empty($tokens)):
                          ?>
                            <tr><td colspan="3" class="text-center text-muted">Belum ada data token</td></tr>
                          <?php else: $no = 1; foreach ($tokens as $t): ?>
                            <tr>
                              <td><?= $no++ ?></td>
                              <td><?= htmlspecialchars($t['token']) ?></td>
                              <td><?= htmlspecialchars($t['kegiatan']) ?></td>
                            </tr>
                          <?php endforeach; endif; ?>
                        </tbody>
                      </table>
                    </div>
                    -->
                  </div>
                </div>

              </div>
            </div>

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

<script src="https://cdn.sheetjs.com/xlsx-latest/package/dist/xlsx.full.min.js"></script>
<script>
document.getElementById('uploadBtn').addEventListener('click', function(e) {
    e.preventDefault();
    const fileInput = document.getElementById('excelFile');
    if (!fileInput.files.length) {
        alert('Pilih file Excel dulu ya');
        return;
    }
    const file = fileInput.files[0];
    const reader = new FileReader();
    reader.onload = function(e) {
        const data = new Uint8Array(e.target.result);
        const workbook = XLSX.read(data, {type: 'array'});
        const sheetName = workbook.SheetNames[0];
        const worksheet = workbook.Sheets[sheetName];
        const jsonData = XLSX.utils.sheet_to_json(worksheet, {header:1});

        // ✅ URL otomatis sesuai domain saat ini, tanpa BASE_URL
        fetch(window.location.origin + '/dashboard/pegawai-uploadstaftoken/ajax', {
            method: 'POST',
            headers: {'Content-Type': 'application/json'},
            body: JSON.stringify({data: jsonData})
        })
        .then(res => res.json())
        .then(res => {
            alert(res.message);
            if (res.success) {
                window.location.reload();
            }
        })
        .catch(err => {
            console.error(err);
            alert('Error: ' + err.message);
        });
    };
    reader.readAsArrayBuffer(file);
});
</script>

<?php include_once __DIR__ . '/../layouts/footer.php'; ?>
