$(function(){
    // 切换
    $(".icon_appearset").click(function(){
        var index = $(this).index();
        $(".wjContent").eq(index).show().siblings().hide();
    })
    // 其它选项
    $("body").on("blur", ".option_other input[type='text']", function () {
        if ($.trim($(this).val())!=""){
            $(this).parents(".option_other").find("input[type='radio'],input[type='checkbox']").prop("checked", "checked");
        }
    })
    // 查看图片
    $("body").on("click", ".QImgCon", function () {
        var url = $(this).find("img").attr("src");
        layer.open({
            type: 1,
            title: false,
            closeBtn: 1,
            area: ['auto'],
            skin: 'layui-layer-nobg', //没有背景色
            shadeClose: true,
            content: '<img src="' + url +'">'
        });
    })
    // 查看选项说明
    $("body").on("click", ".option_explain", function (e) {
        var val = $(this).data("explain-text");
        if ($(this).parents(".option_img").length){
            var title = $(this).parents(".option_img").find(".option_name").text();
        }else{
            var title = $(this).parents(".option").find(".option_name").text();
        }
        layer.open({
            type: 1,
            title:title,
            skin: "layer-explain",
            shadeClose: true,
            resize: false,
            move: false,
            content: '<div class="con">' + val + '</div>',
        });
        e.stopPropagation(); // 阻止事件冒泡
    })
    // 级联题
    var data_cascader = [
        {
            "value": "cascader_0",
            "name": "选项1",
            "cascader_0": [
                {
                    "value": "cascader_0_0",
                    "name": "选项1-1",
                    "cascader_0_0": [
                        {
                            "value": "cascader_0_0_0",
                            "name": "选项1-1-1",
                            "cascader_0_0_0": [
                                {
                                    "value": "cascader_0_0_0_0",
                                    "name": "选项1-1-1-1"
                                },
                                {
                                    "value": "cascader_0_0_0_1",
                                    "name": "选项1-1-1-2"
                                }
                            ]
                        },
                        {
                            "value": "cascader_0_0_1",
                            "name": "选项1-1-2",
                            "cascader_0_0_1": [
                                {
                                    "value": "cascader_0_0_1_0",
                                    "name": "选项1-1-2-1"
                                },
                                {
                                    "value": "cascader_0_0_1_1",
                                    "name": "选项1-1-2-2"
                                }
                            ]
                        }
                    ]
                },
                {
                    "value": "cascader_0_1",
                    "name": "选项1-2",
                    "cascader_0_1": [
                        {
                            "value": "cascader_0_1_0",
                            "name": "选项1-2-1",
                            "cascader_0_1_0": [
                                {
                                    "value": "cascader_0_1_0_0",
                                    "name": "选项1-2-1-1"
                                },
                                {
                                    "value": "cascader_0_1_0_1",
                                    "name": "选项1-2-1-2"
                                }
                            ]
                        },
                        {
                            "value": "cascader_0_1_1",
                            "name": "选项1-2-2",
                            "cascader_0_1_1": [
                                {
                                    "value": "cascader_0_1_1_0",
                                    "name": "选项1-2-2-1"
                                },
                                {
                                    "value": "cascader_0_1_1_1",
                                    "name": "选项1-2-2-2"
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        {
            "value": "cascader_1",
            "name": "选项2",
            "cascader_1": [
                {
                    "value": "cascader_1_0",
                    "name": "选项2-1",
                    "cascader_1_0": [
                        {
                            "value": "cascader_1_0_0",
                            "name": "选项2-1-1",
                            "cascader_1_0_0": [
                                {
                                    "value": "cascader_1_0_0_0",
                                    "name": "选项2-1-1-1"
                                },
                                {
                                    "value": "cascader_1_0_0_1",
                                    "name": "选项2-1-1-2"
                                }
                            ]
                        },
                        {
                            "value": "cascader_1_0_1",
                            "name": "选项2-1-2",
                            "cascader_1_0_1": [
                                {
                                    "value": "cascader_1_0_1_0",
                                    "name": "选项2-1-2-1"
                                },
                                {
                                    "value": "cascader_1_0_1_1",
                                    "name": "选项2-1-2-2"
                                }
                            ]
                        }
                    ]
                },
                {
                    "value": "cascader_1_1",
                    "name": "选项2-2",
                    "cascader_1_1": [
                        {
                            "value": "cascader_1_1_0",
                            "name": "选项2-2-1",
                            "cascader_1_1_0": [
                                {
                                    "value": "cascader_1_1_0_0",
                                    "name": "选项2-2-1-1"
                                },
                                {
                                    "value": "cascader_1_1_0_1",
                                    "name": "选项2-2-1-2"
                                }
                            ]
                        },
                        {
                            "value": "cascader_1_1_1",
                            "name": "选项2-2-2",
                            "cascader_1_1_1": [
                                {
                                    "value": "cascader_1_1_1_0",
                                    "name": "选项2-2-2-1"
                                },
                                {
                                    "value": "cascader_1_1_1_1",
                                    "name": "选项2-2-2-2"
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ];
    cascader(data_cascader);
    // 分类题
    var classify_l = $(".classify-list .classify-wrap ul");
    Sortable.create(classify_l.get(0), {
        group: 'shared',
        animation: 150,
    });
    $(".category-list .category-inner").each(function () {
        Sortable.create($(this).get(0), {
            group: 'shared',
            animation: 150,
            onAdd: function (evt) {
                console.log(evt);
                $(evt.to).find(".drag-area").remove();
                $(evt.to).css({ "background": "#f7f8fa" });
            },
            onRemove: function (/**Event*/evt) {
                if ($(evt.to).find(".classify-item").length <= 0) {
                    $(evt.to).html('<div class="drag-area">拖入此处</div>');
                    $(evt.to).css({ "background": "#fff" });
                }
            },
        });
    })
    // 自增表格题,处理下拉组件被覆盖的问题
    $(".table_auto").on("click",".am-selected",function(){
        var p_left=$(this).parents(".table_auto").offset().left;
        var left = $(this).offset().left;
        var p_top = $(this).parents(".table_auto").offset().top;
        var top = $(this).offset().top;
        var height = $(this).height();
        $(this).find(".am-selected-content").css({ left: left - p_left, top: top - p_top + height});
    })
    // 评价题
    $(".option_score .score_icon").hover(function () {
        $(this).find(".option_name").css({ "font-size": "34px" });
        $(this).addClass("hover");
        $(this).prevAll().addClass("hover");
    }, function () {
        $(this).find(".option_name").css({ "font-size": "28px" });
        $(this).removeClass("hover");
        $(this).prevAll().removeClass("hover");
    })
    $(".option_score").on("click",".score_icon",function () {
        $(this).addClass("active");
        $(this).prevAll().addClass("active");
        $(this).nextAll().removeClass("active");
    })
    $(".content[data-question-type='evaluation']").on("click",".score_icon",function(){
        var index = $(this).index(), html = "", text="";
        var init = '<span class="evaluate-label evaluate">写评价&nbsp;<i class="am-icon-pencil"></i></span>';
        var evaluateArr = ["态度冷淡", "推销多", "技术差"];
        if (index == 0) {
            text = '"非常不满意"';
            for (var i = 0; i < evaluateArr.length; i++) {
                html += '<span class="evaluate-label">' + evaluateArr[i] +'</span>';
            }
        } else if (index == 1) {
            text = '"比较不满意"';
            for (var i = 0; i < evaluateArr.length; i++) {
                html += '<span class="evaluate-label">' + evaluateArr[i] +'</span>';
            }
        }else {
            init = "";
        }
        $(this).parents(".content").find(".evaluation_desc .score_text span").text(text);
        $(this).parents(".content").find(".evaluation_desc .evaluate_list").html(html + init);
    })
    $("body").on("click", ".evaluate-label", function () {
        $(this).toggleClass("active");
    })
    $("body").on("click", ".evaluate", function () {
        if ($(this).is(".active")) {
            $(".evaluation-trea").html('<textarea rows="6" placeholder="说说你的评价"></textarea>');
        } else {
            $(".evaluation-trea").html("");
        }
    })
    // NPS题
    $(".content[data-question-type='nps_score'] .am-table td").click(function () {
        $(this).addClass("active").siblings().removeClass("active");
    })
    // 排序题
    $(".sort-r ul li").each(function () {
        Sortable.create($(this).get(0), {
            group: {
                name: 'sort',
                put: false // 不允许拖拽进这个列表
            },
            animation: 150,
            sort: false, // 设为false，禁止sort
        });
    })
    $(".sort-l ul li .sortable").each(function () {
        Sortable.create($(this).get(0), {
            group: 'sort',
            animation: 150,
        });
    })
    // 比重题
    $(".proportion-edit").click(function () {
        var input = $(this).prev(".min-value");
        $(this).prev(".min-value").removeAttr("disabled").focus();
        $(this).hide();
        // input[0].setSelectionRange(0, input.val().length);
    })
    $(".min-value").blur(function () {
        var this_ = $(this);
        var total = 100; // 比重总和
        var val = this_.val();
        if (val <= 0) {
            this_.val(0);
            val = 0;
        } else if (val > total) {
            this_.val(0);
            val = 0;
        }
        if (this_.parents(".list-unstyled").find("li").length == 2) {
            if (this_.parents("li").next("li").length) {
                this_.parents("li").next("li").find(".tint").css({ left: 100 - val + "%" });
                this_.parents("li").next("li").find(".selected-bar").css({ width: 100 - val + "%" });
                this_.parents("li").next("li").find(".min-value").val(100 - val);
            } else if (this_.parents("li").prev("li").length) {
                this_.parents("li").prev("li").find(".tint").css({ left: 100 - val + "%" });
                this_.parents("li").prev("li").find(".selected-bar").css({ width: 100 - val + "%" });
                this_.parents("li").prev("li").find(".min-value").val(100 - val);
            }
        }
        this_.attr("disabled", "disabled");
        this_.next(".proportion-edit").show();
        this_.parents(".option-proportion").find(".tint").css({ left: val + "%" });
        this_.parents(".option-proportion").find(".selected-bar").css({ width: val + "%" })
    })
    $(".tint").on("mousedown", function () {
        var this_ = $(this);
        var total = 100; // 比重总和
        var width = this_.parent(".proportion-bar-primary").width(); // 总长度
        $(document).on("mousemove", function (e) {
            this_.offset({ left: e.pageX });
            var left = this_.position().left;
            var mean = Math.ceil(total / (width / left)); // 平均值
            if (left <= 0) {
                left = 0;
                this_.css({ left: 0 });
                mean = 0;
            } else if (left >= width) {
                left = width;
                this_.css({ left: width });
                mean = total;
            }
            if (this_.parents(".list-unstyled").find("li").length == 2) {
                if (this_.parents("li").next("li").length) {
                    this_.parents("li").next("li").find(".tint").css({ left: width - left });
                    this_.parents("li").next("li").find(".selected-bar").css({ width: 100 - mean + "%" });
                    this_.parents("li").next("li").find(".min-value").val(100 - mean);
                } else if (this_.parents("li").prev("li").length) {
                    this_.parents("li").prev("li").find(".tint").css({ left: width - left });
                    this_.parents("li").prev("li").find(".selected-bar").css({ width: 100 - mean + "%" });
                    this_.parents("li").prev("li").find(".min-value").val(100 - mean);
                }
            }
            this_.parents(".option-proportion").find(".selected-bar").css({ width: mean + "%" });
            this_.parents(".option-proportion").find(".min-value").val(mean);
        })
        $(document).on("mouseup", function () {
            $(document).off('mousemove');
        })
    })
    // 文件上传题
    $('body').on('click', '.file', function () {
        var this_ = $(this); // 点击的元素
        this_.parents(".file-box").find("input[type=file]").trigger("click");
        this_.parents(".file-box").find("input[type=file]").off("change").on("change", function () {
            var name = $(this)[0].files[0].name; // 文件名称
            var type = $(this)[0].files[0].type.split("/")[1]; // 文件类型
            var size = $(this)[0].files[0].size; // 文件大小
            $(this).parents(".file-box").find(".upload").hide();
            $(this).parents(".file-box").find(".uploaded").show();
            $(this).parents(".file-box").find(".file-name").text(name);
            if (type) {
                $(this).parents(".file-box").find(".info-img img").remove();
                $(this).parents(".file-box").find(".info-img").attr("file-type", type);
            } else {
                $(this).parents(".file-box").find(".info-img img").remove();
                $(this).parents(".file-box").find(".info-img").attr("file-type", "default");
            }
            if (type == "png" || type == "jpg" || type == "jpeg") {
                viewImage($(this), this_)
            }
        })
    });
    // 删除文件
    $('body').on('click', '.delete-link', function () {
        $(this).parents(".file-box").find(".file-name").text("");
        $(this).parents(".file-box").find(".info-img").attr("file-type", "");
        $(this).parents(".file-box").find(".info-img img").remove();
        $(this).parents(".uploaded").hide();
        $(this).parents(".file-box").find(".upload").show();
    })

    // 验证
    $("#action").click(function(){
        $(".content").each(function(){
            var this_ = $(this);
            var question_type = $(this).data("question-type"); // 题目类型
            var question_id = $(this).data("question-id"); // 题目id
            switch (question_type) {
                case "single_choice":
                    // this_.find("input[type=radio]").each(function(){
                    //     if ($(this).prop("checked")==false){
                    //         alert("请选择选项");
                    //     }
                    // })
                    // //选择题
                    // if ($(this).find('input[type=radio]:checked').length == 0 && $(this).find('input[type=checkbox]:checked').length ==
                    //     0 && $(this).find('.asterisk').length > 0) {
                    //     boo = true;
                    //     that = $(this);
                    //     return false;
                    // }
                    break;

                default:
                    break;
            }
        })
        return false;
    })
})
// 级联题
var cascader = function (data) {
    var question_id = $(this).parents(".content").data("question-id");
    var cascader_0 = $(".cascader-item[data-cascader-index=0]"),cascader_1 = "",cascader_2 = "",cascader_3 = "";
    if (Array.isArray(data) && data.length > 0) {
        data.forEach(function (i) {
            cascader_0.append('<option value=' + i.value + '>' + i.name + '</option>');
        })
        var val;
        $(cascader_0).on("change", function () {
            val = $(this).val();
            cascader_1 = $(this).parents(".option_select").next(".option_select").find(".cascader-item");
            cascader_1.html("<option>请选择</option>");
            if (cascader_2!=""){
                cascader_2.html("<option>请选择</option>");
            }
            if (cascader_3 != "") {
                cascader_3.html("<option>请选择</option>");
            }
            data.forEach(function (i) {
                if (i[val]) {
                    i[val].forEach(function (j) {
                        cascader_1.append('<option value=' + j.value + '>' + j.name + '</option>');
                    })
                }

            })
        })
        var val_;
        $(cascader_1).on("change", function () {
            val_ = $(this).val();
            cascader_2 = $(this).parents(".option_select").next(".option_select").find(".cascader-item");
            if (cascader_2 != "") {
                cascader_2.html("<option>请选择</option>");
            }
            if (cascader_3 != "") {
                cascader_3.html("<option>请选择</option>");
            }
            data.forEach(function (i) {
                if (i[val]) {
                    i[val].forEach(function (j) {
                        if (j[val_]) {
                            j[val_].forEach(function (k) {
                                cascader_2.append('<option value=' + k.value + '>' + k.name + '</option>');
                            })

                        }
                    })
                }
            })
        })
        $(cascader_2).on("change", function () {
            var val__ = $(this).val();
            cascader_3 = $(this).parents(".option_select").next(".option_select").find(".cascader-item");
            cascader_3.html("<option>请选择</option>");
            data.forEach(function (i) {
                if (i[val]) {
                    i[val].forEach(function (j) {
                        if (j[val_]) {
                            j[val_].forEach(function (k) {
                                if (k[val__]) {
                                    k[val__].forEach(function (l) {
                                        cascader_3.append('<option value=' + l.value + '>' + l.name + '</option>');
                                    })
                                }
                            })

                        }
                    })
                }
            })
        })
    }
}
// 文件上传题
function viewImage(e, this_) {
    var file = e.prop('files')[0];
    if (file) {
        var reader = new FileReader();
        var formData = new FormData();
        reader.readAsDataURL(file);
        reader.onloadend = function (e) {
            // 必须项,不可修改
            formData.append("action", "upload");
            // 图片数据'file'为必须的命名不可更改,第二个参数为图片数据
            formData.append("file", file);  // 添加自定义属性(name)
            // formData.append("file", this_[0].files[0]);
            // 图片存放路径,第一个参数'dir'不可更改,第二个为upload文件夹下的文件夹名称,没有的话自动创建
            formData.append("dir", "vote_sys");
            // 图片保存名称,可有,没有则按年月日命名
            // formData.append("filename", "my_file_name");
            $.ajax({
                scriptCharset: 'UTF-8',
                type: 'POST', // 请求方式
                url: '/api/admin_process', // 必须的请求路径
                data: formData, // 参数
                cache: false,
                processData: false,
                contentType: false,
                dataType: 'json', // 数据类型
                success: function (data) {  // 请求成功后的回调函
                    this_.parents(".file-box").find(".info-img").attr("file-type", "");
                    this_.parents(".file-box").find(".info-img").html('<img src="' + data.url + '" alt="">');
                },
                error: function () { // 请求失败后的回调函数
                    layer.msg('上传失败', { time: 1000 });
                },
                complete: function () {  // 结束执行
                },
            })
        }
    }
}