<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<link href="<?php echo base_url('assets/css/chosen.css') ?>" rel="stylesheet">
<link href="<?php echo base_url('assets/css/jquery.datetimepicker.css') ?>" rel="stylesheet">
<style>
.form-group label { font-size:11px; font-weight:700; color:#888; text-transform:uppercase; letter-spacing:.5px; margin-bottom:5px; display:block; }
.gender-opt { display:inline-flex; align-items:center; gap:5px; margin-right:18px; font-weight:400; font-size:13px; }
</style>

<section class="content-header">
    <h1><i class="fa fa-id-badge"></i> <?php echo lang('employee') ?> <small><?php echo lang('edit') ?></small></h1>
    <ol class="breadcrumb">
        <li><a href="<?php echo site_url('admin') ?>"><i class="fa fa-dashboard"></i> Dashboard</a></li>
        <li><a href="<?php echo site_url('admin/employees') ?>"><?php echo lang('employees') ?></a></li>
        <li class="active"><?php echo lang('edit') ?></li>
    </ol>
</section>

<section class="content">

<?php if(validation_errors()): ?>
<div class="alert alert-danger alert-dismissible">
    <button class="close" data-dismiss="alert">&times;</button>
    <i class="fa fa-ban"></i> <?php echo validation_errors() ?>
</div>
<?php endif; ?>

<div class="box box-primary">
    <div class="box-header with-border" style="background:linear-gradient(135deg,#1e3a5f,#2e75b6);border-radius:10px 10px 0 0">
        <h3 class="box-title" style="color:#fff"><i class="fa fa-pencil"></i> Editar Colaborador</h3>
    </div>
    <form method="post" enctype="multipart/form-data">
    <div class="box-body">

        <div class="row">
            <div class="col-md-3">
                <div class="form-group">
                    <label><?php echo lang('employee_id') ?></label>
                    <input type="text" name="employee_id" value="<?php echo htmlspecialchars($employee->employee_id ?? '') ?>" class="form-control" disabled>
                </div>
            </div>
            <div class="col-md-5">
                <div class="form-group">
                    <label><?php echo lang('name') ?></label>
                    <input type="text" name="name" value="<?php echo htmlspecialchars($employee->name ?? '') ?>" class="form-control" required>
                </div>
            </div>
            <div class="col-md-4">
                <div class="form-group">
                    <label><?php echo lang('profile') ?> <?php echo lang('picture') ?></label>
                    <div style="display:flex;align-items:center;gap:10px">
                        <input type="file" name="img" class="form-control" style="flex:1">
                        <?php if(!empty($employee->image)): ?>
                        <img src="<?php echo base_url('assets/uploads/images/'.$employee->image) ?>" style="width:44px;height:44px;border-radius:50%;object-fit:cover;border:2px solid #e2e6ea">
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-md-3">
                <div class="form-group">
                    <label><?php echo lang('gender') ?></label>
                    <div style="padding-top:6px">
                        <label class="gender-opt"><input type="radio" name="gender" value="Male" <?php echo ($employee->gender ?? '') == 'Male' ? 'checked' : '' ?>> <?php echo lang('male') ?></label>
                        <label class="gender-opt"><input type="radio" name="gender" value="Female" <?php echo ($employee->gender ?? '') == 'Female' ? 'checked' : '' ?>> <?php echo lang('female') ?></label>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="form-group">
                    <label><?php echo lang('date_of_birth') ?></label>
                    <input type="text" name="dob" value="<?php echo $employee->dob ?? '' ?>" class="form-control datepicker" placeholder="AAAA-MM-DD">
                </div>
            </div>
            <div class="col-md-3">
                <div class="form-group">
                    <label><?php echo lang('status') ?></label>
                    <div style="padding-top:6px">
                        <label class="gender-opt"><input type="radio" name="status" value="1" <?php echo ($employee->status ?? 0) == 1 ? 'checked' : '' ?>> <?php echo lang('active') ?></label>
                        <label class="gender-opt"><input type="radio" name="status" value="0" <?php echo ($employee->status ?? 0) == 0 ? 'checked' : '' ?>> <?php echo lang('inactive') ?></label>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="form-group">
                    <label><?php echo lang('phone') ?></label>
                    <input type="text" name="contact" value="<?php echo htmlspecialchars($employee->contact ?? '') ?>" class="form-control">
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-md-4">
                <div class="form-group">
                    <label><?php echo lang('user_role') ?></label>
                    <select name="role_id" class="form-control chzn">
                        <option value="">-- <?php echo lang('select') ?> --</option>
                        <?php foreach($roles as $new):
                            $sel = ($new->id == ($employee->user_role ?? 0)) ? 'selected' : '';
                        ?>
                        <option value="<?php echo $new->id ?>" <?php echo $sel ?>><?php echo htmlspecialchars($new->name) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>
            <div class="col-md-4">
                <div class="form-group">
                    <label><?php echo lang('departments') ?></label>
                    <select name="department_id" id="department_id" class="form-control chzn">
                        <option value="">-- <?php echo lang('select') ?> --</option>
                        <?php foreach($departments as $new):
                            $sel = ($new->id == ($employee->department_id ?? 0)) ? 'selected' : '';
                        ?>
                        <option value="<?php echo $new->id ?>" <?php echo $sel ?>><?php echo htmlspecialchars($new->name) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>
            <div class="col-md-4">
                <div class="form-group">
                    <label><?php echo lang('designation') ?></label>
                    <select name="designation_id" id="degi" class="form-control chzn">
                        <option value="">-- <?php echo lang('select') ?> --</option>
                        <?php foreach($designations as $new):
                            $sel = ($new->id == ($employee->designation_id ?? 0)) ? 'selected' : '';
                        ?>
                        <option value="<?php echo $new->id ?>" <?php echo $sel ?>><?php echo htmlspecialchars($new->designation) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-md-3">
                <div class="form-group">
                    <label><?php echo lang('joining_date') ?></label>
                    <input type="text" name="joining_date" value="<?php echo $employee->joining_date ?? '' ?>" class="form-control datepicker" placeholder="AAAA-MM-DD">
                </div>
            </div>
            <div class="col-md-3">
                <div class="form-group">
                    <label><?php echo lang('joining_salary') ?></label>
                    <input type="text" name="joining_salary" value="<?php echo htmlspecialchars($employee->joining_salary ?? '') ?>" class="form-control">
                </div>
            </div>
            <div class="col-md-6">
                <div class="form-group">
                    <label><?php echo lang('email') ?></label>
                    <input type="text" name="email" value="<?php echo htmlspecialchars($employee->email ?? '') ?>" class="form-control">
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-md-12">
                <div class="form-group">
                    <label><?php echo lang('address') ?></label>
                    <textarea name="address" class="form-control" rows="2"><?php echo htmlspecialchars($employee->address ?? '') ?></textarea>
                </div>
            </div>
        </div>

        <hr>
        <h4 style="font-size:13px;font-weight:700;color:#1e3a5f;margin-bottom:14px"><i class="fa fa-key"></i> Acesso ao Sistema</h4>
        <div class="row">
            <div class="col-md-4">
                <div class="form-group">
                    <label><?php echo lang('username') ?></label>
                    <input type="text" name="username" value="<?php echo htmlspecialchars($employee->username ?? '') ?>" class="form-control">
                </div>
            </div>
            <div class="col-md-4">
                <div class="form-group">
                    <label><?php echo lang('password') ?> <small class="text-muted">(deixe em branco para manter)</small></label>
                    <input type="password" name="password" value="" class="form-control" autocomplete="new-password">
                </div>
            </div>
            <div class="col-md-4">
                <div class="form-group">
                    <label><?php echo lang('confirm') ?> <?php echo lang('password') ?></label>
                    <input type="password" name="confirm" value="" class="form-control" autocomplete="new-password">
                </div>
            </div>
        </div>

        <?php
        $CI = get_instance();
        if(!empty($fields)):
        ?>
        <hr>
        <h4 style="font-size:13px;font-weight:700;color:#1e3a5f;margin-bottom:14px"><i class="fa fa-list-alt"></i> Informações Adicionais</h4>
        <div class="row">
        <?php foreach($fields as $doc):
            $result = $CI->db->query("SELECT * FROM rel_form_custom_fields WHERE custom_field_id='".$doc->id."' AND table_id='".$employee->id."' AND form='".$doc->form."'")->row();
            $reply  = $result->reply ?? '';
            $values = explode(',', $doc->values ?? '');
        ?>
            <div class="col-md-4">
                <div class="form-group">
                    <label><?php echo htmlspecialchars($doc->name) ?></label>
                    <?php if($doc->field_type==1): ?>
                    <input type="text" class="form-control" name="reply[<?php echo $doc->id ?>]" value="<?php echo htmlspecialchars($reply) ?>">
                    <?php elseif($doc->field_type==2): ?>
                    <select name="reply[<?php echo $doc->id ?>]" class="form-control">
                        <?php foreach($values as $val): ?>
                        <option value="<?php echo trim($val) ?>" <?php echo trim($val)==$reply?'selected':'' ?>><?php echo trim($val) ?></option>
                        <?php endforeach; ?>
                    </select>
                    <?php elseif($doc->field_type==3): ?>
                    <div style="padding-top:6px">
                        <?php foreach($values as $val): ?>
                        <label class="gender-opt"><input type="radio" name="reply[<?php echo $doc->id ?>]" value="<?php echo trim($val) ?>" <?php echo trim($val)==$reply?'checked':'' ?>> <?php echo trim($val) ?></label>
                        <?php endforeach; ?>
                    </div>
                    <?php elseif($doc->field_type==4): ?>
                    <div style="padding-top:6px">
                        <?php foreach($values as $val): ?>
                        <label class="gender-opt"><input type="checkbox" name="reply[<?php echo $doc->id ?>][]" value="<?php echo trim($val) ?>" <?php echo trim($val)==$reply?'checked':'' ?>> <?php echo trim($val) ?></label>
                        <?php endforeach; ?>
                    </div>
                    <?php elseif($doc->field_type==5): ?>
                    <textarea class="form-control" name="reply[<?php echo $doc->id ?>]" rows="2"><?php echo htmlspecialchars($reply) ?></textarea>
                    <?php elseif($doc->field_type==6): ?>
                    <input type="url" class="form-control" name="reply[<?php echo $doc->id ?>]" value="<?php echo htmlspecialchars($reply) ?>">
                    <?php elseif($doc->field_type==7): ?>
                    <input type="email" class="form-control" name="reply[<?php echo $doc->id ?>]" value="<?php echo htmlspecialchars($reply) ?>">
                    <?php elseif($doc->field_type==8): ?>
                    <input type="number" class="form-control" name="reply[<?php echo $doc->id ?>]" value="<?php echo htmlspecialchars($reply) ?>">
                    <?php endif; ?>
                </div>
            </div>
        <?php endforeach; ?>
        </div>
        <?php endif; ?>

    </div>
    <div class="box-footer">
        <button type="submit" class="btn btn-primary"><i class="fa fa-save"></i> <?php echo lang('update') ?></button>
        <a href="<?php echo site_url('admin/employees/view/'.($employee->id ?? '')) ?>" class="btn btn-default"><i class="fa fa-arrow-left"></i> Cancelar</a>
    </div>
    <?php form_close() ?>
</div>

</section>

<script src="<?php echo base_url('assets/js/chosen.jquery.min.js') ?>"></script>
<script src="<?php echo base_url('assets/js/jquery.datetimepicker.js') ?>"></script>
<script>
$(function() {
    $('.chzn').chosen({ width:'100%' });
    $('.datepicker').datetimepicker({ timepicker:false, format:'Y-m-d' });

    $(document).on('change', '#department_id', function() {
        var department_id = $(this).val();
        $('#degi').html('<option>Carregando...</option>');
        $.ajax({
            url: '<?php echo site_url('admin/employees/get_degi') ?>',
            type: 'POST',
            data: { id: department_id },
            success: function(result) {
                $('#degi').html(result);
                $('#degi').trigger('chosen:updated');
            }
        });
    });
});
</script>
