/**
 * 回访 问卷结果信息
 * */

var wid = helper.request.queryString("oid");
$(document).ready(function() {
	if(wid != null && wid != undefined && wid != "") {
		GetHJJG(); //获取呼叫结果
		getWorkOrderInfo(); //获取工单信息
		getQuestionPager(); //获取问卷结果
	}
});

//获取问卷
function getQuestionPager() {
	$(".tbody").empty();
	$.ajax({
		type: "get",
		url: huayi.config.callcenter_url + "QuestionAnswer/GetQuestionPagerResult",
		async: true,
		dataType: "json",
		data: {
			workorderid: wid,
			token: $.cookie("token"),
		},
		success: function(data) {
			if(data.state.toLowerCase() == 'success') {
				var questions = data.data;
				var html = '';
				if(questions && questions.length > 0) {
					for(var i = 0; i < questions.length; i++) {
						html += '<li>';
						html += '<div class="Back">';
						html += '<h3 class="size-14 QuesTitle">' + questions[i].title + '</h3>';
						html += '</div>';
						//选项
						var quesitems = questions[i].quesitems;
						if(quesitems && quesitems.length > 0) { //单选
							html += '<div class="radio clearfix">';
							html += '<ul class="Ul">'
							for(var j = 0; j < quesitems.length; j++) {
								var chk = '';
								if(quesitems[j].ischeck === 1) {
									chk = 'checked="checked"';
								}
								html += '<li>'
								html += '<label style="font-weight: normal;">'
								html += '' + (j + 1) + '.';
								html += '<input disabled type="radio" ' + chk + ' class="quesitems" value="' + quesitems[j].id + '" name="radio_' + questions[i].id + '"><span>' + quesitems[j].title + ' （' + quesitems[j].score + '分）</span></label>'
								html += '</li>'
							}
							html += '</ul>'
							html += '</div>'
							html += '</li>'
						}
					}
					$(html).appendTo("#questionBody");
				}
			}
		}
	});
}

//获取工单信息和呼叫结果
function getWorkOrderInfo() {
	$.getJSON(huayi.config.callcenter_url + 'WorkOrder/GetWorkOrder', {
		"orderid": wid,
		"token": $.cookie("token")
	}, function(result) {
		if(result.state.toLowerCase() == "success") {
			if(result.data.data && result.data.data.length > 0) {
				var data = result.data.data[0];
				$('input[name="check"][value="' + data.IsReturn + '"]').prop("checked", true); //呼叫结果
				$("#visit_name").text(data.Customer); //客户姓名
				$("#visit_tel").text(data.CustomerTel); //客户电话
				$('#visit_regin').text(data.areaname); //区域名称		
				$('#visit_pro').text(data.proname); //项目名称
				$('#visit_floor').text(data.buildingname); //期号名称
				$('#visit_buildingnum').text(data.BuildingNum);//	否	string	 楼号
				$('#visit_unitnum').text(data.UnitNum);//	否	string	单元
				$('#visit_room').text(data.Address); //房间号
				$("#visit_iseffective").html(formatterIsEffective(data.IsEffective)); //工单有效性
				$('#visit_otype').text(data.GDLXName); //工单类型
				$('#visit_state').html(formatterState(data.State)); //工单状态
				$("#visit_source").text(data.GDLYName); //工单来源
				$("#visit_ownerrelations").text(data.OwnerrelationsName); //房主关系
				$("#visit_customerresponse").text(data.OustomerResponseName); //客户反应

				if(data.TypeClass === 107) { //咨询
					$('.visit_complain').hide();
				} else if(data.TypeClass === 108) { //投诉
					$('.visit_complain').show();
					data.ComplaintDepartment = data.ComplaintDepartment === null ? '' : data.ComplaintDepartment;
					data.CustomerTypeName = data.CustomerTypeName === null ? '' : data.CustomerTypeName;
					$("#visit_fkdep").text(data.ComplaintDepartment + data.CustomerTypeName); //投诉类型
					if(data.ResponDept != "-1") {
						data.ResponDept = data.ResponDept === 1 ? '是' : '否';
					} else {
						data.ResponDept = '保密';
					}
					$("#visit_isquality").text(data.ResponDept); //是否过质保
					data.AuditUserNameRel = data.AuditUserNameRel === "" ? '——' : data.AuditUserNameRel;
					$("#visit_responseUser").text(data.AuditUserNameRel); //接收人
				}
				$("#visit_code").text(data.CreateUserNameRel); //创建人
				$("#visit_detail").html(decodeURIComponent(data.Detail)); //反馈内容
				$("#visit_clcontent").html(decodeURIComponent(data.clcontent)); //处理内容
				if(data.IsEffective === 0) {
					$('.visit_notes').hide();
				} else {
					$('.visit_notes').show();
					$("#visit_note").text(data.Province); //无效原因
				}
			}
		}
	});
}

//获取呼叫结果
function GetHJJG() {
	$.ajax({
		type: "get",
		url: huayi.config.callcenter_url + "Dictionary/GetDicValueListByFlag",
		dataType: 'json',
		data: {
			flag: 'HJJG',
			token: $.cookie("token")
		},
		async: true,
		success: function(data) {
			var Count = data.data;
			for(var i = 0; i < Count.length; i++) {
				var listItem = '<li>' +
					'<input disabled type="radio" name="check" value="' + Count[i].F_DictionaryValueId + '" />' +
					'<label>' + Count[i].F_Name + '</label>' +
					'</li>';
				$(listItem).appendTo($("#Tgetruset"));
			}
		}
	});
}

//格式化 工单状态
function formatterState(val) {
	var stateStr = '-';
	var stateClass = '';
	switch(val) { //工单状态0待指派，1待处理，2已处理
		case 0:
			stateStr = '待指派';
			stateClass = 'audit_wait';
			break;
		case 1:
			stateStr = '待处理';
			stateClass = 'audit_revoke';
			break;
		case 2:
			stateStr = '已处理';
			stateClass = 'audit_done';
			break;
		default:
			break;
	}

	return '<span class="' + stateClass + '">' + stateStr + '</span>';
}

//格式化 工单有效性
function formatterIsEffective(val) {
	var stateStr = '-';
	var stateClass = '';
	switch(val) { //是否有效工单项(0有效，1无效)
		case 0:
			stateStr = '有效';
			stateClass = 'audit_done';
			break;
		case 1:
			stateStr = '无效';
			stateClass = 'audit_revoke';
			break;
		default:
			break;
	}
	return '<span class="' + stateClass + '">' + stateStr + '</span>';
}