let layer, laydate, table, form;
$(function () {
    layui.use(["layer", "laydate", "table", "form"], function () {
        layer = layui.layer;
        laydate = layui.laydate;
        form = layui.form;
        table = layui.table;
        laydate.render({
            elem: "#startDate",
            type: "month",
            format: "yyyy-MM",
        });
        businessDivisionPullDown(form, null);
        initTable();
    });
});
function search(type) {
    if (type === 1) {
    } else {
        $("#keyWord").val("");
        $("#evaluateDeptId").val("");
        $("#evStatus").val("");
        $("#auditStatus").val("");
        $("#startDate").val("");
        form.render("select");
    }
    table.reload("baseTable", {
        url: ctxPath + "/outsourcer/outsourcerEvaluateList",
        page: {
            curr: 1,
        },
        where: {
            keyWord: $("#keyWord").val(),
            evaluateDeptId: $("#evaluateDeptId").val(),
            evStatus: $("#evStatus").val(),
            auditStatus: $("#auditStatus").val(),
            startDate: $("#startDate").val(),
            endDate: $("#endDate").val(),
        },
    });
}

/*function exportData() {
    let keyWord = $("#keyWord").val();
    let evaluateDeptId = $("#evaluateDeptId").val();
    let evStatus = $("#evStatus").val();
    let auditStatus = $("#auditStatus").val();
    let startDate = $("#startDate").val();
    let token = localStorage.getItem("token")
    let params = "?keyWord=" + keyWord + "&evaluateDeptId=" + evaluateDeptId + "&auditStatus=" + auditStatus + "&evStatus=" + evStatus + "&startDate=" + startDate + '&token=' + token;
    $(location).attr("href", ctxPath + '/outsourcer/outsourcerEvaluateList' + params);
}*/

function initTable() {
    let loginUser = localStorage.getItem("loginUser");
    let user = JSON.parse(loginUser);
    //渲染表格
    table.render({
        elem: "#baseTable",
        url: ctxPath + "/outsourcer/outsourcerEvaluateList", //数据接口
        method: "get", //方式默认是get
        toolbar: "default", //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
        where: {}, //post请求必须加where ，post请求需要的参数
        cellMinWidth: 80,
        cols: [
            [
                //表头
                {
                    field: "number",
                    width: 80,
                    title: "序号",
                    align: "center",
                    type: "numbers",
                },
                { field: "evaluateDept", align: "center", title: "评价事业部" },
                { field: "evaluateTitle", align: "center", title: "评价标题" },
                { field: "evaluateMonth", align: "center", title: "评价年月" },
                { field: "projectNum", align: "center", title: "项目数量" },
                {
                    field: "outsourcerNum",
                    align: "center",
                    title: "外包商数量",
                },
                {
                    field: "evaluateStatus",
                    align: "center",
                    title: "评价状态",
                    templet: (d) =>
                        Number(d.evaluateNum) > 0 && d.rejectReason
                            ? "评价驳回"
                            : Number(d.evaluateNum) > 0
                            ? "已评价"
                            : "未评价",
                },
                {
                    field: "auditStatus",
                    align: "center",
                    title: "审核状态",
                    templet: (d) => {
                        if (
                            Number(d.isApprove) > 0 &&
                            !isEmpty(d.rejectReason)
                        ) {
                            return "已驳回";
                        }
                        if (Number(d.isApprove) > 0) {
                            return "已通过";
                        }
                        if (
                            Number(d.evaluateNum) > 0 &&
                            Number(d.isApprove) === 0 &&
                            isEmpty(d.rejectReason)
                        ) {
                            return "待审批";
                        }
                        return "待评价";
                    },
                },
                { field: "rejectReason", align: "center", title: "驳回原因" },
                {
                    fixed: "right",
                    width: 180,
                    title: "操作",
                    align: "center",
                    templet: (d) => {
                        let text = "";
                        if (
                            Number(d.isApprove) === 0 &&
                            Number(d.evaluateNum) === 0
                        ) {
                            text +=
                                '<a lay-event="evaluation" style="color: #009688;cursor: pointer;font-size: 15px"' +
                                '  id="evaluation">评价</a>';
                        } else if (Number(d.isApprove) === 1) {
                            text +=
                                '<a lay-event="viewReviews" style="color: #009688;cursor: pointer;font-size: 15px"' +
                                '  id="viewReviews">查看评价</a>';
                        } else if (
                            (Number(d.isApprove) === 0 ||
                                Number(d.isApprove) === 2) &&
                            Number(d.evaluateNum) > 0
                        ) {
                            text +=
                                '<a lay-event="reevaluate" style="color: #009688;cursor: pointer;font-size: 15px"' +
                                '  id="reevaluate">重新评价</a>';
                        }
                        return text;
                    },
                },
            ],
        ],
        id: "baseTable",
        page: true, //开启分页
        loading: true, //数据加载中。。。
        limits: [10, 20, 100], //一页选择显示3,5或10条数据
        limit: 10, //一页显示5条数据
        response: {
            statusCode: 200, //规定成功的状态码，默认：0
        },
        parseData: function (res) {
            //将原始数据解析成 table 组件所规定的数据，res为从url中get到的数据
            let result;
            if (res.data !== "" && res.data != null && res.data !== "null") {
                if (this.page.curr) {
                    result = res.data.slice(
                        this.limit * (this.page.curr - 1),
                        this.limit * this.page.curr
                    );
                } else {
                    result = res.data.slice(0, this.limit);
                }
            }
            return {
                code: res.code, //解析接口状态
                msg: res.msg, //解析提示文本
                count: res.count, //解析数据长度
                data: result, //解析数据列表
            };
        },
        toolbar: "#toolbar",
    });

    table.on("tool(test)", function (obj) {
        var data = obj.data; //当前行数据
        var rowIndex = obj.index;
        var layEvent = obj.event; //当前点击的事件名
        if (layEvent === "evaluation") {
            evaluatingSubpages(0, data);
        }
        if (layEvent === "reevaluate") {
            evaluatingSubpages(2, data);
        }

        if (layEvent === "viewReviews") {
            evaluatingSubpages(1, data);
        }
    });

    //评价子页面
    function evaluatingSubpages(type, data) {
        data.type = type;
        let title = "";
        if (type === 0) {
            title = "评价";
        } else if (type === 1) {
            title = "查看评价";
        } else if (type === 2) {
            title = "重新评价";
        }
        openPage(
            title,
            "./evaluationForm.html?type=" +
                type +
                "&templateId=" +
                data.templateId +
                "&id=" +
                data.id +
                "&detailsId=" +
                data.detailsId
        );
    }

    function openPage(title, url) {
        //如果是查看页面，不需要弹出层的确定按钮
        const layerOptions = {
            type: 2,
            title: title,
            content: url,
            maxmin: false,
            area: ["90%", "95%"],
            success: function (layero, index) {},
        };

        if (title.indexOf("查看") !== -1) {
            layer.open(layerOptions);
        } else {
            layerOptions.btn = ["确定提交"];
            layerOptions.yes = function (index, layero) {
                //提交子页面时执行
                // 获取弹出层中的form表单元素
                const formSubmit = layer.getChildFrame("form", index);
                // 查找class样式为submitBtn的按钮
                const submited = formSubmit.find("button.subBtn");
                // 触发点击事件，会对表单进行验证，验证成功则提交表单，失败则返回错误信息
                submited.click();
            };
            layer.open(layerOptions);
        }
    }
}
