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

check_login();
check_role('admin');

$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
$msg = '';
$error = '';

if(!$id) {
    header("Location: manage_employers.php");
    exit();
}

// Handle Update
if(isset($_POST['update'])) {
    $company_name = sanitize_input($_POST['company_name']);
    $br_no = sanitize_input($_POST['br_no']);
    $contact_no = sanitize_input($_POST['contact_no']);
    $address = sanitize_input($_POST['address']);
    $status = sanitize_input($_POST['status']);
    
    // Update employer_details
    $stmt = $conn->prepare("UPDATE employer_details SET company_name=?, business_reg_no=?, contact_no=?, address=?, status=? WHERE user_id=?");
    $stmt->bind_param("sssssi", $company_name, $br_no, $contact_no, $address, $status, $id);
    
    if($stmt->execute()) {
        $msg = "Employer details updated successfully.";

        // Handle Password Reset
        if(!empty($_POST['reset_password'])) {
            $new_pass = password_hash($_POST['reset_password'], PASSWORD_DEFAULT);
            $up_pass = $conn->prepare("UPDATE employer_login SET password=? WHERE id=?");
            $up_pass->bind_param("si", $new_pass, $id);
            if($up_pass->execute()) {
                $msg .= " Password has been reset.";
            }
        }
    } else {
        $error = "Error updating details.";
    }
}

// Fetch current details
$stmt = $conn->prepare("SELECT * FROM employer_details WHERE user_id=?");
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
$employer = $result->fetch_assoc();

if(!$employer) {
    $error = "Employer details not found (User might not have completed profile yet).";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit Employer | Admin Panel</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <script src="https://unpkg.com/lucide@latest"></script>
    <style> body { font-family: 'Poppins', sans-serif; } </style>
</head>
<body class="bg-indigo-50 min-h-screen">

    <!-- Navbar -->
    <nav class="bg-white shadow-sm 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-20 items-center">
                <a href="<?= BASE_URL ?>/index.php" class="text-2xl font-bold italic text-slate-800">
                    Work For <span class="text-indigo-600">Sri Lanka</span>
                    <span class="text-xs font-normal text-white bg-slate-800 px-2 py-0.5 rounded ml-2 align-middle">Admin Panel</span>
                </a>
                <div class="flex items-center gap-6">
                    <span class="text-sm font-medium text-slate-500 hidden md:inline">Logged in as: <span class="text-indigo-600"><?=$_SESSION['username']?></span></span>
                    <a href="../logout.php" class="text-slate-500 hover:text-red-600 font-medium transition text-sm flex items-center">
                        <i data-lucide="log-out" class="w-4 h-4 mr-2"></i> Logout
                    </a>
                </div>
            </div>
        </div>
    </nav>

    <!-- Header -->
    <div class="bg-indigo-900 py-10 px-4 shadow-lg text-white mb-8">
        <div class="max-w-4xl mx-auto flex flex-col md:flex-row justify-between items-center gap-4">
            <div>
                <h1 class="text-3xl font-bold mb-2">Edit Employer</h1>
                <p class="text-indigo-200">Update company details.</p>
            </div>
            <a href="manage_employers.php" class="bg-white/10 hover:bg-white/20 text-white px-5 py-2 rounded-xl font-bold transition flex items-center backdrop-blur-sm shadow-sm text-sm">
                <i data-lucide="arrow-left" class="w-4 h-4 mr-2"></i> Back to List
            </a>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-4xl mx-auto px-4 pb-20">
        
        <?php if($msg): ?>
            <div class="bg-green-50 border border-green-200 text-green-700 px-4 py-3 rounded-xl mb-6 flex items-center shadow-sm">
                <i data-lucide="check-circle" class="w-5 h-5 mr-3"></i>
                <span class="font-medium"><?=$msg?></span>
            </div>
        <?php endif; ?>

        <?php if($error): ?>
            <div class="bg-red-50 border border-red-200 text-red-700 px-4 py-3 rounded-xl mb-6 flex items-center shadow-sm">
                <i data-lucide="alert-circle" class="w-5 h-5 mr-3"></i>
                <span class="font-medium"><?=$error?></span>
            </div>
        <?php endif; ?>

        <?php if($employer): ?>
        <div class="bg-white rounded-3xl shadow-xl overflow-hidden border border-slate-100">
            <div class="p-6 border-b border-slate-100 bg-slate-50/50">
                <h2 class="text-lg font-bold text-slate-800 flex items-center">
                    <i data-lucide="edit" class="w-5 h-5 mr-2 text-indigo-600"></i> Update Information
                </h2>
            </div>
            
            <form method="post" class="p-8 space-y-6">
                <input type="hidden" name="update" value="1">
                
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <div class="md:col-span-2">
                        <label class="block text-sm font-bold text-slate-700 mb-2">Company Name</label>
                        <input type="text" name="company_name" value="<?=$employer['company_name']?>" class="w-full px-4 py-3 bg-slate-50 border border-slate-200 rounded-xl text-sm focus:bg-white focus:border-indigo-500 focus:ring-2 focus:ring-indigo-200 transition outline-none">
                    </div>

                    <div class="md:col-span-2">
                        <label class="block text-sm font-bold text-slate-700 mb-2">Business Registration Number</label>
                        <input type="text" name="br_no" value="<?=$employer['business_reg_no']?>" class="w-full px-4 py-3 bg-slate-50 border border-slate-200 rounded-xl text-sm focus:bg-white focus:border-indigo-500 focus:ring-2 focus:ring-indigo-200 transition outline-none">
                    </div>
                    
                    <div>
                        <label class="block text-sm font-bold text-slate-700 mb-2">Contact Number</label>
                        <input type="text" name="contact_no" value="<?=$employer['contact_no']?>" class="w-full px-4 py-3 bg-slate-50 border border-slate-200 rounded-xl text-sm focus:bg-white focus:border-indigo-500 focus:ring-2 focus:ring-indigo-200 transition outline-none">
                    </div>
                    
                    <div>
                        <label class="block text-sm font-bold text-slate-700 mb-2">Business Address</label>
                         <input type="text" name="address" value="<?=$employer['address']?>" class="w-full px-4 py-3 bg-slate-50 border border-slate-200 rounded-xl text-sm focus:bg-white focus:border-indigo-500 focus:ring-2 focus:ring-indigo-200 transition outline-none">
                    </div>

                    <div class="md:col-span-2">
                        <label class="block text-sm font-bold text-slate-700 mb-2">Account Status</label>
                        <div class="flex gap-4">
                            <label class="flex items-center gap-2 cursor-pointer bg-slate-50 px-4 py-3 rounded-xl border border-slate-200 hover:border-indigo-300 transition w-full">
                                <input type="radio" name="status" value="Approved" <?=$employer['status']=='Approved'?'checked':''?> class="w-4 h-4 text-indigo-600 focus:ring-indigo-500">
                                <span class="text-sm font-medium text-slate-700">Approved</span>
                            </label>
                            <label class="flex items-center gap-2 cursor-pointer bg-slate-50 px-4 py-3 rounded-xl border border-slate-200 hover:border-indigo-300 transition w-full">
                                <input type="radio" name="status" value="Pending" <?=$employer['status']=='Pending'?'checked':''?> class="w-4 h-4 text-yellow-500 focus:ring-yellow-500">
                                <span class="text-sm font-medium text-slate-700">Pending</span>
                            </label>
                            <label class="flex items-center gap-2 cursor-pointer bg-slate-50 px-4 py-3 rounded-xl border border-slate-200 hover:border-indigo-300 transition w-full">
                                <input type="radio" name="status" value="Rejected" <?=$employer['status']=='Rejected'?'checked':''?> class="w-4 h-4 text-red-500 focus:ring-red-500">
                                <span class="text-sm font-medium text-slate-700">Rejected</span>
                            </label>
                        </div>
                    </div>

                    <div class="md:col-span-2 border-t border-slate-100 pt-6 mt-2">
                         <h3 class="text-sm font-bold text-slate-800 mb-4 flex items-center"><i data-lucide="lock" class="w-4 h-4 mr-2"></i> Security</h3>
                         <label class="block text-sm font-bold text-slate-700 mb-2">Reset Password <span class="text-xs font-normal text-slate-400">(Leave empty to keep current)</span></label>
                         <input type="text" name="reset_password" class="w-full px-4 py-3 bg-red-50 border border-red-100 rounded-xl text-sm focus:bg-white focus:border-red-500 focus:ring-2 focus:ring-red-200 transition outline-none placeholder-red-200 text-red-600" placeholder="Enter new password to force reset">
                    </div>
                </div>

                <div class="pt-4 border-t border-slate-100 flex justify-end">
                    <button type="submit" class="bg-indigo-600 hover:bg-indigo-700 text-white px-8 py-3 rounded-xl font-bold shadow-lg shadow-indigo-200 hover:shadow-indigo-400 transition transform active:scale-95 flex items-center">
                        <i data-lucide="save" class="w-5 h-5 mr-2"></i> Save Changes
                    </button>
                </div>
            </form>
        </div>
        <?php endif; ?>
    </div>

    <!-- Footer -->
    <footer class="bg-white border-t border-slate-200 py-8 text-center text-slate-500 text-sm mt-12">
        <p>&copy; <?=date('Y')?> Work For Sri Lanka. All rights reserved.</p>
    </footer>

    <script>
        lucide.createIcons();
    </script>
</body>
</html>
