<?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');
?>
<?php if ($pertanyaan->count() > 0) : ?>
    <div class="row">
        <?php foreach ($pertanyaan as $res1) : ?>
            <?php
            $pilihan = $res1->pilihan;
            $options = [
                'name' => 'pertanyaan[' . $res1->id . ']',
                'class' => 'form-control',
                'id' => 'pertanyaan_' . $res1->id,
                'data-id' => $res1->id,
                'autocomplete' => 'off',
                'value' => isset($jawaban[$res1->id]) ? $jawaban[$res1->id] : null
            ];
            $error_class = isset($errors[$options['name']]) ? ' has-error' : null;
            ?>
            <div class="col-xs-<?= $res1->kolom ?>">
                <div class="form-group<?= $error_class ?>">
                    <label class="control-label"><?= $res1->nama ?></label>
                    <?php if ($res1->tipe == Form_pertanyaan_model::TIPE_TEXT): ?>
                        <?= form_input($options) ?>
                    <?php elseif ($res1->tipe == Form_pertanyaan_model::TIPE_DATE) : ?>
                        <?php
                        $options['class'] = 'form-control datepicker';
                        $options['value'] = $options['value'] ? $options['value'] : date('d-m-Y');
                        ?>
                        <?= form_input($options) ?>
                    <?php elseif ($res1->tipe == Form_pertanyaan_model::TIPE_DESCRIPTION) : ?>
                        <?= form_textarea($options) ?>
                    <?php elseif ($res1->tipe == Form_pertanyaan_model::TIPE_OPTIONS) : ?>
                        <?php if ($pilihan->count() > 0): ?>
                            <div class="mt-radio-list">
                                <?php foreach ($pilihan as $res2) : ?>
                                    <?php $checked = !empty($options['value']) && $res2->nilai == $options['value']; ?>
                                    <label class="mt-radio">
                                        <?= form_radio('pertanyaan[' . $res1->id . ']', $res2->nilai, $checked, ['class' => 'form-control']) ?>
                                        <?= $res2->nama ?>
                                        <span></span>
                                    </label>
                                <?php endforeach; ?>
                            </div>
                        <?php endif; ?>
                    <?php elseif ($res1->tipe == Form_pertanyaan_model::TIPE_NUMERIC) : ?>
                        <?php $fake_name = 'fake-' . $options['name']; ?>
                        <?= form_input($fake_name, $options['value'] ? number_format($options['value'], 0, ',', '.') : 0, ['class' => 'form-control fake-nominal', 'data-hidden' => $options['id'], 'autocomplete' => 'off']); ?>
                        <input type="hidden" name="<?= $options['name'] ?>" id="<?= $options['id'] ?>" value="<?= $options['value'] ?>" />
                    <?php elseif ($res1->tipe == Form_pertanyaan_model::TIPE_FILE) : ?>
                        <?= form_upload($options) ?>
                        <?php if ($options['value']) : ?>
                            <br />
                            <a href="<?= base_url('files/uploads/' . $options['value']) ?>" class="btn btn-link" target="_BLANK">Link File</a>
                        <?php else : ?>
                            <span>Belum ada file </span>
                        <?php endif; ?>
                    <?php elseif ($res1->tipe == Form_pertanyaan_model::TIPE_IMAGES) : ?>
                        <?= form_upload($options) ?>
                        <?php if ($options['value']) : ?>
                            <br /><br />
                            <img src="<?= base_url('img/uploads/' . $options['value']) ?>" class="img img-responsive img-thumbnail" />
                        <?php else : ?>
                            <span>Belum ada gambar </span>
                        <?php endif; ?>
                    <?php elseif ($res1->tipe == Form_pertanyaan_model::TIPE_EMAIL) : ?>
                        <?= form_input($options) ?>
                    <?php elseif ($res1->tipe == Form_pertanyaan_model::TIPE_EXPIRE_DATE) : ?>
                        <?php
                        $options['class'] = 'form-control datepicker';
                        $options['value'] = $options['value'] ? $options['value'] : date('d-m-Y');
                        ?>
                        <?= form_input($options) ?>
                    <?php elseif ($res1->tipe == Form_pertanyaan_model::TIPE_BIRTH_DATE) : ?>
                        <?php
                        $options['class'] = 'form-control datepicker';
                        $options['value'] = $options['value'] ? $options['value'] : date('d-m-Y');
                        ?>
                        <?= form_input($options) ?>
                    <?php elseif ($res1->tipe == Form_pertanyaan_model::TIPE_INLINE_OPTION) : ?>
                        <?php if ($pilihan->count() > 0): ?>
                            <div class="mt-radio-inline">
                                <?php foreach ($pilihan as $res2) : ?>
                                    <?php $checked = !empty($options['value']) && $res2->nilai == $options['value']; ?>
                                    <label class="mt-radio">
                                        <?= form_radio('pertanyaan[' . $res1->id . ']', $res2->nilai, $checked, ['class' => 'form-control']) ?>
                                        <?= $res2->nama ?>
                                        <span></span>
                                    </label>
                                <?php endforeach; ?>
                            </div>
                        <?php endif; ?>
                    <?php elseif ($res1->tipe == Form_pertanyaan_model::TIPE_CHECKBOX) : ?>
                        <?php if ($pilihan->count() > 0): ?>
                            <div class="mt-checkbox-list">
                                <?php foreach ($pilihan as $res2) : ?>
                                    <?php $checked = !empty($options['value']) && $res2->nilai == $options['value']; ?>
                                    <label class="mt-checkbox">
                                        <?= form_checkbox('pertanyaan[' . $res1->id . '][]', $res2->nilai, $checked, ['class' => 'form-control']) ?>
                                        <?= $res2->nama ?>
                                        <span></span>
                                    </label>
                                <?php endforeach; ?>
                            </div>
                        <?php endif; ?>
                    <?php elseif ($res1->tipe == Form_pertanyaan_model::TIPE_INLINE_CHECKBOX) : ?>
                        <?php if ($pilihan->count() > 0): ?>
                            <div class="mt-checkbox-inline">
                                <?php foreach ($pilihan as $res2) : ?>
                                    <?php $checked = !empty($options['value']) && $res2->nilai == $options['value']; ?>
                                    <label class="mt-checkbox">
                                        <?= form_checkbox('pertanyaan[' . $res1->id . '][]', $res2->nilai, $checked, ['class' => 'form-control']) ?>
                                        <?= $res2->nama ?>
                                        <span></span>
                                    </label>
                                <?php endforeach; ?>
                            </div>
                        <?php endif; ?>
                    <?php elseif ($res1->tipe == Form_pertanyaan_model::TIPE_PHONE) : ?>
                        <div class="input-group">
                            <span class="input-group-addon">+62</span>
                            <?= form_input($options) ?>
                        </div>
                    <?php endif; ?>
                    <?php if (isset($errors[$options['name']])) : ?>
                        <p class="help-block"><?= $errors[$options['name']] ?></p>
                    <?php endif; ?>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
<?php endif; ?>
<?php
// datepicker
Ci::set_style('../plugins/jquery-ui-1.12.1/jquery-ui.min.css', 'plugin');
Ci::set_style('../plugins/jquery-ui-1.12.1/jquery-ui.theme.min.css', 'plugin');
Ci::set_script('../plugins/jquery-ui-1.12.1/jquery-ui.min.js', 'plugin');
Ci::set_script(<<<EOD
    var page_script = function () {
        var the;
        return {
            init: function () {
                the = this;
                script.handleDatePicker();
                script.handleFakeNominal();
               
            }
        };
    }();
    $(document).ready(function () {
        page_script.init();
    });
EOD
        , 'embed');
