// 专责审批 js文件
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("");
        $("#status").val("");
        $("#startDate").val("");
        $("#endDate").val("");
        form.render("select");
    }
    table.reload("baseTable", {
        url: ctxPath + "/backstage/evaluate/dedicatedEvalSummaryList",
        page: {
            curr: 1,
        },
        where: {
            type: "audit",
            keyWord: $("#keyWord").val(),
            evaluateDeptId: $("#evaluateDeptId").val(),
            evStatus: $("#evStatus").val(),
            status: $("#status").val(),
            startDate: $("#startDate").val(),
            endDate: $("#endDate").val(),
        },
    });
}

function initTable() {
    //渲染表格
    table.render({
        elem: "#baseTable",
        url: ctxPath + "/backstage/evaluate/dedicatedEvalSummaryList", //数据接口
        method: "get", //方式默认是get
        toolbar: "default", //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
        where: {
            type: "audit",
        }, //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: "isApprove",
                    align: "center",
                    title: "评价状态",
                    templet: (d) => {
                        if (d.rejectReason) {
                            return "评价驳回";
                        } else {
                            return Number(d.isApprove) === 0
                                ? "待评价"
                                : Number(d.isApprove) === 1
                                ? "已评价"
                                : "";
                        }
                        // Number(d.isApprove) === 0  ? '待评价' : (Number(d.isApprove) === 1 ? '已评价' : '')
                    },
                },
                {
                    field: "status",
                    align: "center",
                    title: "审核状态",
                    templet: (d) =>
                        Number(d.status) === 0
                            ? "待审核"
                            : Number(d.status) === 1
                            ? "已审核"
                            : "审核中",
                },
                { field: "rejectReason", align: "center", title: "驳回原因" },
                {
                    fixed: "right",
                    width: 180,
                    title: "操作",
                    align: "center",
                    templet: (d) => {
                        let text = "";
                        if (d.isAudit == "1") {
                            text +=
                                '<a lay-event="examine" style="color: #009688;cursor: pointer;font-size: 15px"' +
                                '  id="examine">审核</a>';
                        }
                        text +=
                            '<a lay-event="evaluateTable" style="color: #009688;cursor: pointer;font-size:' +
                            ' 15px;margin-left: 10px"' +
                            '  id="evaluateTable">查看</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) {
        const data = obj.data; //当前行数据
        const rowIndex = obj.index;
        const layEvent = obj.event; //当前点击的事件名
        switch (layEvent) {
            case "examine":
                ViewReviews(data, "审核");
                break;
            case "evaluateTable":
                ViewReviews(data, "查看");
                break;
            default:
                console.warn(`未知事件: ${layEvent}`);
        }
    });

    //查看子页面
    function ViewReviews(data, title) {
        try {
            localStorage.setItem("AllAuditTitle", title);
            const layerIndex = layer.open({
                type: 2,
                title: title,
                shade: 0.3,
                area: ["98%", "98%"],
                scrollbar: true,
                move: true,
                anim: 2,
                shadeClose: false,
                content:
                    "./summaryAuditView.html?evaluateId=" +
                    data.id +
                    "&templateId=" +
                    data.templateId,
                end: function () {
                    search(2);
                },
            });
        } catch (error) {
            console.error("ViewReviews 错误:", error);
        }
    }
}
