<?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">
<link href="<?php echo base_url('assets/css/redactor.css') ?>" rel="stylesheet">
<link href="<?php echo base_url() ?>assets/css/ionslider/ion.rangeSlider.css" rel="stylesheet">
<link href="<?php echo base_url() ?>assets/css/ionslider/ion.rangeSlider.skinNice.css" rel="stylesheet">
<link href="<?php echo base_url() ?>assets/css/bootstrap-slider/slider.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-tasks"></i> <?php echo lang('tasks') ?> <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/tasks') ?>"><?php echo lang('tasks') ?></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-plus"></i> Nova Tarefa</h3>
    </div>
    <?php echo form_open_multipart('admin/tasks/add/') ?>
    <div class="box-body">
        <div class="row">
            <div class="col-md-6">
                <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-6">
                <div class="form-group">
                    <label><?php echo lang('priority') ?></label>
                    <select name="priority" class="form-control chzn">
                        <option value="">-- <?php echo lang('select') ?> --</option>
                        <option value="1">Baixa</option>
                        <option value="2">Média</option>
                        <option value="3">Alta</option>
                    </select>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-md-6">
                <div class="form-group">
                    <label><?php echo lang('due_date') ?></label>
                    <input type="text" name="due_date" value="<?php echo set_value('due_date') ?>" class="form-control datepicker" placeholder="AAAA-MM-DD" required>
                </div>
            </div>
            <div class="col-md-6">
                <div class="form-group">
                    <label><?php echo lang('case') ?></label>
                    <select name="case_id" class="form-control chzn">
                        <option value="">-- <?php echo lang('select') ?> <?php echo lang('case') ?> --</option>
                        <?php foreach($cases as $new):
                            $sel = set_select('case_id', $new->id) ? 'selected' : '';
                        ?>
                        <option value="<?php echo $new->id ?>" <?php echo $sel ?>>#<?php echo $new->case_no ?> — <?php echo htmlspecialchars($new->title) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-md-12">
                <div class="form-group">
                    <label><?php echo lang('assigned_to') ?></label>
                    <select name="employee_id[]" class="form-control chzn" multiple="multiple" data-placeholder="<?php echo lang('select') ?> <?php echo lang('employees') ?>">
                        <?php foreach($employees as $new):
                            $sel = set_select('employee_id', $new->id) ? 'selected' : '';
                        ?>
                        <option value="<?php echo $new->id ?>" <?php echo $sel ?>><?php echo htmlspecialchars($new->name) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-md-12">
                <div class="form-group">
                    <label><?php echo lang('progress') ?></label>
                    <input type="text" value="" name="progress" class="slider form-control"
                        data-slider-min="0" data-slider-max="100" data-slider-step="1"
                        data-slider-value="0" data-slider-orientation="horizontal"
                        data-slider-selection="before" data-slider-tooltip="show" data-slider-id="red">
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-md-12">
                <div class="form-group">
                    <label><?php echo lang('description') ?></label>
                    <textarea name="description" class="form-control redactor" style="min-height:150px"><?php echo set_value('description') ?></textarea>
                </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/tasks') ?>" 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 src="<?php echo base_url() ?>assets/js/ionslider/ion.rangeSlider.min.js"></script>
<script src="<?php echo base_url('assets/js/redactor.min.js') ?>"></script>
<script>
$(function() {
    $('.chzn').chosen({ width:'100%' });
    $('.datepicker').datetimepicker({ timepicker:false, format:'Y-m-d' });
    $('.slider').ionRangeSlider({ min:0, max:100, from:0, postfix:'%' });
    $('.redactor').redactor({
        minHeight: 150,
        imageUpload: '<?php echo base_url(config_item('admin_folder').'/wysiwyg/upload_image') ?>',
        fileUpload:  '<?php echo base_url(config_item('admin_folder').'/wysiwyg/upload_file') ?>'
    });
});
</script>
