<?php
ob_start();
include_once '../db/koneksi.php';

if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

if (!isset($_SESSION['kode_admin'])) {
    echo '
    <script>
        alert("Terjadi kesalahan, harap masuk kembali!");
        window.location.replace("' . $alamat_admin . 'keluar.php");
    </script>
    ';
    exit();
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $extplayer   = $_POST['extplayer'];
    $username    = $_POST['username'];
    $ref         = $_POST['ref'];
    $jumlah      = $_POST['jumlah'];
    $bonus       = $_POST['bonus'];
    $bukti       = $_POST['bukti'];
    $bank_asal   = $_POST['bank_asal'];
    $bank_tujuan = $_POST['bank_tujuan'];
    $status      = $_POST['status'];

    $query = mysqli_query($koneksi, "INSERT INTO tb_deposit (extplayer, username, ref, jumlah, bonus, bukti, bank_asal, bank_tujuan, status, created_at, updated_at)
                VALUES ('$extplayer', '$username', '$ref', '$jumlah', '$bonus', '$bukti', '$bank_asal', '$bank_tujuan', '$status', NOW(), NOW())");

    if (!$query) {
        die('Gagal menyimpan data deposit: ' . mysqli_error($koneksi));
    } else {
        echo '
        <script>
            alert("Data deposit berhasil disalin.");
            window.location.replace("' . $alamat_admin . 'deposit");
        </script>
        ';
    }
}
?>

<div class="container-xxl flex-grow-1 container-p-y">
  <div class="row gy-4 mb-4">
    <div class="col-md-6">
      <div class="fw-bold fs-4 text-center text-md-start">Deposit</div>
    </div>
    <div class="col-md-6 text-center text-md-end">
      <span><?php echo ucapan() . ', ' . tanggalIndonesia(date('Y-m-d'), true) . ', '; ?></span>
      <span id="jam_sekarang">Jam </span>
    </div>
  </div>

  <div class="card table-responsive p-3">
    <?php
    $deposit = mysqli_query($koneksi, "SELECT * FROM tb_deposit ORDER BY created_at DESC");
    ?>
    <table class="table" id="example">
      <thead>
        <tr>
          <th class="text-center">No.</th>
          <th class="text-center">Ext Player</th>
          <th class="text-center">Username</th>
          <th class="text-center">Referral</th>
          <th class="text-center">Jumlah</th>
          <th class="text-center">Bonus</th>
          <th class="text-center">Bukti</th>
          <th class="text-center">Bank Asal</th>
          <th class="text-center">Bank Tujuan</th>
          <th class="text-center">Status</th>
          <th class="text-center">Dibuat</th>
          <th class="text-center">Aksi</th>
        </tr>
      </thead>
      <tbody>
        <?php
        $no = 1;
        if (mysqli_num_rows($deposit) > 0) {
          while ($data = mysqli_fetch_assoc($deposit)) {
            $id = $data['id'];
            $extplayer = $data['extplayer'];
            $username = $data['username'];
            $ref = $data['ref'];
            $jumlah = $data['jumlah'];
            $bonus = $data['bonus'];
            $bukti = $data['bukti'];
            $bank_asal = $data['bank_asal'];
            $bank_tujuan = $data['bank_tujuan'];
            $status = $data['status'];
            $created = $data['created_at'];

            // Status label
            switch ($status) {
                case "Pending": $status_text = '<span class="text-warning">Diproses</span>'; break;
                case "Sukses": $status_text = '<span class="text-success">Disetujui</span>'; break;
                case "Rejected": $status_text = '<span class="text-danger">Dibatalkan</span>'; break;
                default: $status_text = '<span class="text-muted">Tidak Diketahui</span>'; break;
            }
        ?>
        <tr>
          <td class="text-center"><?php echo $no++; ?></td>
          <td class="text-center"><?php echo $extplayer; ?></td>
          <td class="text-center"><?php echo $username; ?></td>
          <td class="text-center"><?php echo $ref ?: '-'; ?></td>
          <td class="text-center"><?php echo 'Rp' . number_format($jumlah, 0, ',', '.'); ?></td>
          <td class="text-center"><?php echo 'Rp' . number_format($bonus, 0, ',', '.'); ?></td>
          <td class="text-center">
            <?php if (!empty($bukti)) { ?>
              <a href="<?php echo $bukti; ?>" target="_blank" class="btn btn-sm btn-outline-info">Lihat</a>
            <?php } else { echo '-'; } ?>
          </td>
          <td class="text-center"><?php echo $bank_asal; ?></td>
          <td class="text-center"><?php echo $bank_tujuan; ?></td>
          <td class="text-center"><?php echo $status_text; ?></td>
          <td class="text-center"><?php echo jamTanggalIndonesia($created); ?></td>
          <td class="text-center">
            <div class="btn-group">
              <a href="<?php echo $alamat_admin.'ubah_deposit/'.$id; ?>" class="btn btn-sm btn-primary">
                <span class="tf-icons mdi mdi-pencil"></span>
              </a>
              <button type="button" class="btn btn-sm btn-success" data-bs-toggle="modal" data-bs-target="#copyDeposit<?php echo $id; ?>">
                <span class="tf-icons mdi mdi-content-copy"></span>
              </button>
            </div>
          </td>
        </tr>

        <div class="modal fade" id="copyDeposit<?php echo $id; ?>" tabindex="-1" aria-labelledby="copyDepositLabel<?php echo $id; ?>" aria-hidden="true">
          <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
              <div class="modal-header">
                <h5 class="modal-title">Salin Data Deposit</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
              </div>
              <div class="modal-body">
                <form method="POST" action="">
                  <div class="mb-3">
                    <label class="form-label">Ext Player</label>
                    <input type="text" class="form-control" name="extplayer" value="<?php echo $extplayer; ?>" required>
                  </div>
                  <div class="mb-3">
                    <label class="form-label">Username</label>
                    <input type="text" class="form-control" name="username" value="<?php echo $username; ?>" required>
                  </div>
                  <div class="mb-3">
                    <label class="form-label">Referral</label>
                    <input type="text" class="form-control" name="ref" value="<?php echo $ref; ?>">
                  </div>
                  <div class="mb-3">
                    <label class="form-label">Jumlah</label>
                    <input type="number" class="form-control" name="jumlah" value="<?php echo $jumlah; ?>" required>
                  </div>
                  <div class="mb-3">
                    <label class="form-label">Bonus</label>
                    <input type="number" class="form-control" name="bonus" value="<?php echo $bonus; ?>" required>
                  </div>
                  <div class="mb-3">
                    <label class="form-label">URL Bukti</label>
                    <input type="text" class="form-control" name="bukti" value="<?php echo $bukti; ?>">
                  </div>
                  <div class="mb-3">
                    <label class="form-label">Bank Asal</label>
                    <input type="text" class="form-control" name="bank_asal" value="<?php echo $bank_asal; ?>" required>
                  </div>
                  <div class="mb-3">
                    <label class="form-label">Bank Tujuan</label>
                    <input type="text" class="form-control" name="bank_tujuan" value="<?php echo $bank_tujuan; ?>" required>
                  </div>
                  <div class="mb-3">
                    <label class="form-label">Status</label>
                    <select class="form-control" name="status" required>
                      <option value="0" <?php if ($status == 0) echo 'selected'; ?>>Diproses</option>
                      <option value="1" <?php if ($status == 1) echo 'selected'; ?>>Disetujui</option>
                      <option value="2" <?php if ($status == 2) echo 'selected'; ?>>Dibatalkan</option>
                    </select>
                  </div>
                  <div class="text-end">
                    <button type="submit" class="btn btn-primary">Simpan</button>
                  </div>
                </form>
              </div>
            </div>
          </div>
        </div>

        <?php
          }
        }
        ?>
      </tbody>
    </table>
  </div>
</div>
<script>
$(document).ready(function () {
    $('#example').DataTable({
        "pageLength": 25,
        "autoWidth": false,
        "ordering": true
    });
});
</script>

<script>
$(document).ready(function(){
    var table = $('#example').DataTable();
    $('#searchTable').on('keyup', function(){
        table.search(this.value).draw();
    });
});
</script>
