<?php
require_once 'config.php';
session_start();

// Cek autentikasi admin
if (!isset($_SESSION['admin_logged_in'])) {
    header('Location: login.php');
    exit;
}

// Ambil data markets
$stmt = $db->prepare("SELECT * FROM markets WHERE market_name IN ('BANDUNG', 'BOGOR')");
$stmt->execute();
$markets = $stmt->fetchAll(PDO::FETCH_ASSOC);

$bandung_time = null;
$bogor_time = null;

foreach ($markets as $market) {
    if ($market['market_name'] == 'BANDUNG') {
        $bandung_time = $market;
    } elseif ($market['market_name'] == 'BOGOR') {
        $bogor_time = $market;
    }
}

// Proses update jika form disubmit
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $bandung_time_input = $_POST['bandung_time'];
    $bandung_end_time_input = $_POST['bandung_end_time'];
    $bogor_time_input = $_POST['bogor_time'];
    $bogor_end_time_input = $_POST['bogor_end_time'];
    
    // Validasi format waktu
    function isValidTime($time) {
        return preg_match('/^([01]?[0-9]|2[0-3]):[0-5][0-9]$/', $time);
    }
    
    $errors = [];
    
    if (!isValidTime($bandung_time_input)) {
        $errors[] = "Format waktu Bandung tidak valid";
    }
    
    if (!isValidTime($bandung_end_time_input)) {
        $errors[] = "Format waktu akhir Bandung tidak valid";
    }
    
    if (!isValidTime($bogor_time_input)) {
        $errors[] = "Format waktu Bogor tidak valid";
    }
    
    if (!isValidTime($bogor_end_time_input)) {
        $errors[] = "Format waktu akhir Bogor tidak valid";
    }
    
    if (empty($errors)) {
        try {
            $db->beginTransaction();
            
            // Update Bandung
            $stmt = $db->prepare("UPDATE markets SET result_time = ?, result_end_time = ?, updated_at = NOW() WHERE market_name = 'BANDUNG'");
            $stmt->execute([$bandung_time_input . ':00', $bandung_end_time_input . ':00']);
            
            // Update Bogor
            $stmt = $db->prepare("UPDATE markets SET result_time = ?, result_end_time = ?, updated_at = NOW() WHERE market_name = 'BOGOR'");
            $stmt->execute([$bogor_time_input . ':00', $bogor_end_time_input . ':00']);
            
            $db->commit();
            $success = "Pengaturan waktu berhasil diperbarui";
        } catch (Exception $e) {
            $db->rollBack();
            $errors[] = "Terjadi kesalahan: " . $e->getMessage();
        }
    }
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Pengaturan Waktu Result - Admin Panel</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .container { max-width: 800px; }
    </style>
</head>
<body>
    <div class="container py-4">
        <h1 class="mb-4">Pengaturan Waktu Result</h1>
        
        <?php if (!empty($errors)): ?>
            <div class="alert alert-danger">
                <ul class="mb-0">
                    <?php foreach ($errors as $error): ?>
                        <li><?= htmlspecialchars($error) ?></li>
                    <?php endforeach; ?>
                </ul>
            </div>
        <?php endif; ?>
        
        <?php if (isset($success)): ?>
            <div class="alert alert-success">
                <?= htmlspecialchars($success) ?>
            </div>
        <?php endif; ?>
        
        <form method="post">
            <div class="card mb-4">
                <div class="card-header bg-primary text-white">
                    <h5 class="mb-0">Bandung</h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label for="bandung_time" class="form-label">Waktu Mulai Result</label>
                            <input type="time" class="form-control" id="bandung_time" name="bandung_time" 
                                   value="<?= substr($bandung_time['result_time'], 0, 5) ?>" required>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label for="bandung_end_time" class="form-label">Waktu Selesai Result</label>
                            <input type="time" class="form-control" id="bandung_end_time" name="bandung_end_time" 
                                   value="<?= substr($bandung_time['result_end_time'], 0, 5) ?>" required>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="card mb-4">
                <div class="card-header bg-success text-white">
                    <h5 class="mb-0">Bogor</h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label for="bogor_time" class="form-label">Waktu Mulai Result</label>
                            <input type="time" class="form-control" id="bogor_time" name="bogor_time" 
                                   value="<?= substr($bogor_time['result_time'], 0, 5) ?>" required>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label for="bogor_end_time" class="form-label">Waktu Selesai Result</label>
                            <input type="time" class="form-control" id="bogor_end_time" name="bogor_end_time" 
                                   value="<?= substr($bogor_time['result_end_time'], 0, 5) ?>" required>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="card mb-4">
                <div class="card-header bg-success text-white">
                    <h5 class="mb-0">Papua Barat</h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label for="papuabarat_time" class="form-label">Waktu Mulai Result</label>
                            <input type="time" class="form-control" id="papuabarat_time" name="papuabarat_time" 
                                   value="<?= substr($papuabarat_time['result_time'], 0, 5) ?>" required>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label for="bogor_end_time" class="form-label">Waktu Selesai Result</label>
                            <input type="time" class="form-control" id="bogor_end_time" name="bogor_end_time" 
                                   value="<?= substr($bogor_time['result_end_time'], 0, 5) ?>" required>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="d-flex justify-content-between">
                <a href="../index.php" class="btn btn-secondary">Kembali ke Dashboard</a>
                <button type="submit" class="btn btn-primary">Simpan Perubahan</button>
            </div>
        </form>
    </div>
    
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>