<?php
// admin/edit-employee.php
require_once __DIR__ . '/../includes/config.php';
require_once __DIR__ . '/../includes/functions.php';

if (!isAdmin()) {
    header('Location: login.php');
    exit;
}

$id = intval($_GET['id'] ?? 0);
if (!$id) {
    header('Location: employees.php');
    exit;
}

// Get employee details
$stmt = $pdo->prepare("SELECT * FROM employees WHERE id = ?");
$stmt->execute([$id]);
$employee = $stmt->fetch();

if (!$employee) {
    header('Location: employees.php');
    exit;
}

// Get departments and designations
$departments = $pdo->query("SELECT * FROM departments WHERE status = 1 ORDER BY name")->fetchAll();
$designations = $pdo->query("SELECT * FROM designations WHERE status = 1 ORDER BY name")->fetchAll();

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $first_name = sanitize($_POST['first_name'] ?? '');
    $last_name = sanitize($_POST['last_name'] ?? '');
    $email = sanitize($_POST['email'] ?? '');
    $phone = sanitize($_POST['phone'] ?? '');
    $alternative_phone = sanitize($_POST['alternative_phone'] ?? '');
    $address = sanitize($_POST['address'] ?? '');
    $city = sanitize($_POST['city'] ?? '');
    $date_of_birth = $_POST['date_of_birth'] ?? null;
    $gender = sanitize($_POST['gender'] ?? 'male');
    $joining_date = $_POST['joining_date'] ?? date('Y-m-d');
    $department = sanitize($_POST['department'] ?? '');
    $designation = sanitize($_POST['designation'] ?? '');
    $salary_type = sanitize($_POST['salary_type'] ?? 'monthly');
    $base_salary = floatval($_POST['base_salary'] ?? 0);
    $allowance = floatval($_POST['allowance'] ?? 0);
    $bonus = floatval($_POST['bonus'] ?? 0);
    $bank_name = sanitize($_POST['bank_name'] ?? '');
    $bank_account_no = sanitize($_POST['bank_account_no'] ?? '');
    $emergency_contact_name = sanitize($_POST['emergency_contact_name'] ?? '');
    $emergency_contact_phone = sanitize($_POST['emergency_contact_phone'] ?? '');
    $emergency_contact_relation = sanitize($_POST['emergency_contact_relation'] ?? '');
    $employee_type = sanitize($_POST['employee_type'] ?? 'full_time');
    $status = isset($_POST['status']) ? 1 : 0;

    if (empty($first_name) || empty($phone)) {
        $error = 'First name and phone are required';
    } else {
        $stmt = $pdo->prepare("UPDATE employees SET 
            first_name = ?, last_name = ?, email = ?, phone = ?, alternative_phone = ?,
            address = ?, city = ?, date_of_birth = ?, gender = ?, joining_date = ?,
            department = ?, designation = ?, salary_type = ?, base_salary = ?,
            allowance = ?, bonus = ?, bank_name = ?, bank_account_no = ?,
            emergency_contact_name = ?, emergency_contact_phone = ?,
            emergency_contact_relation = ?, employee_type = ?, status = ?
            WHERE id = ?");

        $stmt->execute([
            $first_name, $last_name, $email, $phone, $alternative_phone,
            $address, $city, $date_of_birth, $gender, $joining_date,
            $department, $designation, $salary_type, $base_salary,
            $allowance, $bonus, $bank_name, $bank_account_no,
            $emergency_contact_name, $emergency_contact_phone,
            $emergency_contact_relation, $employee_type, $status,
            $id
        ]);

        $_SESSION['success_msg'] = 'Employee updated successfully!';
        header('Location: employees.php');
        exit;
    }
}

include 'header.php';
?>

<div class="admin-content">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2><i class="fa-solid fa-edit me-2"></i> Edit Employee</h2>
        <div>
            <a href="view-employee.php?id=<?php echo $id; ?>" class="btn btn-primary">
                <i class="fa-solid fa-eye"></i> View
            </a>
            <a href="employees.php" class="btn btn-secondary">
                <i class="fa-solid fa-arrow-left"></i> Back
            </a>
        </div>
    </div>

    <?php if ($error): ?>
    <div class="alert alert-danger"><?php echo $error; ?></div>
    <?php endif; ?>

    <div class="admin-card">
        <form method="POST">
            <div class="row">
                <div class="col-md-6">
                    <h6 class="mb-3">Personal Information</h6>

                    <div class="mb-3">
                        <label class="form-label">Employee Code</label>
                        <input type="text" class="form-control" value="<?php echo $employee['employee_code']; ?>" disabled>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">First Name *</label>
                        <input type="text" name="first_name" class="form-control" value="<?php echo htmlspecialchars($employee['first_name']); ?>" required>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Last Name</label>
                        <input type="text" name="last_name" class="form-control" value="<?php echo htmlspecialchars($employee['last_name']); ?>">
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Email</label>
                        <input type="email" name="email" class="form-control" value="<?php echo htmlspecialchars($employee['email']); ?>">
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Phone *</label>
                        <input type="text" name="phone" class="form-control" value="<?php echo htmlspecialchars($employee['phone']); ?>" required>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Alternative Phone</label>
                        <input type="text" name="alternative_phone" class="form-control" value="<?php echo htmlspecialchars($employee['alternative_phone']); ?>">
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Address</label>
                        <textarea name="address" class="form-control" rows="2"><?php echo htmlspecialchars($employee['address']); ?></textarea>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">City</label>
                        <input type="text" name="city" class="form-control" value="<?php echo htmlspecialchars($employee['city']); ?>">
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Date of Birth</label>
                        <input type="date" name="date_of_birth" class="form-control" value="<?php echo $employee['date_of_birth']; ?>">
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Gender</label>
                        <select name="gender" class="form-control">
                            <option value="male" <?php echo $employee['gender'] == 'male' ? 'selected' : ''; ?>>Male</option>
                            <option value="female" <?php echo $employee['gender'] == 'female' ? 'selected' : ''; ?>>Female</option>
                            <option value="other" <?php echo $employee['gender'] == 'other' ? 'selected' : ''; ?>>Other</option>
                        </select>
                    </div>
                </div>

                <div class="col-md-6">
                    <h6 class="mb-3">Employment Information</h6>

                    <div class="mb-3">
                        <label class="form-label">Joining Date *</label>
                        <input type="date" name="joining_date" class="form-control" value="<?php echo $employee['joining_date']; ?>" required>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Department</label>
                        <select name="department" class="form-control">
                            <option value="">Select Department</option>
                            <?php foreach ($departments as $dept): ?>
                            <option value="<?php echo $dept['name']; ?>" <?php echo $dept['name'] == $employee['department'] ? 'selected' : ''; ?>>
                                <?php echo $dept['name']; ?>
                            </option>
                            <?php endforeach; ?>
                        </select>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Designation</label>
                        <select name="designation" class="form-control">
                            <option value="">Select Designation</option>
                            <?php foreach ($designations as $des): ?>
                            <option value="<?php echo $des['name']; ?>" <?php echo $des['name'] == $employee['designation'] ? 'selected' : ''; ?>>
                                <?php echo $des['name']; ?>
                            </option>
                            <?php endforeach; ?>
                        </select>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Employee Type</label>
                        <select name="employee_type" class="form-control">
                            <option value="full_time" <?php echo $employee['employee_type'] == 'full_time' ? 'selected' : ''; ?>>Full Time</option>
                            <option value="part_time" <?php echo $employee['employee_type'] == 'part_time' ? 'selected' : ''; ?>>Part Time</option>
                            <option value="contract" <?php echo $employee['employee_type'] == 'contract' ? 'selected' : ''; ?>>Contract</option>
                            <option value="intern" <?php echo $employee['employee_type'] == 'intern' ? 'selected' : ''; ?>>Intern</option>
                        </select>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Salary Type</label>
                        <select name="salary_type" class="form-control">
                            <option value="monthly" <?php echo $employee['salary_type'] == 'monthly' ? 'selected' : ''; ?>>Monthly</option>
                            <option value="hourly" <?php echo $employee['salary_type'] == 'hourly' ? 'selected' : ''; ?>>Hourly</option>
                            <option value="project" <?php echo $employee['salary_type'] == 'project' ? 'selected' : ''; ?>>Project</option>
                        </select>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Base Salary (TK)</label>
                        <input type="number" name="base_salary" class="form-control" step="0.01" value="<?php echo $employee['base_salary']; ?>">
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Allowance (TK)</label>
                        <input type="number" name="allowance" class="form-control" step="0.01" value="<?php echo $employee['allowance']; ?>">
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Bonus (TK)</label>
                        <input type="number" name="bonus" class="form-control" step="0.01" value="<?php echo $employee['bonus']; ?>">
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Bank Name</label>
                        <input type="text" name="bank_name" class="form-control" value="<?php echo htmlspecialchars($employee['bank_name']); ?>">
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Bank Account No</label>
                        <input type="text" name="bank_account_no" class="form-control" value="<?php echo htmlspecialchars($employee['bank_account_no']); ?>">
                    </div>

                    <h6 class="mb-3 mt-4">Emergency Contact</h6>

                    <div class="mb-3">
                        <label class="form-label">Contact Name</label>
                        <input type="text" name="emergency_contact_name" class="form-control" value="<?php echo htmlspecialchars($employee['emergency_contact_name']); ?>">
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Contact Phone</label>
                        <input type="text" name="emergency_contact_phone" class="form-control" value="<?php echo htmlspecialchars($employee['emergency_contact_phone']); ?>">
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Relation</label>
                        <input type="text" name="emergency_contact_relation" class="form-control" value="<?php echo htmlspecialchars($employee['emergency_contact_relation']); ?>">
                    </div>

                    <div class="mb-3">
                        <div class="form-check">
                            <input type="checkbox" name="status" class="form-check-input" id="status" <?php echo $employee['status'] ? 'checked' : ''; ?>>
                            <label class="form-check-label" for="status">Active</label>
                        </div>
                    </div>
                </div>
            </div>

            <div class="mt-3">
                <button type="submit" class="btn btn-primary">
                    <i class="fa-solid fa-save"></i> Update Employee
                </button>
                <a href="employees.php" class="btn btn-secondary">Cancel</a>
            </div>
        </form>
    </div>
</div>

<?php include 'footer.php'; ?>