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

var wid = helper.request.queryString("oid");
$(document).ready(function() {
	if(wid != null && wid != undefined && wid != "") {
		getWorkOrderInfo(); //获取工单信息
		GetHJJG(); //获取呼叫结果
	}
	$('#btn_submit').on('click', submitPages);
	//点击电话号码外呼
	$("#CallOut").click(function(event) {
		if($("#visit_tel").text()) {
			calloutopt($("#visit_tel").text());
		}
	});
});

//外呼
function calloutopt(telphone) {
	event.stopPropagation();
	if(telphone) {
		$.ajax({
			type: "get",
			url: huayi.config.callcenter_url + "CallOutOpt/GetCallOutprefix",
			async: true,
			dataType: 'json',
			data: {
				token: $.cookie("token"),
				phone: telphone
			},
			success: function(result) {
				if(result.state.toLowerCase() == "success") {
					var guid = uuid();
					$("#TelCode").val(guid);
					top.obj.Type = "MakeCall";
					top.obj.DestinationNumber = result.data.phone;
					top.obj.Header = result.data.fix;
					top.obj.TaskType = "1"; //0:拨号外呼；1:问卷外呼；
					top.obj.TaskPhoneID = $("#F_Id").val(); //回访的电话工单ID
					top.Send();
				}

			}
		});
	}
}
$('input[name="man"]').change(function () {
	if ($('input[name="man"]:checked').val()=="不满意") {
		$("#qta").show();
	}else{
		$("#qta").hide();
	}
})
//提交问卷
function submitPages() {
	var noSati;
	if ($('input[name="man"]:checked').val()) {
		if ($('input[name="man"]:checked').val()=="不满意") {
			noSati=$('input[name="fwu"]:checked').val()
		}
	}else{
		noSati=""
	}
	if(!$('input[name="check"]:checked').val()) {
		layer.confirm('呼叫结果必须选择！', {
			icon: 2,
			btn: ['确定']
		});
		return;
	}
	//请求
	$.ajax({
		type: "POST",
		url: huayi.config.callcenter_url + "WorkOrderNew/ReturnvisitOrder",
		dataType: 'json',
		data: {
			cont:$('input[name="man"]:checked').val(), //回访结果
			reason:noSati,  //不满意原因
			orderid: wid, //	是	string	工单来源
			reason: $('#chusur').val(), //是否完结
			call: $('input[name="check"]:checked').val(), //呼叫结果ID
			isover: $('input[name="wan"]:checked').val(), //是否完结
			token: $.cookie("token")
		},
		async: true,
		success: function(data) {
			if(data.state === "success") {
				debugger
				var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
				parent.layer.close(index); //再执行关闭
				parent.$('#workorderlist').bootstrapTable('refresh');
				parent.layer.msg('提交成功');
			}
		},
	});

}

//获取工单信息
function getWorkOrderInfo() {
	$.getJSON(huayi.config.callcenter_url + 'WorkOrderNew/GetDetails', {
		id: wid,
		"token": $.cookie("token")
	}, function(result) {
		if(result.state.toLowerCase() == "success") {
			if(result.rows && result.rows.length > 0) {
				var data = result.rows[0];
				$("#F_WorkOrderCode").text(data.F_WorkOrderCode), //工单编号
				$("#F_Type").text(data.F_TypeName); //工单类型
				$("#F_State").text(data.F_StateName); //工单状态
				$("#F_Source").text(data.F_SourceName); //工单来源
				$('#F_CusName').text(data.F_CusName); //客户姓名		
				$('#CallOut').text(data.F_CusPhone); //客户电话
				$('#F_BranchName').text(data.F_CompanyName); //分公司
				$('#F_Salesman').text(data.F_Salesman);//	业务员
				$('#F_SalesPhone').text(data.F_SalesPhone);//	业务员电话
				$('#F_Model').text(data.F_Model); //型号
				$("#F_DealBy").text(data.F_DealBy); //处理人
				$("#F_DealContent").text(data.F_DealContent); //处理内容
				// $('#visit_state').html(formatterState(data.State)); //工单状态
				// $("#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 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>';
}

//获取呼叫结果
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 id="listR_' + i + '" type="radio" name="check" value="' + Count[i].F_Name + '" />' +
					'<label for="listR_' + i + '">' + Count[i].F_Name + '</label>' +
					'</li>';
				$(listItem).appendTo($("#Tgetruset"));
			}
		}
	});
}

