<?php
require_once('includes/load.php'); // Memuat file load.php
session_start(); // Memulai session
page_require_level(7); // Pastikan level pengguna yang diperlukan

// Inisialisasi pesan
$messages = [];

// Cek lokasi yang dipilih, jika tidak ada set default ke 'jakarta'
if (isset($_POST['location'])) {
    $_SESSION['location'] = $_POST['location'];
}
$location = isset($_SESSION['location']) ? $_SESSION['location'] : 'jakarta';
$table_name = ($location == 'surabaya') ? 'resi_uploads_surabaya' : 'resi_uploads';

// Direktori file resi yang diunggah
$upload_dir = "uploads/resi/";

// Proses penghapusan resi berdasarkan tanggal
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['start_date']) && isset($_POST['end_date'])) {
    $start_date = $_POST['start_date'];
    $end_date = $_POST['end_date'];

    // Query untuk memilih file yang akan dihapus berdasarkan rentang tanggal
    $query = $db->prepare("SELECT file_name FROM $table_name WHERE upload_date BETWEEN ? AND ?");
    $query->bind_param('ss', $start_date, $end_date);
    $query->execute();
    $result = $query->get_result();

    // Loop melalui hasil dan hapus file
    while ($row = $result->fetch_assoc()) {
        $file_name = $row['file_name'];
        $file_path = $upload_dir . $file_name;

        // Hapus file dari server jika ada
        if (file_exists($file_path)) {
            unlink($file_path);
        }

        // Hapus data dari database
        $delete_query = $db->prepare("DELETE FROM $table_name WHERE file_name = ?");
        $delete_query->bind_param('s', $file_name);
        $delete_query->execute();
    }

    $messages[] = "Resi dari tanggal $start_date hingga $end_date berhasil dihapus.";
}

// Memuat header
include_once('layouts/header.php');
?>

<div class="container-fluid">
    <div class="row">
        <div class="col-md-12">
            <h1 class="text-center">Hapus Resi PDF Berdasarkan Tanggal</h1>

            <!-- Form untuk memilih lokasi -->
            <form action="" method="post" class="form-horizontal">
                <div class="form-group">
                    <label for="location" class="col-sm-2 control-label">Pilih Lokasi:</label>
                    <div class="col-sm-10">
                        <select name="location" class="form-control" onchange="this.form.submit()">
                            <option value="jakarta" <?php echo $location == 'jakarta' ? 'selected' : ''; ?>>Jakarta</option>
                            <option value="surabaya" <?php echo $location == 'surabaya' ? 'selected' : ''; ?>>Surabaya</option>
                        </select>
                    </div>
                </div>
            </form>

            <!-- Form untuk memilih tanggal -->
            <form action="" method="post" class="form-horizontal">
                <input type="hidden" name="location" value="<?php echo $location; ?>">
                <div class="form-group">
                    <label for="start_date" class="col-sm-2 control-label">Tanggal Mulai:</label>
                    <div class="col-sm-4">
                        <input type="date" name="start_date" id="start_date" class="form-control" required>
                    </div>
                    <label for="end_date" class="col-sm-2 control-label">Tanggal Akhir:</label>
                    <div class="col-sm-4">
                        <input type="date" name="end_date" id="end_date" class="form-control" required>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button type="submit" class="btn btn-danger">Hapus Resi</button>
                    </div>
                </div>
            </form>

            <!-- Pesan notifikasi -->
            <?php if (!empty($messages)) : ?>
                <div class="alert alert-info">
                    <?php echo implode('<br>', $messages); ?>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<?php include_once('layouts/footer.php'); ?>
