<form class="form-horizontal" action="<?= site_url('test/submit') ?>" method="post" id="testForm">
    <?php
    $table = '<div class="panel-group question-list"><p ><h5 id="time" class="bg-success text-center"></h5></p>';
    $i     = 1;
    $ques  = [];
    foreach ($questions as $k => $q) {
        switch ($k) {
            case 1:
                $title = "单选题";
                $input = "radio";
                break;
            case 2:
                $title = "多选题";
                $input = "checkbox";
                break;
            case 3:
                $title = "判断题";
                $input = "radio";
                $an    = ['正确', '错误'];
                break;
            case 4:
                $title = "填空题";
                $input = "text";
                break;
            case 5:
                $title = "问答题";
                $input = "textarea";
                break;
        }
        echo <<<EOT
<div class="well bg-info">
    <h5 class="">{$title}</h5>
</div>
EOT;

        $table .= '<div class="panel panel-default"><div class="panel-heading"><h4 class="panel-title">' . $title . '</h4></div><div class="panel-body">';
        foreach ($q as $question) {
            $options = json_decode($question['option'], true);
            if ($page['upset'] && ($k == 1 || $k == 2)) {
                shuffle($options);
            }

            echo <<<EOT
<div class="panel panel-primary" id="{$i}">
    <div class="panel-heading">
        <span class="title">{$i}、{$question['name']}</span>
    </div>
    <div class="panel-body">
        <ol type="A" class="option">
EOT;
            $answer = [];
            if ($k != 5) {
                foreach ($options as $a => $option) {
                    if (isset($option['a']) && $option['a'] == 't') $answer[] = $a;
                    $t = isset($option['a']) ? : '';
                    echo "<li><label><input type=\"{$input}\" name=\"answer[$k][{$question['id']}][]\" value=\"{$a}\"/>{$option['t']}</label></li>";
                }
            } else {
                echo "<textarera name=\"answer[$k][{$question['id']}]\"></textarera>";
            }
            echo <<<EOT
         </ol>
    </div>
    </div>
EOT;

            $table .= '<button type="button" class="btn btn-xs error" data-id="' . $i . '">' . $i . '</button>';
            $i++;
            $question['answer']        = $answer;
            $ques[$k][$question['id']] = ['o' => $options, 'a' => $answer];
        }
        $table .= '</div></div>';
    }
    $exam_page = [
        'id'         => 0,
        'user'       => $user['id'],
        'city'       => $user['city'],
        'room'       => $room['id'],
        'score'      => '',
        'page'       => $page['id'],
        'question'   => json_encode($ques, JSON_UNESCAPED_UNICODE),
        'start_time' => date('Y-m-d H:i:s')
    ];
    if (isset($examPage)) {
        $exam_page['id'] = $examPage['id'];
        $this->db->replace('exam_page', $exam_page);
        $e_id = $examPage['id'];
    } else {
        $this->db->insert('exam_page', $exam_page);
        $e_id = $this->db->insert_id();
    }
    echo $table;
    ?>
    <input type="hidden" name="start" value="<?= date('Y-m-d H:i:s') ?>"/>
    <input type="hidden" name="usetime" value=""/>
    <input type="hidden" name="id" value="<?= $e_id ?>"/>

</form>

<script src="<?= THEMEPATH ?>/js/validator.js"></script>

<script type="text/javascript">
    $(function () {
        var start = new Date().getTime();
        var m =<?=($page['time']-1)?>;
        var s = 59;
        var c = 0;

        setInterval(function () {
            if (s < 10) {
                $('#time').html(m + ':0' + s);
            } else {
                $('#time').html(m + ':' + s);
            }
            s--;
            if (s < 0) {
                s = 59;
                m--;
            }
            if (m <= 0) {
                $('#testForm').submit();
            }
        }, 1000);

        /*window.onblur = function () {
         c++;
         if (c < 3) {
         fullScreen();
         BootstrapDialog.alert({
         title: '警告',
         message: '请不要切换屏幕。<br/>已切换'+c+'次，第3次会自动提交试卷。',
         type: BootstrapDialog.TYPE_WARNING,
         });
         return;
         }
         $('#testForm').submit();
         };*/

        $('#submitTest').click(function () {
            BootstrapDialog.confirm({
                title: "确认提交试卷",
                message: "一旦提交，将不能在参加此场考试。<br/>您确认提交试卷？",
                type: BootstrapDialog.TYPE_WARNING,
                btnCancelLabel: '取消',
                btnOKLabel: '交卷!',
                callback: function (result) {
                    if (result) {
                        $('#testForm').submit();
                    }
                }
            });
        });

        $('.question-list').on('click', 'button', function () {
            var id = $(this).text();
            window.location.href = '#' + id;
        });

        $('input').click(function () {
            var id = $(this).parents('.panel').attr('id');
            $('.question-list').find('[data-id=' + id + ']').removeClass('error').addClass('right');
        });

        $('#testForm').formValidator({
            before: function () {
                var end = new Date().getTime();
                $('input[name=usetime]').val((end - start) / 60000);
            },
            sending: {
                type: 'ajax',
                success: function (data) {
                    var e = $.parseJSON(data);
                    if (e.error) {
                        show_error({'message': e.error});
                    } else {
                        BootstrapDialog.alert('您的得分为：' + e.score, function () {
                            window.opener.location.reload();
                            window.close();
                        });
                    }
                },
                error: function () {
                    show_error('提交表单失败！');
                }
            }
        })
    });
</script>
