mui.init({})
var id = helper.request.queryString("id");
var state = helper.request.queryString("type");
var staff = helper.request.queryString("typeStaff");
var openid = helper.cookies.get("openid");
//var openid = "oXF5e1mWpX5DajW5_yjNLPKb8ThE";
var roleCode = helper.cookies.get("RoleCode");
var typeUser = helper.cookies.get("typeUser");
var backType=0,turn=1;
//var staff=1;
//var state=1;
if(!state){
	// 显示导航
	$('#mNavbar').hide();
	$('#mContent').css('marginTop', '0px');	
	$('#mContentList').css('marginTop', '0px');	
}
if(staff==1){
	// 显示导航
	$('#mNavbar').show();
	$('#mContent').css('marginTop', '45px');	
	$('#mContentList').css('marginTop', '10px');	
}
if(typeUser==1){
	$(".typeStaff").show();
}
function formatterDealDetail(val, row) {
	return str;
}
if (roleCode=="JDYPTZX"||roleCode=="JDYBZZX") {
	$(".sendBackBtn").remove();
} else if(roleCode=="JG"){
	$(".sendBackBtn").show();
	$(".unusualBtn").show();
}else if(roleCode=="BLRY"){
	$(".unusualBtn").show();
	$(".allotBtn").hide();
	backType=1; 
}
$(function () {
	Ajax();
	//返回列表
	$('.return').on('tap', function() {   
		if (state==0) {
			window.location.href = "orderList.html?Itype=" + state+'&turn='+turn;
		}else if (state==1) {
			window.location.href = "orderList.html?Itype=" + state+'&turn='+turn;
		}else if (state==2) {
			window.location.href = "orderList.html?Itype=" + state+'&turn='+turn;
		} else if (state==3){
			window.location.href = "orderList.html?Itype=" + state+'&turn='+turn;
		}else if (state==4){
			window.location.href = "orderList.html?Itype=" + state+'&turn='+turn;
		}else if(staff==1){
			window.location.href = "orderList.html?turn=" + turn;
		}
		//window.location.href = "client_index.html?Itype=" + itype;
	});
})
//接单
$(".redeploy").click(function(){
	var btnArray = ['否', '是'];
	mui.confirm('确定接单？', '提示', btnArray, function(f) {
		if(f.index == 1) {
			$.ajax({
				type: "post",
				dataType: 'json',
				url: huayi.config.callcenter_url + '/WxLogin/SureWorkOrder',
				async: true,
				data: {
					orderid: id, //否	strubg	工单编号
					openid: openid, //微信openid 测试用
				},
				success: function(data) {
					if(data.state == "success") {
						debugger
						mui.toast('接单成功');
						window.location.href = "orderList.html?Itype=1";
					} else {
						mui.toast(data.message);
					}
				}
			});
		} else {
			mui.toast("取消");
		}
	})
})
//退回
$(".backBtn").click(function(){   
    $(".pop_up_bj").fadeIn();
    $(".pop_up_div3").fadeIn();
    backType=0;
    itype=1;
})
$(".unusualBtn").click(function(){   
    $(".pop_up_bj").fadeIn();
    $(".pop_up_div3").fadeIn();
    backType=1;
    itype=2;
})
$(".pop_up_bj" ).click(function(){   
    $(".pop_up_bj").hide();
    $(".pop_up_div3").hide();
    
})
$(".pop_up_div3 .btn .fl" ).click(function(){   
    $(".pop_up_bj").hide();
    $(".pop_up_div3").hide();
    mui.alert("取消");
})
$(".pop_up_div3 .btn .fr").click(function(){
	if($(".textcont_back").val()=="") {
		mui.alert("请输入退回内容");
	}else{
		$(".pop_up_bj").hide();
	    $(".pop_up_div3").hide();
		$.ajax({
			type: "post",
			dataType: 'json',
			url: huayi.config.callcenter_url + '/WxLogin/BackWorkOrder',
			async: true,
			data: {
				openid: openid, //微信openid 测试用
				orderid: id, //否	strubg	工单编号
				cont:$(".textcont_back").val(),
				type:backType,
			},
			success: function(data) {
				if(data.state == "success") {
					debugger
					mui.toast('退回成功');
					window.location.href = "orderList.html?Itype="+itype;
				} else {
					mui.toast(data.message);
				}
			}
		});
	}
	
})
	
//处理
$(".deal").click(function(){   
    $(".pop_up_bj").fadeIn();
    $(".pop_up_div4").fadeIn();
})
$(".pop_up_bj" ).click(function(){   
    $(".pop_up_bj").hide();
    $(".pop_up_div4").hide();
})
$(".pop_up_div4 .btn .fl" ).click(function(){   
    $(".pop_up_bj").hide();
    $(".pop_up_div4").hide();
    mui.alert("取消");
})
$(".pop_up_div4 .btn .fr" ).click(function(){ 
	$(".pop_up_bj").hide();
    $(".pop_up_div4").hide();
    if($(".textcont_deal").val()=="") {
		mui.alert("请输入处理内容");
	}else{
		$(".pop_up_bj").hide();
    	$(".pop_up_div4").hide();
    	$.ajax({
			type: "post",
			dataType: 'json',
			url: huayi.config.callcenter_url + '/WxLogin/DealWorkOrder',
			async: true,
			data: {
				orderid: id, //否	strubg	工单编号
				isover:$('.order_state').find('input[type="radio"]:checked').val(), //处理状态
				cont: $(".textcont_deal").val(), //否	int	处理内容
				openid: openid, //微信openid,
			},
			success: function(data) {
				if(data.state == "success") {
					debugger
					mui.toast('处理成功');
					window.location.href = "orderList.html?Itype=2";
				} else {
					mui.toast(data.message);
				}
			}
		});
	}
		
})
$(".allot").click(function(){ 
	window.location.href = "redeploy.html?id=" + id + '&itype=' + state;
})
function Ajax() {
	$.ajax({
		url: huayi.config.callcenter_url + '/WxLogin/GetWoInfo',
		data: {
			workorderid: id, //工单编号
			openid: openid, //微信openid,
		},
		async: true,
		dataType: 'json',
		type: 'get', //HTTP请求类型
		success: function(data) {
			if(data.state.toLowerCase() == "success") {
				
				var result = data.data.data[0]
				var stateText;
				if (result.State==0) {
					stateText="待指派"
				}else if(result.State==1){
					stateText="待接单"
					$(".dispose2").show();
				}else if(result.State==2){
					stateText="待处理"
					$(".dispose3").show();
				}else if(result.State==3){
					$(".dispose3").show();
					$(".unbackBtn").remove();
					stateText="退回待处理"
				}else if(result.State==4){
					$(".dispose3").show();
					$(".unbackBtn").remove();
					stateText="异常退回待处理"
				}else if(result.State==5){
					$(".dispose3").show();
					stateText="处理中"
				}else if(result.State==10){
					stateText="已完结"
				}
				console.log(result.State);
				$("#workOrderID").text(result.WorkOrderID); //工单编号
				$("#gdlxName").text(result.GDLXName); //工单类型
				$("#gdlyName").text(result.GDLYName); //工单来源
				$("#state").text(stateText); //工单状态
				$("#customer").text(result.Customer); //客户姓名
				$("#clDetail").text(result.clcontent); //客户姓名
				if (state==3) {
					var str="";
					var val1 = result.CustomerTel.substring(0, 3);
					var val2 = result.CustomerTel.substr(-4);
					str = val1 + '****' + val2;
					$("#customerTel").text(str); //客户电话
				}else{
					$("#customerTel").text(result.CustomerTel); //客户电话
				}
				
				$("#state").text(stateText); //工单状态
				$("#ywlxName").text(result.YWLXName); //业务类型
				$("#detail").text(result.Detail); //工单内容
				//$("#Work_areaname").attr('data-index',result.IsAdminSend);
				var record=data.data.item;
				if(data.data.item &&record.length > 0) {
					$(record).each(function(i, row) {
						$('<h5 class="mui-badge-success mui-badge-inverted">' + record[i].F_CreateTime+
						'</h5><h5><span style="line-height: 20px;">跟踪信息:'+
						'</span><span class="Font" style="line-height: 20px;">' + record[i].F_OptContent + 
						'</span></h5>').appendTo("#GDgz")
						
					})
				}
			}
		},
		error: function(xhr, type, errorThrown) {
			//异常处理；
		}
	})
}