<?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();
}

$technician_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($technician_id <= 0) {
    header("Location: technician-management.php");
    exit();
}

// Fetch Technician Details
$stmt = $conn->prepare("SELECT * FROM technician_master WHERE id = ?");
$stmt->bind_param("i", $technician_id);
$stmt->execute();
$technician = $stmt->get_result()->fetch_assoc();
$stmt->close();

if (!$technician) {
    header("Location: technician-management.php");
    exit();
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>View Technician - <?= htmlspecialchars($technician['technician_code']) ?></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: 1100px; 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;
        }

        .content { padding: 45px 40px; }

        .tech-header {
            text-align: center;
            margin-bottom: 35px;
        }

        .tech-code {
            font-size: 2.2rem;
            font-weight: 700;
            color: #c4b5fd;
            margin-bottom: 8px;
        }

        .status-badge {
            padding: 10px 26px;
            border-radius: 50px;
            font-weight: 700;
            display: inline-block;
            margin-top: 10px;
        }
        .status-Active   { background: #22c55e; color: white; }
        .status-Inactive { background: #64748b; color: white; }

        .info-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 25px;
            margin: 35px 0;
        }

        .info-card {
            background: rgba(255,255,255,0.06);
            border: 1px solid rgba(255,255,255,0.15);
            border-radius: 20px;
            padding: 30px;
        }

        .info-card h3 {
            margin-bottom: 22px;
            font-size: 1.4rem;
            display: flex;
            align-items: center;
            gap: 12px;
            color: #ddd6fe;
        }

        .detail-row {
            margin-bottom: 20px;
        }

        .label {
            font-size: 0.95rem;
            opacity: 0.75;
            margin-bottom: 6px;
        }

        .value {
            font-size: 1.15rem;
            font-weight: 600;
            word-break: break-word;
        }

        .actions {
            margin-top: 40px;
            display: flex;
            gap: 15px;
            flex-wrap: wrap;
            justify-content: center;
        }

        .btn {
            padding: 14px 32px;
            border-radius: 16px;
            font-weight: 700;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            gap: 10px;
            transition: all 0.3s;
        }

        .btn-back {
            background: rgba(255,255,255,0.15);
            color: white;
        }

        .btn-edit {
            background: linear-gradient(135deg, var(--primary), var(--accent));
            color: white;
        }

        @media (max-width: 768px) {
            .glass-container { border-radius: 24px; }
            .header, .content { padding: 35px 25px; }
            .info-grid { grid-template-columns: 1fr; }
        }
    </style>
</head>
<body>

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

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

        <!-- Header -->
        <div class="header">
            <a href="technician-management.php" style="color:#c4b5fd; text-decoration:none; position:absolute; left:40px; top:35px; font-weight:500;">
                ← Back to Technicians
            </a>
            <h1><i class="fas fa-tools"></i> Technician Profile</h1>
        </div>

        <div class="content">

            <div class="tech-header">
                <div class="tech-code"><?= htmlspecialchars($technician['technician_code']) ?></div>
                <h2><?= htmlspecialchars($technician['full_name']) ?></h2>
            </div>

            <div class="info-grid">

                <!-- Contact Information -->
                <div class="info-card">
                    <h3><i class="fas fa-address-book"></i> Contact Details</h3>
                    <div class="detail-row">
                        <div class="label">Mobile Number</div>
                        <div class="value"><?= htmlspecialchars($technician['mobile']) ?></div>
                    </div>
                    <div class="detail-row">
                        <div class="label">Email Address</div>
                        <div class="value">
                            <?= !empty($technician['email']) ? htmlspecialchars($technician['email']) : '<span style="opacity:0.6;">Not provided</span>' ?>
                        </div>
                    </div>
                    <div class="detail-row">
                        <div class="label">Address</div>
                        <div class="value">
                            <?= !empty($technician['address']) ? nl2br(htmlspecialchars($technician['address'])) : '<span style="opacity:0.6;">Not provided</span>' ?>
                        </div>
                    </div>
                </div>

                <!-- Professional Information -->
                <div class="info-card">
                    <h3><i class="fas fa-user-tie"></i> Professional Details</h3>
                    <div class="detail-row">
                        <div class="label">Skill / Specialization</div>
                        <div class="value"><?= htmlspecialchars($technician['skill']) ?></div>
                    </div>
                    <div class="detail-row">
                        <div class="label">Experience</div>
                        <div class="value"><?= htmlspecialchars($technician['experience'] ?? 'Not specified') ?></div>
                    </div>
                    <div class="detail-row">
                        <div class="label">Joining Date</div>
                        <div class="value"><?= date('d M Y', strtotime($technician['joining_date'])) ?></div>
                    </div>
                    <div class="detail-row">
                        <div class="label">Status</div>
                        <div>
                            <span class="status-badge status-<?= strtolower($technician['status']) ?>">
                                <?= htmlspecialchars($technician['status']) ?>
                            </span>
                        </div>
                    </div>
                </div>
            </div>

            <?php if (!empty($technician['remarks'])): ?>
            <div class="info-card" style="margin-top:20px;">
                <h3><i class="fas fa-comment-dots"></i> Remarks / Notes</h3>
                <p style="line-height:1.7; opacity:0.95;"><?= nl2br(htmlspecialchars($technician['remarks'])) ?></p>
            </div>
            <?php endif; ?>

            <!-- Action Buttons -->
            <div class="actions">
                <a href="technician-management.php" class="btn btn-back">
                    <i class="fas fa-arrow-left"></i> Back to Technicians
                </a>
                <a href="edit_technician.php?id=<?= $technician_id ?>" class="btn btn-edit">
                    <i class="fas fa-edit"></i> Edit Technician
                </a>
            </div>
        </div>
    </div>
</div>

</body>
</html>

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