let files = [];

$(document).on('click', '#addPartWork', function (){
   $.ajax({
    url: '/target/partWorkEditModal',
    type: 'GET',
    dataType:"html",
    success: function(html){
      $("#partWorkEditModalEditModalContent").empty().append(html);
      $("#partWorkEditModal").modal('show');
      setUploadDiv();
      $("#workDt").datepicker({
                format: "yyyy-mm-dd",
                language: "ko"
      });
      $("#workInfo").summernote({
        lang:'ko-KR',
        height: 350,
        disableDragAndDrop: true,
        toolbar: [
          ['style', ['style']],
          ['font', ['bold', 'underline', 'clear']],
          ['color', ['color']],
          ['para', ['ul', 'ol', 'paragraph']],
          ['table', ['table']]
        ]
      });
    },
    error:function(){

    }
  });
})

$(document).on('click', '#saveBtn', function (){
   if($('#piSeq').val() == ''){
		alert('터미널명을 선택해주세요');
		$('#piSeq').focus();
		return false;
   }
   if($('input:radio[name="workType"]:checked').length < 1){
		alert('종류를 선택해주세요');
		return false;
	}
   if(confirm("저장하시겠습니까?")){
	  $('#saveYn').val('Y');
      contentFade("in");
      savePartWork();
    }
})

$(document).on('click', '#temporarySaveBtn', function (){
   if($('#piSeq').val() == ''){
		alert('터미널명을 선택해주세요');
		$('#piSeq').focus();
		return false;
   }
   if($('input:radio[name="workType"]:checked').length < 1){
		alert('종류를 선택해주세요');
		return false;
	}
   if(confirm("임시 저장하시겠습니까?")){
	  $('#saveYn').val('N');
      contentFade("in");
      savePartWork();
    }
})

function savePartWork(){
	const formData = new FormData($("#partWorkSaveFm")[0]);
      for(const file of files) {
        if(!file.isDelete)
          formData.append('uploadFiles', file, file.name);
      }
      $.ajax({
        type : 'POST',
        data : formData,
        url : "/target/savePartWork",
        processData: false,
        contentType: false,
        success : function(result) {
          alert("저장되었습니다.");
          contentFade("out");
		  location.reload();
        },
        error : function(xhr, status) {
          alert("저장에 실패하였습니다.")
          contentFade("out");
        }
      })
}

$(document).on('click', '.partWorkTr', function (){
	const pwSeq = (Number($(this).find(".pwSeq").val()));
	const piSeq = (Number($(this).find(".piSeq").val()));
	showModal(pwSeq,piSeq);
})

function showModal(pwSeq,piSeq){
	$.ajax({
    url: '/target/partWorkView',
    data: {
			pwSeq: pwSeq,
			piSeq: piSeq
			},
    type: 'GET',
    dataType:"html",
    success: function(html){
      $("#partWorkEditModalEditModalContent").empty().append(html);
      $("#partWorkEditModal").modal('show');
      setUploadDiv();
      $("#mWorkDt").datepicker({
                format: "yyyy-mm-dd",
                language: "ko"
      });
      $("#mWorkInfo").summernote({
        lang:'ko-KR',
        height: 350,
        disableDragAndDrop: true,
        toolbar: [
          ['style', ['style']],
          ['font', ['bold', 'underline', 'clear']],
          ['color', ['color']],
          ['para', ['ul', 'ol', 'paragraph']],
          ['table', ['table']]
        ]
      });
    },
    error:function(){

    }
  });
}

$(document).on('click', '#updateBtn', function (){
   if(confirm("수정하시겠습니까?")){
      contentFade("in");
      const formData = new FormData($("#partWorkUpdateFm")[0]);
      formData.append('saveYn','Y');
      for(const file of files) {
        if(!file.isDelete)
          formData.append('uploadFiles', file, file.name);
      }
      $(".text-decoration-line-through").each(function (idx, el){
        formData.append('fileSeq', $(el).attr("data-fileseq"));
      })
      $.ajax({
        type : 'POST',
        data : formData,
        url : "/target/updatePartWork",
        processData: false,
        contentType: false,
        success : function(data) {
          alert("수정되었습니다.");
          contentFade("out");
          showModal(data.pwSeq,data.piSeq);
        },
        error : function(xhr, status) {
          alert("수정에 실패하였습니다.")
          contentFade("out");
        }
      })
    }
})

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

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