var id = helper.request.queryString("id");
var valueid = helper.request.queryString("valueid");
$(document).ready(function () {
//  getReactionCategory();
    $(".addBtn").click(function () {
        saveCon();
    });

    //获取知识库详情
    if (id) {
        $.ajax({
            type: "get",
            url: huayi.config.callcenter_url + "Quality/GetIndexBase",
            async: true,
            dataType: "json",
            data: {
                token: $.cookie("token"),
                id: id,
            },
            success: function (result) {
                if (result.state.toLowerCase() == "success") {
                    var data = result.data;
                    $("#zhiname").val(data.F_Name)
                    $('input[name="addtype"][value="' + data.F_Type + '"]').prop("checked",true)
//                  $("#score").val(data.F_Score)
                    
                    if(data.F_Score<0){
                    	$("#score").val(data.F_Score.toString().slice(1,data.F_Score.length))
                    }else{
                    	$("#score").val(data.F_Score)
                    }
                    $("#remark").val(data.F_Remarks);
                }
            },
        });
    }
});

//保存
function saveCon() {
	if(!$("#zhiname").val()){
		layer.msg("请输入指标名称");
		return;
	}
	if(!$('input[name="addtype"]:checked ').val()){
		layer.msg("请选择加减类型");
		return;
	}
	if(!$("#score").val()){
		layer.msg("请输入分值");
		return;
	}
	var score;
	if($('input[name="addtype"]:checked ').val() == '0'){
		score = $("#score").val()
	}
	if($('input[name="addtype"]:checked ').val() == '1'){
		score = "-"+$("#score").val()
	}
    $.ajax({
        type: "post",
        url: huayi.config.callcenter_url + "Quality/OperationIndex",
        async: true,
        dataType: "json",
        data: {
            token: $.cookie("token"),
            F_ID: id || "0",
            F_Name: $("#zhiname").val(),
            F_Type:$('input[name="addtype"]:checked ').val(),
            F_ValueID:valueid,
            F_Score: score,
            F_Remarks: $("#remark").val(), // 备注
        },
        success: function (data) {
            if (data.state.toLowerCase() == "success") {
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index); //再执行关闭
                parent.layer.msg(data.message); //再执行关闭
                parent.$("#tableList").bootstrapTable("refresh"); //再执行关闭
            }
        },
    });
}


//获取考评内容
function getReactionCategory(pid = 2513, type = 0) {
    $.ajax({
        type: "get",
        url: huayi.config.callcenter_url + "Dictionary/GetDicValueListById",
        async: false,
        dataType: "json",
        data: {
            token: $.cookie("token"),
            id: pid,
        },
        success: function (result) {
            if (result.state.toLowerCase() == "success") {
                var content = result.data;
                if (String(type) === "0") {
                    $("#evaluation").empty();
                    $("#evaluation").append("<option value=''>请选择</option>");
                    $(content).each(function (i, e) {
                        $("#evaluation").append(
                            "<option value='" +
                                e.F_ValueId +
                                "'>" +
                                e.F_Value +
                                "</option>"
                        );
                    });
                } else if (String(type) === "1") {
                    $("#bonusitems").empty();
                    $("#bonusitems").append("<option value=''>请选择</option>");
                    $(content).each(function (i, e) {
                        $("#bonusitems").append(
                            "<option value='" +
                                e.F_ValueId +
                                "'>" +
                                e.F_Value +
                                "</option>"
                        );
                    });
                }
            }
        },
    });
}

$("#evaluation").change(function () {
    var pid = $("#evaluation").val();
    var type = 1;
    getReactionCategory(pid, type);
});
