<?php
// admin/delivery-agents.php
require_once __DIR__ . '/../includes/config.php';
require_once __DIR__ . '/../includes/functions.php';

if (!isAdmin()) {
    header('Location: login.php');
    exit;
}

// Handle Add Agent
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_agent'])) {
    $name = sanitize($_POST['name'] ?? '');
    $phone = sanitize($_POST['phone'] ?? '');
    $email = sanitize($_POST['email'] ?? '');
    $address = sanitize($_POST['address'] ?? '');
    $area = sanitize($_POST['area'] ?? '');
    $vehicle_type = sanitize($_POST['vehicle_type'] ?? '');
    $vehicle_number = sanitize($_POST['vehicle_number'] ?? '');
    $status = isset($_POST['status']) ? 1 : 0;
    
    if (!empty($name) && !empty($phone)) {
        $agent_code = 'AG-' . date('Ymd') . '-' . rand(1000, 9999);
        $stmt = $pdo->prepare("INSERT INTO delivery_agents 
            (agent_code, name, phone, email, address, area, vehicle_type, vehicle_number, status) 
            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
        $stmt->execute([$agent_code, $name, $phone, $email, $address, $area, $vehicle_type, $vehicle_number, $status]);
        $_SESSION['success_msg'] = 'Delivery agent added successfully!';
        header('Location: delivery-agents.php');
        exit;
    }
}

// Get agents
$agents = $pdo->query("SELECT * FROM delivery_agents ORDER BY name")->fetchAll();

$success_msg = $_SESSION['success_msg'] ?? '';
unset($_SESSION['success_msg']);

include 'header.php';
?>

<div class="admin-content">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2><i class="fa-solid fa-user-tie me-2"></i> Delivery Agents</h2>
        <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#agentModal">
            <i class="fa-solid fa-plus"></i> Add Agent
        </button>
    </div>
    
    <?php if ($success_msg): ?>
    <div class="alert alert-success"><?php echo $success_msg; ?></div>
    <?php endif; ?>
    
    <div class="admin-card">
        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>Code</th>
                        <th>Name</th>
                        <th>Phone</th>
                        <th>Area</th>
                        <th>Vehicle</th>
                        <th>Deliveries</th>
                        <th>Status</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($agents)): ?>
                    <tr><td colspan="8" class="text-center">No delivery agents found</td></tr>
                    <?php else: ?>
                    <?php foreach ($agents as $agent): ?>
                    <tr>
                        <td><code><?php echo $agent['agent_code']; ?></code></td>
                        <td><strong><?php echo htmlspecialchars($agent['name']); ?></strong></td>
                        <td><?php echo htmlspecialchars($agent['phone']); ?></td>
                        <td><?php echo htmlspecialchars($agent['area'] ?? '-'); ?></td>
                        <td><?php echo htmlspecialchars($agent['vehicle_type'] ?? '-'); ?></td>
                        <td>
                            <span class="badge bg-info"><?php echo $agent['total_deliveries']; ?></span>
                            <span class="badge bg-success"><?php echo $agent['successful_deliveries']; ?></span>
                        </td>
                        <td>
                            <span class="badge bg-<?php echo $agent['status'] ? 'success' : 'danger'; ?>">
                                <?php echo $agent['status'] ? 'Active' : 'Inactive'; ?>
                            </span>
                        </td>
                        <td>
                            <button class="btn btn-sm btn-warning"><i class="fa-solid fa-edit"></i></button>
                            <button class="btn btn-sm btn-danger"><i class="fa-solid fa-trash"></i></button>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<!-- Add Agent Modal -->
<div class="modal fade" id="agentModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Add Delivery Agent</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST">
                <input type="hidden" name="add_agent" value="1">
                <div class="modal-body">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">Name *</label>
                                <input type="text" name="name" class="form-control" required>
                            </div>
                            <div class="mb-3">
                                <label class="form-label">Phone *</label>
                                <input type="text" name="phone" class="form-control" required>
                            </div>
                            <div class="mb-3">
                                <label class="form-label">Email</label>
                                <input type="email" name="email" class="form-control">
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">Area</label>
                                <input type="text" name="area" class="form-control">
                            </div>
                            <div class="mb-3">
                                <label class="form-label">Vehicle Type</label>
                                <select name="vehicle_type" class="form-control">
                                    <option value="">Select</option>
                                    <option value="Bike">Bike</option>
                                    <option value="Car">Car</option>
                                    <option value="Van">Van</option>
                                    <option value="Truck">Truck</option>
                                </select>
                            </div>
                            <div class="mb-3">
                                <label class="form-label">Vehicle Number</label>
                                <input type="text" name="vehicle_number" class="form-control">
                            </div>
                            <div class="mb-3">
                                <div class="form-check">
                                    <input type="checkbox" name="status" class="form-check-input" checked>
                                    <label class="form-check-label">Active</label>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Address</label>
                        <textarea name="address" class="form-control" rows="2"></textarea>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-primary">Save</button>
                </div>
            </form>
        </div>
    </div>
</div>

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