/**
 * 工单受理
 */
var winId	= $.getUrlParam("winId");
var jobid	= $.getUrlParam("jobid");
var record = {};
$(function(){
	initOrder();
	
	$('#faultpictureurl').click(function(){
		$('#pic').show();
	})
	$('img').click(function(){
		$('#pic').hide();
	})
	
	//监听是否驳回
	$('#isreject').combobox({  
		onChange: function (newValue, oldValue) {
			if(newValue=="1"){
				$("#ishangup").combobox("setValue","0");
    			$("[name='confirmTd']").hide();
    			$("[name='rejectTd']").show();
    			$("[name='rejectTd1']").show();
			}else{
				$("[name='rejectTd']").hide();
				$("[name='confirmTd']").show();
				$("[name='rejectTd1']").hide();
			}
			
		}
	});
	// //监听接收单位
	// $('#feedbackcompany').combobox({
	// 	onChange: function (newValue, oldValue) {
	// 		renderfeedbackPeopleSelect(newValue);
	// 	}
	// });
	// //监听接收人
	// $('#feedbackpeople').combobox({
	// 	onChange: function (newValue, oldValue) {
	// 		renderfeedbackPeoplePhone(newValue);
	// 	}
	// });

	//监听接收单位
	$('#receivercompany').combobox({
		onChange: function (newValue, oldValue) {
			renderreceiverPeopleSelect(newValue);
		}
	});
	//监听接收人
	$('#receiverpeople').combobox({
		onChange: function (newValue, oldValue) {
			renderreceiverPeoplePhone(newValue);
		}
	});

	//初始化
	$("#isreject").combobox("setValue","0");
	parent.parent.$(".l-btn-text").on("click",function(){
		if($(this).text()=="确定"){
			getData();
		}
	})
});
//获取工单数据
function getData(){
	var param = {};
	param.jobreporttime = record.jobreporttime;
	//告警设备类型
	param.faultdevicetype = record.faultdevicetype;
	param.XGTJfaultdeviceid = record.faultdeviceid;
	param.XGTJchannelnum = record.channelnum;
	param.XGTJvideonum = record.faultdeviceid;
	//告警参数
	param.XGTJalarmeventid = record.alarmeventid;
	param.processstatus = 911;//已反馈已确认
	param.processdescription = "";
	//工单参数
	param.XGTJjobid = jobid;
	param.jobid = jobid;
	param.jobprocessstate = 911;//待反馈
	var isreject = $("#isreject").combobox("getValue");//是否驳回
	param.isreject = isreject;
	if(isreject=="1"){//驳回
		var rejectcause = $("#rejectcause").val();
		if(rejectcause==undefined || rejectcause=="" || rejectcause == null){
			$.showMsg("驳回原因不可为空");
			return;
		}
		param.rejectcause = rejectcause;
		param.processstatus = 913;   //维修确认不通过
		param.jobprocessstate = 913; //维修确认不通过
		param.processdescription = rejectcause;
		param.confirmdescription = rejectcause;
		param.extend1 = record.feedbackpeople;
		operationTable(param);
	}else{//确认
		var confirmtime =$('#confirmtime').datetimebox("getValue");//确认时间
		var confirmdescription = $("#confirmdescription").val();//受理人
		var confirmfaulttype = $("#confirmfaulttype").combobox("getValue");//受理人

		param.confirmtime = confirmtime;
		param.processtime = confirmtime;
		param.jobreporttime = record.jobreporttime;
		param.confirmdescription = confirmdescription;
		param.confirmfaulttype = confirmfaulttype;
		param.receivercompany  =$('#receivercompany').combobox('getValue');
		if(param.receivercompany==undefined || param.receivercompany=="" ){
			$.showMsg("验收人单位不能为空");
			return;
		}
		param.receiverpeople=$('#receiverpeople').combobox('getText');
		if(param.receiverpeople==undefined || param.receiverpeople=="" ){
			$.showMsg("工单验收人不能为空");
			return;
		}
		param.receiverphone=$('#receiverphone').text();
		param.processdescription = confirmdescription;
		if(param.processdescription==undefined || param.processdescription=="" ){
			$.showMsg("确认信息不能为空");
			return;
		}
		param.extend1 = param.receiverpeople;
		operationTable(param);
	}
}
//入库
function operationTable(obj){
	if(obj){
		$.ajax({
			url : "ywrepairjobinfoAction!updateWorkOrderInfo.do",
			type : "POST",
			dataType : "JSON",
			data : obj,
			success : function(res){
				$.showMsg("操作成功");
				top.$("#"+winId).dialog("close");
			}
		})
	}
}
//tr联动展示
function displayTR(){
	if(newValue=="1"){
		$("#isreject").combobox("setValue","0");
		$("#hangupTR").css("display","block");
		$("#rejectTR").css("display","none");
		$("#feedbackTR").css("display","none");
		$("#infoTR").css("display","none");
	}else{
		if($("#isreject").combobox("getValue")=="1"){
			$("#hangupTR").css("display","none");
			$("#rejectTR").css("display","block");
			$("#feedbackTR").css("display","none");
			$("#infoTR").css("display","none");
		}else{
			
		}
	}
}
//渲染接收人
function renderfeedbackPeopleSelect(bmbh){
	if(bmbh){
		$.ajax({
			url : "ywrepairjobinfoAction!getReportUser.do",
			type : "POST",
			dataType : "JSON",
			data : {bmbh:bmbh},
			success : function(res){
				if(res && res.length>0){
					var peopleArr = [{"id":"","text":"请选择接收人","selected":true}];
					for(var i in res){
						peopleArr.push( {"id":res[i].yhbh+"-"+res[i].sjhm,"text":res[i].yhmc});
					}
					$("#feedbackpeople").combobox({disabled:false});
					$("#feedbackpeople").combobox("loadData",peopleArr);
				}else{
					$.showMsg("未关联到可审批单位！")
				}
			},
			error : function(res){ }
		});
	}else{
		var peopleArr = [{"id":"","text":"请选择接收人","selected":true}];
		$("#feedbackpeople").combobox({disabled:true});
		$("#feedbackpeople").combobox("loadData",peopleArr);
		$("#feedbackphone").text("");
	}
}
//渲染接收人短信
function renderfeedbackPeoplePhone(yhbh){
	if(yhbh){
		var phone = yhbh.split("-")[1];
		if(phone){
			$("#feedbackphone").text(yhbh.split("-")[1])
		}else{
			$("#feedbackphone").text("");
		}
	}else{
		$("#feedbackphone").text("");
	}
}
//初始化工单
function initOrder(){
	$.ajax({
		url : "ywrepairjobinfoAction!get_Ywrepairjobinfo.do",
		type : "POST",
		dataType : "JSON",
		data : {jobid:jobid},
		success : function(res){
			if(res){
				record = res;
				renderOrder(res);//渲染工单
			}else{
				$.showMsg("未关联到工单！")
			}
		},
		error : function(res){ }
	});
	
	var isrejectArr = [{"id":"0","text":"否","selected":true},{"id":"1","text":"是"}];
	$("#isreject").combobox("loadData",isrejectArr);
	
	var confirmfaulttypeArr = [
	                           {"id":"1","text":"监控告警","selected":true},
	                           {"id":"2","text":"服务器告警"},
	                           {"id":"3","text":"应用系统告警"},
	                           {"id":"4","text":"存储告警"}
	                           ];
	$("#confirmfaulttype").combobox("loadData",confirmfaulttypeArr);

	$.ajax({
		url : "ywrepairjobinfoAction!getReportBm.do",
		type : "POST",
		dataType : "JSON",
//		data : {jobid:jobid},
		success : function(res){
			if(res && res.length>0){
				var departmentArr = [{"id":"","text":"请选择接收单位","selected":true}];
				var peopleArr = [{"id":"","text":"请选择接收人","selected":true}];
				for(var i in res){
					departmentArr.push( {"id":res[i].bmbh,"text":res[i].bmmc});
				}
				$("#receivercompany").combobox("loadData",departmentArr);
				$("#receiverpeople").combobox("loadData",peopleArr);
				$("#receiverphone").text("");
			}else{
				$.showMsg("未关联到可审批单位！")
			}
		},
		error : function(res){ }
	});
	
}


//渲染接收人
function renderreceiverPeopleSelect(bmbh){
	if(bmbh){
		$.ajax({
			url : "ywrepairjobinfoAction!getReportUser.do",
			type : "POST",
			dataType : "JSON",
			data : {bmbh:bmbh},
			success : function(res){
				if(res && res.length>0){
					var peopleArr = [{"id":"","text":"请选择验收人","selected":true}];
					for(var i in res){
						peopleArr.push( {"id":res[i].yhbh+"-"+res[i].sjhm,"text":res[i].yhmc});
					}
					$("#receiverpeople").combobox({disabled:false});
					$("#receiverpeople").combobox("loadData",peopleArr);
				}else{
					$.showMsg("未关联到可审批单位！")
				}
			},
			error : function(res){ }
		});
	}else{
		var peopleArr = [{"id":"","text":"请选择验收人","selected":true}];
		$("#receiverpeople").combobox({disabled:true});
		$("#receiverpeople").combobox("loadData",peopleArr);
		$("#receiverphone").text("");
	}
}
//渲染接收人短信
function renderreceiverPeoplePhone(yhbh){
	if(yhbh){
		var phone = yhbh.split("-")[1];
		if(phone){
			$("#receiverphone").text(yhbh.split("-")[1])
		}else{
			$("#receiverphone").text("");
		}
	}else{
		$("#receiverphone").text("");
	}
}
function renderOrder(res){
//	console.log("工单反馈：",res);
	$("#jobid").text(transEmptyObj(res.jobnum));
	$("#alarmgradename").text(transEmptyObj(res.faultgradename));
	$("#faultdevicetypename").text(transEmptyObj(res.faultdevicetypename));
	$("#faultdevicename").text(transEmptyObj(res.faultdevicename));
	$("#faultdeviceid").text(transEmptyObj(res.faultdeviceid));
	$("#faultoccurtime").text(transEmptyObj(res.faultoccurtime));
	$("#faulttype").text(transEmptyObj(res.faulttypename));
	$("#jobreportcompanyname").text(transEmptyObj(res.jobreportcompanyname));
	$("#jobreportpeoplename").text(transEmptyObj(res.jobreportpeople));
	if (res.jobreportphone == null || res.jobreportphone == "null") {
		$('#jobreportphone').html("-");
	} else {
		$('#jobreportphone').html(res.jobreportphone);
	}
	$("#jobreporttime").text(transEmptyObj(res.jobreporttime));
	$("#faultdescription").text(transEmptyObj(res.faultdescription));
	$("#faultpictureurl").html(transEmptyObj(res.faultpictureurl));//todo
	var picUrl = res.faultpictureurl;
	if(picUrl != null){
		var proName = window.location.pathname.split("/")[1];
		var sysUrl = "http://"+window.location.host+"/"+proName;
	 	var picProxyGetUrl = sysUrl+"/sourceProxy?fileType=picUrlProxy&picUrl="+picUrl;
	 	$('#pic img').attr("src",picProxyGetUrl);
	}

	$("#jobacceptcompanyname").text(transEmptyObj(res.feedbackcompanyname));
	$("#jobacceptpeople").text(transEmptyObj(res.feedbackpeople));
	$("#jobacceptphone").text(transEmptyObj(res.feedbackphone));
	$("#jobaccepttime").text(transEmptyObj(res.jobaccepttime));
	$("#requirefinishtime").text(transEmptyObj(res.requirefinishtime));
	$("#faultacceptinfo").text(transEmptyObj(res.faultacceptinfo));
	$("#isdelay").text(transEmptyObj(res.isdelay)=="1"?"是":"否");
	$("#delaytime").text(transEmptyObj(res.delaytime));
	$("#delayfordescription").text(transEmptyObj(res.delayfordescription));
	$("#feedbackcompanyname").text(transEmptyObj(res.feedbackcompanyname));
	$("#feedbackpeople").text(transEmptyObj(res.feedbackpeople));
	$("#feedbackphone").text(transEmptyObj(res.feedbackphone));
	$("#feedbacktime").text(transEmptyObj(res.feedbacktime));
	$("#feedbackinfo").text(transEmptyObj(res.feedbackinfo));
	$("#confirmtime").datetimebox("setValue",getCurrentTime());
	
	
//	$("#xx7").combobox("setValue",records.xx7);
}
function transEmptyObj(data){
	var result = "";
	if(data){
		result = data;
	}else{
		result = "-";
	}
	return result;
}
function getCurrentTime(){
	var date = new Date();
	var year = date.getFullYear();
	var month = date.getMonth()+1;
	var day = date.getDate();
	var hours = date.getHours();
	var minute = date.getMinutes();   
	var second = date.getSeconds();  
	if(month<10){
		month = "0"+month;
	}
	if(day<10){
		day = "0"+day;
	}
	if(hours<10){
		hours = "0"+hours;
	}
	if(minute<10){
		minute = "0"+minute;
	}
	if(second<10){
		second = "0"+second;
	}
	return year + "-" + month + "-" + day + " " + hours + ":" + minute + ":" +second;
}