var id = helper.request.queryString("id");
var valueid = helper.request.queryString("valueid");
$(document).ready(function() {
	laydate.skin('blue');
    laydate({
        elem: '#times',
        event: 'focus',
        istime: true,
        format: 'YYYY-MM-DD hh:mm:ss'
    });
	Userarr($("#user"))
	Zhibiao($("#zhibiaoone"))
	$(".addBtn").click(function() {
		saveCon();
	});

	//获取知识库详情
	if(id) {
		$.ajax({
			type: "get",
			url: huayi.config.callcenter_url + "Quality/GetExamineBase",
			async: true,
			dataType: "json",
			data: {
				token: $.cookie("token"),
				id: id,
			},
			success: function(result) {
				if(result.state.toLowerCase() == "success") {
					var data = result.data;
					ZhibiaoResult($("#zhibiaotwo"), data.F_ValueID)
                    $("#user option:selected").val(data.F_Salesman)
                    $("#user option:selected").text(data.Salesman)                 
                    $("#zhibiaoone option:selected").val(data.F_ValueID)
                    $("#zhibiaoone option:selected").text(data.ValueName)                 
                    $("#zhibiaotwo option:selected").val(data.F_Index)
                    $("#zhibiaotwo option:selected").text(data.IndexName)                  
					$("#score").val(data.F_Score)
					$("#times").val(data.F_Date)			
					$("#remark").val(data.F_Remarks)				
				}
			},
		});
	}
});

function Userarr(obj) {
	obj.empty();
	obj.append('<option selected="selected" value="">请选择话务员</option>');
	$.ajax({
		type: "get",
		url: huayi.config.callcenter_url + "UserAccount/GetSeatList",
		dataType: "json",
		async: true,
		data: {
			"token": $.cookie("token")
		},
		success: function(data) {
			if(data.state.toLowerCase() == "success") {
				var content = data.data;
				$(content).each(function(i, n) {
					$("<option value='" + n.F_UserCode + "'>" + n.F_UserName + "</option>")
						.appendTo(obj);
				})
			}
		}
	});
}

function Zhibiao(obj) {
	console.log(obj)
	obj.empty();
	obj.append('<option selected="selected" value="">请选择指标分类</option>');
	$.ajax({
		type: "get",
		url: huayi.config.callcenter_url + "Dictionary/GetDicValueListById",
		dataType: "json",
		async: true,
		data: {
			"token": $.cookie("token"),
			id: 4107
		},
		success: function(data) {
			if(data.state.toLowerCase() == "success") {
				var content = data.data;
				$(content).each(function(i, n) {
					$("<option value='" + n.F_ValueId + "'>" + n.F_Value + "</option>")
						.appendTo(obj);
				})
			}
		}
	});
}
$("#zhibiaoone").change(function() {
	$("#score").val('')
	ZhibiaoResult($("#zhibiaotwo"), $("#zhibiaoone").val())
})
var twoarr = []
function ZhibiaoResult(obj, val) {
	obj.empty();
	obj.append('<option selected="selected" value="">请选择指标</option>');
	$.ajax({
		type: "get",
		url: huayi.config.callcenter_url + "Quality/GetIndexList",
		dataType: "json",
		async: true,
		data: {
			"token": $.cookie("token"),
			valueid: val
		},
		success: function(data) {
			var content = data.rows;
			twoarr = data.rows;
			$(content).each(function(i, n) {
				$("<option value='" + n.F_ID + "'>" + n.F_Name + "</option>")
					.appendTo(obj);
			})

		}
	});
}
$("#zhibiaotwo").change(function() {	
	twoarr.forEach(function(v,n){
		if(v.F_ID == $("#zhibiaotwo").val()){
			$("#score").val(v.F_Score)
		}
	})
})
//保存
function saveCon() {
		if(!$("#user").val()){
			layer.msg("请选择话务员");
			return;
		}
		if(!$("#zhibiaoone").val() || !$("#zhibiaotwo").val()){
			layer.msg("请选择指标");
			return;
		}
		if(!$("#score").val()){
			layer.msg("请输入分值");
			return;
		}
		if(!$("#times").val()){
			layer.msg("请选择考核时间");
			return;
		}
	$.ajax({
		type: "post",
		url: huayi.config.callcenter_url + "Quality/OperationExamine",
		async: true,
		dataType: "json",
		data: {
			token: $.cookie("token"),
			F_ID: id || "0",
			F_Salesman:$("#user").val(),
			F_ValueID:$("#zhibiaoone").val(),
			F_Index:$("#zhibiaotwo").val(),
			F_Score:$("#score").val(),
			F_Date:$("#times").val(),
			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);
});