layui.define(['form', 'laydate', 'table'], function (exports) {
    var form = layui.form;
    var laydate = layui.laydate;
    var table = layui.table;
    var resourcesQuestionTable = null;
    var isAdmin = $("#isAdmin").val() === "true"?true:false;

    var view = {
        init: function () {
            this.initTable();
            this.initSearchForm();
            this.initToolBar();
            window.dataReload = function () {
                Lib.doSearchForm($("#searchForm"), resourcesQuestionTable)
            }
        },
        initTable: function () {
            resourcesQuestionTable = table.render({
                elem: '#resourcesQuestionTable',
                height: Lib.getTableHeight(),
                cellMinWidth: 100,
                method: 'post',
                even: true,
                size: "lg",
                url: Common.ctxPath + '/jlw/resourcesQuestion/testItemManageList.json' // 数据接口
                , page: Lib.tablePage // 开启分页
                , limit: 10,
                cols: [[ // 表头
                    {
                        type: 'checkbox', align: "center"
                    },
                    {
                        field: 'questionStem', width: 320,
                        title: '题干', align: "center"
                    },
                    {
                        field: 'questionType', align: "center", width: 80,
                        title: '题型', templet: function (d) {//(1单选 2多选 3判断 4填空 5分析)
                            return d.questionType == 1 ? '单选' : d.questionType == 2 ? '多选' : d.questionType == 3 ? '判断' : '-';
                        }
                    },
                    {
                        field: 'courseInfoParentName',
                        title: '归属课程', align: "center"
                    },
                    {
                        field: 'courseInfoName',
                        title: '归属章节', align: "center"
                    },
                    {
                        field: 'addTypeText',
                        title: '题库来源', align: "center"
                    },
                    /*{
                        field: 'orgIdText',
                        title: '上传院校', align: "center"
                    },*/
                    {
                        field: 'questionStatus', width: 80,
                        title: '状态', align: "center", templet: function (d) {
                            return d.questionStatus === 1
                                ? '<span class="layui-badge layui-bg-green" style="background-color: #abd5ff !important; color: #088eed!important">已上架</span>'
                                : '<span class="layui-badge layui-bg-gray" style="background-color: #dadada !important; color: #6c6c6c!important">已下架</span>'
                        }
                    },
                    {
                        field: 'userId', title: '操作', align: "center", width: 220, style:"text-align: left", templet: function (d) {
                            let htm = "";
                            /*if((d.sourceType == '2' && isAdmin) || (d.sourceType == '1' && !isAdmin)){
                                htm +='<a class="layui-table-link" lay-event="up" data-item="${JSON.stringify(d)}" >上架</a>\n'
                                    +'<a class="layui-table-link" lay-event="down" data-item="${JSON.stringify(d)}" style="color: #bebebe!important">下架</a>'
                                    +'<a class="layui-table-link" lay-event="edit">编辑</a>\n'
                                    +'<a class="layui-table-link" lay-event="del">删除</a>';
                            }else{
                                htm += '<a class="layui-table-link" lay-event="edit">查看</a>';
                            }*/
                            if((d.addType == 'ADMIN_ADD' && isAdmin)){
                                htm +='<a class="layui-table-link" lay-event="up" data-item="${JSON.stringify(d)}" >上架</a>\n'
                                    +'<a class="layui-table-link" lay-event="down" data-item="${JSON.stringify(d)}" style="color: #bebebe!important">下架</a>'
                                    +'<a class="layui-table-link" lay-event="edit">编辑</a>\n'
                                    +'<a class="layui-table-link" lay-event="del">删除</a>';
                            }else if((d.addType == 'ADMIN_ADD' && !isAdmin)){
                                htm += '<a class="layui-table-link" lay-event="edit">查看</a>';
                            }

                            // TODO 上下架前端逻辑待开发
                            return htm;
                        }
                    }

                ]]

            });

            table.on('checkbox(resourcesQuestionTable)', function (obj) {
                var resourcesQuestion = obj.data;
                if (obj.checked) {
                    //按钮逻辑Lib.buttonEnable()
                } else {

                }
            })
        },

        initSearchForm: function () {
            Lib.initSearchForm($("#searchForm"), resourcesQuestionTable, form);
        },
        initToolBar: function () {
            toolbar = {
                add: function () { // 获取选中数据
                    var url = "/jlw/resourcesQuestion/add.do";
                    Common.openDlg(url, "题库管理/新增题目");
                },
                refresh: function () {//刷新
                    searchForm.reset();
                    Lib.doSearchForm($("#searchForm"), resourcesQuestionTable, 1);
                },
                up: function () {
                    var data = Common.getMoreDataFromTable(table, "handsOnTable");
                    var ids = Common.concatBatchId(data, "handsOnId");
                    upDrown(1, data.resourcesQuestionId,data.sourceType);
                },
                down: function () {
                    var data = Common.getMoreDataFromTable(table, "handsOnTable");
                    var ids = Common.concatBatchId(data, "handsOnId");
                    upDrown(2, data.resourcesQuestionId,data.sourceType);
                },
                search: function () {
                    Lib.doSearchForm($("#searchForm"), resourcesQuestionTable, 1);
                }, import: function () {
                    var url = "/jlw/resourcesQuestion/importPage.do";
                    Common.openDlg(url, "");
                }, allDel: function () {
                    var checkStatus = table.checkStatus('resourcesQuestionTable').data;
                    if (checkStatus.length <= 0) {
                        layer.msg("请选择需要删除的题目!", {
                            offset: ['50%'],
                            icon: 2,
                            time: 2000 //2秒关闭(如果不配置,默认是3秒)
                        });
                        return;
                    }
                    var ids = "";
                    for (var i = 0; i < checkStatus.length; i++) {
                        ids += checkStatus[i].resourcesQuestionId;
                        if (i < checkStatus.length - 1) {
                            ids += ",";
                        }
                    }
                    allDelete(ids);
                }, chapter: function () { //批量章节调整
                    $("select[name='courseInfoIds_1'],select[name='courseInfoIds_2']").val("");
                    $("select[name='courseInfoId_1'],select[name='courseInfoId_2']").html("<option value=\"\">请选择</option>");
                    form.render();
                    layer.open({
                        type: 1,
                        offset: 'auto',
                        title: '批量章节调整',
                        shadeClose: 'true',
                        area: ['800px', '300px'],
                        scrollbar: 'false',
                        btnAlign: 'c',
                        content: $("#chapter_dialog"),
                        btn: ['确定', '取消'],
                        yes: function (index, layero) {
                            var oldCourseInfoId = $("#chapter_dialog select[name='courseInfoId_1']").val(),
                                newCourseInfoId = $("#chapter_dialog select[name='courseInfoId_2']").val();
                            if ($.isEmpty(oldCourseInfoId)) {
                                layer.msg("请选择题目来源章节!", {
                                    offset: ['50%'],
                                    icon: 2,
                                    time: 1500 //2秒关闭(如果不配置,默认是3秒)
                                });
                                return;
                            }
                            if ($.isEmpty(newCourseInfoId)) {
                                layer.msg("请选择题去向源章节!", {
                                    offset: ['50%'],
                                    icon: 2,
                                    time: 1500 //2秒关闭(如果不配置,默认是3秒)
                                });
                                return;
                            }
                            layer.confirm('是否确定调整该章节信息?', function (index1) {
                                layer.close(index1);
                                layer.load(3);
                                setTimeout(function () {
                                    var ret = Common.postAjax("/jlw/resourcesQuestion/changAll.json", {
                                        oldCourseInfoId: oldCourseInfoId,
                                        newCourseInfoId: newCourseInfoId
                                    })
                                    layer.closeAll("loading");
                                    layer.msg(ret.msg, {
                                        offset: ['50%'],
                                        icon: ret.code == 0 ? 1 : 2,
                                        time: 1500 //2秒关闭(如果不配置,默认是3秒)
                                    }, function () {
                                        if (ret.code == 0) {
                                            layer.close(index);
                                            Lib.tableRefresh();
                                        }
                                    });
                                }, 200);
                            });
                        }
                    });
                }
            };
            $('.ext-toolbar').on('click', function () {
                var type = $(this).data('type');
                toolbar[type] ? toolbar[type].call(this) : '';
            });
        }, initTableTool: table.on('tool(resourcesQuestionTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var url = "/jlw/resourcesQuestion/add.do?resourcesQuestionId=" + data.resourcesQuestionId;
                Common.openDlg(url, "题库管理/编辑题目");
            } else if (obj.event === "del") {
                allDelete(data.resourcesQuestionId);
            } else if (obj.event === "up") {
                upDrown(1, data.resourcesQuestionId,data.sourceType);
            } else if (obj.event === "down") {
                upDrown(2, data.resourcesQuestionId,data.sourceType);
            }
        })
    }

    //删除
    function allDelete(ids) {
        layer.confirm('是否确定删除该信息?', function (index) {
            var ret = Common.postAjax("/jlw/resourcesQuestion/delete.json", {ids: ids});
            layer.msg(ret.code == 0 ? "删除成功!" : ret.msg, {
                offset: ['50%'],
                icon: ret.code == 0 ? 1 : 2,
                time: 1500 //2秒关闭(如果不配置,默认是3秒)
            }, function () {
                if (ret.code == 0) {
                    Lib.tableRefresh();
                }
            });
        });
    }

    //上下架
    function upDrown(status, ids ,sourceType) {
        layer.confirm(status == 1 ? '是否确定上架该试题?' : '是否确定下架该试题?', function (index) {
            var ret = Common.postAjax("/jlw/resourcesQuestion/putOnShelves.json", {
                resourcesQuestionId: ids,
                questionStatus: status,
                sourceType:sourceType
            });
            layer.msg(ret.code == 0 ? "操作成功!" : ret.msg, {
                offset: ['50%'],
                icon: ret.code == 0 ? 1 : 2,
                time: 1500 //2秒关闭(如果不配置,默认是3秒)
            }, function () {
                if (ret.code == 0) {
                    Lib.tableRefresh();
                }
            });
        });
    };

    //选择归属课程 并且根据选择的课程查询章节 resourcesQuestionIdsIsNotNull:查询题目不为空的章节
    form.on('select(select_courseInfoIds)', function (obj) {
        let sourceType = $("select[name='courseInfoId_0']").find('option:selected').attr("status");
        let courseInfoType = 2;//courseInfoType(2 章节,3 节)
        Lib.getCourseInfo($("select[name='courseInfoId_1']"),obj.value,courseInfoType,sourceType,1);
        $("select[name='courseInfoId_2']").empty();
        layui.form.render();
    });

    //选择归属课程 并且根据选择的课程查询章节
    form.on('select(select_courseInfoIds_)', function (obj) {
        let sourceType = $("select[name='courseInfoId_0']").find('option:selected').attr("status");
        let courseInfoType = 3;
        Lib.getCourseInfo($("select[name='courseInfoId_2']"), obj.value,courseInfoType,sourceType);
    });

    exports('index', view);

});