<?php
// Start session
require_once 'session_config_emp.php';

// Check if employee is logged in
if (!isset($_SESSION['employee_id']) || !isset($_SESSION['cnic'])) {
    header('Location: login_emp.php');
    exit();
}

include 'connect.php';

// Get personal_no from URL
$personal_no = isset($_GET['personal_no']) ? $conn->real_escape_string($_GET['personal_no']) : '';

if (empty($personal_no)) {
    $_SESSION['profile_update_error'] = "Personal number is required.";
    header('Location: employee_profile_view.php');
    exit();
}

// Check if this employee belongs to the logged-in user
$employee_id = $_SESSION['employee_id'];
$cnic = $_SESSION['cnic'];

$check_sql = "SELECT re.emp_numb FROM registrations_emp re 
              WHERE re.id = ? AND re.cnic = ? AND re.emp_numb = ?";
$check_stmt = $conn->prepare($check_sql);
$check_stmt->bind_param("iss", $employee_id, $cnic, $personal_no);
$check_stmt->execute();
$check_result = $check_stmt->get_result();

if ($check_result->num_rows == 0) {
    $_SESSION['profile_update_error'] = "You are not authorized to edit this profile.";
    header('Location: employee_profile_view.php');
    exit();
}

// Fetch employee data
$emp_sql = "SELECT e.* FROM employees e WHERE e.personal_no = ? LIMIT 1";
$stmt_emp = $conn->prepare($emp_sql);
$stmt_emp->bind_param("s", $personal_no);
$stmt_emp->execute();
$emp_result = $stmt_emp->get_result();

if ($emp_result->num_rows == 0) {
    $_SESSION['profile_update_error'] = "Employee not found.";
    header('Location: employee_profile_view.php');
    exit();
}

$row = $emp_result->fetch_assoc();

// Fetch all dropdown data and store in arrays
$districts = [];
$districts_result = $conn->query("SELECT district_id, district_name FROM districts ORDER BY district_name");
while($d = $districts_result->fetch_assoc()) {
    $districts[] = $d;
}

$tehsils = [];
$tehsils_result = $conn->query("SELECT tehsil_id, tehsil_name FROM tehsils ORDER BY tehsil_name");
while($t = $tehsils_result->fetch_assoc()) {
    $tehsils[] = $t;
}

$constituencies = [];
$constituencies_result = $conn->query("SELECT cons_id, cons_name FROM constituency ORDER BY cons_name");
while($c = $constituencies_result->fetch_assoc()) {
    $constituencies[] = $c;
}

$union_councils = [];
$union_councils_result = $conn->query("SELECT uc_id, uc_name FROM union_councils ORDER BY uc_name");
while($u = $union_councils_result->fetch_assoc()) {
    $union_councils[] = $u;
}

$schools = [];
$schools_result = $conn->query("SELECT emis_code, school_name FROM school_emis ORDER BY school_name");
while($s = $schools_result->fetch_assoc()) {
    $schools[] = $s;
}

$employment_types = [];
$employment_types_result = $conn->query("SELECT status_id, status_name FROM employment_status ORDER BY status_name");
while($et = $employment_types_result->fetch_assoc()) {
    $employment_types[] = $et;
}

$designations = [];
$designations_result = $conn->query("SELECT des_id, designation_name FROM designations ORDER BY designation_name");
while($ds = $designations_result->fetch_assoc()) {
    $designations[] = $ds;
}

$next_kin_relations = [];
$next_kin_relations_result = $conn->query("SELECT relationship_id, relationship_name FROM next_kin ORDER BY relationship_name");
while($nk = $next_kin_relations_result->fetch_assoc()) {
    $next_kin_relations[] = $nk;
}

$quota = [];
$quota_result = $conn->query("SELECT quota_id, quota_type FROM quota ORDER BY quota_type");
while($q = $quota_result->fetch_assoc()) {
    $quota[] = $q;
}

$time_scales = [];
$time_scales_result = $conn->query("SELECT ts_id, ts_name FROM time_scales ORDER BY ts_name");
while($ts = $time_scales_result->fetch_assoc()) {
    $time_scales[] = $ts;
}

$bps = [];
$bps_result = $conn->query("SELECT bps_id, bps_name FROM bps ORDER BY bps_id");
while($b = $bps_result->fetch_assoc()) {
    $bps[] = $b;
}

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $error = '';
    $success = '';
    
    // Get form data
    $full_name = $conn->real_escape_string($_POST['full_name']);
    $father_name = $conn->real_escape_string($_POST['father_name']);
    $gender = $conn->real_escape_string($_POST['gender']);
    $marital_status = $conn->real_escape_string($_POST['marital_status']);
    $date_of_birth = $conn->real_escape_string($_POST['date_of_birth']);
    $retirement_date = $conn->real_escape_string($_POST['retirement_date']);
    $birth_place = $conn->real_escape_string($_POST['birth_place']);
    $employment_quota_id = isset($_POST['employment_quota_id']) ? intval($_POST['employment_quota_id']) : 0;
    $mobile_no = $conn->real_escape_string($_POST['mobile_no']);
    $whatsapp_no = $conn->real_escape_string($_POST['whatsapp_no']);
    $gpf_no = $conn->real_escape_string($_POST['gpf_no']);
    $ddo_code = $conn->real_escape_string($_POST['ddo_code']);
    $next_of_kin_name = $conn->real_escape_string($_POST['next_of_kin_name']);
    $next_of_kin_relation_id = isset($_POST['next_of_kin_relation_id']) ? intval($_POST['next_of_kin_relation_id']) : 0;
    $village = $conn->real_escape_string($_POST['village']);
    $post_office = $conn->real_escape_string($_POST['post_office']);
    $district_id = isset($_POST['district_id']) ? intval($_POST['district_id']) : 0;
    $tehsil_id = isset($_POST['tehsil_id']) ? intval($_POST['tehsil_id']) : 0;
    $constituency_id = isset($_POST['constituency_id']) ? intval($_POST['constituency_id']) : 0;
    $union_council_id = isset($_POST['union_council_id']) ? intval($_POST['union_council_id']) : 0;
    $employment_type_id = isset($_POST['employment_type_id']) ? intval($_POST['employment_type_id']) : 0;
    $gross_pay = isset($_POST['gross_pay']) ? floatval($_POST['gross_pay']) : 0;
    $initially_appointed_as_id = isset($_POST['initially_appointed_as_id']) ? intval($_POST['initially_appointed_as_id']) : 0;
    $appointment_date = $conn->real_escape_string($_POST['appointment_date']);
    $current_designation_id = isset($_POST['current_designation_id']) ? intval($_POST['current_designation_id']) : 0;
    $promotion_date = $conn->real_escape_string($_POST['promotion_date']);
    $current_bps_id = isset($_POST['current_bps_id']) ? intval($_POST['current_bps_id']) : 0;
    $current_time_scale_id = isset($_POST['current_time_scale_id']) ? intval($_POST['current_time_scale_id']) : 0;
    $time_scale_date = $conn->real_escape_string($_POST['time_scale_date']);
    $current_school_emis = $conn->real_escape_string($_POST['current_school_emis']);
    $working_since = $conn->real_escape_string($_POST['working_since']);
    $status = $conn->real_escape_string($_POST['status']);
    
    // Validation
    if (empty($full_name) || empty($father_name) || empty($mobile_no)) {
        $error = "Required fields are missing.";
    } else {
        // Update employee record
        $update_sql = "UPDATE employees SET 
            full_name = ?,
            father_name = ?,
            gender = ?,
            marital_status = ?,
            date_of_birth = ?,
            retirement_date = ?,
            birth_place = ?,
            employment_quota_id = ?,
            mobile_no = ?,
            whatsapp_no = ?,
            gpf_no = ?,
            ddo_code = ?,
            next_of_kin_name = ?,
            next_of_kin_relation_id = ?,
            village = ?,
            post_office = ?,
            district_id = ?,
            tehsil_id = ?,
            constituency_id = ?,
            union_council_id = ?,
            employment_type_id = ?,
            gross_pay = ?,
            initially_appointed_as_id = ?,
            appointment_date = ?,
            current_designation_id = ?,
            promotion_date = ?,
            current_bps_id = ?,
            current_time_scale_id = ?,
            time_scale_date = ?,
            current_school_emis = ?,
            working_since = ?,
            status = ?,
            updated_at = NOW()
            WHERE personal_no = ?";
        
        $stmt = $conn->prepare($update_sql);
        $stmt->bind_param(
            "sssssssisssssisiiiiidisssiissssss",
            $full_name,
            $father_name,
            $gender,
            $marital_status,
            $date_of_birth,
            $retirement_date,
            $birth_place,
            $employment_quota_id,
            $mobile_no,
            $whatsapp_no,
            $gpf_no,
            $ddo_code,
            $next_of_kin_name,
            $next_of_kin_relation_id,
            $village,
            $post_office,
            $district_id,
            $tehsil_id,
            $constituency_id,
            $union_council_id,
            $employment_type_id,
            $gross_pay,
            $initially_appointed_as_id,
            $appointment_date,
            $current_designation_id,
            $promotion_date,
            $current_bps_id,
            $current_time_scale_id,
            $time_scale_date,
            $current_school_emis,
            $working_since,
            $status,
            $personal_no
        );
        
        if ($stmt->execute()) {
            $_SESSION['profile_update_success'] = "Profile updated successfully!";
            header('Location: employee_profile_view.php');
            exit();
        } else {
            $error = "Update failed: " . $stmt->error;
        }
    }
}

// Function to format date for input
function formatDateForInput($date) {
    if (empty($date) || $date == '0000-00-00' || $date == '0000-00-00 00:00:00') {
        return '';
    }
    return date("Y-m-d", strtotime($date));
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Edit Employee Profile</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
    :root {
        --primary-green: #198754;
        --secondary-green: #157347;
    }
    
    body {
        font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        background-color: #f8f9fa;
        padding: 20px;
    }
    
    .edit-container {
        max-width: 1200px;
        margin: 0 auto;
    }
    
    .edit-card {
        border: none;
        border-radius: 15px;
        box-shadow: 0 5px 25px rgba(0,0,0,0.1);
        background: white;
        overflow: hidden;
    }
    
    .edit-header {
        background: linear-gradient(135deg, #198754 0%, #0d6efd 100%);
        color: white;
        padding: 25px 30px;
    }
    
    .form-section {
        background: #f8f9fa;
        border-radius: 10px;
        padding: 20px;
        margin-bottom: 20px;
        border-left: 4px solid var(--primary-green);
    }
    
    .section-title {
        font-weight: 600;
        color: var(--primary-green);
        margin-bottom: 20px;
        font-size: 1.1rem;
        display: flex;
        align-items: center;
        gap: 10px;
    }
    
    .btn-save {
        background: var(--primary-green);
        color: white;
        border: none;
        padding: 10px 25px;
        border-radius: 5px;
        font-weight: 500;
        transition: all 0.3s ease;
    }
    
    .btn-save:hover {
        background: var(--secondary-green);
        color: white;
        transform: translateY(-2px);
    }
    
    .btn-cancel {
        background: #6c757d;
        color: white;
        border: none;
        padding: 10px 25px;
        border-radius: 5px;
        font-weight: 500;
        transition: all 0.3s ease;
    }
    
    .btn-cancel:hover {
        background: #5a6268;
        color: white;
        transform: translateY(-2px);
    }
    
    .form-control:focus, .form-select:focus {
        border-color: var(--primary-green);
        box-shadow: 0 0 0 0.25rem rgba(25, 135, 84, 0.25);
    }
    
    .required::after {
        content: " *";
        color: red;
    }
    
    /* Add some spacing for better readability */
    .form-section .row {
        margin-bottom: 10px;
    }
</style>
</head>
<body>
<div class="edit-container">
    <div class="edit-card">
        <div class="edit-header">
            <h2 class="mb-0">
                <i class="fas fa-edit me-2"></i>Edit Employee Profile
            </h2>
            <p class="mb-0 mt-2">Editing profile for: <strong><?php echo htmlspecialchars($row['full_name']); ?></strong> (<?php echo htmlspecialchars($row['personal_no']); ?>)</p>
        </div>
        
        <div class="p-4">
            <?php if(isset($error) && !empty($error)): ?>
                <div class="alert alert-danger alert-dismissible fade show" role="alert">
                    <i class="fas fa-exclamation-circle me-2"></i><?php echo htmlspecialchars($error); ?>
                    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                </div>
            <?php endif; ?>
            
            <form method="POST" action="">
                <!-- SECTION 1: PERSONAL INFORMATION -->
                <div class="form-section">
                    <h5 class="section-title">
                        <i class="fas fa-user"></i>Personal Information
                    </h5>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label required">Full Name</label>
                                <input type="text" class="form-control" name="full_name" value="<?php echo htmlspecialchars($row['full_name']); ?>" required>
                            </div>
                            <div class="mb-3">
                                <label class="form-label required">Father Name</label>
                                <input type="text" class="form-control" name="father_name" value="<?php echo htmlspecialchars($row['father_name']); ?>" required>
                            </div>
                            <div class="mb-3">
                                <label class="form-label required">Gender</label>
                                <select class="form-select" name="gender" required>
                                    <option value="Male" <?php echo $row['gender'] == 'Male' ? 'selected' : ''; ?>>Male</option>
                                    <option value="Female" <?php echo $row['gender'] == 'Female' ? 'selected' : ''; ?>>Female</option>
                                    <option value="Other" <?php echo $row['gender'] == 'Other' ? 'selected' : ''; ?>>Other</option>
                                </select>
                            </div>
                            <div class="mb-3">
                                <label class="form-label">Marital Status</label>
                                <select class="form-select" name="marital_status">
                                    <option value="">Select Status</option>
                                    <option value="Married" <?php echo $row['marital_status'] == 'Married' ? 'selected' : ''; ?>>Married</option>
                                    <option value="Un-Married" <?php echo $row['marital_status'] == 'Un-Married' ? 'selected' : ''; ?>>Un-Married</option>
                                    <option value="Divorced" <?php echo $row['marital_status'] == 'Divorced' ? 'selected' : ''; ?>>Divorced</option>
                                    <option value="Widow" <?php echo $row['marital_status'] == 'Widow' ? 'selected' : ''; ?>>Widow</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label required">Date of Birth</label>
                                <input type="date" class="form-control" name="date_of_birth" value="<?php echo formatDateForInput($row['date_of_birth']); ?>" required>
                            </div>
                            <div class="mb-3">
                                <label class="form-label">Retirement Date</label>
                                <input type="date" class="form-control" name="retirement_date" value="<?php echo formatDateForInput($row['retirement_date']); ?>">
                            </div>
                            <div class="mb-3">
                                <label class="form-label">Birth Place</label>
                                <input type="text" class="form-control" name="birth_place" value="<?php echo htmlspecialchars($row['birth_place']); ?>">
                            </div>
                            <div class="mb-3">
                                <label class="form-label">Employment Quota</label>
                                <select class="form-select" name="employment_quota_id">
                                    <option value="">Select Quota</option>
                                    <?php foreach($quota as $q): ?>
                                        <option value="<?php echo $q['quota_id']; ?>" <?php echo ($row['employment_quota_id'] == $q['quota_id']) ? 'selected' : ''; ?>>
                                            <?php echo htmlspecialchars($q['quota_type']); ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div>
                    </div>
                </div>
                
                <!-- SECTION 2: CONTACT INFORMATION -->
                <div class="form-section">
                    <h5 class="section-title">
                        <i class="fas fa-address-book"></i>Contact Information
                    </h5>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label required">Mobile No</label>
                                <input type="text" class="form-control" name="mobile_no" value="<?php echo htmlspecialchars($row['mobile_no']); ?>" required maxlength="11">
                            </div>
                            <div class="mb-3">
                                <label class="form-label">WhatsApp No</label>
                                <input type="text" class="form-control" name="whatsapp_no" value="<?php echo htmlspecialchars($row['whatsapp_no']); ?>" maxlength="11">
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">GPF No</label>
                                <input type="text" class="form-control" name="gpf_no" value="<?php echo htmlspecialchars($row['gpf_no']); ?>">
                            </div>
                            <div class="mb-3">
                                <label class="form-label required">DDO Code</label>
                                <input type="text" class="form-control" name="ddo_code" value="<?php echo htmlspecialchars($row['ddo_code']); ?>" required maxlength="6">
                            </div>
                        </div>
                    </div>
                </div>
                
                <!-- SECTION 3: NEXT OF KIN -->
                <div class="form-section">
                    <h5 class="section-title">
                        <i class="fas fa-users"></i>Next of Kin Information
                    </h5>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label required">Next of Kin Name</label>
                                <input type="text" class="form-control" name="next_of_kin_name" value="<?php echo htmlspecialchars($row['next_of_kin_name']); ?>" required>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label required">Relationship</label>
                                <select class="form-select" name="next_of_kin_relation_id" required>
                                    <option value="">Select Relationship</option>
                                    <?php foreach($next_kin_relations as $relation): ?>
                                        <option value="<?php echo $relation['relationship_id']; ?>" <?php echo ($row['next_of_kin_relation_id'] == $relation['relationship_id']) ? 'selected' : ''; ?>>
                                            <?php echo htmlspecialchars($relation['relationship_name']); ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div>
                    </div>
                </div>
                
                <!-- SECTION 4: ADDRESS DETAILS -->
                <div class="form-section">
                    <h5 class="section-title">
                        <i class="fas fa-home"></i>Address Details
                    </h5>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">Village</label>
                                <input type="text" class="form-control" name="village" value="<?php echo htmlspecialchars($row['village']); ?>">
                            </div>
                            <div class="mb-3">
                                <label class="form-label">Post Office</label>
                                <input type="text" class="form-control" name="post_office" value="<?php echo htmlspecialchars($row['post_office']); ?>">
                            </div>
                            <div class="mb-3">
                                <label class="form-label required">District</label>
                                <select class="form-select" name="district_id" required>
                                    <option value="">Select District</option>
                                    <?php foreach($districts as $district): ?>
                                        <option value="<?php echo $district['district_id']; ?>" <?php echo ($row['district_id'] == $district['district_id']) ? 'selected' : ''; ?>>
                                            <?php echo htmlspecialchars($district['district_name']); ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <div class="mb-3">
                                <label class="form-label required">Tehsil</label>
                                <select class="form-select" name="tehsil_id" required>
                                    <option value="">Select Tehsil</option>
                                    <?php foreach($tehsils as $tehsil): ?>
                                        <option value="<?php echo $tehsil['tehsil_id']; ?>" <?php echo ($row['tehsil_id'] == $tehsil['tehsil_id']) ? 'selected' : ''; ?>>
                                            <?php echo htmlspecialchars($tehsil['tehsil_name']); ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label required">Constituency</label>
                                <select class="form-select" name="constituency_id" required>
                                    <option value="">Select Constituency</option>
                                    <?php foreach($constituencies as $constituency): ?>
                                        <option value="<?php echo $constituency['cons_id']; ?>" <?php echo ($row['constituency_id'] == $constituency['cons_id']) ? 'selected' : ''; ?>>
                                            <?php echo htmlspecialchars($constituency['cons_name']); ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <div class="mb-3">
                                <label class="form-label required">Union Council</label>
                                <select class="form-select" name="union_council_id" required>
                                    <option value="">Select Union Council</option>
                                    <?php foreach($union_councils as $uc): ?>
                                        <option value="<?php echo $uc['uc_id']; ?>" <?php echo ($row['union_council_id'] == $uc['uc_id']) ? 'selected' : ''; ?>>
                                            <?php echo htmlspecialchars($uc['uc_name']); ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div>
                    </div>
                </div>
                
                <!-- SECTION 5: EMPLOYMENT DETAILS -->
                <div class="form-section">
                    <h5 class="section-title">
                        <i class="fas fa-briefcase"></i>Employment Details
                    </h5>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label required">Employment Type</label>
                                <select class="form-select" name="employment_type_id" required>
                                    <option value="">Select Type</option>
                                    <?php foreach($employment_types as $emp_type): ?>
                                        <option value="<?php echo $emp_type['status_id']; ?>" <?php echo ($row['employment_type_id'] == $emp_type['status_id']) ? 'selected' : ''; ?>>
                                            <?php echo htmlspecialchars($emp_type['status_name']); ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <div class="mb-3">
                                <label class="form-label required">Gross Pay</label>
                                <input type="number" class="form-control" name="gross_pay" value="<?php echo $row['gross_pay']; ?>" required step="0.01" min="0">
                            </div>
                            <div class="mb-3">
                                <label class="form-label required">Initially Appointed As</label>
                                <select class="form-select" name="initially_appointed_as_id" required>
                                    <option value="">Select Designation</option>
                                    <?php foreach($designations as $designation): ?>
                                        <option value="<?php echo $designation['des_id']; ?>" <?php echo ($row['initially_appointed_as_id'] == $designation['des_id']) ? 'selected' : ''; ?>>
                                            <?php echo htmlspecialchars($designation['designation_name']); ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <div class="mb-3">
                                <label class="form-label required">Appointment Date</label>
                                <input type="date" class="form-control" name="appointment_date" value="<?php echo formatDateForInput($row['appointment_date']); ?>" required>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label required">Current Designation</label>
                                <select class="form-select" name="current_designation_id" required>
                                    <option value="">Select Designation</option>
                                    <?php foreach($designations as $designation): ?>
                                        <option value="<?php echo $designation['des_id']; ?>" <?php echo ($row['current_designation_id'] == $designation['des_id']) ? 'selected' : ''; ?>>
                                            <?php echo htmlspecialchars($designation['designation_name']); ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <div class="mb-3">
                                <label class="form-label">Promotion Date</label>
                                <input type="date" class="form-control" name="promotion_date" value="<?php echo formatDateForInput($row['promotion_date']); ?>">
                            </div>
                            <div class="mb-3">
                                <label class="form-label required">Current BPS</label>
                                <select class="form-select" name="current_bps_id" required>
                                    <option value="">Select BPS</option>
                                    <?php foreach($bps as $bps_item): ?>
                                        <option value="<?php echo $bps_item['bps_id']; ?>" <?php echo ($row['current_bps_id'] == $bps_item['bps_id']) ? 'selected' : ''; ?>>
                                            <?php echo htmlspecialchars($bps_item['bps_name']); ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <div class="mb-3">
                                <label class="form-label">Working Since</label>
                                <input type="date" class="form-control" name="working_since" value="<?php echo formatDateForInput($row['working_since']); ?>">
                            </div>
                        </div>
                    </div>
                </div>
                
                <!-- SECTION 6: TIME SCALE INFORMATION -->
                <div class="form-section">
                    <h5 class="section-title">
                        <i class="fas fa-chart-line"></i>Time Scale Information
                    </h5>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label required">Current Time Scale</label>
                                <select class="form-select" name="current_time_scale_id" required>
                                    <option value="">Select Time Scale</option>
                                    <?php foreach($time_scales as $time_scale): ?>
                                        <option value="<?php echo $time_scale['ts_id']; ?>" <?php echo ($row['current_time_scale_id'] == $time_scale['ts_id']) ? 'selected' : ''; ?>>
                                            <?php echo htmlspecialchars($time_scale['ts_name']); ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label required">Time Scale Date</label>
                                <input type="date" class="form-control" name="time_scale_date" value="<?php echo formatDateForInput($row['time_scale_date']); ?>" required>
                            </div>
                        </div>
                    </div>
                </div>
                
                <!-- SECTION 7: SCHOOL INFORMATION -->
                <div class="form-section">
                    <h5 class="section-title">
                        <i class="fas fa-school"></i>School Information
                    </h5>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label required">Current School EMIS</label>
                                <select class="form-select" name="current_school_emis" required>
                                    <option value="">Select School</option>
                                    <?php foreach($schools as $school): ?>
                                        <option value="<?php echo $school['emis_code']; ?>" <?php echo ($row['current_school_emis'] == $school['emis_code']) ? 'selected' : ''; ?>>
                                            <?php echo htmlspecialchars($school['school_name']); ?> (EMIS: <?php echo $school['emis_code']; ?>)
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label required">Status</label>
                                <select class="form-select" name="status" required>
                                    <option value="active" <?php echo $row['status'] == 'active' ? 'selected' : ''; ?>>Active</option>
                                    <option value="inactive" <?php echo $row['status'] == 'inactive' ? 'selected' : ''; ?>>Inactive</option>
                                    <option value="retired" <?php echo $row['status'] == 'retired' ? 'selected' : ''; ?>>Retired</option>
                                </select>
                            </div>
                        </div>
                    </div>
                </div>
                
                <!-- ACTION BUTTONS -->
                <div class="d-flex justify-content-between mt-4">
                    <a href="employee_profile_view.php" class="btn btn-cancel">
                        <i class="fas fa-times me-2"></i>Cancel
                    </a>
                    <button type="submit" class="btn btn-save">
                        <i class="fas fa-save me-2"></i>Save Changes
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
<script>
    // Form validation
    document.querySelector('form').addEventListener('submit', function(e) {
        let isValid = true;
        
        // Check required fields
        const requiredFields = document.querySelectorAll('[required]');
        requiredFields.forEach(field => {
            if (!field.value.trim()) {
                isValid = false;
                field.classList.add('is-invalid');
            } else {
                field.classList.remove('is-invalid');
            }
        });
        
        if (!isValid) {
            e.preventDefault();
            alert('Please fill in all required fields.');
        }
    });
    
    // Add real-time validation
    document.querySelectorAll('[required]').forEach(field => {
        field.addEventListener('blur', function() {
            if (!this.value.trim()) {
                this.classList.add('is-invalid');
            } else {
                this.classList.remove('is-invalid');
            }
        });
    });
</script>
</body>
</html>