<?php
session_start();
if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) {
    header('Location: login.php');
    exit;
}

require_once '../db/config.php';

$settings = [];
$stmt = db()->query('SELECT * FROM settings');
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $settings[$row['name']] = $row['value'];
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $stmt = db()->prepare('UPDATE settings SET value = :value WHERE name = :name');
    foreach ($_POST as $name => $value) {
        $stmt->execute(['value' => $value, 'name' => $name]);
    }
    header('Location: settings.php?success=1');
    exit;
}

include 'header.php';
?>

<div class="container mt-4">
    <h2>Settings</h2>

    <?php if (isset($_GET['success'])): ?>
        <div class="alert alert-success">Settings saved successfully!</div>
    <?php endif; ?>

    <form method="POST">
        <div class="card mb-4">
            <div class="card-header">
                <h5>Fees</h5>
            </div>
            <div class="card-body">
                <div class="form-group">
                    <label for="service_fee_percentage">Service Fee Percentage (%)</label>
                    <input type="number" class="form-control" id="service_fee_percentage" name="service_fee_percentage" value="<?php echo htmlspecialchars($settings['service_fee_percentage'] ?? ''); ?>" step="0.01">
                </div>
                <div class="form-group">
                    <label for="delivery_fee">Delivery Fee ($)</label>
                    <input type="number" class="form-control" id="delivery_fee" name="delivery_fee" value="<?php echo htmlspecialchars($settings['delivery_fee'] ?? ''); ?>" step="0.01">
                </div>
            </div>
        </div>

        <div class="card mb-4">
            <div class="card-header">
                <h5>Driver Pay</h5>
            </div>
            <div class="card-body">
                <h6>Base Pay</h6>
                <div class="form-row">
                    <div class="form-group col-md-6">
                        <label for="driver_base_pay_tier1_miles">Base Pay Tier 1 (miles)</label>
                        <input type="number" class="form-control" id="driver_base_pay_tier1_miles" name="driver_base_pay_tier1_miles" value="<?php echo htmlspecialchars($settings['driver_base_pay_tier1_miles'] ?? ''); ?>" step="1">
                    </div>
                    <div class="form-group col-md-6">
                        <label for="driver_base_pay_tier1_amount">Base Pay Tier 1 Amount ($)</label>
                        <input type="number" class="form-control" id="driver_base_pay_tier1_amount" name="driver_base_pay_tier1_amount" value="<?php echo htmlspecialchars($settings['driver_base_pay_tier1_amount'] ?? ''); ?>" step="0.01">
                    </div>
                </div>
                <div class="form-row">
                    <div class="form-group col-md-6">
                        <label for="driver_base_pay_tier2_miles">Base Pay Tier 2 (miles)</label>
                        <input type="number" class="form-control" id="driver_base_pay_tier2_miles" name="driver_base_pay_tier2_miles" value="<?php echo htmlspecialchars($settings['driver_base_pay_tier2_miles'] ?? ''); ?>" step="1">
                    </div>
                    <div class="form-group col-md-6">
                        <label for="driver_base_pay_tier2_amount">Base Pay Tier 2 Amount ($)</label>
                        <input type="number" class="form-control" id="driver_base_pay_tier2_amount" name="driver_base_pay_tier2_amount" value="<?php echo htmlspecialchars($settings['driver_base_pay_tier2_amount'] ?? ''); ?>" step="0.01">
                    </div>
                </div>
                <div class="form-group">
                    <label for="driver_base_pay_tier3_amount">Base Pay Tier 3 Amount (> Tier 2 miles) ($)</label>
                    <input type="number" class="form-control" id="driver_base_pay_tier3_amount" name="driver_base_pay_tier3_amount" value="<?php echo htmlspecialchars($settings['driver_base_pay_tier3_amount'] ?? ''); ?>" step="0.01">
                </div>

                <hr>

                <h6>Mileage Pay</h6>
                <div class="form-group">
                    <label for="driver_mileage_pay_rate">Mileage Pay Rate ($ per mile)</label>
                    <input type="number" class="form-control" id="driver_mileage_pay_rate" name="driver_mileage_pay_rate" value="<?php echo htmlspecialchars($settings['driver_mileage_pay_rate'] ?? ''); ?>" step="0.01">
                </div>

                <hr>

                <h6>Bonuses</h6>
                <div class="form-group">
                    <label for="driver_busy_hour_bonus">Busy Hour Bonus ($ per delivery)</label>
                    <input type="number" class="form-control" id="driver_busy_hour_bonus" name="driver_busy_hour_bonus" value="<?php echo htmlspecialchars($settings['driver_busy_hour_bonus'] ?? ''); ?>" step="0.01">
                </div>
                <div class="form-row">
                    <div class="form-group col-md-4">
                        <label for="driver_quest_bonus_amount">Quest Bonus Amount ($)</label>
                        <input type="number" class="form-control" id="driver_quest_bonus_amount" name="driver_quest_bonus_amount" value="<?php echo htmlspecialchars($settings['driver_quest_bonus_amount'] ?? ''); ?>" step="0.01">
                    </div>
                    <div class="form-group col-md-4">
                        <label for="driver_quest_bonus_deliveries">Quest Bonus Deliveries</label>
                        <input type="number" class="form-control" id="driver_quest_bonus_deliveries" name="driver_quest_bonus_deliveries" value="<?php echo htmlspecialchars($settings['driver_quest_bonus_deliveries'] ?? ''); ?>" step="1">
                    </div>
                    <div class="form-group col-md-4">
                        <label for="driver_quest_bonus_hours">Quest Bonus Hours</label>
                        <input type="number" class="form-control" id="driver_quest_bonus_hours" name="driver_quest_bonus_hours" value="<?php echo htmlspecialchars($settings['driver_quest_bonus_hours'] ?? ''); ?>" step="1">
                    </div>
                </div>
            </div>
        </div>

        <button type="submit" class="btn btn-primary">Save Settings</button>
    </form>
</div>

<?php include 'footer.php'; ?>
