<?php
session_start();
include '../db_config.php';
include '../functions.php';
check_login();
check_role('employer');

$emp_id = $_SESSION['user_id'];
$wid = sanitize_input($_GET['worker_id']);
$jid = intval($_GET['job_id']);

$worker = $conn->query("SELECT full_name FROM employee_details WHERE employee_nic = '$wid'")->fetch_assoc();

if(isset($_POST['submit_review'])) {
    $rating = intval($_POST['rating']);
    $comment = sanitize_input($_POST['comment']);
    
    // Check if already reviewed
    $check = $conn->query("SELECT id FROM reviews WHERE job_id = $jid AND employee_nic = '$wid' AND employer_id = $emp_id");
    if($check->num_rows == 0) {
        $conn->query("INSERT INTO reviews (job_id, employee_nic, employer_id, rating, comment) VALUES ($jid, '$wid', $emp_id, $rating, '$comment')");
        header("Location: dashboard.php?msg=ReviewSubmitted");
        exit();
    } else {
        echo "You have already reviewed this worker for this job."; exit();
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Rate Worker | Work For Sri Lanka</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <script src="https://unpkg.com/lucide@latest"></script>
    <style>
        body { font-family: 'Inter', sans-serif; }
        .glass-panel {
            background: rgba(30, 41, 59, 0.4);
            backdrop-filter: blur(12px);
            border: 1px solid rgba(255, 255, 255, 0.05);
        }
        .form-input {
            background: rgba(15, 23, 42, 0.6);
            border: 1px solid rgba(255, 255, 255, 0.1);
            color: white;
        }
        .form-input:focus {
            border-color: #6366f1;
            outline: none;
            box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.2);
        }
        /* Star Rating Styles */
        .rating {
            display: flex;
            flex-direction: row-reverse;
            justify-content: flex-end;
            gap: 0.5rem;
        }
        .rating input {
            display: none;
        }
        .rating label {
            cursor: pointer;
            color: #475569;
            transition: color 0.2s;
        }
        .rating label:hover,
        .rating label:hover ~ label,
        .rating input:checked ~ label {
            color: #fbbf24;
            fill: #fbbf24;
        }
        .rating label svg {
            width: 2rem;
            height: 2rem;
        }
    </style>
</head>
<body class="bg-slate-950 min-h-screen flex flex-col text-slate-200 selection:bg-indigo-500 selection:text-white">
    <!-- Navbar -->
    <nav class="bg-slate-900/50 backdrop-blur-md border-b border-slate-800 sticky top-0 z-50">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="flex justify-between h-16 items-center">
                <a href="dashboard.php" class="flex items-center text-slate-400 hover:text-white transition group gap-2">
                    <i data-lucide="arrow-left" class="w-5 h-5 group-hover:-translate-x-1 transition-transform"></i>
                    Back to Dashboard
                </a>
                <div class="text-lg font-bold text-white hidden md:block">Rate Worker</div>
                <div class="w-20 hidden md:block"></div>
            </div>
        </div>
    </nav>

    <div class="flex-1 flex items-center justify-center p-4">
        <div class="glass-panel w-full max-w-lg p-8 rounded-2xl shadow-2xl relative">
            <div class="absolute -top-10 left-1/2 transform -translate-x-1/2">
                <div class="w-20 h-20 bg-indigo-600 rounded-2xl flex items-center justify-center shadow-lg shadow-indigo-500/30 rotate-3">
                    <i data-lucide="star" class="w-10 h-10 text-white fill-current"></i>
                </div>
            </div>

            <h2 class="text-2xl font-bold text-center text-white mt-8 mb-2">Rate Your Experience</h2>
            <p class="text-slate-400 text-center mb-8">
                How was your experience working with <span class="text-indigo-400 font-semibold"><?=$worker['full_name']?></span>?
            </p>

            <form method="post" class="space-y-6">
                <!-- Star Rating -->
                <div class="flex flex-col items-center gap-2">
                    <label class="text-sm font-medium text-slate-300">Select Rating</label>
                    <div class="rating">
                        <input type="radio" id="star5" name="rating" value="5" required/><label for="star5" title="Excellent"><i data-lucide="star"></i></label>
                        <input type="radio" id="star4" name="rating" value="4"/><label for="star4" title="Good"><i data-lucide="star"></i></label>
                        <input type="radio" id="star3" name="rating" value="3"/><label for="star3" title="Average"><i data-lucide="star"></i></label>
                        <input type="radio" id="star2" name="rating" value="2"/><label for="star2" title="Poor"><i data-lucide="star"></i></label>
                        <input type="radio" id="star1" name="rating" value="1"/><label for="star1" title="Terrible"><i data-lucide="star"></i></label>
                    </div>
                </div>

                <!-- Comment -->
                <div class="space-y-2">
                    <label class="block text-sm font-medium text-slate-300">Private Feedback</label>
                    <textarea name="comment" required
                        class="form-input w-full rounded-xl px-4 py-3 min-h-[120px] focus:ring-0 transition-all placeholder-slate-500"
                        placeholder="Share details about your experience..."></textarea>
                </div>

                <div class="flex gap-4 pt-4">
                    <a href="dashboard.php" class="flex-1 py-3 px-4 bg-slate-800 hover:bg-slate-700 text-slate-300 rounded-xl font-medium transition text-center border border-slate-700">
                        Cancel
                    </a>
                    <button type="submit" name="submit_review" class="flex-1 py-3 px-4 bg-indigo-600 hover:bg-indigo-500 text-white rounded-xl font-medium transition shadow-lg shadow-indigo-900/20 flex justify-center items-center gap-2">
                        <span>Submit Review</span>
                        <i data-lucide="send" class="w-4 h-4"></i>
                    </button>
                </div>
            </form>
        </div>
    </div>
    <script>lucide.createIcons();</script>
</body>
</html>
