
$(function(){
  $("#dateSelectorDiv").datepicker({
    format: "yyyy-mm-dd",
    language: "ko"
  });
})

$(document).on('click', '#sendTab', function (){
  location.href = "/faRpt/faRptBoard?activeTab=send";
})
$(document).on('click', '#receiveTab', function (){
  location.href = "/faRpt/faRptBoard?activeTab=receive";
})
$(document).on('click', '#allTab', function (){
  location.href = "/faRpt/faRptBoard?activeTab=all";
})

$(document).on('click', '#addFaRptBtn', function (){
  getFaRptEditModal(null)
})
$(document).on('click', '#editFaRptBtn', function (){
  $("#faRptViewModal").modal('hide');
  getFaRptEditModal(Number($("#faRptViewBody").find("[name='faRptKey']").val()));
})

$(document).on('click', '#faRptAddBtn', function (){
  $("#faRptDiv").append("<input type='text' class='form-control' name='faRptInfos'>")
})

$(document).on('click', '#detailFaRptAddBtn', function (){
  const detailFaRptDiv = $("#detailFaRptDiv");
  detailFaRptDiv.append("<textarea type='text' name='detailFaRptInfos'></textarea>");
  const lastAppendTextarea = detailFaRptDiv.children()[detailFaRptDiv.children().length-1];
  $(lastAppendTextarea).summernote({
    lang:'ko-KR',
    height: 120,
    disableDragAndDrop: true,
    toolbar: [
      ['style', ['style']],
      ['font', ['bold', 'underline', 'clear']],
      ['color', ['color']],
      ['para', ['ul', 'ol', 'paragraph']],
      ['table', ['table']]
    ]
  });
})

$(document).on('click', '#saveFaRptBtn', function (){
  saveFaRpt('DST002')
})

$(document).on('click', '#saveTempBtn', function (){
  saveFaRpt('DST001')
})

$(document).on('click', '.faRptTr', function (){
  const chkBox = $(this).find(".rowChkBox");
  if(chkBox.length>0){
    $(".trChkBox").prop("checked", false);
    chkBox[0].checked = !chkBox[0].checked;
  }
  getFaRptViewModal(Number($(this).find(".faRptKey").val()));
})

$(document).on('click', '.apprvBtn', function (){
  $("#apprvFormFaRptKey").val($("#viewModalFaRptKey").val());
  $("#viewModalApprvValue").val($(this).attr("data-faRptstate"));
  if(confirm($(this).val()+"하시겠습니까?")){
    const formData = new FormData($("#apprvForm")[0]);
    contentFade("in")
    $.ajax({
      type : 'POST',
      data : formData,
      url : "/faRpt/faRptStateChange",
      processData: false,
      contentType: false,
      beforeSend: function (xhr){
        xhr.setRequestHeader($("[name='_csrf_header']").val(), $("[name='_csrf']").val());
      },
      success : function(result) {
        alert("저장되었습니다")
        getFaRptViewModal(result);
        contentFade("out");
      },
      error : function(xhr, status) {
        alert("저장에 실패하였습니다.");
        contentFade("out");
      }
    })
  }
})

function getFaRptViewModal(faRptKey){
  $.ajax({
    url: '/faRpt/faRptViewModal',
    data: {faRptKey: faRptKey},
    type: 'GET',
    dataType:"html",
    success: function(html){
      $("#faRptViewBody").empty().append(html)
      $("#faRptViewModal").modal('show');
    },
    error:function(){

    }
  });
}

function getFaRptEditModal(faRptKey){
  $.ajax({
    url: '/faRpt/faRptEditModal',
    data: {faRptKey: faRptKey},
    type: 'GET',
    dataType:"html",
    success: function(html){
      $("#faRptEditModalContent").empty().append(html)
      $("#faRptEditModal").modal('show');
      $("#faRptDt").datepicker({
        format: "yyyy-mm-dd",
        language: "ko"
      });
      $("#content").summernote({
        lang:'ko-KR',
        height: 360,
        disableDragAndDrop: true,
        toolbar: [
          ['style', ['style']],
          ['font', ['bold', 'underline', 'clear']],
          ['color', ['color']],
          ['para', ['ul', 'ol', 'paragraph']],
          ['table', ['table']]
        ]
      });
      setUploadDiv();
    },
    error:function(){

    }
  });
}
function saveFaRpt(faRptState){
  if(contentCheck()){
    if(confirm("저장하시겠습니까?")){
      $("#faRptState").val(faRptState);
      contentFade("in");
      const formData = new FormData($("#faRptEditForm")[0]);
      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 : "/faRpt/saveFaRpt",
        processData: false,
        contentType: false,
        success : function(result) {
          alert("저장되었습니다.");
          contentFade("out");
          $("#faRptEditModal").modal('hide');
          getFaRptViewModal(result);
        },
        error : function(xhr, status) {
          alert("저장에 실패하였습니다.")
          contentFade("out");
        }
      })
    }
  }
}


function contentCheck(){
  let flag = true;
  if(!$("#contentTitle").val()){
    alert("제목을 입력해주세요.")
    flag = false;
  }
  flag = fileCheck(flag, files);
  return flag;
}