<?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-user-plus"></i> <?php echo lang('employees') ?> <small><?php echo lang('add') ?></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('add') ?></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-user-plus"></i> Novo Colaborador</h3>
    </div>
    <?php echo form_open_multipart('admin/employees/add/') ?>
    <div class="box-body">
        <div class="row">
            <div class="col-md-4">
                <div class="form-group">
                    <label><?php echo lang('employee_id') ?></label>
                    <input type="text" name="employee_id" value="<?php echo $e_id ?>" class="form-control" disabled>
                </div>
            </div>
            <div class="col-md-4">
                <div class="form-group">
                    <label><?php echo lang('name') ?></label>
                    <input type="text" name="name" value="<?php echo set_value('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>
                    <input type="file" name="img" class="form-control">
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-md-4">
                <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 lang('male') ?></label>
                        <label class="gender-opt"><input type="radio" name="gender" value="Female"> <?php echo lang('female') ?></label>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="form-group">
                    <label><?php echo lang('date_of_birth') ?></label>
                    <input type="text" name="dob" value="<?php echo set_value('dob') ?>" class="form-control datepicker" placeholder="AAAA-MM-DD">
                </div>
            </div>
            <div class="col-md-4">
                <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" checked> <?php echo lang('active') ?></label>
                        <label class="gender-opt"><input type="radio" name="status" value="0"> <?php echo lang('inactive') ?></label>
                    </div>
                </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') ?> <?php echo lang('user_role') ?> --</option>
                        <?php foreach($roles as $new): ?>
                        <option value="<?php echo $new->id ?>"><?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') ?> <?php echo lang('department') ?> --</option>
                        <?php foreach($departments as $new): ?>
                        <option value="<?php echo $new->id ?>"><?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') ?> <?php echo lang('designation') ?> --</option>
                    </select>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-md-4">
                <div class="form-group">
                    <label><?php echo lang('joining_date') ?></label>
                    <input type="text" name="joining_date" value="<?php echo set_value('joining_date') ?>" class="form-control datepicker" placeholder="AAAA-MM-DD">
                </div>
            </div>
            <div class="col-md-4">
                <div class="form-group">
                    <label><?php echo lang('joining_salary') ?></label>
                    <input type="text" name="joining_salary" value="<?php echo set_value('joining_salary') ?>" class="form-control">
                </div>
            </div>
            <div class="col-md-4">
                <div class="form-group">
                    <label><?php echo lang('phone') ?></label>
                    <input type="text" name="contact" value="<?php echo set_value('contact') ?>" class="form-control">
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-md-6">
                <div class="form-group">
                    <label><?php echo lang('email') ?></label>
                    <input type="text" name="email" value="<?php echo set_value('email') ?>" class="form-control">
                </div>
            </div>
            <div class="col-md-6">
                <div class="form-group">
                    <label><?php echo lang('address') ?></label>
                    <textarea name="address" class="form-control" rows="2"><?php echo set_value('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 set_value('username') ?>" class="form-control">
                </div>
            </div>
            <div class="col-md-4">
                <div class="form-group">
                    <label><?php echo lang('password') ?></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 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):
            $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 ?>]">
                    <?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) ?></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) ?></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) ?></label><?php endforeach; ?></div>
                    <?php elseif($doc->field_type==5): ?>
                    <textarea class="form-control" name="reply[<?php echo $doc->id ?>]" rows="2"></textarea>
                    <?php elseif($doc->field_type==6): ?>
                    <input type="url" class="form-control" name="reply[<?php echo $doc->id ?>]">
                    <?php elseif($doc->field_type==7): ?>
                    <input type="email" class="form-control" name="reply[<?php echo $doc->id ?>]">
                    <?php elseif($doc->field_type==8): ?>
                    <input type="number" class="form-control" name="reply[<?php echo $doc->id ?>]">
                    <?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('save') ?></button>
        <a href="<?php echo site_url('admin/employees') ?>" class="btn btn-default"><i class="fa fa-arrow-left"></i> Cancelar</a>
    </div>
    <?php echo 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();
        var ajax_load = '<img style="margin-left:100px;" src="<?php echo base_url('assets/img/ajax-loader.gif') ?>"/>';
        $('#degi').html(ajax_load);
        $.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>
