<?php
/*
 * *************************************************
 * Copyright (C) 2021 Earworks.                    *
 * Codeigniter Project Template Modification       *
 * Please contact our email for more information : *
 * contact@earworks.co.id                          *
 * *************************************************
 */

defined('BASEPATH') OR exit('No direct script access allowed');
$user_absen_saved = $model->users;
$user_ids = [];
if ($user_absen_saved) {
    $user_ids = $user_absen_saved->pluck('id')->toArray();
}
?>
<?= form_open(current_url() . '?redir=' . $redir, ['class' => 'form-horizontal']) ?>
<div class="row">
    <div class="col-sm-8">
        <div class="form-group">
            <?= lang('nama', [], 'nama', ['class' => 'col-sm-3 control-label']) ?>
            <div class="col-sm-9">
                <?= form_input('nama', $data['nama'], ['class' => 'form-control']) ?>
            </div>
        </div>

        <?php if ($is_create === false) : ?>
            <div class="form-group">
                <?= lang('slug', [], 'slug', ['class' => 'col-sm-3 control-label']) ?>
                <div class="col-sm-9">
                    <?= form_input('slug', $data['slug'], ['class' => 'form-control']) ?>
                </div>
            </div>
        <?php endif; ?>

        <div class="form-group">
            <?= lang('keterangan', [], 'keterangan', ['class' => 'col-sm-3 control-label']) ?>
            <div class="col-sm-9">
                <?= form_textarea('keterangan', $data['keterangan'], ['class' => 'form-control']) ?>
            </div>
        </div>
        <div class="form-group">
            <?= lang('syarat', [], 'syarat', ['class' => 'col-sm-3 control-label']) ?>
            <div class="col-sm-9">
                <a href="javascript:void(0)" class="btn btn-primary" onclick="page_script.add_syarat();">
                    <i class="fa fa-plus"></i>
                    <?= lang('tambah_syarat') ?>
                </a><br /><br />
                <table class="table sort" id="syarat_list">
                    <?php
                    if ($data['syarat']) {
                        $syarat_list = @unserialize($data['syarat']);
                        if ($syarat_list) {
                            foreach ($syarat_list as $k => $list) {
                                echo Ci::partials('absen/_syarat_form', ['id' => $k, 'value' => $list]);
                            }
                        }
                    }
                    ?>
                </table>
            </div>
        </div>
<!--        <div class="form-group">
            <?= lang('location', [], 'location', ['class' => 'col-sm-3 control-label']) ?>
            <div class="col-sm-9">
                <?=
                form_dropdown('location', [
                    'jakarta' => 'Jakarta',
                    'medan' => 'Medan',
                    'surabaya' => 'Surabaya'
                    ], $data['location'], ['class' => 'form-control'])
                ?>
            </div>
        </div>-->
        <div class="form-group">
            <div class="col-sm-9 col-sm-offset-3">
                <?= form_submit('submit', 'Submit', ['class' => 'btn blue']) ?>
            </div>
        </div>
    </div>
    <div class="col-sm-4">
        <div class="form-group">
            <div class="control-label col-xs-12" style="text-align: left;">
                Users
            </div>
            <div class="col-xs-12">
                <div class="mt-checkbox-inline" style="padding:0;">
                    <?php foreach ($user_absen as $id => $name) : ?>
                        <?php $checked_user = in_array($id, $user_ids) ? ' checked="checked"' : null; ?>
                        <label class="mt-checkbox mt-checkbox-outline">
                            <input type="checkbox" name="user_absen[]" value="<?= $id ?>"  class="data-<?= $name ?>" <?= $checked_user ?> />
                            <span></span>
                            <?= $name ?>
                        </label>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>
    </div>
</div>
<?= form_close() ?>
<script type="text/javascript">

</script>
<?php
Ci::set_style('../plugins/select2/css/select2.min.css', 'plugin');
Ci::set_style('../plugins/select2/css/select2-bootstrap.min.css', 'plugin');
Ci::set_script('../plugins/select2/js/select2.min.js', 'plugin');
$syarat_form_url = base_url('absen/ajax-syarat-form/');
$syarat_modal_url = base_url('absen/ajax-syarat-modal/');
Ci::set_style(<<<CSS
    .select2-container {width:100% !important;}
CSS
    , 'embed');
Ci::set_script(<<<EOD
    var page_script = function () {
        var the;
        var syarat_form = '{$syarat_form_url}';
        return {
            init: function () {
                the = this;
                $('.select2').select2();
            },
            add_syarat: function (value) {
                var id = $('.syarat_item').length;
                $.ajax({
                    url: syarat_form + id, type: 'POST', dataType: 'HTML',
                    data: {value: value}, success: function (res) {
                        $('#syarat_list').append(res);
                    }
                });
            },
            fill_syarat: function (obj) {
                var target = $(obj).data('target');
                var target_title = target + 1;
                the.ajaxlink('{$syarat_modal_url}' + target, 'Isi syarat ' + target_title);
            },
            generate_syarat: function (id) {
                var text_syarat = '';
                $('input[name="syarat_absen"]:checked').each(function () {
                    text_syarat += this.value.trim() + '|';
                });
                $('[name="syarat[' + id + ']"]').val(text_syarat.replace(/^\|+|\|+$/g, ''));
                $('#metronic-modal').modal('hide');
            },
            remove_syarat: function (obj) {
                var target = $(obj).data('target');
                $('#syarat_'+target).remove();
            },
            ajaxlink: function (url, title) {
                $('#metronic-modal').modal('show');
                $.ajax({
                    url: url, type: 'GET', dataType: 'HTML',
                    success: function (res) {
                        $('#modal-title').empty().html(title);
                        $('#modal-body').empty().html(res);
                    }
                });
            },
        };
    }();
    $(document).ready(function () {
        page_script.init();
    });
EOD
    , 'embed');
