<?php
// admin/price-list.php - Versioni për Worker (View Only)
require_once '../includes/config.php';

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

$user_role = $_SESSION['admin_role'] ?? 'admin';
$is_worker = ($user_role == 'worker');

$message = '';
$error = '';

// ============================================
// HANDLE DELETE RULE - VETËM ADMIN
// ============================================
if(isset($_GET['delete_rule']) && !$is_worker) {
    $stmt = $pdo->prepare("DELETE FROM price_rules WHERE id = ?");
    $stmt->execute([$_GET['delete_rule']]);
    if(isset($_GET['car_id'])) {
        syncGroupPriceRules((int)$_GET['car_id'], $pdo);
    }
    $message = "Price rule deleted!";
}

// ============================================
// HANDLE ADD/EDIT RULE - VETËM ADMIN
// ============================================
if($_SERVER['REQUEST_METHOD'] == 'POST' && !$is_worker) {
    $car_id = $_POST['car_id'];
    $rule_type = $_POST['rule_type'];
    
    if($rule_type == 'base_price') {
        $fixed_price = $_POST['fixed_price'];
        $pdo->prepare("UPDATE price_rules SET is_active = 0 WHERE car_id = ? AND rule_type = 'base_price'")->execute([$car_id]);
        $stmt = $pdo->prepare("INSERT INTO price_rules (car_id, rule_type, fixed_price, priority, is_active) VALUES (?, 'base_price', ?, 0, 1)");
        $stmt->execute([$car_id, $fixed_price]);
        $message = "Base price updated!";
        
    } elseif($rule_type == 'date_specific') {
        $specific_date = $_POST['specific_date'];
        $fixed_price = $_POST['fixed_price'];
        $stmt = $pdo->prepare("INSERT INTO price_rules (car_id, rule_type, start_date, end_date, fixed_price, priority, is_active) VALUES (?, 'date_specific', ?, ?, ?, 20, 1)");
        $stmt->execute([$car_id, $specific_date, $specific_date, $fixed_price]);
        $message = "Date-specific price added!";
        
    } elseif($rule_type == 'date_range') {
        $start_date = $_POST['start_date'];
        $end_date = $_POST['end_date'];
        $price_type = $_POST['price_type'];
        
        if($price_type == 'fixed') {
            $fixed_price = $_POST['fixed_price'];
            $stmt = $pdo->prepare("INSERT INTO price_rules (car_id, rule_type, start_date, end_date, fixed_price, priority, is_active) VALUES (?, 'date_range', ?, ?, ?, 15, 1)");
            $stmt->execute([$car_id, $start_date, $end_date, $fixed_price]);
        } else {
            $discount_percent = $_POST['discount_percent'];
            $stmt = $pdo->prepare("INSERT INTO price_rules (car_id, rule_type, start_date, end_date, discount_percent, priority, is_active) VALUES (?, 'date_range', ?, ?, ?, 15, 1)");
            $stmt->execute([$car_id, $start_date, $end_date, $discount_percent]);
        }
        $message = "Date range rule added!";
        
    } elseif($rule_type == 'weekly_discount') {
        $min_days = $_POST['min_days'] ?: 7;
        $discount_percent = $_POST['discount_percent'];
        $stmt = $pdo->prepare("INSERT INTO price_rules (car_id, rule_type, min_days, discount_percent, priority, is_active) VALUES (?, 'weekly_discount', ?, ?, 10, 1)");
        $stmt->execute([$car_id, $min_days, $discount_percent]);
        $message = "Weekly discount added!";
        
    } elseif($rule_type == 'monthly_discount') {
        $min_days = $_POST['min_days'] ?: 30;
        $discount_percent = $_POST['discount_percent'];
        $stmt = $pdo->prepare("INSERT INTO price_rules (car_id, rule_type, min_days, discount_percent, priority, is_active) VALUES (?, 'monthly_discount', ?, ?, 10, 1)");
        $stmt->execute([$car_id, $min_days, $discount_percent]);
        $message = "Monthly discount added!";
    }
    
    // Sinkronizo te gjitha rregullat e kesaj makine tek te gjitha makinat identike te grupit te saj
    syncGroupPriceRules((int)$car_id, $pdo);
}

// Get all cars (vetem makina primare per cdo grup, qe te ndahet 1 cmim per te gjithe grupin)
$cars = $pdo->query("SELECT id, brand, model, license_plate FROM cars WHERE group_id IS NULL OR is_primary = 1 ORDER BY brand")->fetchAll();

// Get selected car
$selected_car = isset($_GET['car_id']) ? (int)$_GET['car_id'] : ($cars[0]['id'] ?? 0);

// Get all active rules for selected car
$rules = $pdo->prepare("SELECT * FROM price_rules WHERE car_id = ? AND is_active = 1 ORDER BY priority DESC, id DESC");
$rules->execute([$selected_car]);
$rules_list = $rules->fetchAll(PDO::FETCH_ASSOC);

// Get base price
$base_price = 0;
foreach($rules_list as $r) {
    if($r['rule_type'] == 'base_price') {
        $base_price = $r['fixed_price'];
        break;
    }
}

// Get car name for display
$car_name = '';
$car_plate = '';
foreach($cars as $c) {
    if($c['id'] == $selected_car) {
        $car_name = $c['brand'] . ' ' . $c['model'];
        $car_plate = $c['license_plate'];
        break;
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Price Rules - Admin</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; background: #f5f5f5; }
        
        .sidebar {
            width: 260px;
            background: #1a1a2e;
            color: white;
            position: fixed;
            left: 0;
            top: 0;
            bottom: 0;
            overflow-y: auto;
            z-index: 100;
        }
        .sidebar-header { padding: 24px 20px; border-bottom: 1px solid rgba(255,255,255,0.1); }
        .sidebar-header h2 { font-size: 18px; font-weight: 600; }
        .sidebar-nav { padding: 20px 0; }
        .sidebar-nav a {
            display: flex;
            align-items: center;
            gap: 12px;
            padding: 12px 20px;
            color: #cbd5e1;
            text-decoration: none;
            font-size: 14px;
            transition: all 0.2s;
        }
        .sidebar-nav a i { width: 20px; }
        .sidebar-nav a:hover, .sidebar-nav a.active { background: #0066cc; color: white; }
        
        .main-content { margin-left: 260px; padding: 20px; }
        .page-header { margin-bottom: 20px; display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 10px; }
        .page-header h1 { font-size: 22px; font-weight: 600; }
        .page-header h1 i { color: #0066cc; margin-right: 8px; }
        
        .worker-badge {
            display: inline-block;
            background: #fef3c7;
            color: #92400e;
            padding: 4px 12px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: 600;
        }
        
        .alert-success { 
            background: #d1fae5; 
            color: #065f46; 
            padding: 10px 16px; 
            border-radius: 8px; 
            margin-bottom: 16px; 
            border-left: 3px solid #10b981;
            font-size: 13px;
        }
        
        /* Three Column Layout */
        .three-columns {
            display: grid;
            grid-template-columns: 1fr 1fr 1fr;
            gap: 16px;
            align-items: start;
        }
        
        .col-card {
            background: white;
            border-radius: 10px;
            border: 1px solid #e5e7eb;
            overflow: hidden;
        }
        .col-header {
            background: #f8fafc;
            padding: 10px 14px;
            border-bottom: 1px solid #e5e7eb;
            font-weight: 600;
            font-size: 13px;
            color: #1e293b;
            display: flex;
            align-items: center;
            gap: 8px;
        }
        .col-header i { color: #0066cc; }
        .col-header .badge {
            margin-left: auto;
            background: #e5e7eb;
            padding: 2px 8px;
            border-radius: 12px;
            font-size: 10px;
            font-weight: 500;
            color: #4b5563;
        }
        .col-body { padding: 12px; }
        
        /* ======================== */
        /* COLUMN 1: CAR LIST */
        /* ======================== */
        .car-search-box {
            position: relative;
            margin-bottom: 10px;
        }
        .car-search-box input {
            width: 100%;
            padding: 6px 10px 6px 30px;
            border: 1px solid #d1d5db;
            border-radius: 6px;
            font-size: 12px;
            background: white;
        }
        .car-search-box input:focus {
            outline: none;
            border-color: #0066cc;
        }
        .car-search-box i {
            position: absolute;
            left: 10px;
            top: 50%;
            transform: translateY(-50%);
            color: #94a3b8;
            font-size: 12px;
        }
        
        .car-list-scroll {
            max-height: calc(100vh - 320px);
            overflow-y: auto;
        }
        .car-list-scroll::-webkit-scrollbar { width: 4px; }
        .car-list-scroll::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 4px; }
        .car-list-scroll::-webkit-scrollbar-track { background: #f1f5f9; }
        
        .car-item-link {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 6px 10px;
            border-radius: 4px;
            margin-bottom: 2px;
            text-decoration: none;
            color: #1e293b;
            font-size: 13px;
            transition: all 0.15s;
            cursor: pointer;
        }
        .car-item-link:hover {
            background: #f1f5f9;
        }
        .car-item-link.active {
            background: #0066cc;
            color: white;
            font-weight: 500;
        }
        .car-item-link .car-name {
            flex: 1;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }
        .car-item-link .car-plate {
            font-size: 10px;
            font-family: monospace;
            color: #94a3b8;
            margin-left: 8px;
            background: #f1f5f9;
            padding: 1px 6px;
            border-radius: 3px;
        }
        .car-item-link.active .car-plate {
            background: rgba(255,255,255,0.2);
            color: rgba(255,255,255,0.8);
        }
        .car-item-link .price-badge {
            font-size: 11px;
            font-weight: 600;
            color: #0066cc;
            background: #eff6ff;
            padding: 1px 8px;
            border-radius: 10px;
            margin-left: 6px;
        }
        .car-item-link.active .price-badge {
            background: rgba(255,255,255,0.2);
            color: white;
        }
        .no-cars-found {
            text-align: center;
            padding: 20px;
            color: #94a3b8;
            font-size: 12px;
        }
        
        /* ======================== */
        /* COLUMN 2: PRICE OPTIONS */
        /* ======================== */
        .selected-car-name {
            font-size: 13px;
            font-weight: 500;
            color: #1e293b;
            padding: 6px 10px;
            background: #f1f5f9;
            border-radius: 6px;
            margin-bottom: 10px;
            text-align: center;
        }
        .selected-car-name .plate {
            font-family: monospace;
            font-size: 11px;
            color: #64748b;
        }
        
        .base-price-display {
            text-align: center;
            padding: 10px;
            background: linear-gradient(135deg, #0066cc 0%, #0052a3 100%);
            border-radius: 6px;
            color: white;
            margin-bottom: 10px;
        }
        .base-price-display .label { font-size: 10px; opacity: 0.8; text-transform: uppercase; letter-spacing: 0.5px; }
        .base-price-display .price { font-size: 26px; font-weight: 700; }
        .base-price-display .price small { font-size: 13px; font-weight: 400; }
        
        .base-price-form {
            display: flex;
            gap: 6px;
            margin-bottom: 10px;
        }
        .base-price-form input {
            flex: 1;
            padding: 5px 8px;
            border: 1px solid #d1d5db;
            border-radius: 4px;
            font-size: 13px;
            text-align: center;
        }
        .base-price-form input:focus {
            outline: none;
            border-color: #0066cc;
        }
        .base-price-form button {
            background: #0066cc;
            color: white;
            border: none;
            padding: 5px 14px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 12px;
            font-weight: 500;
            white-space: nowrap;
        }
        .base-price-form button:hover { background: #0052a3; }
        .base-price-form button:disabled {
            opacity: 0.5;
            cursor: not-allowed;
        }
        
        .rule-divider {
            border-top: 1px solid #e5e7eb;
            margin: 8px 0;
        }
        
        /* Rule Form - Compact */
        .rule-form-group {
            margin-bottom: 6px;
        }
        .rule-form-group label {
            display: block;
            font-size: 10px;
            font-weight: 600;
            color: #64748b;
            text-transform: uppercase;
            letter-spacing: 0.3px;
            margin-bottom: 2px;
        }
        .rule-form-group input,
        .rule-form-group select {
            width: 100%;
            padding: 4px 8px;
            border: 1px solid #d1d5db;
            border-radius: 4px;
            font-size: 12px;
            background: white;
        }
        .rule-form-group input:focus,
        .rule-form-group select:focus {
            outline: none;
            border-color: #0066cc;
        }
        .rule-form-group input:disabled {
            background: #f3f4f6;
            cursor: not-allowed;
        }
        .form-row-2 {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 6px;
        }
        .form-row-3 {
            display: grid;
            grid-template-columns: 1fr 1fr 1fr;
            gap: 6px;
        }
        
        .rule-section-title {
            font-size: 11px;
            font-weight: 600;
            color: #1e293b;
            margin: 6px 0 4px 0;
            display: flex;
            align-items: center;
            gap: 6px;
        }
        .rule-section-title i {
            color: #0066cc;
            font-size: 12px;
        }
        
        .btn-add-rule {
            background: #10b981;
            color: white;
            border: none;
            padding: 5px 12px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 11px;
            font-weight: 500;
            width: 100%;
            transition: background 0.15s;
            margin-top: 2px;
        }
        .btn-add-rule:hover { background: #059669; }
        .btn-add-rule i { font-size: 10px; margin-right: 4px; }
        .btn-add-rule:disabled {
            opacity: 0.5;
            cursor: not-allowed;
        }
        
        /* Weekly/Monthly with days input */
        .discount-group {
            display: flex;
            gap: 4px;
            align-items: center;
        }
        .discount-group .field {
            flex: 1;
        }
        .discount-group .field input {
            width: 100%;
            padding: 4px 6px;
            border: 1px solid #d1d5db;
            border-radius: 4px;
            font-size: 12px;
        }
        .discount-group .field input:focus {
            outline: none;
            border-color: #0066cc;
        }
        .discount-group .field input:disabled {
            background: #f3f4f6;
            cursor: not-allowed;
        }
        .discount-group .field label {
            font-size: 8px;
            color: #94a3b8;
            text-transform: uppercase;
            display: block;
            margin-bottom: 1px;
        }
        .discount-group .btn-add-small {
            background: #10b981;
            color: white;
            border: none;
            padding: 4px 8px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 10px;
            font-weight: 500;
            height: 28px;
            margin-top: 14px;
            transition: background 0.15s;
        }
        .discount-group .btn-add-small:hover { background: #059669; }
        .discount-group .btn-add-small:disabled {
            opacity: 0.5;
            cursor: not-allowed;
        }
        
        /* ======================== */
        /* COLUMN 3: ACTIVE RULES */
        /* ======================== */
        .rules-header-info {
            display: flex;
            justify-content: space-between;
            align-items: center;
            font-size: 11px;
            color: #64748b;
            margin-bottom: 6px;
            padding-bottom: 4px;
            border-bottom: 1px solid #f1f5f9;
        }
        .rules-header-info .count {
            font-weight: 600;
            color: #1e293b;
        }
        
        .rules-list-scroll {
            max-height: calc(100vh - 320px);
            overflow-y: auto;
        }
        .rules-list-scroll::-webkit-scrollbar { width: 4px; }
        .rules-list-scroll::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 4px; }
        .rules-list-scroll::-webkit-scrollbar-track { background: #f1f5f9; }
        
        .rule-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 5px 8px;
            border-radius: 4px;
            margin-bottom: 2px;
            background: #f8fafc;
            border-left: 2px solid #0066cc;
            font-size: 12px;
        }
        .rule-item:hover { background: #f1f5f9; }
        .rule-info { flex: 1; min-width: 0; }
        .rule-name {
            font-weight: 600;
            font-size: 12px;
            display: flex;
            align-items: center;
            gap: 4px;
            flex-wrap: wrap;
        }
        .rule-type-badge {
            display: inline-block;
            background: #e0e7ff;
            color: #3730a3;
            padding: 1px 6px;
            border-radius: 8px;
            font-size: 8px;
            font-weight: 600;
        }
        .rule-detail {
            font-size: 10px;
            color: #64748b;
            margin-top: 1px;
        }
        .rule-value {
            font-weight: 600;
            color: #0066cc;
            font-size: 12px;
            margin-right: 4px;
        }
        .rule-delete {
            color: #dc2626;
            text-decoration: none;
            padding: 2px 4px;
            border-radius: 4px;
            font-size: 11px;
        }
        .rule-delete:hover { background: #fee2e2; }
        .rule-lock {
            color: #94a3b8;
            font-size: 11px;
        }
        
        .empty-rules {
            text-align: center;
            padding: 30px 10px;
            color: #94a3b8;
            font-size: 13px;
        }
        .empty-rules i { font-size: 28px; display: block; margin-bottom: 8px; color: #e2e8f0; }
        
        .info-note {
            margin-top: 16px;
            background: #fef3c7;
            padding: 8px 14px;
            border-radius: 8px;
            font-size: 11px;
            color: #92400e;
            display: flex;
            align-items: center;
            gap: 8px;
        }
        .info-note i { font-size: 14px; }
        .info-note.worker-info {
            background: #eff6ff;
            color: #1e40af;
        }
        
        .lock-icon {
            display: inline-block;
            color: #94a3b8;
            margin-left: 4px;
        }
        
        @media (max-width: 1100px) {
            .three-columns {
                grid-template-columns: 1fr 1fr;
            }
            .three-columns .col-card:last-child {
                grid-column: span 2;
            }
        }
        
        @media (max-width: 768px) {
            .sidebar { width: 100%; position: relative; height: auto; }
            .main-content { margin-left: 0; padding: 12px; }
            .three-columns {
                grid-template-columns: 1fr;
            }
            .three-columns .col-card:last-child {
                grid-column: span 1;
            }
            .car-list-scroll,
            .rules-list-scroll {
                max-height: 300px;
            }
            .form-row-3 {
                grid-template-columns: 1fr 1fr;
            }
            .discount-group {
                flex-wrap: wrap;
            }
            .discount-group .btn-add-small {
                margin-top: 0;
                width: 100%;
            }
        }
    </style>
</head>
<body>
    <div class="sidebar">
        <div class="sidebar-header"><h2><i class="fas fa-car"></i> Car Rental Admin</h2></div>
        <div class="sidebar-nav">
            <a href="dashboard.php"><i class="fas fa-tachometer-alt"></i> Dashboard</a>
            <a href="cars.php"><i class="fas fa-car"></i> Cars</a>
            <a href="create-reservation.php"><i class="fas fa-plus-square"></i> Create Reservation</a>
            <a href="reservations.php"><i class="fas fa-calendar-check"></i> Reservations</a>
            <a href="calendar.php"><i class="fas fa-calendar-alt"></i> Calendar</a>
            <a href="price-list.php" class="active"><i class="fas fa-tags"></i> Price Rules</a>
            <a href="extras.php"><i class="fas fa-cogs"></i> Extras</a>
            <a href="email-settings.php"><i class="fas fa-envelope"></i> Email Settings</a>
            <a href="settings.php"><i class="fas fa-cog"></i> Settings</a>
            <a href="manage-admins.php"><i class="fas fa-user-shield"></i> Users</a>
            <a href="clients.php"><i class="fas fa-users"></i> Clients</a>
            <a href="logout.php"><i class="fas fa-sign-out-alt"></i> Logout</a>
        </div>
    </div>
    
    <div class="main-content">
        <div class="page-header">
            <h1>
                <i class="fas fa-tags"></i> Price Rules
                <?php if($is_worker): ?>
                    <span class="worker-badge"><i class="fas fa-eye"></i> View Only</span>
                <?php endif; ?>
            </h1>
            <?php if($is_worker): ?>
                <span style="font-size: 13px; color: #6b7280;">
                    <i class="fas fa-lock"></i> Worker mode: You can view price rules but cannot edit them.
                </span>
            <?php endif; ?>
        </div>
        
        <?php if($message): ?>
            <div class="alert-success"><i class="fas fa-check-circle"></i> <?php echo htmlspecialchars($message); ?></div>
        <?php endif; ?>
        
        <?php if($is_worker): ?>
            <div class="info-note worker-info" style="margin-bottom:16px;">
                <i class="fas fa-info-circle"></i>
                <div><strong>View Only Mode:</strong> You are viewing price rules. To modify them, please contact an administrator.</div>
            </div>
        <?php endif; ?>
        
        <div class="three-columns">
            <!-- ========================================== -->
            <!-- COLUMN 1: Car List with Search -->
            <!-- ========================================== -->
            <div class="col-card">
                <div class="col-header">
                    <i class="fas fa-car"></i> Vehicles
                    <span class="badge" id="carCount"><?php echo count($cars); ?></span>
                </div>
                <div class="col-body">
                    <div class="car-search-box">
                        <i class="fas fa-search"></i>
                        <input type="text" id="carSearch" placeholder="Search by name or plate..." onkeyup="filterCars()">
                    </div>
                    <div class="car-list-scroll" id="carList">
                        <?php foreach($cars as $car): 
                            $car_base = getCarBasePrice($car['id'], $pdo);
                        ?>
                            <a href="?car_id=<?php echo $car['id']; ?>" 
                               class="car-item-link <?php echo $car['id'] == $selected_car ? 'active' : ''; ?>"
                               data-search="<?php echo strtolower($car['brand'] . ' ' . $car['model'] . ' ' . $car['license_plate']); ?>">
                                <span class="car-name"><?php echo htmlspecialchars($car['brand'] . ' ' . $car['model']); ?></span>
                                <span class="car-plate"><?php echo htmlspecialchars($car['license_plate']); ?></span>
                                <span class="price-badge">€<?php echo number_format($car_base, 0); ?></span>
                            </a>
                        <?php endforeach; ?>
                    </div>
                </div>
            </div>
            
            <!-- ========================================== -->
            <!-- COLUMN 2: Price Options -->
            <!-- ========================================== -->
            <div class="col-card">
                <div class="col-header">
                    <i class="fas fa-sliders-h"></i> Price Options
                    <span class="badge"><?php echo htmlspecialchars($car_name); ?></span>
                </div>
                <div class="col-body">
                    <!-- Selected Car Name -->
                    <div class="selected-car-name">
                        <?php echo htmlspecialchars($car_name); ?>
                        <span class="plate">(<?php echo htmlspecialchars($car_plate); ?>)</span>
                    </div>
                    
                    <!-- BASE PRICE -->
                    <div class="rule-section-title"><i class="fas fa-coin"></i> Base Daily Price</div>
                    <div class="base-price-display">
                        <div class="label">Current Base Price</div>
                        <div class="price">€<?php echo number_format($base_price, 2); ?> <small>/ day</small></div>
                    </div>
                    
                    <?php if(!$is_worker): ?>
                        <form method="POST" class="base-price-form">
                            <input type="hidden" name="car_id" value="<?php echo $selected_car; ?>">
                            <input type="hidden" name="rule_type" value="base_price">
                            <input type="number" step="0.01" name="fixed_price" value="<?php echo $base_price; ?>" required>
                            <button type="submit"><i class="fas fa-save"></i> Update</button>
                        </form>
                    <?php else: ?>
                        <div style="padding:6px 10px; background:#f1f5f9; border-radius:4px; font-size:11px; color:#64748b; text-align:center;">
                            <i class="fas fa-lock"></i> Base price is managed by admins only.
                        </div>
                    <?php endif; ?>
                    
                    <div class="rule-divider"></div>
                    
                    <?php if(!$is_worker): ?>
                        <!-- DATE SPECIFIC -->
                        <div class="rule-section-title"><i class="fas fa-calendar-day"></i> Specific Date Price</div>
                        <form method="POST">
                            <input type="hidden" name="car_id" value="<?php echo $selected_car; ?>">
                            <input type="hidden" name="rule_type" value="date_specific">
                            <div class="form-row-2">
                                <div class="rule-form-group">
                                    <label>Date</label>
                                    <input type="date" name="specific_date" required>
                                </div>
                                <div class="rule-form-group">
                                    <label>Price (€/day)</label>
                                    <input type="number" step="0.01" name="fixed_price" placeholder="e.g. 150" required>
                                </div>
                            </div>
                            <button type="submit" class="btn-add-rule"><i class="fas fa-plus"></i> Add Specific Date Price</button>
                        </form>
                        
                        <div class="rule-divider"></div>
                        
                        <!-- DATE RANGE -->
                        <div class="rule-section-title"><i class="fas fa-calendar-week"></i> Date Range Price</div>
                        <form method="POST">
                            <input type="hidden" name="car_id" value="<?php echo $selected_car; ?>">
                            <input type="hidden" name="rule_type" value="date_range">
                            <div class="form-row-2">
                                <div class="rule-form-group"><label>Start Date</label><input type="date" name="start_date" required></div>
                                <div class="rule-form-group"><label>End Date</label><input type="date" name="end_date" required></div>
                            </div>
                            <div class="form-row-2">
                                <div class="rule-form-group">
                                    <label>Price Type</label>
                                    <select name="price_type" onchange="togglePriceType(this)">
                                        <option value="fixed">Fixed Price (€)</option>
                                        <option value="discount">Discount (%)</option>
                                    </select>
                                </div>
                                <div class="rule-form-group">
                                    <label>Value</label>
                                    <input type="number" step="0.01" name="fixed_price" placeholder="Price or %" required>
                                </div>
                            </div>
                            <button type="submit" class="btn-add-rule"><i class="fas fa-plus"></i> Add Date Range</button>
                        </form>
                        
                        <div class="rule-divider"></div>
                        
                        <!-- WEEKLY & MONTHLY DISCOUNT -->
                        <div class="rule-section-title"><i class="fas fa-percent"></i> Long-Term Discounts</div>
                        <div class="form-row-2">
                            <!-- Weekly Discount -->
                            <form method="POST" style="flex:1;">
                                <input type="hidden" name="car_id" value="<?php echo $selected_car; ?>">
                                <input type="hidden" name="rule_type" value="weekly_discount">
                                <div class="rule-form-group">
                                    <label>Weekly Discount</label>
                                    <div class="discount-group">
                                        <div class="field">
                                            <label>Min Days</label>
                                            <input type="number" name="min_days" value="7" min="1" required>
                                        </div>
                                        <div class="field">
                                            <label>Discount %</label>
                                            <input type="number" step="0.01" name="discount_percent" placeholder="%" required>
                                        </div>
                                        <button type="submit" class="btn-add-small"><i class="fas fa-plus"></i></button>
                                    </div>
                                </div>
                            </form>
                            
                            <!-- Monthly Discount -->
                            <form method="POST" style="flex:1;">
                                <input type="hidden" name="car_id" value="<?php echo $selected_car; ?>">
                                <input type="hidden" name="rule_type" value="monthly_discount">
                                <div class="rule-form-group">
                                    <label>Monthly Discount</label>
                                    <div class="discount-group">
                                        <div class="field">
                                            <label>Min Days</label>
                                            <input type="number" name="min_days" value="30" min="1" required>
                                        </div>
                                        <div class="field">
                                            <label>Discount %</label>
                                            <input type="number" step="0.01" name="discount_percent" placeholder="%" required>
                                        </div>
                                        <button type="submit" class="btn-add-small"><i class="fas fa-plus"></i></button>
                                    </div>
                                </div>
                            </form>
                        </div>
                        
                        <div style="font-size: 9px; color: #94a3b8; margin-top: 4px; text-align: center;">
                            <i class="fas fa-info-circle"></i> Set minimum days and discount percentage
                        </div>
                        
                    <?php else: ?>
                        <!-- Worker View - Only show info -->
                        <div style="padding:12px; background:#f1f5f9; border-radius:8px; margin-top:12px; text-align:center; font-size:12px; color:#64748b;">
                            <i class="fas fa-lock"></i> Price rules are managed by admins only.<br>
                            <span style="font-size:11px;">Contact an administrator to modify pricing.</span>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
            
            <!-- ========================================== -->
            <!-- COLUMN 3: Active Rules -->
            <!-- ========================================== -->
            <div class="col-card">
                <div class="col-header">
                    <i class="fas fa-list"></i> Active Rules
                    <span class="badge"><?php echo count($rules_list); ?></span>
                </div>
                <div class="col-body">
                    <div class="rules-header-info">
                        <span>Rules for <strong><?php echo htmlspecialchars($car_name); ?></strong></span>
                        <span class="count"><?php echo count($rules_list); ?> active</span>
                    </div>
                    <div class="rules-list-scroll">
                        <?php if(count($rules_list) > 0): ?>
                            <?php foreach($rules_list as $rule): ?>
                            <div class="rule-item">
                                <div class="rule-info">
                                    <div class="rule-name">
                                        <?php 
                                            $icons = [
                                                'base_price' => '💰',
                                                'date_specific' => '📅',
                                                'date_range' => '📆',
                                                'weekly_discount' => '📊',
                                                'monthly_discount' => '📈'
                                            ];
                                            echo $icons[$rule['rule_type']] ?? '•';
                                        ?>
                                        <?php 
                                            $labels = [
                                                'base_price' => 'Base Price',
                                                'date_specific' => 'Date Specific',
                                                'date_range' => 'Date Range',
                                                'weekly_discount' => 'Weekly Discount',
                                                'monthly_discount' => 'Monthly Discount'
                                            ];
                                            echo $labels[$rule['rule_type']];
                                        ?>
                                        <?php if($rule['rule_type'] != 'base_price'): ?>
                                            <span class="rule-type-badge">
                                                <?php if($rule['fixed_price']): ?>Fixed<?php else: ?>%<?php endif; ?>
                                            </span>
                                        <?php endif; ?>
                                    </div>
                                    <div class="rule-detail">
                                        <?php if($rule['start_date'] && $rule['end_date'] && $rule['start_date'] == $rule['end_date']): ?>
                                            📅 <?php echo date('d M Y', strtotime($rule['start_date'])); ?>
                                        <?php elseif($rule['start_date'] && $rule['end_date']): ?>
                                            📆 <?php echo date('d M', strtotime($rule['start_date'])); ?> - <?php echo date('d M Y', strtotime($rule['end_date'])); ?>
                                        <?php elseif($rule['min_days'] > 1): ?>
                                            ⏱ <?php echo $rule['min_days']; ?>+ days
                                        <?php else: ?>
                                            🕒 Always active
                                        <?php endif; ?>
                                    </div>
                                </div>
                                <div style="display:flex; align-items:center; gap:4px;">
                                    <span class="rule-value">
                                        <?php if($rule['fixed_price']): ?>
                                            €<?php echo number_format($rule['fixed_price'], 0); ?>
                                        <?php elseif($rule['discount_percent'] > 0): ?>
                                            -<?php echo $rule['discount_percent']; ?>%
                                        <?php else: ?>
                                            -
                                        <?php endif; ?>
                                    </span>
                                    <?php if(!$is_worker): ?>
                                        <a href="?delete_rule=<?php echo $rule['id']; ?>&car_id=<?php echo $selected_car; ?>" 
                                           class="rule-delete" onclick="return confirm('Delete this rule?')">
                                            <i class="fas fa-times"></i>
                                        </a>
                                    <?php else: ?>
                                        <span class="rule-lock"><i class="fas fa-lock"></i></span>
                                    <?php endif; ?>
                                </div>
                            </div>
                            <?php endforeach; ?>
                        <?php else: ?>
                            <div class="empty-rules">
                                <i class="fas fa-inbox"></i>
                                <p>No price rules for this car</p>
                                <?php if(!$is_worker): ?>
                                    <p style="font-size: 11px; margin-top: 4px;">Add rules from the column on the left</p>
                                <?php else: ?>
                                    <p style="font-size: 11px; margin-top: 4px;">Contact an admin to add rules</p>
                                <?php endif; ?>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- Info Note -->
        <div class="info-note <?php echo $is_worker ? 'worker-info' : ''; ?>">
            <i class="fas fa-<?php echo $is_worker ? 'info-circle' : 'lightbulb'; ?>"></i>
            <div>
                <?php if($is_worker): ?>
                    <strong>View Only Mode:</strong> You can see all price rules but cannot add, edit, or delete them.
                <?php else: ?>
                    <strong>Priority Order:</strong> Specific Date (highest) → Date Range → Weekly/Monthly Discount (lowest).
                    The system automatically applies the best available price. For Weekly/Monthly, you can set custom minimum days.
                <?php endif; ?>
            </div>
        </div>
    </div>
    
    <script>
        // Car Search Filter
        function filterCars() {
            const searchTerm = document.getElementById('carSearch').value.toLowerCase();
            const items = document.querySelectorAll('.car-item-link');
            let visibleCount = 0;
            
            items.forEach(item => {
                const searchData = item.getAttribute('data-search') || '';
                if(searchData.includes(searchTerm)) {
                    item.style.display = 'flex';
                    visibleCount++;
                } else {
                    item.style.display = 'none';
                }
            });
            
            // Show/hide no results message
            let noResults = document.getElementById('noCarsMessage');
            if(visibleCount === 0 && searchTerm !== '') {
                if(!noResults) {
                    noResults = document.createElement('div');
                    noResults.id = 'noCarsMessage';
                    noResults.className = 'no-cars-found';
                    noResults.innerHTML = '<i class="fas fa-search"></i> No cars found matching "' + searchTerm + '"';
                    document.getElementById('carList').appendChild(noResults);
                }
            } else {
                if(noResults) noResults.remove();
            }
            
            document.getElementById('carCount').textContent = visibleCount;
        }
        
        // Toggle price type for date range
        function togglePriceType(select) {
            const value = select.value;
            const form = select.closest('form');
            const input = form.querySelector('input[name="fixed_price"]');
            if(value === 'fixed') {
                input.name = 'fixed_price';
                input.placeholder = 'Price (€)';
            } else {
                input.name = 'discount_percent';
                input.placeholder = 'Discount %';
            }
        }
    </script>
</body>
</html>