var HTML = '';
//记录添加的 tr id
var n = 0;
//记录途径顺序
var m = 1;

window.onload = function(){
//	HTML = $('#tr_replace').html();
	$('#tr_replace').hide();
}


//根据用户输入检索
function lineListSearch(listId){
	$.ajax({
			url:'./st-xm/Model/linemanager/stationListSearch.php',             //请求地址
			type: "post",                       //请求方式
//			data: data,        //请求参数
			data:{
				stationName:listId
			},
			async:false,
			dataType: "json",
			success: function (data) {
				console.log(data);
				if(data.code == 0){
					$('#line_name_search').show();
					var line_list = document.getElementById("line_name_search");
					var list = data.list;
					var htmlStr = "";
					for (var i=0;i<list.length;i++) {
						htmlStr +='<div class="div_input" onclick="click_select('+'\''+list[i].res_id+'\''+',\''+list[i].res_name+'\')">'+list[i].res_name+'</div>';
					}
					line_list.innerHTML = htmlStr;
				}
		    },
			fail: function (data) {
					
			}
	});
}
//选中一个站点
function click_select(id,name){
	$('#line_list').val(name);
	$('#line_name_search').hide();
   	$('#station_select').val(id);
}



//添加一条线路
function addOneLine(){
	var line_list = $('#line_list').val();
	if (line_list == "") {
		swal(
			'添加线路失败',
			'未选择站点信息',
			'error'
		);
		return;
	}
	var stationId = $('#station_select').val();
	var poiAry = '';
	var checkPortAry = '';
	var poiIdAry = '';
	var checkPortIdAry = '';
	var ifHasCheckport = '';
	//站点id对应每一条站点信息
//	var stationId = '';
	//请求站点相关的数据
	$.ajax({
			url:'./st-xm/Model/linemanager/stationInfo.php',             //请求地址
			type: "post",                     
			data:{stationId:stationId},
			async:false,
			dataType: "json",
			success: function (data) {
				console.log(data);
				if(data.code == 0){
					//(poi)
					var poiListStr = data.list.parent_area_name_list;
					//得到截取后返回的数组(poi)
					poiAry = intercept(poiListStr);
					//(poi)id
					var poiListIdStr = data.list.parent_area_id_list;
					//得到截取后返回的数组(poi)
					poiIdAry = intercept(poiListIdStr);
					ifHasCheckport = data.list.checkport_res_name;
					if (data.list.checkport_res_id != 0) {
						//检票口数组
						checkPortAry = data.list.checkport_res_name.split(',');
						//检票口id数组
						checkPortIdAry = data.list.checkport_res_id.split(',');
					}
					//站点proid
					stationId = data.list.res_id;
				}
		    },
			fail: function (data) {
					
			}
	});
	
	//tbody
	var line = document.getElementById('line_list_info');
	//tr
	var tr = document.createElement("tr");
	tr.id = m;
	var newHtml = '';
	var tempHTML = $('#tr_replace').html();
	tempHTML = tempHTML.replace('[途径顺序]',m);
	tempHTML = tempHTML.replace('[orderid]','orderid'+m);
	tempHTML = tempHTML.replace('[站点]',$('#line_list').val());
	tempHTML = tempHTML.replace('[station_type_td]','station_type_td'+m);
	tempHTML = tempHTML.replace('[select_poi]','select_poi'+m);
	tempHTML = tempHTML.replace('[teller]','teller'+m);
	tempHTML = tempHTML.replace('[teller_station]','teller_station'+m);
	tempHTML = tempHTML.replace('[stationid]','stationid'+m);
	tempHTML = tempHTML.replace('[timeid]','timeid'+m);
	tempHTML = tempHTML.replace('[updown]','updown'+m);
	tempHTML = tempHTML.replace('[del]',m);
	tempHTML = tempHTML.replace('time_out','time_out'+m);
	tempHTML = tempHTML.replace('[station_type]','station_type'+m);
	tempHTML = tempHTML.replace('[hideid]','hideid'+m);
	tempHTML = tempHTML.replace('[hideid_id]',stationId);
	line.appendChild(tr);
	var str = '<a href="#" onclick="deleteOneRow('+''+m+')">删除';
	tempHTML = tempHTML.replace('[删除]',str);
	tr.innerHTML = tempHTML;
	//替换poi
	var poiHTML = '';
	
	for (var i = poiAry.length-1; i>=0;i --) {
		var tempHTML1 = '<option value="0">请选择</option>';
		tempHTML1 = tempHTML1.replace('请选择',poiAry[i]);
		tempHTML1 = tempHTML1.replace('0',poiIdAry[i]);
		poiHTML += tempHTML1;
	}
	$('#select_poi'+m).html(poiHTML);
	//替换检票口
	if (ifHasCheckport=='') {
		$('#teller'+m).html('-');
	}else{
		var checkportHTML = '';
		
		for (var i = 0; i<checkPortAry.length; i++ ) {
			var tempHTML2 = '<option value="0">请选择</option>';
			tempHTML2 = tempHTML2.replace('请选择',checkPortAry[i]);
			tempHTML2 = tempHTML2.replace('0',checkPortIdAry[i]);
			checkportHTML += tempHTML2;
		}
		$('#teller_station'+m).html(checkportHTML);
	}
	n++;
	m++;
		//重新排布tr的途径顺序
	var trCount = $('#line_list_info').children().length;
	for (var k = 1; k < trCount; k++) {
	   var seltr = $('#line_list_info').children(k);
	   var selid = seltr[k].id;
		$('#orderid'+selid).html(k);
		if(k==1){
			$("#time_out"+selid).attr("readonly","true")
		}
	}
}

//遍历poi字符串
function intercept(str){
//	var arr = new Array();
//	arr = str.split("}");
//	var newStr = '';
//	if(arr.length > 1){
//		for (var i = 0 ; i<arr.length ; i++) {
//			newStr = newStr + arr[i];
//		}
//	}
//	var backAry = new Array();
//	backAry = newStr.split("{");
//	return backAry;
	var newStr = '';
	newStr = str.replace(/}{/g,'@');
	newStr = newStr.substr(1,newStr.length-2);
	var backAry = new Array();
	backAry = newStr.split('@');
	return backAry;
	
}
//删除某一条数据
function deleteOneRow(rowCount){
	var delRow = document.getElementById(rowCount);
	delRow.remove();
	//重新排布tr的途径顺序
	var trCount = $('#line_list_info').children().length;
	for (var k = 1; k < trCount; k++) {
	   var seltr = $('#line_list_info').children(k);
	   var selid = seltr[k].id;
		$('#orderid'+selid).html(k);
//		if(k==1){
//			$("#time_out"+selid).attr("readonly","true")
//		}
		
	}
	
}

//保存按钮
function saveInfo(){
	var urlStr = '';
	
	var checkportUrl = '';
	//遍历所有的tr
	var trCount = $('#line_list_info').children().length;
	if (trCount==1) {
		swal(
			'添加线路失败',
			'无相关的站点信息,无法保存',
			'error'
		);
		return;
	}
	for (var k = 1; k < trCount; k++) {
	   var seltr = $('#line_list_info').children(k);
	   var selid = seltr[k].id;
		$('#orderid'+selid).html(k);
		//取出要传的数据
		var updown = $('#station_type'+selid).val();
		var order = $('#orderid'+selid).text();
		var time = $('#time_out'+selid).val();
		if (time=="") {
			time = 0;
		}
		if ($('#teller'+selid).text()=="-") {
			checkportUrl = 0;
		}else{
			checkportUrl = $('#teller_station'+selid).val();
		}
		var station = $('#hideid'+selid).val();
		var poi = $('#select_poi'+selid).val();
		if ((poi=='')||(poi==null)) {
			poi = 0;
		}
		urlStr = urlStr +'{'+updown+','+order+','+time+','+checkportUrl+','+station+','+poi+'}';
	}
//	var saleStyle_company = $('#company input[name="saleStyle_company"]:checked ').val();
	var lineStyle = $("input[name='bar_style']:checked").val();
	
	$.ajax({
			url:'./st-xm/control.php',             //请求地址
			type: "post",                     
			data:{
				type:'linemanager_add',
				linestyle:lineStyle,
				station_list:urlStr
			},
			async:false,
			dataType: "json",
			success: function (data) {
				console.log(data);
				if(data.code == 0){
					swal({
						title: '添加线路成功',
						type: 'success',
						timer: 20000,
						confirmButtonText: 'OK'
					}).then(
						function () { window.location.href="search_line.html"; },
						// handling the promise rejection
						function (dismiss) {
							window.location.href="search_line.html";
						}
					)
				}
		    },
			fail: function (data) {
					swal({
							title: '添加线路失败',
							type: 'error'
						}
					);
			}
	});
}