<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
ob_start();
include_once '../db/koneksi.php';
include_once '../classes/diamond.php';
include_once '../classes/nexus.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'])) {
    $id = $_GET['id'];
    $anggota = mysqli_query($koneksi, "SELECT * FROM tb_pga WHERE id = '$id'");
    $data_anggota = mysqli_fetch_array($anggota);

    $uuidpga = $data_anggota['uuid'];
} else {
    echo '
        <script>
            alert("Pilih anggota yang ingin diubah!");
            window.location.replace("' . $alamat_admin . 'saldo");
        </script>
    ';
    exit();
}

if (isset($_POST['ubah_data'])) {
    $newuuid = mysqli_real_escape_string($koneksi, $_POST['uuidpga']); 
    
    $ubah_uuidpga = mysqli_query($koneksi, "UPDATE tb_pga SET uuid = '$newuuid' WHERE id = '$id'");
            
    if ($ubah_uuidpga) {
        echo '
            <script>
                alert("Berhasil mengubah uuid.");
                window.location.replace("' . $alamat_admin . 'qris");
            </script>
        ';
    } else {
        echo '
            <script>
                alert("Proses pengubahan gagal");
                window.location.replace("' . $alamat_admin . 'ubah_uuid/' . $id . '");
            </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">Ubah UUID QRIS OTOMATIS</div>
        </div>
        <div class="col-md-6">
            <div class="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>

    <div class="card mb-4">
        <h5 class="card-header">Ubah UUID</h5>
        <form method="post" class="card-body">
            <hr class="my-4 mx-n4">
            <h6>UUID PGA</h6>
            <div class="row g-3">
                <div class="col-md-4">
                    <div class="form-floating form-floating-outline">
                        <input type="text" name="uuidpga" class="form-control" value="<?php echo htmlspecialchars($uuidpga); ?>" required>
                        <label>UUID</label>
                    </div>
                </div>
            </div>
            <div class="pt-4 text-end">
                <button type="submit" name="ubah_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>
