<?php
require_once 'includes/config.php';
requireLogin();

$db = getDB();

// دریافت لیست خودروها
$stmt = $db->query("SELECT id, license_plate, model FROM vehicles ORDER BY license_plate");
$vehicles = $stmt->fetchAll();

// اگر خودرویی وجود ندارد
if (count($vehicles) === 0) {
    $_SESSION['error'] = "ابتدا باید خودرویی ثبت کنید";
    header('Location: vehicles.php');
    exit();
}
?>
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ثبت سوخت جدید - <?php echo SITE_NAME; ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <style>
        body { background-color: #f8f9fa; font-family: Tahoma; padding: 20px; }
        .form-container { background: white; border-radius: 15px; padding: 30px; box-shadow: 0 5px 15px rgba(0,0,0,0.05); }
        .page-title { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 20px; border-radius: 15px; margin-bottom: 20px; }
        .btn-submit { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; border: none; padding: 12px 30px; border-radius: 10px; }
    </style>
</head>
<body>
    <!-- ناوبری ساده -->
    <nav class="navbar navbar-dark mb-4" style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); border-radius: 15px;">
        <div class="container-fluid">
            <a class="navbar-brand" href="index.php">
                <i class="bi bi-arrow-right"></i> بازگشت به داشبورد
            </a>
        </div>
    </nav>
    
    <div class="container">
        <div class="page-title">
            <h2><i class="bi bi-droplet-fill"></i> ثبت عملیات سوخت‌گیری</h2>
        </div>
        
        <?php if (isset($_SESSION['error'])): ?>
            <div class="alert alert-danger alert-dismissible fade show" role="alert">
                <?php echo safe($_SESSION['error']); ?>
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
            <?php unset($_SESSION['error']); ?>
        <?php endif; ?>
        
        <div class="form-container">
            <form action="actions/add_fuel.php" method="POST">
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">خودرو *</label>
                        <select class="form-control" name="vehicle_id" required>
                            <option value="">انتخاب کنید</option>
                            <?php foreach ($vehicles as $vehicle): ?>
                                <option value="<?php echo $vehicle['id']; ?>">
                                    <?php echo safe($vehicle['license_plate']); ?> - <?php echo safe($vehicle['model']); ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    
                    <div class="col-md-6 mb-3">
                        <label class="form-label">نام راننده</label>
                        <input type="text" class="form-control" name="driver_name">
                    </div>
                </div>
                
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">تاریخ *</label>
                        <input type="date" class="form-control" name="fuel_date" 
                               value="<?php echo date('Y-m-d'); ?>" required>
                    </div>
                    
                    <div class="col-md-6 mb-3">
                        <label class="form-label">زمان *</label>
                        <input type="time" class="form-control" name="fuel_time" 
                               value="<?php echo date('H:i'); ?>" required>
                    </div>
                </div>
                
                <div class="row">
                    <div class="col-md-4 mb-3">
                        <label class="form-label">مقدار سوخت (لیتر) *</label>
                        <input type="number" class="form-control" name="fuel_amount" 
                               step="0.1" min="0.1" required>
                    </div>
                    
                    <div class="col-md-4 mb-3">
                        <label class="form-label">قیمت هر لیتر (تومان)</label>
                        <input type="number" class="form-control" name="price_per_liter" 
                               step="100" min="0">
                    </div>
                    
                    <div class="col-md-4 mb-3">
                        <label class="form-label">شماره پمپ</label>
                        <input type="text" class="form-control" name="pump_number">
                    </div>
                </div>
                
                <div class="mb-3">
                    <label class="form-label">یادداشت</label>
                    <textarea class="form-control" name="notes" rows="3"></textarea>
                </div>
                
                <div class="d-flex justify-content-between">
                    <a href="index.php" class="btn btn-secondary">انصراف</a>
                    <button type="submit" class="btn btn-submit">
                        <i class="bi bi-save"></i> ثبت عملیات
                    </button>
                </div>
            </form>
        </div>
    </div>
    
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>