<?php
/**
 * Drivers Management Page
 * Dozar & Tripper Management System
 */

require_once __DIR__ . '/../config/config.php';
require_once __DIR__ . '/../config/db.php';
require_once __DIR__ . '/../includes/auth.php';

$user = getCurrentUser();
$pdo = getPDO();

// Handle add/edit/delete
if ($_SERVER['REQUEST_METHOD'] === 'POST' && hasRole('admin')) {
    $action = $_POST['action'] ?? '';
    
    if ($action === 'add') {
        $full_name = sanitize($_POST['full_name'] ?? '');
        $phone_number = sanitize($_POST['phone_number'] ?? '');
        $address = sanitize($_POST['address'] ?? '');
        $license_number = sanitize($_POST['license_number'] ?? '');
        
        if ($full_name) {
            $stmt = $pdo->prepare("INSERT INTO drivers (full_name, phone_number, address, license_number) VALUES (?, ?, ?, ?)");
            $stmt->execute([$full_name, $phone_number, $address, $license_number]);
            showMessage(t('msg_save_success', 'Driver added successfully!'), 'success');
        }
    } elseif ($action === 'delete') {
        $id = $_POST['id'] ?? '';
        if ($id) {
            $stmt = $pdo->prepare("DELETE FROM drivers WHERE id=?");
            $stmt->execute([$id]);
            showMessage(t('msg_delete_success', 'Driver deleted successfully!'), 'success');
        }
    }
}

// Get all drivers
$drivers = $pdo->query("SELECT * FROM drivers ORDER BY full_name")->fetchAll();
?>
<?php include __DIR__ . '/../includes/header.php'; ?>

<div class="row">
    <div class="col-12">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h2><i class="fas fa-id-card"></i> <?php echo t('nav_drivers', 'Drivers'); ?></h2>
            <?php if (hasRole('admin')): ?>
            <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addModal">
                <i class="fas fa-plus"></i> <?php echo t('action_add', 'Add Driver'); ?>
            </button>
            <?php endif; ?>
        </div>
    </div>
</div>

<?php displayMessage(); ?>

<div class="card">
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Phone</th>
                        <th>Address</th>
                        <th>License Number</th>
                        <?php if (hasRole('admin')): ?>
                        <th>Actions</th>
                        <?php endif; ?>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($drivers)): ?>
                    <tr>
                        <td colspan="<?php echo hasRole('admin') ? 5 : 4; ?>" class="text-center text-muted">No drivers found</td>
                    </tr>
                    <?php else: ?>
                    <?php foreach ($drivers as $d): ?>
                    <tr>
                        <td><?php echo $d['full_name']; ?></td>
                        <td><?php echo $d['phone_number'] ?? '-'; ?></td>
                        <td><?php echo $d['address'] ?? '-'; ?></td>
                        <td><?php echo $d['license_number'] ?? '-'; ?></td>
                        <?php if (hasRole('admin')): ?>
                        <td>
                            <a href="view.php?id=<?php echo $d['id']; ?>" class="btn btn-sm btn-info">
                                <i class="fas fa-eye"></i>
                            </a>
                            <form method="POST" style="display:inline;">
                                <input type="hidden" name="action" value="delete">
                                <input type="hidden" name="id" value="<?php echo $d['id']; ?>">
                                <button type="submit" class="btn btn-sm btn-danger" onclick="return confirm('Delete this driver?')">
                                    <i class="fas fa-trash"></i>
                                </button>
                            </form>
                        </td>
                        <?php endif; ?>
                    </tr>
                    <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<!-- Add Modal -->
<?php if (hasRole('admin')): ?>
<div class="modal fade" id="addModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Add Driver</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST">
                <div class="modal-body">
                    <input type="hidden" name="action" value="add">
                    <div class="mb-3">
                        <label class="form-label">Full Name *</label>
                        <input type="text" name="full_name" class="form-control" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Phone Number</label>
                        <input type="text" name="phone_number" class="form-control">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Address</label>
                        <input type="text" name="address" class="form-control">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">License Number</label>
                        <input type="text" name="license_number" class="form-control">
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-primary">Add</button>
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                </div>
            </form>
        </div>
    </div>
</div>
<?php endif; ?>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<?php include __DIR__ . '/../includes/footer.php'; ?>
