<!DOCTYPE html>
<!--[if IE 8]>
<html lang="en" class="ie8 no-js"> <![endif]-->
<!--[if IE 9]>
<html lang="en" class="ie9 no-js"> <![endif]-->
<!--[if !IE]><!-->
<html>
<!--<![endif]-->
<!-- BEGIN HEAD -->
<head>
    <base href="#springUrl('/assets/adminlte/')"/>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>工作台</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <!-- Bootstrap 3.3.5 -->
    <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="plugins/font-awesome/css/font-awesome.min.css">
    <!-- Ionicons -->
    <link rel="stylesheet" href="plugins/ionicons/css/ionicons.min.css">

    <!-- DataTables -->
    <link rel="stylesheet" href="plugins/datatables/dataTables.bootstrap.css">
    <!-- ztree css -->
    <link rel="stylesheet"
          href="plugins/ztree/css/metroStyle/metroStyle.css" type="text/css"/>
    <!-- Theme style -->
    <link rel="stylesheet" href="dist/css/AdminLTE.min.css">
    <!-- AdminLTE Skins. Choose a skin from the css/skins
         folder instead of downloading all of them to reduce the load. -->
    <link rel="stylesheet" href="dist/css/skins/_all-skins.min.css">
    <!-- iCheck -->
    <link rel="stylesheet" href="plugins/iCheck/flat/blue.css">
    <!-- Date Picker -->
    <link rel="stylesheet" href="plugins/datepicker/datepicker3.css">
    <!-- Daterange picker -->
    <link rel="stylesheet" href="plugins/daterangepicker/daterangepicker-bs3.css">
    <!-- bootstrap wysihtml5 - text editor -->
    <link rel="stylesheet" href="plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.min.css">
    <!--validate css-->
    <link rel="stylesheet" href="plugins/jquery-validation/css/validate.css">
    <!--fileinput css-->
    <link rel="stylesheet" href="plugins/bootstrap-fileinput/bootstrap-fileinput.css">
    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<!-- END HEAD -->
<body class="hold-transition skin-blue sidebar-mini">

<div class="wrapper">
    <div class="content-wrapper" style="margin-left:0;">

        <section class="content-header">
            <h1>
                问卷题库管理
                <small>问卷题库</small>
            </h1>
            <ol class="breadcrumb">
                <li><a href="#"><i class="fa fa-dashboard"></i>首页</a></li>
                <li><a class="active">问卷题库</a></li>
            </ol>
        </section>


        <!-- Main content -->
        <section class="content">
            <div class="row">
                <div class="col-xs-12">
                    <!-- general form elements disabled -->
                    <div class="box box-primary">
                        <form role="form" id="myForm">
                            <input name="id" value="$!{data.id}" hidden="true"/>
                            <div class="box-body">

                                <div class="form-group form-md-line-input" style="display:none;">
                                    <label>分类</label>
                                    <div class="input-group">
                                        <input type="text" id="questionsCategory" name="questionsCategory" value="$!{data.questionsCategory}"
                                               class="form-control" style="display: none">
                                        <input type="text" id="questionsCategoryName" name="questionsCategoryName" value="$!{data.questionsCategoryName}"
                                               class="form-control" disabled>
                                        <span class="input-group-btn">
                                            <button type="button" id="categorySelect"
                                                    class="btn btn-info btn-flat">选择</button>
                                         </span>
                                    </div>
                                </div>

                                <div class="form-group form-md-line-input">
                                    <label>类型</label>
                                    <select id="questionType" name="questionType"
                                            class="form-control"
                                            data-placeholder="请选择问题类型"></select>
                                </div>
                                <input id="optiondetails" name="optiondetails" style="display: none">
                                <input id="questionTitle" name="questionTitle" style="display: none">
                                <input id="questionTitleEn" name="questionTitleEn" style="display: none">
                                <div id="questionDiv"></div>
                                <!--<div id="SINGLESEL" class="form-group form-md-line-input">-->
                                <!--<label>问题</label>-->
                                <!--<input id="title" name="title"    class="form-control" style="width: 20%;" type="text" placeholder="请输入问题名称" maxlength="20">-->
                                <!--<table id="options" class="table table-bordered table-striped dataTable no-footer">-->
                                <!--<th>选项标签(例如 ABCD 1234)</th>-->
                                <!--<th>选项名称</th>-->
                                <!--<th>操作</th>-->
                                <!--</table>-->
                                <!--<p class="addOptions" style="width:100%;text-align:center"><a-->
                                <!--href="javascript:void(0)">添加选项</a></p>-->
                                <!--</div>-->


                            </div>

                            <input type="text" style="display: none" name="_csrf" value="${_csrf.token}"/>
                            <input  type="text" style="display: none" name="_csrf_header" value="${_csrf.headerName}"/>
                            <div class="box-footer">
                                #if($shiro.hasPermission("qy:questions:edit"))
                                <input class="btn btn-primary" id="save" value="保存" type="submit">
                                #end
                                <a href="#springUrl('/a/questions/list')" class="btn btn-default">取消</a>
                            </div>
                        </form>
                        <!-- /.box-body -->
                    </div><!-- /.box -->


                </div><!-- /.col -->
            </div><!-- /.row -->
        </section><!-- /.content -->
    </div><!-- /.content-wrapper -->

    <!-- Add the sidebar's background. This div must be placed
         immediately after the control sidebar -->
    <div class="control-sidebar-bg"></div>
</div><!-- ./wrapper -->

<script src="plugins/jQuery/jQuery-2.1.4.min.js"></script>
<!-- Bootstrap 3.3.5 -->
<script src="bootstrap/js/bootstrap.min.js"></script>
<!-- DataTables -->
<script src="plugins/datatables/jquery.dataTables.min.js"></script>
<script src="plugins/datatables/extensions/i18n/lanauage_ch.js"></script>
<script src="plugins/datatables/dataTables.bootstrap.min.js"></script>
<!-- SlimScroll -->
<script src="plugins/slimScroll/jquery.slimscroll.min.js"></script>
<!-- FastClick -->
<script src="plugins/fastclick/fastclick.min.js"></script>
<!--fileinput js-->
<script src="plugins/bootstrap-fileinput/bootstrap-fileinput.js"></script>
<!--handlerbar js-->
<script src="plugins/handlerbar/handlebars.min-latest.js"></script>
<!-- ztree -->
<script src="plugins/ztree/js/jquery.ztree.all.min.js"
        type="text/javascript"></script>
<!-- AdminLTE App -->
<script src="dist/js/app.min.js"></script>
<script src="plugins/bootstrap-maxlength/bootstrap-maxlength.min.js" type="text/javascript"></script>
<script src="plugins/security/sha256.js" type="text/javascript"></script>
<script src="plugins/jquery-validation/js/jquery.validate.min.js"></script>
<script src="js/jquery.form.min.js"></script>
<script src="common/js/cfapp.js"></script>
<!--question js-->
<script src="common/component/qSelect.js"></script>
<script src="js/jquery.serializejson.js"></script>
<!-- END PAGE LEVEL PLUGINS -->
<script>
    var qselect = null;
    $().ready(function () {
        Cfapp.init();
        recdTypeAdd.init();
        initSelect('$!{questionType}');
        var infos = '$!{data.optiondetails}';
        if(infos!=null && infos!=''){
            infos = JSON.parse(infos);
        }
        initQSelect(infos);



    });

    var initQSelect=function (datas) {
        console.log(datas);
        var type='SINGLESEL';
        if(datas!=''){
            datas.data=datas;
            datas.datas=datas.options;
            type=datas.type;
        }

        qselect = new qSelect({
            tarId: 'questionDiv',
            qtype: type,
            data: datas
        });
        qselect.buildTable();

    };


    var initSelect = function (datas) {
        datas=JSON.parse(datas);
        var optionStr = '';
        var options = [];
        for (var i = 0, j = datas.length; i < j; i++) {
            var type = datas[i];
            var option='';
            if(type=='SINGLESEL'){
                option= '<option value="' +type+ '"  >单选</option>';
            }else if(type=='MULTISEL'){
                option= '<option value="' +type+ '"  >多选</option>';
            }else if(type=='PICUPLOAD'){
                option= '<option value="' +type+ '">图片上传</option>';
            }else if(type=='BLANKFILL'){
                option= '<option value="' +type+ '"  >填空</option>';
            }else if(type=='TRUEFALSE'){
                option= '<option value="' +type+ '"  >对错</option>';
            }else if(type=='PICSINGLESEL'){
                option= '<option value="' +type+ '" >图片单选</option>';
            }else if(type=='PICMULTISEL'){
                option= '<option value="' +type+ '" >图片多选</option>';
            }
            optionStr += option;
        }
        $('#questionType').html(optionStr);
        var queType = '$!{data.questionType}';
        if(queType!=''){
            $("#questionType").find("option[value='" + queType + "']").attr( "selected", true);
        }

        optionSelect();
    };

    var optionSelect=function () {
        $('#questionType').change(function () {
            var option=$(this).val();
            if(option=='SINGLESEL'){
                qselect.changeType('SINGLESEL', '单选题', null);
            }else if(option=='MULTISEL'){
                qselect.changeType('MULTISEL', '多选题', null);

            }else if(option=='PICUPLOAD'){
                qselect.changeType('PICUPLOAD', '图片上传题', null);
            }else if(option=='BLANKFILL'){
                qselect.changeType('BLANKFILL', '填空题', null);

            }else if(option=='TRUEFALSE'){
                qselect.changeType('TRUEFALSE', '对错题', null);
            }else if(option=='PICSINGLESEL'){
                qselect.changeType('PICSINGLESEL', '图片单选题', null);
            }else if(option=='PICMULTISEL'){
                qselect.changeType('PICMULTISEL', '图片多选题', null);
            } else {
                qselect.changeType('SINGLESEL', '单选题', null);
            }
        })
    };

    var validateQuestions=function (datas) {
        var title=datas.title;
        var type=datas.type;
        var min=datas.min;
        var max=datas.max;
        if(title==''||title.trim()==''){//校验题目
            alert("请输入问题！");
            return false;
        }
        if(min.length!=0){//校验最少可选几项
            var reg=/^[-+]?\d*$/;
            if(!reg.test(min)){
                alert("请正确输入数字项!");//请将“整数类型”要换成你要验证的那个属性名称！
                return false;
            }
        }else{
            alert('请填写数字项!');
            return false ;
        }
        if(max.length!=0){//校验最多可选几项
            var reg=/^[-+]?\d*$/;
            if(!reg.test(max)){
                alert("请正确输入数字项!");//请将“整数类型”要换成你要验证的那个属性名称！
                return false;
            }
        }else{
            alert('请填写数字项!');
            return false ;
        }

        if(type=='SINGLESEL'){//单选

            var options=datas.options;
            if(options==undefined){
                alert('请填写问题选项!');
                return false;
            }

            var length=options.length;
            if(length==0){
                alert('请填写问题选项！');
                return false;
            }else{
                for(var i=0;i<length;i++){
                    var label=options[i].label;
                    var name =options[i].name;
                    if(label.trim()==''||name.trim()==''){
                        alert('请正确填写问题选项!');
                        return false;
                    }
                }
            }
        }else if(type=='MULTISEL'){
            var options=datas.options;
            if(options==undefined){
                alert('请填写问题选项!');
                return false;
            }

            var length=options.length;
            if(length==0||length<=1){
                alert('请填写问题选项(多选题选择项最少为两项)！');
                return false;
            }else{
                for(var i=0;i<length;i++){
                    var label=options[i].label;
                    var name =options[i].name;
                    if(label.trim()==''||name.trim()==''){
                        alert('请正确填写问题选项!');
                        return false;
                    }
                }
            }
        }else if(type=='PICSINGLESEL'){
            var options=datas.options;
            if(options==undefined){
                alert('请填写问题选项!');
                return false;
            }

            var length=options.length;
            if(length==0){
                alert('请填写问题选项！');
                return false;
            }else{
                for(var i=0;i<length;i++){
                    var label=options[i].label;
                    var name =options[i].name;
                    var pic=options.pic;
                    if(label.trim()==''||name.trim()==''||pic==''){
                        alert('请正确填写问题选项!');
                        return false;
                    }
                }
            }
        }else if(type=='PICMULTISEL'){
            var options=datas.options;
            if(options==undefined){
                alert('请填写问题选项!');
                return false;
            }

            var length=options.length;
            if(length==0||length<=1){
                alert('请填写问题选项(多选题选择项最少为两项)！');
                return false;
            }else{
                for(var i=0;i<length;i++){
                    var label=options[i].label;
                    var name =options[i].name;
                    var pic=options.pic;
                    if(label.trim()==''||name.trim()==''||pic==''){
                        alert('请正确填写问题选项!');
                        return false;
                    }
                }
            }
        }else if(type=='BLANKFILL'){
            console.log(datas.options);
        } else {
            alert('未知的问题类型!');
            return false;
        }
        return true;
    };

    var recdTypeAdd = function () {
        var initForm = function () {
            var initFormCtrl = function () {
                bindEvent();
            };
            var bindEvent = function () {
                var sSource = "#springUrl('/a/questionscategory/treeData')";
                $('#categorySelect').on('click',
                    function () {
                        Cfapp.treeSingleSelect({
                            title: '分类选择',
                            url: sSource,
                            displayFields: 'questionsCategoryName',
                            storeFields: 'questionsCategory',
                            success: function () {
                                console.log('success');
                            },
                            cancel: function () {
                                console.log('cancel');
                            }
                        })
                    });
                $("#myForm").validate({
                    rules: {},
                    messages: {},
                    submitHandler: function (form) {
                        var questions=qselect.getValues();
                        if(!validateQuestions(questions)){
                            return ;
                        }
                        $("#questionTitle").val(questions.title);
                        $("#questionTitleEn").val(questions.titleEn);
                        $("#optiondetails").val(JSON.stringify(questions));
                        $("#save").attr("disabled", true);

                        $.ajax(
                            {
                                type: "POST",
                                dataType: "json",
                                url:"#springUrl('/a/questions/formData')",
                                data:$("#myForm").serializeJSON(),
                                success:function (returnobj) {
                            $("#save").attr("disabled", false);
                            if (returnobj.errorNo == 2) {  //保存成功
                                Cfapp.confirm({
                                    message: "添加成功",
                                    btnoktext: "继续添加",
                                    btncanceltext: "关闭",
                                    success: function () {
                                        location.href = "#springUrl('/a/questions/form')";
                                    },
                                    cancel: function () {
                                        location.href = "#springUrl('/a/questions/list')";
                                    }
                                });
                            } else if (returnobj.errorNo == 0) {  //修改成功
                                Cfapp.alert({
                                    message: "更新成功",
                                    btntext: "确定",
                                    success: function () {
                                        location.href = "#springUrl('/a/questions/list')";
                                    }
                                });
                            } else {
                                Cfapp.alert({
                                    message: "创建失败",
                                    btntext: "确定",
                                    success: function () {
                                        return;
                                    }
                                });
                            }
                        } });
                    }
                })
            };
            initFormCtrl();
        };
        return {

            //main function to initiate the module
            init: function () {
                initForm();
            }

        };
    }();
</script>
</body>
<!-- END BODY -->
</html>