<?php
/**
 * Enterprise HRM ERP System - Create Employee
 */

require_once __DIR__ . '/../../includes/init.php';
requireLogin();
requirePermission('EMPLOYEE_CREATE');

$pageTitle = 'Create Employee';
$currentModule = 'employee';

$siteId = getCurrentSiteId();
$siteId = $siteId ?: ($_SESSION['site_id'] ?? null);
if (!$siteId) {
    $_SESSION['error'] = 'Site not found. Please select/assign a site and try again.';
    header('Location: ' . BASE_URL . '/modules/employee/list.php');
    exit;
}
$errors = [];
$formData = [];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Get and sanitize form data
    $formData = [
        'employee_code' => sanitize($_POST['employee_code'] ?? ''),
        'first_name' => sanitize($_POST['first_name'] ?? ''),
        'middle_name' => sanitize($_POST['middle_name'] ?? ''),
        'last_name' => sanitize($_POST['last_name'] ?? ''),
        'email' => sanitize($_POST['email'] ?? ''),
        'phone' => sanitize($_POST['phone'] ?? ''),
        'date_of_birth' => $_POST['date_of_birth'] ?? '',
        'gender' => sanitize($_POST['gender'] ?? ''),
        'marital_status' => sanitize($_POST['marital_status'] ?? ''),
        'national_id' => sanitize($_POST['national_id'] ?? ''),
        'address_line1' => sanitize($_POST['address_line1'] ?? ''),
        'address_line2' => sanitize($_POST['address_line2'] ?? ''),
        'city' => sanitize($_POST['city'] ?? ''),
        'state' => sanitize($_POST['state'] ?? ''),
        'postal_code' => sanitize($_POST['postal_code'] ?? ''),
        'country' => sanitize($_POST['country'] ?? ''),
        'department_id' => isset($_POST['department_id']) ? (int)$_POST['department_id'] : null,
        'designation_id' => isset($_POST['designation_id']) ? (int)$_POST['designation_id'] : null,
        'date_of_joining' => $_POST['date_of_joining'] ?? '',
        'employment_type' => sanitize($_POST['employment_type'] ?? 'full_time'),
        'employment_status' => sanitize($_POST['employment_status'] ?? 'active'),
        'employee_category' => sanitize($_POST['employee_category'] ?? ''),
        'salary' => isset($_POST['salary']) ? (float)$_POST['salary'] : 0.00,
        'bank_name' => sanitize($_POST['bank_name'] ?? ''),
        'bank_account_number' => sanitize($_POST['bank_account_number'] ?? ''),
        'bank_ifsc' => sanitize($_POST['bank_ifsc'] ?? ''),
        'emergency_contact_name' => sanitize($_POST['emergency_contact_name'] ?? ''),
        'emergency_contact_phone' => sanitize($_POST['emergency_contact_phone'] ?? ''),
        'emergency_contact_relation' => sanitize($_POST['emergency_contact_relation'] ?? ''),
        // Statutory fields
        'esic_number' => sanitize($_POST['esic_number'] ?? ''),
        'uan_number' => sanitize($_POST['uan_number'] ?? ''),
        'pan_number' => sanitize($_POST['pan_number'] ?? ''),
        'aadhar_number' => sanitize($_POST['aadhar_number'] ?? ''),
        'remarks' => sanitize($_POST['remarks'] ?? ''),
        // Payment fields
        'pay_mode' => sanitize($_POST['pay_mode'] ?? ''),
        'bank_mode' => sanitize($_POST['bank_mode'] ?? 'NEFT'),
        // Salary components
        'basic_salary' => isset($_POST['basic_salary']) ? (float)$_POST['basic_salary'] : 0.00,
        'hra' => isset($_POST['hra']) ? (float)$_POST['hra'] : 0.00,
        'da' => isset($_POST['da']) ? (float)$_POST['da'] : 0.00,
        'ta' => isset($_POST['ta']) ? (float)$_POST['ta'] : 0.00,
        'special_allowance' => isset($_POST['special_allowance']) ? (float)$_POST['special_allowance'] : 0.00,
    ];
    
    // Validation
    if (empty($formData['employee_code'])) {
        $formData['employee_code'] = generateCode('EMP', 'employees', 'employee_code', $siteId);
    }
    
    if (empty($formData['first_name'])) {
        $errors[] = 'First name is required.';
    }
    
    if (empty($formData['last_name'])) {
        $errors[] = 'Last name is required.';
    }
    
    if (!empty($formData['email']) && !validateEmail($formData['email'])) {
        $errors[] = 'Invalid email address.';
    }
    
    if (empty($formData['date_of_joining'])) {
        $errors[] = 'Date of joining is required.';
    }
    
    // Check if employee code already exists
    if (!empty($formData['employee_code'])) {
        try {
            $db = getDB();
            $stmt = $db->prepare("SELECT COUNT(*) FROM employees WHERE site_id = ? AND employee_code = ?");
            $stmt->execute([$siteId, $formData['employee_code']]);
            if ($stmt->fetchColumn() > 0) {
                $errors[] = 'Employee code already exists.';
            }
        } catch (Exception $e) {
            error_log("Error checking employee code: " . $e->getMessage());
        }
    }
    
    // If no errors, insert employee
    if (empty($errors)) {
        try {
            $db = getDB();
            $userId = getCurrentUserId();
            
            // Check which columns exist in employees table
            $columns = [];
            $values = [];
            $placeholders = [];
            
            $basicFields = [
                'site_id' => $siteId,
                'employee_code' => $formData['employee_code'],
                'first_name' => $formData['first_name'],
                'last_name' => $formData['last_name'],
                'middle_name' => $formData['middle_name'] ?: null,
                'email' => $formData['email'] ?: null,
                'phone' => $formData['phone'] ?: null,
                'date_of_birth' => $formData['date_of_birth'] ?: null,
                'gender' => $formData['gender'] ?: null,
                'marital_status' => $formData['marital_status'] ?: null,
                'national_id' => $formData['national_id'] ?: null,
                'address_line1' => $formData['address_line1'] ?: null,
                'address_line2' => $formData['address_line2'] ?: null,
                'city' => $formData['city'] ?: null,
                'state' => $formData['state'] ?: null,
                'postal_code' => $formData['postal_code'] ?: null,
                'country' => $formData['country'] ?: null,
                'department_id' => $formData['department_id'] ?: null,
                'designation_id' => $formData['designation_id'] ?: null,
                'date_of_joining' => $formData['date_of_joining'],
                'employment_type' => $formData['employment_type'],
                'employment_status' => $formData['employment_status'],
                'employee_category' => $formData['employee_category'] ?: null,
                'salary' => $formData['salary'],
                'bank_name' => $formData['bank_name'] ?: null,
                'bank_account_number' => $formData['bank_account_number'] ?: null,
                'bank_ifsc' => $formData['bank_ifsc'] ?: null,
                'emergency_contact_name' => $formData['emergency_contact_name'] ?: null,
                'emergency_contact_phone' => $formData['emergency_contact_phone'] ?: null,
                'emergency_contact_relation' => $formData['emergency_contact_relation'] ?: null,
                'profile_picture' => null,
                'created_by' => $userId,
                'updated_by' => $userId,
            ];
            
            // Add statutory fields if they exist in table
            $statutoryFields = [
                'esic_number' => $formData['esic_number'] ?: null,
                'uan_number' => $formData['uan_number'] ?: null,
                'pan_number' => $formData['pan_number'] ?: null,
                'aadhar_number' => $formData['aadhar_number'] ?: null,
                'pf_number' => null, // Can be added later
            ];
            
            // Add remarks field if it exists in table
            $remarksField = [
                'remarks' => $formData['remarks'] ?: null,
            ];
            
            // Check which columns exist
            $stmt = $db->query("SHOW COLUMNS FROM employees");
            $existingColumns = $stmt->fetchAll(PDO::FETCH_COLUMN);
            
            foreach ($basicFields as $col => $val) {
                if (in_array($col, $existingColumns)) {
                    $columns[] = $col;
                    $values[] = $val;
                    $placeholders[] = '?';
                }
            }
            
            foreach ($statutoryFields as $col => $val) {
                if (in_array($col, $existingColumns)) {
                    $columns[] = $col;
                    $values[] = $val;
                    $placeholders[] = '?';
                }
            }
            
            foreach ($paymentFields as $col => $val) {
                if (in_array($col, $existingColumns)) {
                    $columns[] = $col;
                    $values[] = $val;
                    $placeholders[] = '?';
                }
            }
            
            foreach ($remarksField as $col => $val) {
                if (in_array($col, $existingColumns)) {
                    $columns[] = $col;
                    $values[] = $val;
                    $placeholders[] = '?';
                }
            }
            
            $sql = "INSERT INTO employees (" . implode(', ', $columns) . ") VALUES (" . implode(', ', $placeholders) . ")";
            $stmt = $db->prepare($sql);
            $stmt->execute($values);
            
            $employeeId = $db->lastInsertId();
            
            // Save salary components to employee_salary_structure if table exists
            if (!empty($formData['basic_salary']) || !empty($formData['hra']) || !empty($formData['da']) || !empty($formData['ta']) || !empty($formData['special_allowance'])) {
                try {
                    // Check if employee_salary_structure table exists
                    $stmt = $db->query("SHOW TABLES LIKE 'employee_salary_structure'");
                    if ($stmt->rowCount() > 0) {
                        // Get component IDs or create them
                        $components = [
                            'BASIC' => $formData['basic_salary'],
                            'HRA' => $formData['hra'],
                            'DA' => $formData['da'],
                            'TA' => $formData['ta'],
                            'SPECIAL_ALLOWANCE' => $formData['special_allowance'],
                        ];
                        
                        foreach ($components as $compCode => $amount) {
                            if ($amount > 0) {
                                // Get or create component
                                $compStmt = $db->prepare("SELECT component_id FROM salary_components WHERE site_id = ? AND component_code = ?");
                                $compStmt->execute([$siteId, $compCode]);
                                $component = $compStmt->fetch();
                                
                                if (!$component) {
                                    // Create component
                                    $compType = 'earning';
                                    $compStmt = $db->prepare("INSERT INTO salary_components (site_id, component_code, component_name, component_type, calculation_type, calculation_value) VALUES (?, ?, ?, ?, 'fixed', 0)");
                                    $compStmt->execute([$siteId, $compCode, ucfirst(str_replace('_', ' ', $compCode)), $compType]);
                                    $compId = $db->lastInsertId();
                                } else {
                                    $compId = $component['component_id'];
                                }
                                
                                // Insert salary structure
                                $structStmt = $db->prepare("INSERT INTO employee_salary_structure (site_id, employee_id, component_id, amount, effective_from, structure_status) VALUES (?, ?, ?, ?, CURDATE(), 'active')");
                                $structStmt->execute([$siteId, $employeeId, $compId, $amount]);
                            }
                        }
                    }
                } catch (Exception $e) {
                    error_log("Error saving salary structure: " . $e->getMessage());
                    // Continue even if salary structure save fails
                }
            }
            
            $employeeId = $db->lastInsertId();
            
            // Log audit
            logAudit('CREATE', 'employees', $employeeId, null, $formData);
            
            redirect(BASE_URL . '/modules/employee/list.php', 'Employee created successfully.');
            
        } catch (Exception $e) {
            error_log("Error creating employee: " . $e->getMessage());
            $errors[] = 'Error creating employee: ' . htmlspecialchars($e->getMessage());
        }
    }
}

// Get departments and designations
try {
    $db = getDB();
    $stmt = $db->prepare("SELECT department_id, department_name FROM departments WHERE site_id = ? AND department_status = 'active' ORDER BY department_name");
    $stmt->execute([$siteId]);
    $departments = $stmt->fetchAll();
    
    $stmt = $db->prepare("SELECT designation_id, designation_name FROM designations WHERE site_id = ? AND designation_status = 'active' ORDER BY designation_name");
    $stmt->execute([$siteId]);
    $designations = $stmt->fetchAll();
} catch (Exception $e) {
    error_log("Error loading departments/designations: " . $e->getMessage());
    $departments = [];
    $designations = [];
}

include __DIR__ . '/../../includes/header.php';
?>

<div class="card">
    <div class="card-header">
        <h2 class="card-title">Create New Employee</h2>
        <a href="<?php echo BASE_URL; ?>/modules/employee/list.php" class="btn btn-secondary">Back to List</a>
    </div>
    
    <?php if (!empty($errors)): ?>
    <div class="alert alert-error">
        <ul style="margin: 0; padding-left: 20px;">
            <?php foreach ($errors as $error): ?>
            <li><?php echo htmlspecialchars($error); ?></li>
            <?php endforeach; ?>
        </ul>
    </div>
    <?php endif; ?>
    
    <form method="POST" id="employeeForm">
        <div style="display: grid; grid-template-columns: repeat(2, 1fr); gap: 20px;">
            <!-- Basic Information -->
            <div>
                <h3 style="margin-bottom: 15px; padding-bottom: 10px; border-bottom: 1px solid var(--border-color);">Basic Information</h3>
                
                <div class="form-group">
                    <label class="form-label" for="employee_code">Employee Code *</label>
                    <input type="text" id="employee_code" name="employee_code" class="form-control" 
                           value="<?php echo htmlspecialchars($formData['employee_code'] ?? ''); ?>" 
                           placeholder="Auto-generated if left empty">
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="first_name">First Name *</label>
                    <input type="text" id="first_name" name="first_name" class="form-control" required
                           value="<?php echo htmlspecialchars($formData['first_name'] ?? ''); ?>">
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="middle_name">Middle Name</label>
                    <input type="text" id="middle_name" name="middle_name" class="form-control"
                           value="<?php echo htmlspecialchars($formData['middle_name'] ?? ''); ?>">
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="last_name">Last Name *</label>
                    <input type="text" id="last_name" name="last_name" class="form-control" required
                           value="<?php echo htmlspecialchars($formData['last_name'] ?? ''); ?>">
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="email">Email</label>
                    <input type="email" id="email" name="email" class="form-control"
                           value="<?php echo htmlspecialchars($formData['email'] ?? ''); ?>">
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="phone">Phone</label>
                    <input type="text" id="phone" name="phone" class="form-control"
                           value="<?php echo htmlspecialchars($formData['phone'] ?? ''); ?>">
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="date_of_birth">Date of Birth</label>
                    <input type="date" id="date_of_birth" name="date_of_birth" class="form-control"
                           value="<?php echo htmlspecialchars($formData['date_of_birth'] ?? ''); ?>">
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="gender">Gender</label>
                    <select id="gender" name="gender" class="form-control">
                        <option value="">Select Gender</option>
                        <option value="male" <?php echo ($formData['gender'] ?? '') === 'male' ? 'selected' : ''; ?>>Male</option>
                        <option value="female" <?php echo ($formData['gender'] ?? '') === 'female' ? 'selected' : ''; ?>>Female</option>
                        <option value="other" <?php echo ($formData['gender'] ?? '') === 'other' ? 'selected' : ''; ?>>Other</option>
                    </select>
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="marital_status">Marital Status</label>
                    <select id="marital_status" name="marital_status" class="form-control">
                        <option value="">Select Status</option>
                        <option value="single" <?php echo ($formData['marital_status'] ?? '') === 'single' ? 'selected' : ''; ?>>Single</option>
                        <option value="married" <?php echo ($formData['marital_status'] ?? '') === 'married' ? 'selected' : ''; ?>>Married</option>
                        <option value="divorced" <?php echo ($formData['marital_status'] ?? '') === 'divorced' ? 'selected' : ''; ?>>Divorced</option>
                        <option value="widowed" <?php echo ($formData['marital_status'] ?? '') === 'widowed' ? 'selected' : ''; ?>>Widowed</option>
                    </select>
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="national_id">National ID</label>
                    <input type="text" id="national_id" name="national_id" class="form-control"
                           value="<?php echo htmlspecialchars($formData['national_id'] ?? ''); ?>">
                </div>
            </div>
            
            <!-- Employment & Address Information -->
            <div>
                <h3 style="margin-bottom: 15px; padding-bottom: 10px; border-bottom: 1px solid var(--border-color);">Employment Information</h3>
                
                <div class="form-group">
                    <label class="form-label" for="department_id">Department</label>
                    <select id="department_id" name="department_id" class="form-control">
                        <option value="">Select Department</option>
                        <?php foreach ($departments as $dept): ?>
                        <option value="<?php echo $dept['department_id']; ?>" 
                                <?php echo ($formData['department_id'] ?? 0) == $dept['department_id'] ? 'selected' : ''; ?>>
                            <?php echo htmlspecialchars($dept['department_name']); ?>
                        </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="designation_id">Designation</label>
                    <select id="designation_id" name="designation_id" class="form-control">
                        <option value="">Select Designation</option>
                        <?php foreach ($designations as $desig): ?>
                        <option value="<?php echo $desig['designation_id']; ?>" 
                                <?php echo ($formData['designation_id'] ?? 0) == $desig['designation_id'] ? 'selected' : ''; ?>>
                            <?php echo htmlspecialchars($desig['designation_name']); ?>
                        </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="date_of_joining">Date of Joining *</label>
                    <input type="date" id="date_of_joining" name="date_of_joining" class="form-control" required
                           value="<?php echo htmlspecialchars($formData['date_of_joining'] ?? ''); ?>">
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="employment_type">Employment Type</label>
                    <select id="employment_type" name="employment_type" class="form-control">
                        <option value="full_time" <?php echo ($formData['employment_type'] ?? 'full_time') === 'full_time' ? 'selected' : ''; ?>>Full Time</option>
                        <option value="part_time" <?php echo ($formData['employment_type'] ?? '') === 'part_time' ? 'selected' : ''; ?>>Part Time</option>
                        <option value="contract" <?php echo ($formData['employment_type'] ?? '') === 'contract' ? 'selected' : ''; ?>>Contract</option>
                        <option value="temporary" <?php echo ($formData['employment_type'] ?? '') === 'temporary' ? 'selected' : ''; ?>>Temporary</option>
                    </select>
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="employment_status">Employment Status</label>
                    <select id="employment_status" name="employment_status" class="form-control">
                        <option value="active" <?php echo ($formData['employment_status'] ?? 'active') === 'active' ? 'selected' : ''; ?>>Active</option>
                        <option value="inactive" <?php echo ($formData['employment_status'] ?? '') === 'inactive' ? 'selected' : ''; ?>>Inactive</option>
                        <option value="terminated" <?php echo ($formData['employment_status'] ?? '') === 'terminated' ? 'selected' : ''; ?>>Terminated</option>
                        <option value="resigned" <?php echo ($formData['employment_status'] ?? '') === 'resigned' ? 'selected' : ''; ?>>Resigned</option>
                    </select>
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="employee_category">Category</label>
                    <select id="employee_category" name="employee_category" class="form-control">
                        <option value="">Select Category</option>
                        <option value="Unskilled" <?php echo ($formData['employee_category'] ?? '') === 'Unskilled' ? 'selected' : ''; ?>>Unskilled</option>
                        <option value="Semi-Skilled" <?php echo ($formData['employee_category'] ?? '') === 'Semi-Skilled' ? 'selected' : ''; ?>>Semi-Skilled</option>
                        <option value="Skilled" <?php echo ($formData['employee_category'] ?? '') === 'Skilled' ? 'selected' : ''; ?>>Skilled</option>
                        <option value="Highly Skilled" <?php echo ($formData['employee_category'] ?? '') === 'Highly Skilled' ? 'selected' : ''; ?>>Highly Skilled</option>
                    </select>
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="salary">Salary</label>
                    <input type="number" id="salary" name="salary" class="form-control currency-input" step="0.01" min="0"
                           value="<?php echo htmlspecialchars($formData['salary'] ?? '0.00'); ?>">
                </div>
                
                <h3 style="margin-top: 30px; margin-bottom: 15px; padding-bottom: 10px; border-bottom: 1px solid var(--border-color);">Address Information</h3>
                
                <div class="form-group">
                    <label class="form-label" for="address_line1">Address Line 1</label>
                    <input type="text" id="address_line1" name="address_line1" class="form-control"
                           value="<?php echo htmlspecialchars($formData['address_line1'] ?? ''); ?>">
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="address_line2">Address Line 2</label>
                    <input type="text" id="address_line2" name="address_line2" class="form-control"
                           value="<?php echo htmlspecialchars($formData['address_line2'] ?? ''); ?>">
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="city">City</label>
                    <input type="text" id="city" name="city" class="form-control"
                           value="<?php echo htmlspecialchars($formData['city'] ?? ''); ?>">
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="state">State</label>
                    <input type="text" id="state" name="state" class="form-control"
                           value="<?php echo htmlspecialchars($formData['state'] ?? ''); ?>">
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="postal_code">Postal Code</label>
                    <input type="text" id="postal_code" name="postal_code" class="form-control"
                           value="<?php echo htmlspecialchars($formData['postal_code'] ?? ''); ?>">
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="country">Country</label>
                    <input type="text" id="country" name="country" class="form-control"
                           value="<?php echo htmlspecialchars($formData['country'] ?? ''); ?>">
                </div>
            </div>
        </div>
        
        <!-- Bank & Emergency Contact -->
        <div style="display: grid; grid-template-columns: repeat(2, 1fr); gap: 20px; margin-top: 20px;">
            <div>
                <h3 style="margin-bottom: 15px; padding-bottom: 10px; border-bottom: 1px solid var(--border-color);">Bank Information</h3>
                
                <div class="form-group">
                    <label class="form-label" for="bank_name">Bank Name</label>
                    <input type="text" id="bank_name" name="bank_name" class="form-control"
                           value="<?php echo htmlspecialchars($formData['bank_name'] ?? ''); ?>">
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="bank_account_number">Account Number</label>
                    <input type="text" id="bank_account_number" name="bank_account_number" class="form-control"
                           value="<?php echo htmlspecialchars($formData['bank_account_number'] ?? ''); ?>">
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="bank_ifsc">IFSC Code</label>
                    <input type="text" id="bank_ifsc" name="bank_ifsc" class="form-control"
                           value="<?php echo htmlspecialchars($formData['bank_ifsc'] ?? ''); ?>">
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="pay_mode">Pay Mode</label>
                    <select id="pay_mode" name="pay_mode" class="form-control">
                        <option value="">Select Pay Mode</option>
                        <option value="Cash" <?php echo ($formData['pay_mode'] ?? '') === 'Cash' ? 'selected' : ''; ?>>Cash</option>
                        <option value="Bank Transfer" <?php echo ($formData['pay_mode'] ?? '') === 'Bank Transfer' ? 'selected' : ''; ?>>Bank Transfer</option>
                        <option value="Cheque" <?php echo ($formData['pay_mode'] ?? '') === 'Cheque' ? 'selected' : ''; ?>>Cheque</option>
                    </select>
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="bank_mode">Bank Mode</label>
                    <select id="bank_mode" name="bank_mode" class="form-control">
                        <option value="NEFT" <?php echo ($formData['bank_mode'] ?? 'NEFT') === 'NEFT' ? 'selected' : ''; ?>>NEFT</option>
                        <option value="RTGS" <?php echo ($formData['bank_mode'] ?? '') === 'RTGS' ? 'selected' : ''; ?>>RTGS</option>
                        <option value="IMPS" <?php echo ($formData['bank_mode'] ?? '') === 'IMPS' ? 'selected' : ''; ?>>IMPS</option>
                        <option value="UPI" <?php echo ($formData['bank_mode'] ?? '') === 'UPI' ? 'selected' : ''; ?>>UPI</option>
                    </select>
                </div>
                
                <h3 style="margin-top: 20px; margin-bottom: 15px; padding-bottom: 10px; border-bottom: 1px solid var(--border-color);">Statutory Information</h3>
                
                <div class="form-group">
                    <label class="form-label" for="esic_number">ESIC Number</label>
                    <input type="text" id="esic_number" name="esic_number" class="form-control"
                           value="<?php echo htmlspecialchars($formData['esic_number'] ?? ''); ?>">
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="uan_number">UAN Number</label>
                    <input type="text" id="uan_number" name="uan_number" class="form-control"
                           value="<?php echo htmlspecialchars($formData['uan_number'] ?? ''); ?>">
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="pan_number">PAN Number</label>
                    <input type="text" id="pan_number" name="pan_number" class="form-control" maxlength="10"
                           value="<?php echo htmlspecialchars($formData['pan_number'] ?? ''); ?>">
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="aadhar_number">Aadhar Number</label>
                    <input type="text" id="aadhar_number" name="aadhar_number" class="form-control" maxlength="12"
                           value="<?php echo htmlspecialchars($formData['aadhar_number'] ?? ''); ?>">
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="remarks">Remarks</label>
                    <textarea id="remarks" name="remarks" class="form-control" rows="3"
                              placeholder="Any additional notes or remarks"><?php echo htmlspecialchars($formData['remarks'] ?? ''); ?></textarea>
                </div>
            </div>
            
            <div>
                <h3 style="margin-bottom: 15px; padding-bottom: 10px; border-bottom: 1px solid var(--border-color);">Emergency Contact</h3>
                
                <div class="form-group">
                    <label class="form-label" for="emergency_contact_name">Contact Name</label>
                    <input type="text" id="emergency_contact_name" name="emergency_contact_name" class="form-control"
                           value="<?php echo htmlspecialchars($formData['emergency_contact_name'] ?? ''); ?>">
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="emergency_contact_phone">Contact Phone</label>
                    <input type="text" id="emergency_contact_phone" name="emergency_contact_phone" class="form-control"
                           value="<?php echo htmlspecialchars($formData['emergency_contact_phone'] ?? ''); ?>">
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="emergency_contact_relation">Relation</label>
                    <input type="text" id="emergency_contact_relation" name="emergency_contact_relation" class="form-control"
                           value="<?php echo htmlspecialchars($formData['emergency_contact_relation'] ?? ''); ?>">
                </div>
                
                <h3 style="margin-top: 20px; margin-bottom: 15px; padding-bottom: 10px; border-bottom: 1px solid var(--border-color);">Salary Components</h3>
                
                <div class="form-group">
                    <label class="form-label" for="basic_salary">Basic Salary</label>
                    <input type="number" id="basic_salary" name="basic_salary" class="form-control currency-input" step="0.01" min="0"
                           value="<?php echo htmlspecialchars($formData['basic_salary'] ?? '0.00'); ?>">
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="hra">HRA (House Rent Allowance)</label>
                    <input type="number" id="hra" name="hra" class="form-control currency-input" step="0.01" min="0"
                           value="<?php echo htmlspecialchars($formData['hra'] ?? '0.00'); ?>">
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="da">DA (Dearness Allowance)</label>
                    <input type="number" id="da" name="da" class="form-control currency-input" step="0.01" min="0"
                           value="<?php echo htmlspecialchars($formData['da'] ?? '0.00'); ?>">
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="ta">TA (Travel Allowance)</label>
                    <input type="number" id="ta" name="ta" class="form-control currency-input" step="0.01" min="0"
                           value="<?php echo htmlspecialchars($formData['ta'] ?? '0.00'); ?>">
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="special_allowance">Special Allowance</label>
                    <input type="number" id="special_allowance" name="special_allowance" class="form-control currency-input" step="0.01" min="0"
                           value="<?php echo htmlspecialchars($formData['special_allowance'] ?? '0.00'); ?>">
                </div>
            </div>
        </div>
        
        <div style="margin-top: 30px; text-align: right;">
            <a href="<?php echo BASE_URL; ?>/modules/employee/list.php" class="btn btn-secondary">Cancel</a>
            <button type="submit" class="btn btn-primary">Create Employee</button>
        </div>
    </form>
</div>

<?php include __DIR__ . '/../../includes/footer.php'; ?>

