<?php
session_start();
require_once '../../../config.php';
load('middleware', 'user');

if ($_SESSION['user']['level'] !== "Admin") {
    header('location:' . base_url('id'));
}

if (isset($_GET['id'])) {
    $id = addslashes(trim(htmlspecialchars($_GET['id'])));
    
    if (!empty($id)) {
        $query_pembelian = mysqli_query($db, "SELECT * FROM pembelian_guest WHERE id = '$id'");
        
        if (mysqli_num_rows($query_pembelian) === 1) {
            $fetch_pembelian = mysqli_fetch_assoc($query_pembelian);
        } else {
            header('location:/admin/guest/transaksi.php');
        }
    } else {
        header('location:/admin/guest/transaksi.php');
    }
} else {
    header('location:/admin/guest/transaksi.php');
}

if (isset($_POST['tombol'])) {
    $sn = $_POST['sn'];
    $status = $_POST['status'];
    
    mysqli_query($db, "UPDATE pembelian_guest SET sn = '$sn', status = '$status' WHERE id = '$id'");
    
    $_SESSION['alert'] = ['success', 'Success!', 'Data berhasil di simpan.'];
    exit(header('location:/admin/guest/transaksi.php'));
}

include_once '../../../layouts/header-admin.php'; ?>

<div class="row justify-content-center">
    <div class="col-md-8 col-12">
        <div class="card bg-dark shadow">
            <div class="card-header">
                <h4 class="card-title">Edit Transaksi</h4>
            </div>
            <div class="card-body card-dashboard">
                <form action="" method="POST" enctype="multipart/form-data">
                    <div class="row">
                        <div class="col-6">
                        <label>Username</label>
                        <input type="text" class="form-control" autocomplete="off" name="username" value="<?= $fetch_pembelian['username']; ?>" disabled>
                        </div>
                    <div class="col-6">
                        <label>Order ID</label>
                        <input type="text" class="form-control" autocomplete="off" name="order_id" value="<?= $fetch_pembelian['order_id']; ?>" disabled>
                    </div>
                    </div>
                    <br>
                    <div class="row">
                        <div class="col-6">
                        <label>Games</label>
                        <input type="text" class="form-control" autocomplete="off" name="games" value="<?= $fetch_pembelian['games']; ?>" disabled>
                    </div>
                    <div class="col-6">
                        <label>Produk</label>
                        <input type="text" class="form-control" autocomplete="off" name="layanan" value="<?= $fetch_pembelian['layanan']; ?>" disabled>
                    </div>
                    </div>
                    <br>
                    <?php if($fetch_pembelian['additional_target'] == null):?>
                    <div class="row">
                        <div class="col-6">
                        <label>Target</label>
                        <input type="text" class="form-control" autocomplete="off" name="target" value="<?= $fetch_pembelian['target']; ?>" disabled>
                    </div>
                    <?php else:?>
                    <div class="row">
                    <div class="col-6">
                        <label>Target</label>
                        <input type="text" class="form-control" autocomplete="off" name="target" value="<?= $fetch_pembelian['target'];?>(<?= $fetch_pembelian['additional_target'];?>)" disabled>
                    </div>
                    <?php endif;?>
                    <div class="col-6">
                        <label>Harga</label>
                        <input type="number" class="form-control" autocomplete="off" name="harga" value="<?= $fetch_pembelian['harga']; ?>" disabled>
                    </div>
                    </div>
                    <br>
                    <div class="form-group mb-3">
                        <label>SN</label>
                        <input type="text" class="form-control" autocomplete="off" name="sn" value="<?= $fetch_pembelian['sn']; ?>">
                    </div>
                    <div class="form-group mb-3">
                        <label>Status</label>
                        <select class="form-control" name="status">
                            <option value="Pending" <?= $fetch_pembelian['status'] == 'Pending' ? 'selected' : ''; ?>>Pending</option>
                            <option value="Waiting" <?= $fetch_pembelian['status'] == 'Waiting' ? 'selected' : ''; ?>>Waiting</option>
                            <option value="Processing" <?= $fetch_pembelian['status'] == 'Processing' ? 'selected' : ''; ?>>Processing</option>
                            <option value="Success" <?= $fetch_pembelian['status'] == 'Success' ? 'selected' : ''; ?>>Success</option>
                            <option value="Canceled" <?= $fetch_pembelian['status'] == 'Canceled' ? 'selected' : ''; ?>>Canceled</option>
                        </select>
                    </div>
                    <div class="form-group mb-3">
                        <label>Tanggal</label>
                        <input type="text" class="form-control" autocomplete="off" name="date_create" value="<?= $fetch_pembelian['date_create']; ?>" disabled>
                    </div>
                    <a href="/admin/guest/transaksi.php" class="btn btn-warning float-left">Kembali</a>
                    <div class="text-right">
                        <button type="reset" class="btn btn-default">Batal</button>
                        <button type="submit" class="btn btn-primary" name="tombol">Simpan</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<?php include_once '../../../layouts/footer-admin.php'; ?>
<script src="https://cdn.ckeditor.com/4.17.2/standard/ckeditor.js"></script>
<script>
    CKEDITOR.replace('keterangan', {
        height: 400,
    });
</script>
<script>
    function modal_tambah() {
        $("#exampleModal").modal('show');
    }
</script>
<script>
    $(".custom-file-input").on("change", function() {
        var fileName = $(this).val().split("\\").pop();
        $(this).siblings(".custom-file-label").addClass("selected").html(fileName);
    });
</script>