$(document).on('click', '#krcnTab', function (){
	location.href="/target/ipShipList?siType=KRCN";
})

$(document).on('click', '#krjpruTab', function (){
	location.href="/target/ipShipList?siType=KRJPRU";
})

$(document).on('click', '#addKRCN', function (){
	const siType = 'KRCN';
	showEditModal(siType);
})

$(document).on('click', '#addKRJPRU', function (){
	const siType = 'KRJPRU';
	showEditModal(siType);
})

function showEditModal(siType){
	$.ajax({
    url: '/target/ipShipEditModal',
     data: {siType: siType},
    type: 'GET',
    dataType:"html",
    success: function(html){
      $("#ipShipModalContent").empty().append(html);
      $("#ipShipModal").modal('show');

    },
    error:function(){

    }
  });
}

$(document).on('click', '#saveShipInfo', function (){
	if($('#startPoint').val() == ''){
		alert('출발지를 입력해주세요.');
		$('#startPoint').focus();
		return false;
   }
   if($('#endPoint').val() == ''){
		alert('도착지를 입력해주세요');
		$('#endPoint').focus();
		return false;
   }
   if($('#distanceNm').val() == ''){
		alert('거리(해리)를 입력해주세요');
		$('#distanceNm').focus();
		return false;
   }
   if($('#distanceKm').val() == ''){
		alert('거리(Km)를 입력해주세요');
		$('#distanceKm').focus();
		return false;
   }
   if($('#ownerNm').val() == ''){
		alert('사업자를를 입력해주세요');
		$('#ownerNm').focus();
		return false;
   }
   if($('#shipNm').val() == ''){
		alert('선명을 입력해주세요');
		$('#shipNm').focus();
		return false;
   }

   if(confirm("저장하시겠습니까?")){
      contentFade("in");
      const formData = new FormData($("#shipInfoSave")[0]);
      $.ajax({
        type : 'POST',
        data : formData,
        url : "/target/saveShipInfo",
        processData: false,
        contentType: false,
        success : function(result) {
          alert("저장되었습니다.");
          contentFade("out");
		      location.reload();
        },
        error : function(xhr, status) {
          alert("저장에 실패하였습니다.")
          contentFade("out");
        }
      })
    }
})

$(document).on('click', '.shipInfoTr', function (){
	const siSeq = (Number($(this).find(".siSeq").val()));
	const siType = $(this).find(".siType").val();
	showViewModal(siSeq,siType);
})

$(document).on('click', '#viewTab', function (){
	const siSeq = (Number($(this).data('siseq')));
	const siType = $(this).data('sitype');
	showViewModal(siSeq,siType);
})

function showViewModal(siSeq,siType){
	$.ajax({
    url: '/target/shipInfoView',
    data: {
			siSeq: siSeq,
			siType: siType
			},
    type: 'GET',
    dataType:"html",
    success: function(html){
     $("#ipShipModalContent").empty().append(html);
     $("#ipShipModal").modal('show');
    },
    error:function(){

    }
  });
}

$(document).on('click', '#historyTab', function (){
	const siSeq = (Number($(this).data('siseq')));
	const siType = $(this).data('sitype');
	showHistory(siSeq,siType);
})

function showHistory(siSeq,siType){
	$.ajax({
    url: '/target/ipShipHistory',
    data: {
			siSeq: siSeq,
			siType: siType
			},
    type: 'GET',
    dataType:"html",
    success: function(html){
     $("#ipShipModalContent").empty().append(html);
     $("#ipShipModal").modal('show');
    },
    error:function(){

    }
  });
}

$(document).on('click', '#updateShipInfo', function (){
	if($('#startPointM').val() == ''){
		alert('출발지를 입력해주세요.');
		$('#startPointM').focus();
		return false;
   }
   if($('#endPointM').val() == ''){
		alert('도착지를 입력해주세요');
		$('#endPointM').focus();
		return false;
   }
   if($('#distanceNmM').val() == ''){
		alert('거리(해리)를 입력해주세요');
		$('#distanceNmM').focus();
		return false;
   }
   if($('#distanceKmM').val() == ''){
		alert('거리(Km)를 입력해주세요');
		$('#distanceKmM').focus();
		return false;
   }
   if($('#ownerNmM').val() == ''){
		alert('사업자를를 입력해주세요');
		$('#ownerNmM').focus();
		return false;
   }
   if($('#shipNmM').val() == ''){
		alert('선명을 입력해주세요');
		$('#shipNmM').focus();
		return false;
   }
   if(confirm("수정하시겠습니까?")){
      contentFade("in");
      const formData = new FormData($("#shipInfoUpdate")[0]);
      $.ajax({
        type : 'POST',
        data : formData,
        url : "/target/updateShipInfo",
        processData: false,
        contentType: false,
        success : function(result) {
          alert("수정되었습니다.");
          contentFade("out");
		  showViewModal(result.siSeq,result.siType);
        },
        error : function(xhr, status) {
          alert("수정에 실패하였습니다.")
          contentFade("out");
        }
      })
    }
})

$(document).on('click', '.historyInfoTr', function (){
	$(this).find('.hisChk').prop('checked',true) 
   if($(this).find('.hisChk').prop('checked')){
   		$('.hisChk').prop('checked',false);
   		$(this).find('.hisChk').prop('checked',true)
   }	
  $.ajax({
    url: '/target/ipShipHistoryView',
    data: {
			siSeq: Number($(this).find(".siSeq").val()),
			siType: $(this).find(".siType").val(),
			versionNo : Number($(this).find(".verNo").val())
			},
    type: 'GET',
    dataType:"json",
    success: function(data){
		$('#hHangro').val(data.startPoint+' / '+data.endPoint+'('+data.distanceNm+','+data.distanceKm+')');
		$('#hOwnerNm').val(data.ownerNm);
		$('#hShipNm').val(data.shipNm);
		$('#hShipWeight').val(data.shipWeight);
		$('#hSusong').val('여객 : '+data.passengerCnt+'명'+'\n'+'화물 : '+data.freightCnt+'TEU');
		$('#hOperationCnt').val(data.operationCnt);
		$('#hCloseYn').val(data.closeYn);
    },
    error:function(){

    }
  });
})

$(document).on('click', '#deleteShipInfo', function (){
	const siSeq = $('#vSiSeq').val();
	const siType = $('#vSiType').val();
   if(confirm("삭제하시겠습니까?")){
      contentFade("in");
      $.ajax({
	    type : 'POST',
	    url : "/target/deleteShipInfo",
	    data : JSON.stringify({siSeq:siSeq,siType:siType}),
	    contentType: 'application/json',
	    beforeSend: function (xhr){
	      xhr.setRequestHeader($("[name='_csrf_header']").val(), $("[name='_csrf']").val());
	    },
	    success : function(data) {
	      alert("삭제 처리되었습니다.");
	      location.reload();
	    },
	    error : function(xhr, status) {
	      alert("삭제 처리에 실패하였습니다");
	    }
	  })

	}
})

$(document).on('click', '#goExcel', function (){
	if(confirm("엑셀로 다운로드 하시겠습니까?")){
		$('input[name=excel]').val('Y');
		$('#searchFm').submit();
		$('input[name=excel]').val('');
	}else{
		false;
	}
})

$(document).on('click', '#btn-close', function (){
	location.reload();
})