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

if (isset($_POST['tambah_data'])) {
  $username        = strtolower(trim($_POST['username']));
  $password        = $_POST['password'];
  $email           = trim($_POST['email']);
  $nomor           = trim($_POST['nomor']);
  $bank            = trim($_POST['bank']);
  $nama_rekening   = trim($_POST['nama_rekening']);
  $nomor_rekening  = trim($_POST['nomor_rekening']);
  $referral        = trim($_POST['referral']);
  $kode_referral   = strtoupper(substr(md5(uniqid(rand(), true)), 0, 8));
  $ip              = $_SERVER['REMOTE_ADDR'];

  // saldo default
  $saldo           = 0;
  $saldo_nexus     = 0;
  $saldo_sgx       = 0;
  $claimed         = 0;
  $status          = 'aktif';

  // hash password
  $kata_sandi_hash = password_hash($password, PASSWORD_BCRYPT, ['cost' => 12]);

  // validasi username
  if (preg_match('/^[a-zA-Z0-9_]+$/', $username)) {
    $cek_user = mysqli_query($koneksi, "SELECT * FROM tb_user WHERE username = '$username'");
    if (mysqli_num_rows($cek_user) >= 1) {
      echo '
        <script>
          alert("Username sudah digunakan, silakan pilih yang lain.");
          window.location.replace("'.$alamat_admin.'anggota");
        </script>
      ';
    } else {
      // buat extplayer unik (misal: USER12345)
      $extplayer = 'USR' . rand(10000, 99999);

      $query = "INSERT INTO tb_user 
        (extplayer, username, password, email, ip, saldo, nomor, bank, nomor_rekening, nama_rekening, referral, kode_referral, status, claimed, saldo_nexus, saldo_sgx, created_at, updated_at)
        VALUES 
        ('$extplayer', '$username', '$kata_sandi_hash', '$email', '$ip', '$saldo', '$nomor', '$bank', '$nomor_rekening', '$nama_rekening', '$referral', '$kode_referral', '$status', '$claimed', '$saldo_nexus', '$saldo_sgx', NOW(), NOW())";

      $tambah = mysqli_query($koneksi, $query);

      if ($tambah) {
        echo '
          <script>
            alert("Berhasil menambahkan anggota baru.");
            window.location.replace("'.$alamat_admin.'anggota");
          </script>
        ';
      } else {
        echo "Proses Gagal<br>Error : ".mysqli_error($koneksi);
      }
    }
  } else {
    echo '
      <script>
        alert("Username hanya boleh huruf, angka, dan underscore (_)");
        window.location.replace("'.$alamat_admin.'anggota");
      </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">Tambah Anggota</div>
    </div>
    <div class="col-md-6 text-center text-md-end">
      <a href="<?php echo $alamat_admin.'anggota'; ?>" class="btn btn-sm btn-primary waves-effect waves-light">
        <span class="tf-icons mdi mdi-chevron-double-left me-1"></span>Kembali
      </a>
    </div>
  </div>

  <div class="card mb-4">
    <h5 class="card-header">Tambah Data Anggota</h5>
    <form method="post" class="card-body">
      <h6>1. Akun</h6>
      <div class="row g-3">
        <div class="col-md-6">
          <div class="form-floating form-floating-outline">
            <input type="text" name="username" class="form-control" placeholder="Username" required>
            <label>Username</label>
          </div>
        </div>
        <div class="col-md-6">
          <div class="form-password-toggle">
            <div class="input-group input-group-merge">
              <div class="form-floating form-floating-outline">
                <input type="password" name="password" class="form-control" placeholder="········" required>
                <label>Password</label>
              </div>
              <span class="input-group-text cursor-pointer" id="toggle-password"><i class="mdi mdi-eye-off-outline"></i></span>
            </div>
          </div>
        </div>
        <div class="col-md-6">
          <div class="form-floating form-floating-outline">
            <input type="email" name="email" class="form-control" placeholder="Email" required>
            <label>Email</label>
          </div>
        </div>
        <div class="col-md-6">
          <div class="form-floating form-floating-outline">
            <input type="text" name="nomor" class="form-control" placeholder="Nomor HP" required>
            <label>Nomor HP</label>
          </div>
        </div>
      </div>

      <hr class="my-4 mx-n4">
      <h6>2. Bank</h6>
      <div class="row g-3">
        <div class="col-md-4">
          <div class="form-floating form-floating-outline mb-4">
            <select name="bank" class="form-select select2" required>
              <option value="" selected>-- Pilih Bank --</option>
              <option value="BCA">BCA</option>
              <option value="BNI">BNI</option>
              <option value="BRI">BRI</option>
              <option value="MANDIRI">MANDIRI</option>
              <option value="DANA">DANA</option>
              <option value="OVO">OVO</option>
              <option value="GOPAY">GOPAY</option>
              <option value="SHOPEEPAY">SHOPEEPAY</option>
              <option value="BTPN">BTPN</option>
            </select>
            <label>Bank</label>
          </div>
        </div>
        <div class="col-md-4">
          <div class="form-floating form-floating-outline">
            <input type="text" name="nama_rekening" class="form-control" placeholder="Nama Rekening" required>
            <label>Nama Rekening</label>
          </div>
        </div>
        <div class="col-md-4">
          <div class="form-floating form-floating-outline">
            <input type="text" name="nomor_rekening" class="form-control" placeholder="Nomor Rekening" required>
            <label>Nomor Rekening</label>
          </div>
        </div>
      </div>

      <hr class="my-4 mx-n4">
      <h6>3. Referral (Opsional)</h6>
      <div class="row g-3">
        <div class="col-md-6">
          <div class="form-floating form-floating-outline">
            <input type="text" name="referral" class="form-control" placeholder="Kode Referral (Opsional)">
            <label>Kode Referral</label>
          </div>
        </div>
      </div>

      <div class="pt-4 text-end">
        <button type="submit" name="tambah_data" class="btn btn-primary waves-effect waves-light">
          <span class="tf-icons mdi mdi-content-save me-1"></span>Simpan
        </button>
      </div>
    </form>
  </div>
</div>
