let form, layer;
let objParam;

function setParams(obj) {
    objParam = JSON.parse(obj);
    $('#title').html(objParam.proName + objParam.subName + objParam.teamName + "评价表");
    $('#subName').html(objParam.subName);
    $('#teamName').html(objParam.teamName);
    $('#userName').html(objParam.userName);
    layui.use(['form', 'layer'], function () {
        form = layui.form;
        layer = layui.layer;
        form.verify({
            isLength: function (value, item) {
                if (value) {
                    if (value.trim().length < 20) {
                        return "评价内容至少20字";
                    }
                }
            },
        });
        form.on('submit(formData)', function (data) {
            submitApply(data);
        });
        form.render();
        if(objParam.type === 2){ // 重新评价
            getDataDetails();
        }
    });
}

// 评价详情
function getDataDetails() {
    $.ajax({
        url: `${ctxPath}` + '/backstage/teamEvaluate/getEvaluateData?id=' + objParam.id,
        type: 'GET',
        dataType: "JSON",
        success: function (result) {
            console.error(result)
            if (result.res === 1) {
                setFormData(result.obj);
            }
        }
    });

    // 表单赋值
    function setFormData(obj) {
        let mark = obj.mark;
        let markArr = mark.split('@@');
        for (let i = 0; i < markArr.length; i++) {
            if(i === 0){
                $('#name').val(markArr[i]);
            }else{
                $('#name' + (i + 1)).val(markArr[i]);
            }

        }
        $('input[name="level"][value="' + obj.level + '"]').prop("checked",true);
        $('#level' + obj.level).html("✔");
    }
}

function saveData2() {
    $('#formSubmit').trigger('click')
}


function print() {
    Print('#main-box', {
        onStart: function () {
            console.log('onStart', new Date())
        },
        onEnd: function () {
            console.log('onEnd', new Date())
        }
    })
}

// 提交
function submitApply(data) {
    let obj = {};
    obj.level = $('input[name="level"]:checked').val();
    obj.operType = 1;
    obj.id = objParam.id;
    obj.mark = data.field.name + "@@" + data.field.name2 + "@@" + data.field.name3 + "@@" + data.field.name4 + "@@" + data.field.name5;
    let loadingMsg = layer.msg('正在提交保存,请稍等...', {icon: 16, shade: 0.01, time: '0'});
    $.ajax({
        url: `${ctxPath}` + '/backstage/teamEvaluate/evaluateData',
        type: 'POST',
        data: JSON.stringify(obj),
        dataType: 'json',
        contentType: "application/json",
        beforeSend: function () {
            $('.save').addClass("layui-btn-disabled").attr("disabled", true);
            $('.cancel').addClass("layui-btn-disabled").attr("disabled", true);
        },
        success: function (result) {
            layer.close(loadingMsg); // 关闭提示层
            $('.save').removeClass("layui-btn-disabled").attr("disabled", false);
            $('.cancel').removeClass("layui-btn-disabled").attr("disabled", false);
            console.error(result)
            if (result.res === 1) {
                parent.layer.msg(result.resMsg, {icon: 1});
                closePage(1);
            } else {
                layer.msg(result.resMsg, {icon: 2});
            }
        },
        error: function (result) {
            layer.close(loadingMsg); // 关闭提示层
            layer.msg('服务异常，请稍后重试', {icon: 16, scrollbar: false, time: 2000});
            $('.save').removeClass("layui-btn-disabled").attr("disabled", false);
            $('.cancel').removeClass("layui-btn-disabled").attr("disabled", false);
        }
    });
}

// 关闭页面
function closePage(type) {
    let index = parent.layer.getFrameIndex(window.name); // 先得到当前 iframe层的索引
    if (type === 1) {
        if (objParam.operType === 'back') {
            window.parent.reloadTreeTable();
        }else{
            window.parent.search(1);
        }
    }
    parent.layer.close(index); // 再执行关闭
}