<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
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>';
  exit();
}

if (isset($_GET['id_anggota'])) {
  $id_anggota = $_GET['id_anggota'];
  $anggota = mysqli_query($koneksi, "SELECT * FROM tb_user WHERE id = '$id_anggota'");
  $data_anggota = mysqli_fetch_assoc($anggota);

  if (!$data_anggota) {
    echo '
      <script>
        alert("Data anggota tidak ditemukan!");
        window.location.replace("' . $alamat_admin . 'anggota");
      </script>';
    exit();
  }

  $username = $data_anggota['username'];
  $email = $data_anggota['email'];
  $nomor = $data_anggota['nomor'];
  $bank = $data_anggota['bank'];
  $nama_rekening = $data_anggota['nama_rekening'];
  $nomor_rekening = $data_anggota['nomor_rekening'];
  $saldo = $data_anggota['saldo'];
  $status = $data_anggota['status'];
} else {
  echo '
    <script>
      alert("Pilih anggota yang ingin diubah!");
      window.location.replace("' . $alamat_admin . 'anggota");
    </script>';
  exit();
}

if (isset($_POST['ubah_data'])) {
  $username_baru = strtolower(trim($_POST['username']));
  $password_baru = $_POST['password'];
  $email_baru = $_POST['email'];
  $nomor_baru = $_POST['nomor'];
  $bank_baru = $_POST['bank'];
  $nama_rekening_baru = $_POST['nama_rekening'];
  $nomor_rekening_baru = $_POST['nomor_rekening'];
  $saldo_baru = $_POST['saldo'];
  $status_baru = $_POST['status_anggota'];

  $cek_user = mysqli_query($koneksi, "SELECT id FROM tb_user WHERE username='$username_baru' AND id!='$id_anggota'");
  if (mysqli_num_rows($cek_user) > 0) {
    echo '
      <script>
        alert("Nama pengguna sudah terdaftar, gunakan yang lain!");
        window.location.replace("' . $alamat_admin . 'ubah_anggota/' . $id_anggota . '");
      </script>';
    exit();
  }

  if (!empty($password_baru)) {
    $hash_opsi = ['cost' => 12];
    $password_hash = password_hash($password_baru, PASSWORD_BCRYPT, $hash_opsi);
    $query_update = "
      UPDATE tb_user SET
        username = '$username_baru',
        password = '$password_hash',
        email = '$email_baru',
        nomor = '$nomor_baru',
        bank = '$bank_baru',
        nama_rekening = '$nama_rekening_baru',
        nomor_rekening = '$nomor_rekening_baru',
        saldo = '$saldo_baru',
        status = '$status_baru',
        updated_at = NOW()
      WHERE id = '$id_anggota'";
  } else {
    $query_update = "
      UPDATE tb_user SET
        username = '$username_baru',
        email = '$email_baru',
        nomor = '$nomor_baru',
        bank = '$bank_baru',
        nama_rekening = '$nama_rekening_baru',
        nomor_rekening = '$nomor_rekening_baru',
        saldo = '$saldo_baru',
        status = '$status_baru',
        updated_at = NOW()
      WHERE id = '$id_anggota'";
  }

  $ubah = mysqli_query($koneksi, $query_update);
  if ($ubah) {
    echo '
      <script>
        alert("Berhasil ubah data anggota!");
        window.location.replace("' . $alamat_admin . 'anggota");
      </script>';
  } else {
    echo "Gagal memperbarui data: " . mysqli_error($koneksi);
  }
}

if (isset($_POST['hapus_data'])) {
  $hapus = mysqli_query($koneksi, "DELETE FROM tb_user WHERE id='$id_anggota'");
  if ($hapus) {
    echo '
      <script>
        alert("Berhasil menghapus data anggota!");
        window.location.replace("' . $alamat_admin . 'anggota");
      </script>';
  } else {
    echo "Gagal menghapus data: " . mysqli_error($koneksi);
  }
}
?>

<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">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">
        <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">Ubah 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" value="<?php echo $username; ?>" required>
            <label>Nama Pengguna</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="············">
                <label>Kata Sandi</label>
              </div>
              <span class="input-group-text cursor-pointer"><i class="mdi mdi-eye-off-outline"></i></span>
            </div>
            <div class="form-text">Kosongkan jika tidak ingin mengubah kata sandi.</div>
          </div>
        </div>
        <div class="col-md-6">
          <div class="form-floating form-floating-outline">
            <input type="email" name="email" class="form-control" value="<?php echo $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" value="<?php echo $nomor; ?>" required>
            <label>Telepon</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="<?php echo $bank; ?>" selected><?php echo $bank; ?></option>
              <option value="BCA">BCA</option>
              <option value="BNI">BNI</option>
              <option value="BRI">BRI</option>
              <option value="MANDIRI">MANDIRI</option>
              <option value="CIMB">CIMB</option>
              <option value="DANA">DANA</option>
              <option value="OVO">OVO</option>
              <option value="GOPAY">GOPAY</option>
              <option value="LINKAJA">LINKAJA</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" value="<?php echo $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" value="<?php echo $nomor_rekening; ?>" required>
            <label>Nomor Rekening</label>
          </div>
        </div>
      </div>

      <hr class="my-4 mx-n4">

      <h6>3. Lainnya</h6>
      <div class="row g-3">
        <div class="col-md-6">
          <div class="form-floating form-floating-outline">
            <input type="text" name="saldo" class="form-control" value="<?php echo $saldo; ?>" required>
            <label>Saldo</label>
          </div>
        </div>
        <div class="col-md-6">
          <div class="form-floating form-floating-outline mb-4">
            <select name="status_anggota" class="form-select select2" required>
              <option value="aktif" <?php echo ($status == 'aktif') ? 'selected' : ''; ?>>Aktif</option>
              <option value="terkunci" <?php echo ($status == 'terkunci') ? 'selected' : ''; ?>>Terkunci</option>
            </select>
            <label>Status Akun</label>
          </div>
        </div>
      </div>

      <div class="pt-4 text-end">
        <button type="button" class="btn btn-danger me-2" data-bs-toggle="modal" data-bs-target="#hapus_data">
          <span class="tf-icons mdi mdi-delete me-1"></span>Hapus
        </button>
        <button type="submit" name="ubah_data" class="btn btn-primary">
          <span class="tf-icons mdi mdi-content-save me-1"></span>Simpan
        </button>
      </div>
    </form>
  </div>
</div>

<!-- Modal Hapus -->
<div class="modal fade" id="hapus_data" tabindex="-1" data-bs-backdrop="static">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h1 class="modal-title fs-5">Hapus Data</h1>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>
      <form method="post">
        <div class="modal-body">Yakin ingin menghapus data ini?</div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
          <button type="submit" name="hapus_data" class="btn btn-danger">Hapus</button>
        </div>
      </form>
    </div>
  </div>
</div>
