<style>
    .emp_form {
        padding: 20px;
        font-family: 'Segoe UI', Arial, sans-serif;
        background: #f0f4ff;
        min-height: 100%;
    }

    .emp_card {
        background: rgba(255, 255, 255, 0.75);
        backdrop-filter: blur(12px);
        border: 1px solid rgba(255, 255, 255, 0.9);
        border-radius: 14px;
        padding: 22px 26px;
        box-shadow: 0 4px 16px rgba(0, 0, 0, 0.06);
        margin-bottom: 16px;
    }

    .emp_section_title {
        font-size: 12px;
        font-weight: 700;
        color: #f47f20;
        text-transform: uppercase;
        letter-spacing: 0.8px;
        margin-bottom: 16px;
        padding-bottom: 8px;
        border-bottom: 2px solid rgba(244, 127, 32, 0.2);
    }

    .emp_grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
        gap: 12px 20px;
    }

    .emp_group {
        display: flex;
        flex-direction: column;
        gap: 5px;
    }

    .emp_group label {
        font-size: 11px;
        font-weight: 700;
        color: #666;
        text-transform: uppercase;
        letter-spacing: 0.4px;
    }

    .emp_group label.required::after {
        content: ' *';
        color: #dc3545;
    }

    .emp_group input[type=text],
    .emp_group input[type=password],
    .emp_group textarea {
        border: 1px solid #e0e0e0;
        border-radius: 8px;
        padding: 9px 12px;
        font-size: 13px;
        color: #333;
        outline: none;
        transition: all 0.2s;
        background: white;
        font-family: 'Segoe UI', Arial, sans-serif;
        width: 100%;
        box-sizing: border-box;
    }

    .emp_group input:focus,
    .emp_group textarea:focus {
        border-color: #f47f20;
        box-shadow: 0 0 0 3px rgba(244, 127, 32, 0.12);
    }

    .emp_group textarea {
        resize: vertical;
        min-height: 70px;
    }

    .permission_grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
        gap: 10px;
    }

    .permission_item {
        display: flex;
        align-items: flex-start;
        gap: 10px;
        background: rgba(244, 127, 32, 0.04);
        border: 1px solid rgba(244, 127, 32, 0.12);
        border-radius: 8px;
        padding: 10px 12px;
        transition: all 0.2s;
    }

    .permission_item:hover {
        background: rgba(244, 127, 32, 0.08);
        border-color: rgba(244, 127, 32, 0.25);
    }

    .permission_item input[type=checkbox] {
        width: 16px;
        height: 16px;
        accent-color: #f47f20;
        cursor: pointer;
        margin-top: 2px;
        flex-shrink: 0;
    }

    .permission_item .perm_text {
        flex: 1;
    }

    .permission_item .perm_name {
        font-size: 13px;
        font-weight: 600;
        color: #333;
        display: block;
    }

    .permission_item .perm_desc {
        font-size: 11px;
        color: #999;
        display: block;
        margin-top: 2px;
    }

    .emp_submit_btn {
        background: linear-gradient(135deg, #f47f20, #e05f00);
        color: white;
        border: none;
        border-radius: 10px;
        padding: 12px 32px;
        font-size: 14px;
        font-weight: 700;
        cursor: pointer;
        box-shadow: 0 4px 14px rgba(244, 127, 32, 0.4);
        transition: all 0.2s;
        width: 100%;
        margin-top: 4px;
        letter-spacing: 0.3px;
        font-family: 'Segoe UI', Arial, sans-serif;
    }

    .emp_submit_btn:hover {
        background: linear-gradient(135deg, #ff8f30, #f47f20);
        box-shadow: 0 6px 20px rgba(244, 127, 32, 0.5);
        transform: translateY(-1px);
    }

    #error_message_box {
        background: rgba(220, 53, 69, 0.08);
        border: 1px solid rgba(220, 53, 69, 0.2);
        border-radius: 8px;
        padding: 10px 14px;
        margin-bottom: 14px;
        color: #dc3545;
        font-size: 12px;
        list-style: none;
    }

    #error_message_box li {
        margin-left: 12px;
        list-style: disc;
    }

    /* ── Responsive ── */
    @media (max-width: 768px) {

        .emp_form {
            padding: 12px;
        }

        .emp_card {
            padding: 14px 16px;
            margin-bottom: 12px;
        }

        .emp_grid {
            grid-template-columns: 1fr !important;
        }

        .permission_grid {
            grid-template-columns: 1fr !important;
        }

        .emp_group input[type=text],
        .emp_group input[type=password],
        .emp_group textarea {
            font-size: 14px;
            padding: 10px 12px;
        }

        .permission_item {
            padding: 10px;
        }

        .permission_item .perm_name {
            font-size: 12px;
        }

        .permission_item .perm_desc {
            font-size: 10px;
        }

        .emp_submit_btn {
            padding: 12px;
            font-size: 14px;
        }
    }

    @media (max-width: 480px) {

        .emp_form {
            padding: 8px;
        }

        .emp_card {
            padding: 12px;
        }

        .emp_section_title {
            font-size: 11px;
        }

        .emp_group label {
            font-size: 10px;
        }
    }
</style>

<?php echo form_open('employees/save/' . $person_info->person_id, array('id' => 'employee_form')); ?>

<div class="emp_form">

    <ul id="error_message_box" style="display:none;"></ul>

    <!-- Basic Info -->
    <div class="emp_card">
        <div class="emp_section_title">👤 <?php echo $this->lang->line('employees_basic_information'); ?></div>
        <div class="emp_grid">
            <div class="emp_group">
                <label class="required"><?php echo $this->lang->line('common_first_name'); ?></label>
                <?php echo form_input(array('name' => 'first_name', 'id' => 'first_name', 'value' => $person_info->first_name)); ?>
            </div>
            <div class="emp_group">
                <label class="required"><?php echo $this->lang->line('common_last_name'); ?></label>
                <?php echo form_input(array('name' => 'last_name', 'id' => 'last_name', 'value' => $person_info->last_name)); ?>
            </div>
            <div class="emp_group">
                <label><?php echo $this->lang->line('common_email'); ?></label>
                <?php echo form_input(array('name' => 'email', 'id' => 'email', 'value' => $person_info->email)); ?>
            </div>
            <div class="emp_group">
                <label><?php echo $this->lang->line('common_phone_number'); ?></label>
                <?php echo form_input(array('name' => 'phone_number', 'id' => 'phone_number', 'value' => $person_info->phone_number)); ?>
            </div>
            <div class="emp_group">
                <label><?php echo $this->lang->line('common_address_1'); ?></label>
                <?php echo form_input(array('name' => 'address_1', 'id' => 'address_1', 'value' => $person_info->address_1)); ?>
            </div>
            <div class="emp_group">
                <label><?php echo $this->lang->line('common_address_2'); ?></label>
                <?php echo form_input(array('name' => 'address_2', 'id' => 'address_2', 'value' => $person_info->address_2)); ?>
            </div>
            <div class="emp_group">
                <label><?php echo $this->lang->line('common_city'); ?></label>
                <?php echo form_input(array('name' => 'city', 'id' => 'city', 'value' => $person_info->city)); ?>
            </div>
            <div class="emp_group">
                <label><?php echo $this->lang->line('common_state'); ?></label>
                <?php echo form_input(array('name' => 'state', 'id' => 'state', 'value' => $person_info->state)); ?>
            </div>
            <div class="emp_group">
                <label><?php echo $this->lang->line('common_zip'); ?></label>
                <?php echo form_input(array('name' => 'zip', 'id' => 'zip', 'value' => $person_info->zip)); ?>
            </div>
            <div class="emp_group">
                <label><?php echo $this->lang->line('common_country'); ?></label>
                <?php echo form_input(array('name' => 'country', 'id' => 'country', 'value' => $person_info->country)); ?>
            </div>
        </div>
        <div class="emp_grid" style="grid-template-columns:1fr;margin-top:12px;">
            <div class="emp_group">
                <label><?php echo $this->lang->line('common_comments'); ?></label>
                <?php echo form_textarea(array('name' => 'comments', 'id' => 'comments', 'rows' => 3, 'cols' => 17, 'value' => $person_info->comments)); ?>
            </div>
        </div>
    </div>

    <!-- Login Info -->
    <div class="emp_card">
        <div class="emp_section_title">🔐 <?php echo $this->lang->line('employees_login_info'); ?></div>
        <?php $pwd_required = $person_info->person_id == "" ? array('class' => 'required') : array(); ?>
        <div class="emp_grid">
            <div class="emp_group">
                <label class="required"><?php echo $this->lang->line('employees_username'); ?></label>
                <?php echo form_input(array('name' => 'username', 'id' => 'username', 'value' => $person_info->username)); ?>
            </div>
            <div class="emp_group" style="grid-column:1/-1;"></div>
            <div class="emp_group">
                <label <?php if ($person_info->person_id == "") echo 'class="required"'; ?>><?php echo $this->lang->line('employees_password'); ?></label>
                <?php echo form_password(array('name' => 'password', 'id' => 'password')); ?>
            </div>
            <div class="emp_group">
                <label <?php if ($person_info->person_id == "") echo 'class="required"'; ?>><?php echo $this->lang->line('employees_repeat_password'); ?></label>
                <?php echo form_password(array('name' => 'repeat_password', 'id' => 'repeat_password')); ?>
            </div>
        </div>
    </div>

    <!-- Permissions -->
    <div class="emp_card">
        <div class="emp_section_title">🛡️ <?php echo $this->lang->line('employees_permission_info'); ?></div>
        <p style="font-size:13px;color:#888;margin-bottom:14px;"><?php echo $this->lang->line('employees_permission_desc'); ?></p>
        <div class="permission_grid">
            <?php foreach ($all_modules->result() as $module): ?>
                <div class="permission_item">
                    <?php echo form_checkbox("permissions[]", $module->module_id, $this->Employee->has_permission($module->module_id, $person_info->person_id)); ?>
                    <div class="perm_text">
                        <span class="perm_name"><?php echo $this->lang->line('module_' . $module->module_id); ?></span>
                        <span class="perm_desc"><?php echo $this->lang->line('module_' . $module->module_id . '_desc'); ?></span>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>

        <button type="submit" class="emp_submit_btn" style="margin-top:20px;">
            💾 <?php echo $this->lang->line('common_submit'); ?>
        </button>
    </div>

</div>

<?php echo form_close(); ?>

<script type='text/javascript'>
    $(document).ready(function() {
        $('#employee_form').validate({
            submitHandler: function(form) {
                $(form).ajaxSubmit({
                    success: function(response) {
                        tb_remove();
                        post_person_form_submit(response);
                    },
                    dataType: 'json'
                });
            },
            errorLabelContainer: "#error_message_box",
            wrapper: "li",
            rules: {
                first_name: "required",
                last_name: "required",
                username: {
                    required: true,
                    minlength: 5
                },
                password: {
                    <?php if ($person_info->person_id == ""): ?>required: true,
                <?php endif; ?>
                minlength: 8
                },
                repeat_password: {
                    equalTo: "#password"
                },
                email: "email"
            },
            messages: {
                first_name: "<?php echo $this->lang->line('common_first_name_required'); ?>",
                last_name: "<?php echo $this->lang->line('common_last_name_required'); ?>",
                username: {
                    required: "<?php echo $this->lang->line('employees_username_required'); ?>",
                    minlength: "<?php echo $this->lang->line('employees_username_minlength'); ?>"
                },
                password: {
                    <?php if ($person_info->person_id == ""): ?>
                        required: "<?php echo $this->lang->line('employees_password_required'); ?>",
                    <?php endif; ?>
                    minlength: "<?php echo $this->lang->line('employees_password_minlength'); ?>"
                },
                repeat_password: {
                    equalTo: "<?php echo $this->lang->line('employees_password_must_match'); ?>"
                },
                email: "<?php echo $this->lang->line('common_email_invalid_format'); ?>"
            },
            showErrors: function(errorMap, errorList) {
                $('#error_message_box').show();
                this.defaultShowErrors();
            }
        });
    });
</script>