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

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $market_id = (int)$_POST['market_id'];
    $start = $_POST['generate_start'];
    $end = $_POST['generate_end'];
    
    // Validasi waktu
    if (strtotime($start) >= strtotime($end)) {
        $_SESSION['error'] = "Waktu mulai harus sebelum waktu selesai";
    } else {
        $stmt = $db->prepare("UPDATE markets SET 
                             auto_generate_start = ?,
                             auto_generate_end = ?,
                             auto_generate_active = 0 
                             WHERE id = ?");
        $stmt->execute([$start, $end, $market_id]);
        
        $_SESSION['success'] = "Waktu generate berhasil diupdate";
    }
    
    header("Location: set_generate_time.php");
    exit;
}

$markets = $db->query("SELECT * FROM markets")->fetchAll();
?>

<!DOCTYPE html>
<html>
<head>
    <title>Set Waktu Generate</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css">
</head>
<body>
    <div class="container mt-4">
        <h2>Atur Waktu Auto-Generate</h2>
        
        <?php if (isset($_SESSION['error'])): ?>
            <div class="alert alert-danger"><?= $_SESSION['error'] ?></div>
            <?php unset($_SESSION['error']); ?>
        <?php endif; ?>
        
        <?php if (isset($_SESSION['success'])): ?>
            <div class="alert alert-success"><?= $_SESSION['success'] ?></div>
            <?php unset($_SESSION['success']); ?>
        <?php endif; ?>
        
        <form method="post" class="mb-4">
            <div class="row">
                <div class="col-md-4">
                    <label>Pasar</label>
                    <select name="market_id" class="form-select" required>
                        <?php foreach ($markets as $m): ?>
                            <option value="<?= $m['id'] ?>"><?= htmlspecialchars($m['market_name']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="col-md-3">
                    <label>Mulai Generate</label>
                    <input type="time" name="generate_start" class="form-control" step="1" required>
                </div>
                <div class="col-md-3">
                    <label>Selesai Generate</label>
                    <input type="time" name="generate_end" class="form-control" step="1" required>
                </div>
                <div class="col-md-2 d-flex align-items-end">
                    <button type="submit" class="btn btn-primary">Simpan</button>
                </div>
            </div>
        </form>
        
        <h4>Jadwal Saat Ini</h4>
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th>Pasar</th>
                    <th>Mulai</th>
                    <th>Selesai</th>
                    <th>Status</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($markets as $m): ?>
                    <tr>
                        <td><?= htmlspecialchars($m['market_name']) ?></td>
                        <td><?= $m['auto_generate_start'] ?></td>
                        <td><?= $m['auto_generate_end'] ?></td>
                        <td><?= $m['auto_generate_active'] ? 'Aktif' : 'Nonaktif' ?></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</body>
</html>