// JavaScript Document
var setTargetArr = [];
var zommHarvorRinsetsuCd = '';
var zoomCallBackFnc = [];
var zoomCallbackFncIdx = '';
var zoomDairitenKbn = '';
var zoomHarborPrg ='A510';
function setupZoomModal(zoomType){
	switch(zoomType){
		case 'harborAgency':
			zoomHarborPrg ='A530';
		case 'harbor':
			createHarborZoomModai();
			$('.slctZoomHarvorPd').on('change',function(){
				var tikuCd = $(this).attr('data-slctIdx');
				var minatoCd = $(this).val();
				callZoomHarvor('0',tikuCd,minatoCd,'');
			});
			
			$('.linkZoomHarvorRireki').on('click',function(){
				var rirekiFlg = $(this).attr('data-rireki');
				var tikuCd = $(this).attr('data-chiku');
				var minatoCd = $(this).attr('data-minato');
				callZoomHarvor(rirekiFlg,tikuCd,minatoCd,'');
			});
			
			$('#mdlHarvorZoom').on('show.bs.modal', function (e) {
				setTargetArr = [];
				var callBtn = e.relatedTarget;
				setTargetArr[0] = $(callBtn).attr('data-cda');
				setTargetArr[1] = $(callBtn).attr('data-cdb');
				setTargetArr[2] = $(callBtn).attr('data-name');
				setTargetArr[3] = $(callBtn).attr('data-ganpeki');
				zoomCallbackFncIdx = $(callBtn).attr('data-callback');
				var initRirekiFlg = '1';
				var zommHarvorRinsetsuSelectID = $(callBtn).attr('data-rinsetsu');
				if(zommHarvorRinsetsuSelectID) {
					zommHarvorRinsetsuCd = $(zommHarvorRinsetsuSelectID).val();
				}else {
					zommHarvorRinsetsuCd = '';
				}
				if(zommHarvorRinsetsuCd !== '' && zommHarvorRinsetsuCd !== undefined){
					$('#divHarvorZoomHead').hide();
					initRirekiFlg = '0';
				}else {
					$('#divHarvorZoomHead').show();
				}
				var initCdA = $(setTargetArr[0]).val();
				callZoomHarvor(initRirekiFlg,'','',initCdA);
			});
			break;
			
		case 'dairiten':
			createDairitenZoomModai();
			$('#mdlDairitenZoom').on('show.bs.modal', function (e) {
				setTargetArr = [];
				var callBtn = e.relatedTarget;
				setTargetArr[0] = $(callBtn).attr('data-cda');
				setTargetArr[1] = $(callBtn).attr('data-cdb');
				setTargetArr[2] = $(callBtn).attr('data-name');
				zoomCallbackFncIdx = $(callBtn).attr('data-callback');
				if($(callBtn).attr('data-tag_kbn')){
					zoomDairitenKbn = '4';
				}else {
					zoomDairitenKbn = '3';
				}
				var zoomTitle = $(callBtn).attr('data-zoom_title');
				if(zoomTitle){
					$('#mdlDairitenZoomLabel').text(zoomTitle);
				}
				callZoomDairiten('履歴');
			});
			$('.linkZoomDairiten').on('click',function(){
				var callParam = $(this).text();
				callZoomDairiten(callParam);
			});
			break;
			
		case 'shipName':
			createShipNameZoomModai();
			$('#mdlShipNameZoom').on('show.bs.modal', function (e) {
				$('#tblShipNameZoom tbody').empty();
				setTargetArr = [];
				var callBtn = e.relatedTarget;
				setTargetArr[0] = $(callBtn).attr('data-cda');
				setTargetArr[1] = $(callBtn).attr('data-cdb');
				setTargetArr[2] = $(callBtn).attr('data-name');
				setTargetArr[3] = $(callBtn).attr('data-GT');
				setTargetArr[4] = $(callBtn).attr('data-DW');
				setTargetArr[5] = $(callBtn).attr('data-shipType');
				setTargetArr[6] = $(callBtn).attr('data-DF');
				setTargetArr[7] = $(callBtn).attr('data-LOA');
				zoomCallbackFncIdx = $(callBtn).attr('data-callback');
			});
			$('#btnShipNameZoomSearch').on('click',function(){
				if($('#inpShipNameZoomSearchStr').val() === ''){
					$('#lblErrShipName').text('船名を入力してください');
				}else {
					$('#lblErrShipName').text('&nbsp;');
					callZoomShipName();
				}
			});
			break;
	}
}


function createHarborZoomModai(){
	var addHtml = 	'<div class="modal fade" id="mdlHarvorZoom" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="mdlHarvorZoomLabel" aria-hidden="true">'+
						'<div class="modal-dialog modal-lg modal-dialog-scrollable">'+
							'<div class="modal-content h-100">'+
								'<div class="modal-header">'+
									'<h5 class="modal-title" id="mdlHarvorZoomLabel">地名検索</h5>'+
									'<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>'+
								'</div>'+
								'<div class="modal-body">'+
									'<div id="divHarvorZoomHead">' +
										'<div class="mb-1">' +
											'<a href="#" class="linkZoomParam linkZoomHarvorRireki m-1" data-rireki="1" data-chiku="" data-minato="" >履歴</a>'+
											'<select class=" m-1 slctZoomHarvorPd" id="slctZoomHarvorPd1" data-slctIdx="1"></select>'+
											'<select class=" m-1 slctZoomHarvorPd" id="slctZoomHarvorPd2" data-slctIdx="2"></select>'+
											'<select class=" m-1 slctZoomHarvorPd" id="slctZoomHarvorPd3" data-slctIdx="3"></select>'+
											'<select class=" m-1 slctZoomHarvorPd" id="slctZoomHarvorPd4" data-slctIdx="4"></select>'+
										'</div>'+
										'<div class="mb-1 container" id="divZoomHarverChimei">' +
										'</div>' +
										'<div class="container-fluid text-end">'+
											'<span id="spnZoomHarvorExample"></span>'+
										'</div>' +
									'</div>' +
									'<table id="tblHarvorZoom" class="tableZoom w-100">'+
										'<thead>' +
											'<tr>' +
												'<th class="p-1">地名</th>'+
												'<th class="p-1 tblZoomBtn">&nbsp;</th>'+
											'</tr>' +
										'</thead>' +
										'<tbody>'+
										'</tbody>'+
									'</table>'+
								'</div>'+
								'<div class="modal-footer">'+
									'<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">閉じる</button>'+
								'</div>'+
							'</div>'+
						'</div>'+
					'</div>';
	$('body').append(addHtml);
}
function createDairitenZoomModai(){
	var addHtml = 	'<div class="modal fade" id="mdlDairitenZoom" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="mdlDairitenZoomLabel" aria-hidden="true">'+
						'<div class="modal-dialog modal-dialog-scrollable">'+
							'<div class="modal-content h-100">'+
								'<div class="modal-header">'+
									'<h5 class="modal-title" id="mdlDairitenZoomLabel">国内総代理店検索</h5>'+
									'<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>'+
								'</div>'+
								'<div class="modal-body">'+
									'<div class="mb-1">' +
//										'<a href="#" class="linkZoomParam linkZoomDairiten m-1">主要会社</a>'+
										'<a href="#" class="linkZoomParam linkZoomDairiten m-1">履歴</a>'+
									'</div>'+
									'<div class="mb-1">' +
										'<a href="#" class="linkZoomParam linkZoomDairiten m-1">A</a>'+
										'<a href="#" class="linkZoomParam linkZoomDairiten m-1">B</a>'+
										'<a href="#" class="linkZoomParam linkZoomDairiten m-1">C</a>'+
										'<a href="#" class="linkZoomParam linkZoomDairiten m-1">D</a>'+
										'<a href="#" class="linkZoomParam linkZoomDairiten m-1">E</a>'+
										'<a href="#" class="linkZoomParam linkZoomDairiten m-1">F</a>'+
										'<a href="#" class="linkZoomParam linkZoomDairiten m-1">G</a>'+
										'<a href="#" class="linkZoomParam linkZoomDairiten m-1">H</a>'+
										'<a href="#" class="linkZoomParam linkZoomDairiten m-1">I</a>'+
										'<a href="#" class="linkZoomParam linkZoomDairiten m-1">J</a>'+
										'<a href="#" class="linkZoomParam linkZoomDairiten m-1">K</a>'+
										'<a href="#" class="linkZoomParam linkZoomDairiten m-1">L</a>'+
										'<a href="#" class="linkZoomParam linkZoomDairiten m-1">M</a>'+
										'<a href="#" class="linkZoomParam linkZoomDairiten m-1">N</a>'+
										'<a href="#" class="linkZoomParam linkZoomDairiten m-1">O</a>'+
										'<a href="#" class="linkZoomParam linkZoomDairiten m-1">P</a>'+
										'<a href="#" class="linkZoomParam linkZoomDairiten m-1">Q</a>'+
										'<a href="#" class="linkZoomParam linkZoomDairiten m-1">R</a>'+
										'<a href="#" class="linkZoomParam linkZoomDairiten m-1">S</a>'+
										'<a href="#" class="linkZoomParam linkZoomDairiten m-1">T</a>'+
										'<a href="#" class="linkZoomParam linkZoomDairiten m-1">U</a>'+
										'<a href="#" class="linkZoomParam linkZoomDairiten m-1">V</a>'+
										'<a href="#" class="linkZoomParam linkZoomDairiten m-1">W</a>'+
										'<a href="#" class="linkZoomParam linkZoomDairiten m-1">X</a>'+
										'<a href="#" class="linkZoomParam linkZoomDairiten m-1">Y</a>'+
										'<a href="#" class="linkZoomParam linkZoomDairiten m-1">Z</a>'+
									'</div>' +
									'<table id="tblDairitenZoom" class="tableZoom w-100">'+
										'<thead>' +
											'<tr>' +
												'<th class="p-1">会社</th>'+
												'<th class="p-1 tblZoomBtn">&nbsp;</th>'+
											'</tr>' +
										'</thead>' +
										'<tbody>'+
										'</tbody>'+
									'</table>'+
								'</div>'+
								'<div class="modal-footer">'+
									'<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">閉じる</button>'+
								'</div>'+
							'</div>'+
						'</div>'+
					'</div>';
	$('body').append(addHtml);
}
function createShipNameZoomModai(){
	var addHtml = 	'<div class="modal fade" id="mdlShipNameZoom" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="mdlShipNameZoomLabel" aria-hidden="true">'+
						'<div class="modal-dialog modal-lg modal-dialog-scrollable">'+
							'<div class="modal-content h-100">'+
								'<div class="modal-header">'+
									'<h5 class="modal-title" id="mdlShipNameZoomLabel">船名検索</h5>'+
									'<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>'+
								'</div>'+
								'<div class="modal-body">'+
									'<div id="divHarvorZoomHead">' +
										'<div class="mb-1">' +
											'<label class="lblTitle_short p-1">船名</label>'+
											'<input type="text" class="inp_20em mx-1" id="inpShipNameZoomSearchStr">'+
											'<input type="button" value="検索" id="btnShipNameZoomSearch">'+
										'</div>'+
										'<div>'+
											'<label id="lblErrShipName" class="errLabel p-1">&nbsp;</label>'+
										'</div>'+
										'<div>'+
											'<span class="text-primary">※船名を先頭から半角英数字で入力してください。（前方一致で検索します）</span>'+
										'</div>'+
									'</div>' +
									'<table id="tblShipNameZoom" class="tableZoom w-100">'+
										'<thead>' +
											'<tr>' +
												'<th class="p-1">船名</th>'+
												'<th class="p-1 tblZoom_5em">IMO No.</th>'+
												'<th class="p-1 tblZoom_5em">ｺｰﾙｻｲﾝ</th>'+
												'<th class="p-1 tblZoom_5em">全長</th>'+
												'<th class="p-1 tblZoom_5em">総屯数</th>'+
												'<th class="p-1 tblZoom_5em">吃水</th>'+
												'<th class="p-1 tblZoomBtn">&nbsp;</th>'+
											'</tr>' +
										'</thead>' +
										'<tbody>'+
										'</tbody>'+
									'</table>'+
								'</div>'+
								'<div class="modal-footer">'+
									'<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">閉じる</button>'+
								'</div>'+
							'</div>'+
						'</div>'+
					'</div>';
	$('body').append(addHtml);
}

//一覧取得関数
function callZoomHarvor(rirekiClick,chikuCd,minatoCd,timeiCdA){
	$('#tblHarvorZoom tbody').empty();
	
	var param = {
		'appname': nasiappname,
		'prgname': zoomHarborPrg,
		'param1': nasiappname,
		'param2': $('#ssid').val(),
		'param3': '乗船地', //タイトル
		'param4': '0', //P.中継港ﾌﾗｸﾞ
		'param5': zommHarvorRinsetsuCd, //隣接水先人乗船港
		'param6': rirekiClick, //履歴ﾌﾗｸﾞ
		'param7': chikuCd, //地区ｺｰﾄﾞ
		'param8': minatoCd, //港/地名Aｺｰﾄﾞ
		'param9': timeiCdA, //港/地名Aｺｰﾄﾞ
		'arguments':'param1,param2,param3,param4,param5,param6,param7,param8,param9'
	};
	var zoomXml = ajaxFunction(param);
	var kekka = $(zoomXml).find('ret').text();
	if(kekka === 'I001'){
		if(zommHarvorRinsetsuCd === '' || zommHarvorRinsetsuCd === undefined){
			$('#spnZoomHarvorExample').text($(zoomXml).find('example').text());
			// 地名ドロップダウンリスト作成
			for(var idx=0; idx<4; idx++){
				var pdIdx = String(idx+1);
				$('#slctZoomHarvorPd'+pdIdx).empty();
				if($(zoomXml).find('pd'+pdIdx).text() !== ''){
					$('#slctZoomHarvorPd'+pdIdx).append($(zoomXml).find('pd'+pdIdx).text());
					$('#slctZoomHarvorPd'+pdIdx).show();
				}else {
					$('#slctZoomHarvorPd'+pdIdx).hide();
				}
			}
			// 港リンク作成
			$('#divZoomHarverChimei').empty();
			var minatoIdx = 0;
			var linkHtml = '';
			$(zoomXml).find('minatoLine').each(function(){
				minatoIdx = minatoIdx + 1;
				if(minatoIdx % 5 === 1){
					linkHtml = linkHtml + '<div class="row">';
				}
				var minatoCd = $(this).find('minato_href').text();
				var minatoNm = $(this).find('minato_name').text();
				linkHtml = linkHtml + '<div class="col-sm text-center"><a href="#" class="linkZoomParam linkZoomHarvor m-1" data-rireki="" data-chiku="" data-minato="'+minatoCd+'" >'+minatoNm+'</a></div>';
				if(minatoIdx % 5 === 0){
					linkHtml = linkHtml + '</div>';
				}
			});
			$('#divZoomHarverChimei').append(linkHtml);
			
			$('.linkZoomHarvor').on('click',function(){
				var rirekiFlg = $(this).attr('data-rireki');
				var tikuCd = $(this).attr('data-chiku');
				var minatoCd = $(this).attr('data-minato');
				callZoomHarvor(rirekiFlg,tikuCd,minatoCd);
			});
		}
		$(zoomXml).find('line').each(function(){
			var timei_cda = $(this).find('timei_cda').text();
			var timei_cdb = $(this).find('timei_cdb').text();
			var timei_name = $(this).find('timei_name').text();
			var timei_ganpeki = $(this).find('ganpeki').text();
			var addRow = '<tr>' +
							'<td class="p-1">'+timei_name+'</td>'+
							'<td class="p-1 tblZoomBtn"><input type="button" class="btnZoomSelectHarvor" value="選択" data-cda="'+timei_cda+'" data-cdb="'+timei_cdb+'" data-name="'+timei_name+'" data-ganpeki="'+timei_ganpeki+'" data-bs-dismiss="modal" aria-label="Close"></td>'+
						'</tr>';
			$('#tblHarvorZoom tbody').append(addRow);
		});
		$('.btnZoomSelectHarvor').on('click',function(){
			var cda = $(this).attr('data-cda');
			var cdb = $(this).attr('data-cdb');
			var name = $(this).attr('data-name');
			var ganpeki = $(this).attr('data-ganpeki');
			$(setTargetArr[0]).val(cda);
			$(setTargetArr[1]).val(cdb);
			$(setTargetArr[2]).text(name);
			if(setTargetArr[3]){
				$(setTargetArr[3]).val(ganpeki);
			}
			if( typeof zoomCallbackFncIdx === 'undefined' ){
			}else{
				var callbackFnc = zoomCallBackFnc[zoomCallbackFncIdx]
				callbackFnc();
			}
		});
	}else if(kekka === 'E001' ){
		// セッションエラーを表示
		$('#prgname').val('A040');
		$('#menuForm').submit();
	}else if(kekka === 'P001' ){
		// パラメータエラーを表示
		$('#prgname').val('A000');
		$('#menuForm').submit();
	}
}
function callZoomDairiten(callParam){
	$('#tblDairitenZoom tbody').empty();
	
	var param = {
		'appname': nasiappname,
		'prgname':'A500',
		'param1': nasiappname,
		'param2': $('#ssid').val(),
		'param3': zoomDairitenKbn, //代理店区分
		'param4': '国内総代理店', //タイトル
		'param5': callParam,
		'arguments':'param1,param2,param3,param4,param5'
	};
	var zoomXml = ajaxFunction(param);
	var kekka = $(zoomXml).find('ret').text();
	if(kekka === 'I001'){
		$(zoomXml).find('line').each(function(){
			var tokui_cda = $(this).find('tokui_cda').text();
			var tokui_cdb = $(this).find('tokui_cdb').text();
			var tokui_name = $(this).find('tokui_name').text();
			var addRow = '<tr>' +
							'<td class="p-1">'+tokui_name+'</td>'+
							'<td class="p-1 tblZoomBtn"><input type="button" class="btnZoomSelectDairiten" value="選択" data-cda="'+tokui_cda+'" data-cdb="'+tokui_cdb+'" data-name="'+tokui_name+'" data-bs-dismiss="modal" aria-label="Close"></td>'+
						'</tr>';
			$('#tblDairitenZoom tbody').append(addRow);
		});
		$('.btnZoomSelectDairiten').on('click',function(){
			var cda = $(this).attr('data-cda');
			var cdb = $(this).attr('data-cdb');
			var name = $(this).attr('data-name');
			$(setTargetArr[0]).val(cda);
			$(setTargetArr[1]).val(cdb);
			$(setTargetArr[2]).text(name);
			if( typeof zoomCallbackFncIdx === 'undefined' ){
			}else{
				var callbackFnc = zoomCallBackFnc[zoomCallbackFncIdx]
				callbackFnc();
			}
		});
	}else if(kekka === 'E001' ){
		// セッションエラーを表示
		$('#prgname').val('A040');
		$('#menuForm').submit();
	}else if(kekka === 'P001' ){
		// パラメータエラーを表示
		$('#prgname').val('A000');
		$('#menuForm').submit();
	}
}
function callZoomShipName(){
	$('#tblShipNameZoom tbody').empty();
	
	var param = {
		'appname': nasiappname,
		'prgname':'A520',
		'param1': nasiappname,
		'param2': $('#ssid').val(),
		'param3': '', //IMO No.
		'param4': '', //ｺｰﾙｻｲﾝ
		'param5': $('#inpShipNameZoomSearchStr').val(), //検索文字列
		'arguments':'param1,param2,param3,param4,param5'
	};
	var zoomXml = ajaxFunction(param);
	var kekka = $(zoomXml).find('ret').text();
	var msg = $(zoomXml).find('message').text();
	if(kekka === 'I001'){
		$('#lblErrShipName').text(msg);
		$(zoomXml).find('line').each(function(){
			var shipName = $(this).find('shipName').text();
			var IMONo = $(this).find('IMONo').text();
			var callSigne = $(this).find('callSigne').text();
			var totalW = $(this).find('totalW').text();
			var totalTon = $(this).find('totalTon').text();
			var kissui = $(this).find('kissui').text();
			var GT = $(this).find('GT').text();
			var newGT = $(this).find('newGT').text();
			var DW = $(this).find('DW').text();
			var shipType = $(this).find('shipType').text();
			var DF = $(this).find('DF').text();
			var LOA = $(this).find('LOA').text();
			var addRow = '<tr>' +
							'<td class="p-1">'+shipName+'</td>'+
							'<td class="p-1 tblZoom_5em">'+IMONo+'</td>'+
							'<td class="p-1 tblZoom_5em">'+callSigne+'</td>'+
							'<td class="p-1 tblZoom_5em text-end">'+totalW+'</td>'+
							'<td class="p-1 tblZoom_5em text-end">'+totalTon+'</td>'+
							'<td class="p-1 tblZoom_5em text-end">'+kissui+'</td>'+
							'<td class="p-1 tblZoomBtn"><input type="button" class="btnZoomSelectShipName" value="選択" '+
							' data-cda="'+IMONo+'"'+
							' data-cdb="'+callSigne+'"'+ 
							' data-name="'+shipName+'"'+
							' data-GT="'+GT+'"' + 
							' data-newGT="'+newGT+'"' + 
							' data-DW="'+DW+'"' + 
							' data-shipType="'+shipType+'"' +  
							' data-DF="'+DF+'"' + 
							' data-LOA="'+LOA+'"' + 
							' data-bs-dismiss="modal" aria-label="Close"></td>'+
						'</tr>';
			$('#tblShipNameZoom tbody').append(addRow);
		});
		$('.btnZoomSelectShipName').on('click',function(){
			var cda = $(this).attr('data-cda');
			var cdb = $(this).attr('data-cdb');
			var name = $(this).attr('data-name');
			var GT = Number( $(this).attr('data-GT'));
			var newGT = Number( $(this).attr('data-newGT'));
			var DW =  Number($(this).attr('data-DW'));
			var shipType =  $(this).attr('data-shipType');
			var DF =  Number($(this).attr('data-DF'));
			var LOA =  Number($(this).attr('data-LOA'));
			
			$(setTargetArr[0]).val(cda);
			$(setTargetArr[1]).val(cdb);
			if($(setTargetArr[2]).prop('tagName') === 'INPUT'){
				$(setTargetArr[2]).val(name);
			}else {
				$(setTargetArr[2]).text(name);
			}
			if(setTargetArr[3]){
				if(newGT !== 0){
					$(setTargetArr[3]).val(newGT);
				}else {
					$(setTargetArr[3]).val(GT);
				}
			}
			if(setTargetArr[4]){
				$(setTargetArr[4]).val(DW);
			}
			if(setTargetArr[5]){
				$(setTargetArr[5]).val(shipType);
			}
			/* 吃水はセットしない
			if(setTargetArr[6]){
				$(setTargetArr[6]).val(DF);
			}
			*/
			if(setTargetArr[7]){
				$(setTargetArr[7]).val(LOA);
			}
			if( typeof zoomCallbackFncIdx === 'undefined' ){
			}else{
				var callbackFnc = zoomCallBackFnc[zoomCallbackFncIdx]
				callbackFnc();
			}
		});
	}else if(kekka === 'E001' ){
		// セッションエラーを表示
		$('#prgname').val('A040');
		$('#menuForm').submit();
	}else if(kekka === 'P001' ){
		// パラメータエラーを表示
		$('#prgname').val('A000');
		$('#menuForm').submit();
	}
}