<?php
session_start();
require_once("../config/config.php");

// Allowed roles
$allowed_roles = ['store manager', 'admin', 'project manager'];
if (!isset($_SESSION['user_id']) || !in_array($_SESSION['role'], $allowed_roles)) {
    header("Location: ../login.php");
    exit();
}

// Handle Delete
$success = $error = "";
if (isset($_GET['delete'])) {
    $id = (int)$_GET['delete'];
    $stmt = $conn->prepare("DELETE FROM technician_master WHERE id = ?");
    $stmt->bind_param("i", $id);
    if ($stmt->execute()) {
        $success = "Technician deleted successfully.";
    } else {
        $error = "Error deleting technician.";
    }
}

// Search
$search = trim($_GET['search'] ?? '');

$where = "";
$params = [];
$types = "";

if (!empty($search)) {
    $where = "WHERE (technician_code LIKE ? OR full_name LIKE ? OR mobile LIKE ? OR skill LIKE ?)";
    $like = "%$search%";
    $params = [$like, $like, $like, $like];
    $types = "ssss";
}

$query = "
    SELECT * FROM technician_master 
    $where 
    ORDER BY id DESC
";

$stmt = $conn->prepare($query);
if (!empty($params)) {
    $stmt->bind_param($types, ...$params);
}
$stmt->execute();
$result = $stmt->get_result();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Technician Management</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
    <style>
        :root {
            --primary: #a855f7;
            --accent: #c084fc;
            --glass: rgba(255,255,255,0.10);
            --glass-border: rgba(255,255,255,0.20);
        }

        * { margin:0; padding:0; box-sizing:border-box; }

        body {
            font-family: 'Inter', system-ui, sans-serif;
            background: linear-gradient(135deg, rgba(0,0,0,0.78), rgba(0,0,0,0.92)), 
                        url('../background.jpeg') center/cover fixed;
            color: #f1f5f9;
            padding: 20px 15px;
            min-height: 100vh;
        }

        .container { max-width: 1350px; margin: auto; }

        .glass-container {
            background: var(--glass);
            backdrop-filter: blur(24px);
            border-radius: 32px;
            border: 1px solid var(--glass-border);
            box-shadow: 0 25px 80px rgba(0,0,0,0.65);
            overflow: hidden;
        }

        .header {
            background: linear-gradient(90deg, rgba(168,85,247,0.35), rgba(147,51,234,0.25));
            padding: 50px 40px;
            text-align: center;
            position: relative;
        }
        .header h1 {
            font-size: clamp(28px, 5vw, 42px);
            font-weight: 700;
        }

        .top-bar {
            padding: 25px 40px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 15px;
            background: rgba(255,255,255,0.05);
        }

        .search-box {
            flex: 1;
            min-width: 300px;
        }
        .search-box input {
            width: 100%;
            padding: 14px 20px;
            background: rgba(255,255,255,0.09);
            border: 1px solid rgba(255,255,255,0.25);
            border-radius: 16px;
            color: white;
            font-size: 16px;
        }

        .add-btn {
            background: linear-gradient(135deg, var(--primary), var(--accent));
            color: white;
            padding: 14px 28px;
            border-radius: 16px;
            text-decoration: none;
            font-weight: 700;
            display: inline-flex;
            align-items: center;
            gap: 10px;
        }

        table {
            width: 100%;
            border-collapse: collapse;
        }
        th {
            background: rgba(168,85,247,0.3);
            padding: 18px 16px;
            text-align: left;
            font-weight: 600;
        }
        td {
            padding: 18px 16px;
            border-bottom: 1px solid rgba(255,255,255,0.1);
        }

        tr:hover {
            background: rgba(255,255,255,0.08);
        }

        .status {
            padding: 8px 18px;
            border-radius: 50px;
            font-size: 0.9rem;
            font-weight: 700;
        }
        .status-Active   { background: #22c55e; color: white; }
        .status-Inactive { background: #64748b; color: white; }

        .btn-view  { background: rgba(59,130,246,0.2); color: #60a5fa; }
        .btn-edit  { background: rgba(234,179,8,0.2); color: #fde047; }
        .btn-delete{ background: rgba(239,68,68,0.2); color: #f87171; }

        option{
            color: black;
            
        }
        .action-btns{
            display: flex;
            align-items: center;
            justify-content: center;

        }
        .action-btns a {
            margin: 0 6px;
            padding: 8px 14px;
            border-radius: 10px;
            text-decoration: none;
            font-size: 0.95rem;
            
        }
        .no-records {
            text-align: center;
            padding: 100px 20px;
            color: #94a3b8;
        }

        @media (max-width: 768px) {
            .top-bar { flex-direction: column; align-items: stretch; }
            .glass-container { border-radius: 24px; }
        }
    </style>
</head>
<body>

<?php include '../config/header.php'; ?>

<div class="container">
    <div class="glass-container">

        <div class="header">
            <h1><i class="fas fa-tools"></i> Technician Management</h1>
            <p style="margin-top:12px; opacity:0.9;">Manage your service technicians</p>
        </div>

        <div class="top-bar">
            <div class="search-box">
                <form method="GET">
                    <input type="text" name="search" 
                           placeholder="Search by code, name, mobile or skill..." 
                           value="<?= htmlspecialchars($search) ?>">
                </form>
            </div>

            <a href="add_technician.php" class="add-btn">
                <i class="fas fa-plus"></i> Add New Technician
            </a>
        </div>

        <?php if ($success): ?>
            <div style="margin:20px 40px; padding:16px 24px; background:rgba(34,197,94,0.25); border:1px solid #4ade80; color:#86efac; border-radius:16px; font-weight:600;">
                <?= htmlspecialchars($success) ?>
            </div>
        <?php endif; ?>

        <div style="padding:0 40px 40px;">
            <?php if ($result->num_rows > 0): ?>
                <table>
                    <thead>
                        <tr>
                            <th>Technician Code</th>
                            <th>Full Name</th>
                            <th>Mobile</th>
                            <th>Skill / Specialization</th>
                            <th>Status</th>
                            <th style="text-align:center; width:160px;">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php while ($row = $result->fetch_assoc()): ?>
                        <tr>
                            <td><strong><?= htmlspecialchars($row['technician_code']) ?></strong></td>
                            <td><?= htmlspecialchars($row['full_name']) ?></td>
                            <td><?= htmlspecialchars($row['mobile']) ?></td>
                            <td><?= htmlspecialchars($row['skill'] ?: '—') ?></td>
                            <td>
                                <span class="status status-<?= strtolower($row['status'] ?? 'Active') ?>">
                                    <?= htmlspecialchars($row['status'] ?? 'Active') ?>
                                </span>
                            </td>
                            <td class="action-btns" style="text-align:center;">
                                <a href="view_technician.php?id=<?= $row['id'] ?>" title="View" class="btn-view"><i class="fas fa-eye"></i></a>
                                <a href="edit_technician.php?id=<?= $row['id'] ?>" title="Edit" class="btn-edit"><i class="fas fa-edit"></i></a>
                                <a href="?delete=<?= $row['id'] ?>" 
                                   onclick="return confirm('Are you sure you want to delete this technician?')" 
                                   title="Delete" class="btn-delete">
                                    <i class="fas fa-trash"></i>
                                </a>
                            </td>
                        </tr>
                        <?php endwhile; ?>
                    </tbody>
                </table>
            <?php else: ?>
                <div class="no-records">
                    <i class="fas fa-tools" style="font-size:5rem; opacity:0.3; margin-bottom:20px;"></i>
                    <h3>No Technicians Found</h3>
                    <p>Add your first technician to get started.</p>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>

</body>
</html>

<?php $conn->close(); ?>