/**
 * 工单受理
 */
var winId	= $.getUrlParam("winId");
var jobid	= $.getUrlParam("jobid");
var type	= $.getUrlParam("type");
var faultdevicetype = $.getUrlParam("faultdevicetype");
var dqyhzh,dbyhzh,dbhangupcause,dbhanguptime;
var record = {};
$(function(){
	console.log("----------");
	initOrder();
	
	
	$('#faultpictureurl').click(function(){
		$('#pic').show();
	})
	$('img').click(function(){
		$('#pic').hide();
	})
	
	
	//监听是否挂起
	$('#ishangup').combobox({  
        onChange: function (newValue, oldValue) {
    		if(newValue=="1"){
    			$("#isreject").combobox("setValue","0");
    			$("#hangupTR").show();
    			$("#unhangupTR").hide();
    			$("#rejectTR").hide();
    			$("#feedbackTR").hide();
    			$("#infoTR").hide();
    			
    			$("#hanguppeople").text(dqyhzh);
    			$("#hanguptime").datetimebox("setValue",getCurrentTime());
    			$("#hangupcause").val("");
    		}else if (newValue=="0"){
    			if($("#isreject").combobox("getValue")=="1"){
    				$("#hangupTR").hide();
    				$("#unhangupTR").hide();
    				$("#rejectTR").show();
    				$("#feedbackTR").hide();
    				$("#infoTR").hide();
    			}else{
    				$("#hangupTR").hide();
    				$("#unhangupTR").hide();
    				$("#rejectTR").hide();
    				$("#feedbackTR").show();
    				$("#infoTR").show();
    			}
    		} else {
    			$("#isreject").combobox("setValue","0");
    			$("#unhangupTR").show();
    			$("#hangupTR").show();
    			$("#rejectTR").hide();
    			$("#feedbackTR").show();
    			$("#infoTR").show();
    			
    			$("#hanguppeople").text(dbyhzh);
    			$("#hanguptime").datetimebox("setValue",dbhanguptime);
    			$("#hanguptime").datetimebox({ disabled: true });
    			$("#hangupcause").val(dbhangupcause);
    			$('#hangupcause').attr('disabled',true);
    		}
        		
        }
	});
	//监听是否驳回
	$('#isreject').combobox({  
		onChange: function (newValue, oldValue) {
			if(newValue=="1"){
				$("#ishangup").combobox("setValue","0");
				$("#hangupTR").hide();
    			$("#rejectTR").show();
    			$("#feedbackTR").hide();
    			$("#infoTR").hide();
			}else{
				if($("#ishangup").combobox("getValue")=="1"){
    				$("#hangupTR").show();
    				$("#rejectTR").hide();
    				$("#feedbackTR").hide();
    				$("#infoTR").hide();
    			}else{
    				$("#hangupTR").hide();
    				$("#rejectTR").hide();
    				$("#feedbackTR").show();
    				$("#infoTR").show();
    				
    			}
			}
			
		}
	});
	//初始化
	if("1"==type){
		$("#ishangup").combobox("setValue","0");
	}else {
		$("#ishangup").combobox("setValue","2");
	}
	$("#isreject").combobox("setValue","0");
	//监听接收单位
	$('#feedbackcompany').combobox({  
		onChange: function (newValue, oldValue) {
			renderfeedbackPeopleSelect(newValue);
		}
	});
	//监听接收人
	$('#feedbackpeople').combobox({  
		onChange: function (newValue, oldValue) {
			renderfeedbackPeoplePhone(newValue);
		}
	});
	parent.parent.$(".l-btn.l-btn-small").on("click",function(){
		if($(this).find(".l-btn-text").text()=="确定"){
			getData();
		}
	})
});
//获取工单数据
function getData(){
	var param = {};
	//告警设备类型
	param.faultdevicetype = record.faultdevicetype;
	param.XGTJfaultdeviceid = record.faultdeviceid;
	param.XGTJchannelnum = record.channelnum;
	param.XGTJvideonum = record.faultdeviceid;
	//告警参数
	param.XGTJalarmeventid = record.alarmeventid;
	param.processstatus = 902;//待反馈
	param.jobprocessstate = 902;//待反馈
	param.processtime = $('#jobaccepttime').datetimebox("getValue");
	param.processdescription = "";
	
	//工单参数
	param.XGTJjobid = jobid;
	var ishangup = $("#ishangup").combobox("getValue");//是否挂起
	var isreject = $("#isreject").combobox("getValue");//是否驳回
	var jobaccepttime = $('#jobaccepttime').datetimebox("getValue");//工单受理时间
	param.isreject = isreject;
	param.ishangup = ishangup;
	param.jobaccepttime = jobaccepttime;
	if(ishangup=="1"){//挂起
		var hangupcause = $("#hangupcause").val();
		var hanguptime = $("#hanguptime").datetimebox("getValue");
		param.hanguptime = hanguptime;
		param.hangupcause = hangupcause;
		param.processstatus = 904;
		param.jobprocessstate = 904;
		param.processdescription = hangupcause;
		operationTable(param);
	} else if (ishangup == "2") { //取消挂起
//		param.processstatus = 905;
//		param.jobprocessstate = 905;
		var unhanguptime = $("#unhanguptime").datetimebox("getValue");
		param.unhanguptime = unhanguptime;
		var feedbackcompany = $("#feedbackcompany").combobox("getValue");//受理人
		var feedbackpeople = $("#feedbackpeople").combobox("getText");//受理人
		var feedbackpeoplebh = $("#feedbackpeople").combobox("getValue");//受理人
		var feedbackphone = $("#feedbackphone").text();//手机号码
		var faultacceptinfo = $("#faultacceptinfo").val();//受理描述
		var requirefinishtime = $("#requirefinishtime").datetimebox("getValue");//要求完成时间
		param.feedbackcompany = feedbackcompany;
		param.feedbackpeople = feedbackpeople;
		param.feedbackphone = feedbackphone;
		param.extend1 = feedbackpeople;
		param.faultacceptinfo = faultacceptinfo;
		param.processdescription = faultacceptinfo;
		param.requirefinishtime = requirefinishtime;
		if(feedbackcompany==undefined || feedbackcompany=="" ){
			$.showMsg("接收单位不能为空");
			return;
		}
		if(feedbackpeoplebh==undefined || feedbackpeoplebh=="" ){
			$.showMsg("接收人不能为空");
			return ;
		}
		if(feedbackphone==undefined || feedbackphone==""){
			$.showMsg("接收人手机号码不能为空，请更新接收人联系电话");
			return ;
		}
		var reg = /^[0-9]{11}$/;
		if (!reg.test(feedbackphone)) {
			$.showMsg('接收人联系电话错误');
			return;
		}
		if(requirefinishtime==undefined || requirefinishtime==""){
			$.showMsg("要求完成时间不能为空");
			return ;
		}
		operationTable(param);
	} else{
		if(isreject=="1"){//驳回
			var rejectcause = $("#rejectcause").val();
			if(rejectcause == undefined || rejectcause =="" || rejectcause == null){
				$.showMsg("驳回原因不能为空");
				return ;
			}
			param.rejectcause = rejectcause;
			param.processstatus = 903;
			param.jobprocessstate = 903;
			param.processdescription = hangupcause;
			operationTable(param);
		}else{//反馈
			var feedbackcompany = $("#feedbackcompany").combobox("getValue");//受理人
			var feedbackpeople = $("#feedbackpeople").combobox("getText");//受理人
			var feedbackpeoplebh = $("#feedbackpeople").combobox("getValue");//受理人
			var feedbackphone = $("#feedbackphone").text();//手机号码
			var faultacceptinfo = $("#faultacceptinfo").val();//受理描述
			var requirefinishtime = $("#requirefinishtime").datetimebox("getValue");//要求完成时间
			param.feedbackcompany = feedbackcompany;
			param.feedbackpeople = feedbackpeople;
			param.feedbackphone = feedbackphone;
			param.faultacceptinfo = faultacceptinfo;
			param.processdescription = faultacceptinfo;
			param.requirefinishtime = requirefinishtime;
			param.extend1 = feedbackpeople;
			if(feedbackcompany==undefined || feedbackcompany=="" ){
				$.showMsg("接收单位不能为空");
				return;
			}
			if(feedbackpeoplebh==undefined || feedbackpeoplebh=="" ){
				$.showMsg("接收人不能为空");
				return ;
			}
			if(feedbackphone==undefined || feedbackphone==""){
				$.showMsg("请到平台用户管理修改接收人联系电话");
				return ;
			}
			var reg = /^[0-9]{11}$/;
			if (!reg.test(feedbackphone)) {
				$.showMsg('请修改为有效11位接收人电话');
				return;
			}
			if(requirefinishtime==undefined || requirefinishtime==""){
				$.showMsg("要求完成时间不能为空");
				return ;
			}
			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");
//				parent.$("#"+winId).panel("options").data.flag = true;
			}
		})
	}
}
//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].yhzh});
					}
					$("#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==undefined || phone=="" || phone == null || phone == "null"){
			$("#feedbackphone").text("");
		}else{
			$("#feedbackphone").text(yhbh.split("-")[1])
		}
	}else{
		$("#feedbackphone").text("");
	}
}

//初始化工单
function initOrder(){
	$("#hangupTR").hide();
	$("#rejectTR").hide();
	$.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){ }
	});
	//初始化接收单位、接收人
	$.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});
				}
				$("#feedbackcompany").combobox("loadData",departmentArr);
				$("#feedbackpeople").combobox("loadData",peopleArr);
				$("#feedbackphone").text("");
			}else{
				$.showMsg("未关联到可审批单位！")
			}
		},
		error : function(res){ }
	});
	//初始化挂起驳回
	// 根据当前状态判断是否显示取消挂起
	var ishangupArr = [{"id":"0","text":"否","selected":true},{"id":"1","text":"是"}];
	if ("2" == type) {
		var ishangupArr = [{"id":"2","text":"取消挂起","selected":true}];
	}
	var isrejectArr = [{"id":"0","text":"否","selected":true},{"id":"1","text":"是"}];
	$("#ishangup").combobox("loadData",ishangupArr);
	$("#isreject").combobox("loadData",isrejectArr);
}
function renderOrder(res){
	$("#jobnum").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));
	$("#jobreportpeople").text(transEmptyObj(res.jobreportpeople));
	if (res.jobreportphone == null || res.jobreportphone == "null") {
		$("#jobreportphone").text("-");
	} else {
		$("#jobreportphone").text(transEmptyObj(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);
		}
	
	$("#unhanguppeople").text(transEmptyObj(res.yhzh));
	$("#unhangupcompany").text(transEmptyObj(res.bmmc));

	$("#jobaccepttime").datetimebox("setValue",getCurrentTime());
	$("#requirefinishtime").datetimebox("setValue",getCurrentTime());
	$("#unhanguptime").datetimebox("setValue",getCurrentTime());
	
	dqyhzh = transEmptyObj(res.yhzh);
	
	dbyhzh = transEmptyObj(res.hanguppeoplename);
	dbhangupcause = transEmptyObj(res.hangupcause);
	dbhanguptime = res.hanguptime;
	if(type == "2"){
		$("#hanguppeople").text(dbyhzh);
		$("#hanguptime").datetimebox("setValue",dbhanguptime);
		$("#hangupcause").val(dbhangupcause);
	}
//	$("#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;
}