<!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">
    <link rel="stylesheet" href="common/css/uePicPicker.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 id="hiddenDiv" style="display:none">$!{data.content_des}</div>
<div id="hiddenDivEn" style="display:none">$!{data.content_des_en}</div>
<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="paperCategory" name="paperCategory" value="0"
                                               class="form-control" style="display: none">
                                        <input type="text" id="paperCategoryName" name="paperCategoryName" value="$!{data.paperCategoryName}"
                                               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" style="display: none;">
                                    <label>问卷类型</label>
                                    <select id="paperType" name="paperType"
                                            class="form-control"
                                            data-placeholder="请选择问卷类型"></select>
                                </div>

                                <div class="form-group form-md-line-input">
                                    <label>问卷标题(CN)</label>
                                    <input type="text"
                                           class="form-control" name="paperTitle"
                                           id="paperTitle"
                                           placeholder="问卷标题(CN)"
                                           value="$!{data.paperTitle}"
                                           required="required"  >
                                </div>
                                <div class="form-group form-md-line-input">
                                    <label>问卷标题(EN)</label>
                                    <input type="text"
                                           class="form-control" name="paperTitleEn"
                                           id="paperTitleEn"
                                            placeholder="问卷标题(EN)"
                                           value="$!{data.paperTitleEn}"
                                           required="required"  >
                                </div>
                                <div class="form-group form-md-line-input col-md-12">
                                    <label>问卷描述(CN)</label>
                                    <script id="ueditor" type="text/plain"></script>
                                </div>
                                <div class="form-group form-md-line-input col-md-12">
                                    <label>问卷描述(EN)</label>
                                    <script id="ueditorEn" type="text/plain"></script>
                                </div>

                                <!--大图-->
                                <div  class="form-group form-md-line-input col-md-12" id="act_start_pic">

                                </div>
                                <!-- <div class="form-group form-md-line-input">
                                     <label>问卷banner</label>
                                     <div class="input-group" id="paperPicGroup">
                                         <input type="text" style="display: none"
                                                class="form-control" name="paperPic"
                                                id="paperPic"
                                                maxlength="500" placeholder="问卷banner"
                                                value="$!{data.paperPic}"
                                         >

                                         &lt;!&ndash;<span class="input-group-btn">&ndash;&gt;
                                         &lt;!&ndash;<button type="button" id="smallImgSelect"&ndash;&gt;
                                         &lt;!&ndash;class="btn btn-info btn-flat">选择</button>&ndash;&gt;
                                         &lt;!&ndash;</span>&ndash;&gt;
                                     </div>
                                     <div id="ImgSelect" style="text-align: center;line-height: 200px;width: 200px;height: 200px;border: 1px #ccc solid;position: relative;">
                                         <i class="fa fa-plus" style="font-size:50px;color:#ccc"></i>
                                         <img src="" style="width:auto;height: 200px;border:0px;display:none;position: absolute;z-index:999;left: 0px;"  id="previewImg"/>
                                     </div>
                                 </div>
 -->

                                <div class="form-group form-md-line-input">
                                    <label>问题</label>
                                    <div class="input-group">
                                        <input type="text" id="questions" name="questions" value=""
                                               class="form-control" style="display: none">
                                        <input type="text" id="questionsName" name="paperCategoryName" placeholder="请点击右侧按钮选择问题"
                                               class="form-control" disabled>
                                        <span class="input-group-btn">
                                            <button type="button" id="selQuestions"
                                                        class="btn btn-info btn-flat">选择</button>
                                        </span>
                                    </div>
                                    <ul class="list-group" id="queOptions" style="margin-top: 20px">
                                    </ul>

                                    <div class="form-group form-md-line-input" id="activitylist">
                                        <label>选择活动</label>
                                        <div class="input-group">
                                            <input type="text" name="activityid" id="activityid"
                                                   value="$!{activityName}"
                                                   class="form-control" style="display: none" >
                                            <input type="text" name="activityName" id="activityName"
                                                   value="$!{name}"
                                                   class="form-control" disabled>
                                            <input type="text" id="activitytype"
                                                   name="activitytype" value=""
                                                   class="form-control"style="display: none" >
                                            <span class="input-group-btn">
                                            <button type="button" id="selectactivity"
                                                    class="btn btn-info btn-flat">选择活动</button>
                                                </span>
                                            <br/>
                                            <span id="activity">

                                            </span>

                                        </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>

                            <div class="box-footer">
                                #if($shiro.hasPermission("qy:paper:edit"))
                                <input class="btn btn-primary" id="save" value="保存" type="submit">
                                #end
                                <a href="#springUrl('/a/paper/list')" class="btn btn-default">取消</a>
                            </div>
                                </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>
<!-- 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 type="text/javascript" charset="utf-8" src="plugins/ueditor-min-1.4.3/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="plugins/ueditor-min-1.4.3/ueditor.all.js"></script>
<script type="text/javascript" charset="utf-8" src="plugins/ueditor-min-1.4.3/lang/zh-cn/zh-cn.js"></script>
<!--handlerbar js-->
<script src="plugins/handlerbar/handlebars.min-latest.js"></script>
<script type="text/javascript"  src="plugins/Sortable/Sortable.min.js"></script>
<script src="common/js/cfapp.js"></script>
<script src="common/component/qTreeF7.js"></script>
<script src="common/component/qSelect.js"></script>
<script src="common/component/qSortable.js"></script>
<script src="plugins/Sortable/Sortable.min.js"></script>
<script src="common/component/uePicPicker.js"></script>
<script src="common/component/ueSPicPicker.js"></script>
<script src="js/jquery.serializejson.js"></script>
<script src="js/jquery.serializejson.js"></script>
<!-- END PAGE LEVEL PLUGINS -->
<script>

    var coverPP = null;
    var initPicsSel = function () {

        var smallimg = "$!{data.act_start_pic}";
        coverPP = new uePicPicker({
            tarId: 'act_start_pic ',
            title: '图片',
            sizeDes: '750*360',
            max: 1,
            datas: smallimg=='' ? null : smallimg.split(","),
            success: null
        });
        coverPP.init();
    };

    var qSortable=null;
    function formatDates(now) {
        var now = new Date(now);
        var year = now.getFullYear();
        var month = now.getMonth() + 1;
        var date = now.getDate();
        var hour = now.getHours();
        var minute = now.getMinutes();
        var second = now.getSeconds();
        return year + "-" + month + "-" + date + "   " + hour + ":"
            + minute + ":" + second;
    }


    $().ready(function () {
        initPicsSel();
        Cfapp.init();
        recdTypeAdd.init();
        initSelect('$!{paperType}');
        //initPic($("#paperPic").val());
        initTreeF7('$!{data.questions}');
        initSortable();

        ue = UE.getEditor('ueditor', {
            toolbars: [[
                'insertimage',
                'insertvideo',
                'redo', //重做
                'bold', //加粗
                'indent', //首行缩进
                'italic', //斜体
                'underline', //下划线
                'strikethrough', //删除线
                'subscript', //下标
                'fontborder', //字符边框
                'superscript', //上标
                'formatmatch', //格式刷
                'source', //源代码
                'blockquote', //引用
                'pasteplain', //纯文本粘贴模式
                'selectall', //全选
                'print', //打印
                'preview', //预览
                'horizontal', //分隔线
                'removeformat', //清除格式
                'time', //时间
                'date', //日期
                'unlink', //取消链接
                'insertrow', //前插入行
                'insertcol', //前插入列
                'mergeright', //右合并单元格
                'mergedown', //下合并单元格
                'deleterow', //删除行
                'deletecol', //删除列
                'splittorows', //拆分成行
                'splittocols', //拆分成列
                'splittocells', //完全拆分单元格
                'deletecaption', //删除表格标题
                'inserttitle', //插入标题
                'mergecells', //合并多个单元格
                'deletetable', //删除表格
                'cleardoc', //清空文档
                'insertparagraphbeforetable', //"表格前插入行"
                'insertcode', //代码语言
                'fontfamily', //字体
                'fontsize', //字号
                'paragraph', //段落格式
                'edittable', //表格属性
                'edittd', //单元格属性
                'link', //超链接
                'emotion', //表情
                'spechars', //特殊字符
                'insertvideo', //视频
                'help', //帮助
                'justifyleft', //居左对齐
                'justifyright', //居右对齐
                'justifycenter', //居中对齐
                'justifyjustify', //两端对齐
                'forecolor', //字体颜色
                'backcolor', //背景色
                'insertorderedlist', //有序列表
                'insertunorderedlist', //无序列表
                'fullscreen', //全屏
                'directionalityltr', //从左向右输入
                'directionalityrtl', //从右向左输入
                'rowspacingtop', //段前距
                'rowspacingbottom', //段后距
                'pagebreak', //分页
                'insertframe', //插入Iframe
                'imagenone', //默认
                'imageleft', //左浮动
                'imageright', //右浮动
                'attachment', //附件
                'imagecenter', //居中
                'wordimage', //图片转存
                'lineheight', //行间距
                'edittip ', //编辑提示
                'customstyle', //自定义标题
                'autotypeset', //自动排版
                'touppercase', //字母大写
                'tolowercase', //字母小写
                'template', //模板
                'inserttable', //插入表格
                'drafts', // 从草稿箱加载
                'charts', // 图表
            ]]
        });
        //数据
        ue.addListener("ready", function () {
            var hiddenHtml = $("#hiddenDiv").html();
            ue.setContent(hiddenHtml);
        });


        ueEn = UE.getEditor('ueditorEn', {
            toolbars: [[
                'insertimage',
                'insertvideo',
                'redo', //重做
                'bold', //加粗
                'indent', //首行缩进
                'italic', //斜体
                'underline', //下划线
                'strikethrough', //删除线
                'subscript', //下标
                'fontborder', //字符边框
                'superscript', //上标
                'formatmatch', //格式刷
                'source', //源代码
                'blockquote', //引用
                'pasteplain', //纯文本粘贴模式
                'selectall', //全选
                'print', //打印
                'preview', //预览
                'horizontal', //分隔线
                'removeformat', //清除格式
                'time', //时间
                'date', //日期
                'unlink', //取消链接
                'insertrow', //前插入行
                'insertcol', //前插入列
                'mergeright', //右合并单元格
                'mergedown', //下合并单元格
                'deleterow', //删除行
                'deletecol', //删除列
                'splittorows', //拆分成行
                'splittocols', //拆分成列
                'splittocells', //完全拆分单元格
                'deletecaption', //删除表格标题
                'inserttitle', //插入标题
                'mergecells', //合并多个单元格
                'deletetable', //删除表格
                'cleardoc', //清空文档
                'insertparagraphbeforetable', //"表格前插入行"
                'insertcode', //代码语言
                'fontfamily', //字体
                'fontsize', //字号
                'paragraph', //段落格式
                'edittable', //表格属性
                'edittd', //单元格属性
                'link', //超链接
                'emotion', //表情
                'spechars', //特殊字符
                'insertvideo', //视频

                'justifyleft', //居左对齐
                'justifyright', //居右对齐
                'justifycenter', //居中对齐
                'justifyjustify', //两端对齐
                'forecolor', //字体颜色
                'backcolor', //背景色
                'insertorderedlist', //有序列表
                'insertunorderedlist', //无序列表
                'fullscreen', //全屏
                'directionalityltr', //从左向右输入
                'directionalityrtl', //从右向左输入
                'rowspacingtop', //段前距
                'rowspacingbottom', //段后距
                'pagebreak', //分页
                'insertframe', //插入Iframe
                'imagenone', //默认
                'imageleft', //左浮动
                'imageright', //右浮动
                'attachment', //附件
                'imagecenter', //居中
                'wordimage', //图片转存
                'lineheight', //行间距
                'edittip ', //编辑提示
                'customstyle', //自定义标题
                'autotypeset', //自动排版
                'touppercase', //字母大写
                'tolowercase', //字母小写

                'inserttable', //插入表格
                'drafts', // 从草稿箱加载
                'charts', // 图表
            ]]
        });
        //数据
        ueEn.addListener("ready", function () {
            var hiddenHtml = $("#hiddenDivEn").html();
            ueEn.setContent(hiddenHtml);
        });
    });

    var initSortable=function () {
        var list=document.getElementById("queOptions");
        Sortable.create(list, {
            animation: 150,
            handle: ".glyphicon-th-list"
        });
        $("#queOptions").on("click","li .close",function () {
            $(this).parent().remove();
        })
    };

    var initTreeF7=function (datas) {
        if(datas==""){
            datas=[];
        }
        qSortable = new QSortable({
            tarId: 'queOptions',
            data: datas
        });
        qSortable.buildTable();
        $("#selQuestions").click(function () {
            qTreeF7.f7("#springUrl('/a/questionscategory/treeData')", "#springUrl('/a/questions/listData')","questionsName","questions",qSortable);
        })
    };
    var initPic=function (datas) {
        var paperPic = datas;
        var protocol=window.location.protocol;
        var host=window.location.host;
        if(paperPic && paperPic!=null){
            if(paperPic.substring(0,7)=="http://"){
                $("#previewImg").attr("src",paperPic).show();
            }else{
                $("#previewImg").attr("src",protocol+'//'+host+paperPic).show();
            }
        }
        /**
         * 小图选择
         */
        $("#ImgSelect").click(function(){
            Cfapp.sm({
                title:"问卷banner图选择",
                success:function(data){
                    $("#paperPic").val(data[0]);
                    $("#previewImg").attr("src",data[0]).show();
                }
            })
        })
    };
    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=='QUESTION'){
                option= '<option value="' +type+ '"  >问卷</option>';
            }else if(type=='FORM'){
                option= '<option value="' +type+ '"  >表单</option>';
            }else if(type=='TESTING'){
                option= '<option value="' +type+ '">测评</option>';
            }
            optionStr += option;
        }
        $('#paperType').html(optionStr);

        var queType = '$!{data.paperType}';
        if(queType!=''){
            $("#paperType").find("option[value='" + queType + "']").attr( "selected", true);
        }
    };

    var recdTypeAdd = function () {
        var initForm = function () {
            var initFormCtrl = function () {
                bindEvent();
            };
            var bindEvent = function () {
                var sSource = "#springUrl('/a/papercategory/treeData')";
                $('#categorySelect').on('click',
                    function () {
                        Cfapp.treeSingleSelect({
                            title: '上级分类选择',
                            url: sSource,
                            displayFields: 'paperCategoryName',
                            storeFields: 'paperCategory',
                            success: function () {
                                console.log('success');
                            },
                            cancel: function () {
                                console.log('cancel');
                            }
                        })
                    });

                $("#myForm").validate({
                    rules: {},
                    messages: {},
                    submitHandler: function (form) {
                        var questions=qSortable.getValues();
                        var dataJson=$("#myForm").serializeJSON();
                        var picurl = coverPP.getStringValues();
                        dataJson.act_start_pic = picurl.substring(0,picurl.lastIndexOf(','));
                        dataJson.questions=JSON.stringify(questions);
                        dataJson.content_des = ue.getContent();
                        dataJson.content_des_en = ueEn.getContent();
                        if(questions.length==0){
                            alert("请正确填写或选择问题");
                            return;
                        }
                        //$("#questions").val(JSON.stringify(questions));
                        $("#save").attr("disabled", true);
                        $.ajax(
                            {
                                type: "POST",
                                dataType: "json",
                                url:"#springUrl('/a/paper/formData')",
                                data:dataJson,
                                success:function (returnobj) {
                            $("#save").attr("disabled", false);
                            if (returnobj.errorNo == 2) {  //保存成功
                                Cfapp.confirm({
                                    message: "添加成功",
                                    btnoktext: "继续添加",
                                    btncanceltext: "关闭",
                                    success: function () {
                                        location.href = "#springUrl('/a/paper/form')";
                                    },
                                    cancel: function () {
                                        location.href = "#springUrl('/a/paper/list')";
                                    }
                                });
                            } else if (returnobj.errorNo == 0) {  //修改成功
                                Cfapp.alert({
                                    message: "更新成功",
                                    btntext: "确定",
                                    success: function () {
                                        location.href = "#springUrl('/a/paper/list')";
                                    }
                                });
                            } else if(returnobj.errorNo == 3){
                                Cfapp.alert({
                                    message: "活动已经被别的问卷绑定！",
                                    btntext: "确定",
                                    success: function () {
                                        return;
                                    }
                                });
                            }else{
                                Cfapp.alert({
                                    message: "创建失败",
                                    btntext: "确定",
                                    success: function () {
                                        return;
                                    }
                                });
                            }
                        } });
                    }
                })
            };
            initFormCtrl();
        };
        return {

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

        };
    }();
    //选择活动
    $("#selectactivity").click(function () {
       // alert("a")
        Cfapp.f7({
            dataUrl: "#springUrl('/a/paper/selectactivity')",
            checkType: 'single', // 多选为multi
            title: "选择活动",
            btnoktext: "确定",
            btncanceltext: "取消",
            columns: ['选择', '活动名称', '活动类型','创建时间'],
            isSearch: '0',
            searchColumns: ["title"],//搜索的字段名称
            searchColumnsRemarks: ['活动名称'],//搜索的字段备注
            aoColumns: [{
                "mData": "id"
            }, {
                "mData": "title"
            }, {
                "mData": "type"
            },{
                "mData":"create_time"
            }],
            aoColumnDefs: [{
                "aTargets": [0],
                "mData": "id",
                "mRender": function (a, b, c, d) {
                    return '<input type="radio" id="' + a + ','+c.type+'" name="sel" data-value="' + a + ','+c.type+'" data-name="' + c.title + '">';
                }
            }, {
                "aTargets": [2],
                "mData": "type",
                "mRender": function (a, b, c, d) {//a表示createtime对应的值，c表示当前记录行对象
                    if(a==0){
                        return '大转盘';
                    }else if(a==1){
                        return '砸金蛋';
                    }else if(a==2){
                        return '刮刮卡';
                    }else{
                        return '拆礼盒';
                    }
                }
            },{
                "aTargets":[3],
                "mData":"create_time",
                "mRender":function (a,b,c,d) {
                    return formatDates(a)
                }
            }],
            success: function () {

            },
            cancel: function () {

            },
            storeFields: 'activityid', // 储存的字段
            displayFields: 'activityName'
        })
    });
</script>
</body>
<!-- END BODY -->
</html>