<?php
/**
 * @var object $employee
 * @var array $users
 * @var array $branches
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-user-edit me-2 text-gold"></i>Edit Employee</h5>
</div>

<div class="glass-card p-0">
    <div class="card-header">
        <div class="card-header-left">
            <i class="fas fa-user-tie card-icon"></i>
            <h6 class="card-title">Employee Information</h6>
        </div>
        <div>
            <span class="text-muted small"><span class="text-danger">*</span> Required fields</span>
        </div>
    </div>
    <div class="card-body">
        <form method="POST" action="<?= url('employees/update/' . $employee->id) ?>" enctype="multipart/form-data">
            <?= csrf_field() ?>

            <div class="row g-3">
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Photo</label>
                        <?php if (!empty($employee->photo)): ?>
                            <div class="mb-2">
                                <img src="<?= uploadUrl($employee->photo) ?>" alt="" class="rounded" width="100" height="100" style="object-fit:cover;">
                            </div>
                        <?php endif; ?>
                        <input type="file" name="photo" class="form-control form-control-sm bg-transparent" accept="image/jpeg,image/png,image/gif,image/webp">
                        <div class="form-text text-muted small">Leave empty to keep current photo.</div>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">User Account</label>
                        <select name="user_id" class="form-select bg-transparent <?= errorClass('user_id') ?>">
                            <option value="">No Account</option>
                            <?php foreach ($users as $user): ?>
                                <option value="<?= $user->id ?>" <?= (old('user_id', $employee->user_id) == $user->id) ? 'selected' : '' ?>>
                                    <?= e($user->first_name . ' ' . $user->last_name . ' (' . $user->email . ')') ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                        <?= errorMsg('user_id') ?>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Branch</label>
                        <select name="branch_id" class="form-select bg-transparent <?= errorClass('branch_id') ?>">
                            <option value="">Select Branch</option>
                            <?php foreach ($branches as $branch): ?>
                                <option value="<?= $branch->id ?>" <?= (old('branch_id', $employee->branch_id) == $branch->id) ? 'selected' : '' ?>>
                                    <?= e($branch->name) ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                        <?= errorMsg('branch_id') ?>
                    </div>
                </div>
            </div>

            <hr class="border-secondary">
            <h6 class="text-gold mb-3"><i class="fas fa-user me-2"></i>Personal Details</h6>

            <div class="row g-3">
                <div class="col-md-6">
                    <div class="mb-3">
                        <label class="form-label">First Name <span class="text-danger">*</span></label>
                        <input type="text" name="first_name" class="form-control bg-transparent <?= errorClass('first_name') ?>"
                               value="<?= e(old('first_name', $employee->first_name)) ?>" required maxlength="100">
                        <?= errorMsg('first_name') ?>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="mb-3">
                        <label class="form-label">Last Name <span class="text-danger">*</span></label>
                        <input type="text" name="last_name" class="form-control bg-transparent <?= errorClass('last_name') ?>"
                               value="<?= e(old('last_name', $employee->last_name)) ?>" required maxlength="100">
                        <?= errorMsg('last_name') ?>
                    </div>
                </div>
            </div>

            <div class="row g-3">
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Email</label>
                        <input type="email" name="email" class="form-control bg-transparent <?= errorClass('email') ?>"
                               value="<?= e(old('email', $employee->email)) ?>" maxlength="255">
                        <?= errorMsg('email') ?>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Phone</label>
                        <input type="text" name="phone" class="form-control bg-transparent <?= errorClass('phone') ?>"
                               value="<?= e(old('phone', $employee->phone)) ?>" maxlength="20">
                        <?= errorMsg('phone') ?>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Mobile</label>
                        <input type="text" name="mobile" class="form-control bg-transparent <?= errorClass('mobile') ?>"
                               value="<?= e(old('mobile', $employee->mobile)) ?>" maxlength="20">
                        <?= errorMsg('mobile') ?>
                    </div>
                </div>
            </div>

            <div class="row g-3">
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Date of Birth</label>
                        <input type="date" name="date_of_birth" class="form-control bg-transparent <?= errorClass('date_of_birth') ?>"
                               value="<?= e(old('date_of_birth', $employee->date_of_birth)) ?>">
                        <?= errorMsg('date_of_birth') ?>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Gender</label>
                        <select name="gender" class="form-select bg-transparent <?= errorClass('gender') ?>">
                            <option value="">Select Gender</option>
                            <option value="male" <?= (old('gender', $employee->gender) === 'male') ? 'selected' : '' ?>>Male</option>
                            <option value="female" <?= (old('gender', $employee->gender) === 'female') ? 'selected' : '' ?>>Female</option>
                            <option value="other" <?= (old('gender', $employee->gender) === 'other') ? 'selected' : '' ?>>Other</option>
                        </select>
                        <?= errorMsg('gender') ?>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Job Title</label>
                        <input type="text" name="job_title" class="form-control bg-transparent <?= errorClass('job_title') ?>"
                               value="<?= e(old('job_title', $employee->job_title)) ?>" maxlength="255">
                        <?= errorMsg('job_title') ?>
                    </div>
                </div>
            </div>

            <hr class="border-secondary">
            <h6 class="text-gold mb-3"><i class="fas fa-map-marker-alt me-2"></i>Address</h6>

            <div class="row g-3">
                <div class="col-md-12">
                    <div class="mb-3">
                        <label class="form-label">Address</label>
                        <textarea name="address" class="form-control bg-transparent <?= errorClass('address') ?>"
                                  rows="2" maxlength="500"><?= e(old('address', $employee->address)) ?></textarea>
                        <?= errorMsg('address') ?>
                    </div>
                </div>
            </div>

            <div class="row g-3">
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">City</label>
                        <input type="text" name="city" class="form-control bg-transparent <?= errorClass('city') ?>"
                               value="<?= e(old('city', $employee->city)) ?>" maxlength="100">
                        <?= errorMsg('city') ?>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">State</label>
                        <input type="text" name="state" class="form-control bg-transparent <?= errorClass('state') ?>"
                               value="<?= e(old('state', $employee->state)) ?>" maxlength="100">
                        <?= errorMsg('state') ?>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">ZIP Code</label>
                        <input type="text" name="zip" class="form-control bg-transparent <?= errorClass('zip') ?>"
                               value="<?= e(old('zip', $employee->zip)) ?>" maxlength="20">
                        <?= errorMsg('zip') ?>
                    </div>
                </div>
            </div>

            <hr class="border-secondary">
            <h6 class="text-gold mb-3"><i class="fas fa-briefcase me-2"></i>Employment Details</h6>

            <div class="row g-3">
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Hire Date</label>
                        <input type="date" name="hire_date" class="form-control bg-transparent <?= errorClass('hire_date') ?>"
                               value="<?= e(old('hire_date', $employee->hire_date)) ?>">
                        <?= errorMsg('hire_date') ?>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Salary Type</label>
                        <select name="salary_type" class="form-select bg-transparent <?= errorClass('salary_type') ?>">
                            <option value="fixed" <?= (old('salary_type', $employee->salary_type) === 'fixed') ? 'selected' : '' ?>>Fixed Salary</option>
                            <option value="hourly" <?= (old('salary_type', $employee->salary_type) === 'hourly') ? 'selected' : '' ?>>Hourly</option>
                            <option value="commission_only" <?= (old('salary_type', $employee->salary_type) === 'commission_only') ? 'selected' : '' ?>>Commission Only</option>
                        </select>
                        <?= errorMsg('salary_type') ?>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Salary Amount</label>
                        <div class="input-group">
                            <span class="input-group-text bg-transparent border-gold"><?= APP_CURRENCY_SYMBOL ?></span>
                            <input type="number" step="0.01" name="salary_amount"
                                   class="form-control bg-transparent <?= errorClass('salary_amount') ?>"
                                   value="<?= e(old('salary_amount', $employee->salary_amount)) ?>" min="0">
                        </div>
                        <?= errorMsg('salary_amount') ?>
                    </div>
                </div>
            </div>

            <div class="row g-3">
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Hourly Rate</label>
                        <div class="input-group">
                            <span class="input-group-text bg-transparent border-gold"><?= APP_CURRENCY_SYMBOL ?></span>
                            <input type="number" step="0.01" name="hourly_rate"
                                   class="form-control bg-transparent <?= errorClass('hourly_rate') ?>"
                                   value="<?= e(old('hourly_rate', $employee->hourly_rate)) ?>" min="0">
                        </div>
                        <?= errorMsg('hourly_rate') ?>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Commission (%)</label>
                        <input type="number" step="0.01" name="commission_percent"
                               class="form-control bg-transparent <?= errorClass('commission_percent') ?>"
                               value="<?= e(old('commission_percent', $employee->commission_percent)) ?>" min="0" max="100">
                        <?= errorMsg('commission_percent') ?>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Target Amount</label>
                        <div class="input-group">
                            <span class="input-group-text bg-transparent border-gold"><?= APP_CURRENCY_SYMBOL ?></span>
                            <input type="number" step="0.01" name="target_amount"
                                   class="form-control bg-transparent <?= errorClass('target_amount') ?>"
                                   value="<?= e(old('target_amount', $employee->target_amount)) ?>" min="0">
                        </div>
                        <?= errorMsg('target_amount') ?>
                    </div>
                </div>
            </div>

            <hr class="border-secondary">
            <h6 class="text-gold mb-3"><i class="fas fa-phone-alt me-2"></i>Emergency Contact</h6>

            <div class="row g-3">
                <div class="col-md-6">
                    <div class="mb-3">
                        <label class="form-label">Emergency Contact Name</label>
                        <input type="text" name="emergency_contact" class="form-control bg-transparent <?= errorClass('emergency_contact') ?>"
                               value="<?= e(old('emergency_contact', $employee->emergency_contact)) ?>" maxlength="255">
                        <?= errorMsg('emergency_contact') ?>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="mb-3">
                        <label class="form-label">Emergency Phone</label>
                        <input type="text" name="emergency_phone" class="form-control bg-transparent <?= errorClass('emergency_phone') ?>"
                               value="<?= e(old('emergency_phone', $employee->emergency_phone)) ?>" maxlength="20">
                        <?= errorMsg('emergency_phone') ?>
                    </div>
                </div>
            </div>

            <hr class="border-secondary">

            <div class="row g-3">
                <div class="col-md-12">
                    <div class="mb-3">
                        <label class="form-label">Notes</label>
                        <textarea name="notes" class="form-control bg-transparent <?= errorClass('notes') ?>"
                                  rows="3" maxlength="2000"><?= e(old('notes', $employee->notes)) ?></textarea>
                        <?= errorMsg('notes') ?>
                    </div>
                </div>
            </div>

            <div class="row g-3">
                <div class="col-md-12">
                    <div class="mb-3">
                        <div class="form-check form-switch">
                            <input type="hidden" name="is_active" value="0">
                            <input type="checkbox" name="is_active" id="is_active" class="form-check-input" value="1"
                                <?= (old('is_active', $employee->is_active) == 1) ? 'checked' : '' ?>>
                            <label class="form-check-label" for="is_active">Active</label>
                        </div>
                    </div>
                </div>
            </div>

            <div class="mt-4 d-flex gap-2">
                <button type="submit" class="btn btn-gold">
                    <i class="fas fa-save me-1"></i>Update Employee
                </button>
                <a href="<?= url('employees') ?>" class="btn btn-outline-secondary">
                    <i class="fas fa-times me-1"></i>Cancel
                </a>
            </div>
        </form>
    </div>
</div>
