<script>
//===================================================================*/
/* イベント
//===================================================================*/
//　F2 表示
$(document).on("click", ".popaf241x1Func2:not('.noevent')", function (e) {
  $('#popaf241x1Search').trigger('click');
});
//　F5 クリア
$(document).on("click", ".popaf241x1Func5:not('.noevent')", function (e) {
  af241x1.initial();
});
//　F9 決定
$(document).on("click", ".popaf241x1Func9:not('.noevent')", function (e) {
  $("#popaf241x1Select").trigger("click");
});
//　F12 メニュー/戻る
$(document).on("click", ".popaf241x1Func12:not('.noevent')", function (e) {
  $('#popaf241x1Close').trigger('popClose');
});

$(document).on("focus", "#af241x1Dummy", function(e) {
  $(".popaf241x1Func2").focus();
});

$(document).on("click", ".cmdAf241x1", function (e) { {*伝票検索*}
  dspPopupAfm241x1($(this), "0");
});
$(document).on("click", ".cmdAf241x1_list", function (e) { {*伝票一覧*}
  dspPopupAfm241x1($(this), "1");
});
$(document).on("click", ".cmdAf241x1_w", function (e) { {*伝票重複*}
  $(".popaf241x1Func5").addClass("noevent");
  dspPopupAfm241x1($(this), "2");
});

$(document).on("keyup", ".popAf241x1", function (e) { {*伝票検索*}
  if (e.keyCode == 40) {
    dspPopupAfm241x1($(this), "0");
  }
});

function dspPopupAfm241x1($me, pmode) { {*pmode:0=検索 1=一覧 2=伝票重複*}
    var grp = $me.closest(".af241x1popup");
    var frmh = grp.closest("#headerTop");

    popup.openPopup("af241x1", $(grp));

    af241x1.initial();

    if (pmode == "0" || pmode == "2") {
      $("#af241x1Cucd").val($(grp).find(".cucd").val());
      $("#af241x1Cunm").val($(grp).find(".cunm").val());
      $("#af241x1Cuscd").val($(grp).find(".cuscd").val());
      $("#af241x1Spnm").val($(grp).find(".spnm").val());
      $("#af241x1Vseq").val($(grp).find(".vseq241").val()); //af241x用 同一伝票番号複数あった時
    }
    if (pmode == "2") {
      $("#af241x1Sts").val($(grp).find(".sts").val());
      $("#af241x1Sbat").val($(grp).find(".sbat").val());
    }

    common.setValue($("#af241x1OrgTitle"), common.getValue($("#topPrgTitle")));
    if (pmode == "0") {
      common.setValue($("#topPrgTitle"),"伝票番号検索");
    } else {
      common.setValue($("#topPrgTitle"),"伝票一覧");
    }

    common.setValue($("#af241x1Pmode"), pmode)

    common.setDetailHeight($("#af241x1Detail"));

    {*返品日初期設定*}
    if (pmode != "2") { {*伝票重複じゃない場合*}
      var bt = new Date("{$_SYSTEM_NAME_EN_SDDAY}");
      bt.setDate(bt.getDate() - 6); // 入力日時の6日前
      var y = bt.getFullYear();
      var m = ("00" + (bt.getMonth()+1)).slice(-2);
      var d = ("00" + bt.getDate()).slice(-2);
      var result = y + "/" + m + "/" + d;
      common.setValue($("#af241x1Dat1s"),result);
      common.setValue($("#af241x1Dat1e"),"{$_SYSTEM_NAME_EN_SDDAY}");
    }

    $('#popaf241x1Search').trigger('click');
}

$(document).on("click", "#popaf241x1Search", function (e) {
  var me = this;
  // 読込
  var top = $(this).closest(".af241x1");

  common.clearFormError(top);


  var errflg = false;

  {*必須*}
  top.find(".required").each(function (index) {
    var el = $(this);
    // 必須チェック
    if(stringUtil.isEmpty(el.val())) {
      common.addError(el, "必須です", null, true);
      errflg = true;
    }
  });

  if(!stringUtil.isEmpty($("#af241x1Dat1s").val()) && !stringUtil.isEmpty($("#af241x1Dat1e").val())) {
    let wadats = dateUtil.getYYYYMMDDDate($("#af241x1Dat1s").val());
    let wadate = dateUtil.getYYYYMMDDDate($("#af241x1Dat1e").val());

    if(wadats > wadate) {
      common.addError($("#af241x1Dat1e"), "正しい範囲を入力してください。", null, true);
      common.addError($("#af241x1Dat1s"), "正しい範囲を入力してください。", null, true);
      errflg = true;
    }
    if (!errflg) {
      if (wadate > dateUtil.getAddMonthDate($("#af241x1Dat1s").val(), 1)) {
        common.addError($("#af241x1Dat1s"), "返品日範囲は1ヶ月以内を指定してください。", null, true);
        errflg = true;
      }
    }
  }

  if(errflg) {
    alert("エラーがあります");
    return false;
  }


  af241x1.deldetail();  //明細削除

  var sendData = {};

  sendData["popmode"] = "af241x1";
  sendData["cucd"] = $("#af241x1Cucd").val();
  sendData["cuscd"] = $("#af241x1Cuscd").val();
  sendData["vseq"] = $("#af241x1Vseq").val();
  sendData["dat1s"] = $("#af241x1Dat1s").val();
  sendData["dat1e"] = $("#af241x1Dat1e").val();
  sendData["spktcd"] = $("#af241x1Spktcd").val();
  if ($("#af241x1Pmode").val() == "2") {  {*重複*}
    sendData["pmode"] = "W";
  } else {
    sendData["pmode"] = "";
  }

  setComplianceData("search", sendData);

  var me = $(this);
  common.postData(
    common.getApiUrl() + "getPopupSearch.php"
    , sendData
    , function (data, options) { $(me).trigger("regSuccess", data); }
    , function (error, options) { $(me).trigger("regError", error); }
  );

});

$(document).on("regSuccess", "#popaf241x1Search", function (e, data) {
  var list = JSON.parse(data);

  if (!(list != null)) {
    return false;
  }

  $("#af241x1Detail .detail").remove();
  var el = "";
  for (var i = 0; i < list.length; i++) {

    if(i == 0) {
      $("#af241x1Tamt").val(common.addFigure(list[i].t_amt));
      $("#af241x1Cnt").val(common.addFigure(list[i].t_vseq));
    }

    el += af241x1.appendRow(list[i]);
  }
  $("#af241x1Detail").append(el);

  common.setDetailHeight($("#af241x1Detail"));

  $("#af241x1Detail .detail").first().find("a").focus();
});
$(document).on("regError", "#popaf241x1Search", function (e, data) {
  alert(data.msg);
});

$(document).on("dblclick", "#af241x1Detail .selectRow", function (e) {
  $("#popaf241x1Select").trigger("click");
});

$(document).on("click", "#popaf241x1Select", function (e) {
  if (e.bubbles == false) {
    return false;
  }

  var sel = $("#af241x1Detail .selectRow");
  var selVseq = common.getValue($("#af241x1Detail .selectRow .vseq"));
  var selViseq = common.getValue($("#af241x1Detail .selectRow .viseq"));
  var selCuiseq = common.getValue($("#af241x1Detail .selectRow .cuiseq"));
  var selCucd = common.getValue($("#af241x1Detail .selectRow .cucd"));
  var selCuscd = common.getValue($("#af241x1Detail .selectRow .cuscd"));
  var selCunm = common.getValue($("#af241x1Detail .selectRow .cunm"));
  var selSpnm = common.getValue($("#af241x1Detail .selectRow .spnm"));

  var grp = popup.closePopup("af241x1");
  var frmh = grp.closest(".header");

  var vseq = grp.find(".vseq")[0];
  var viseq = grp.find(".viseq")[0];
  var cuiseq = grp.find(".cuiseq")[0];
  var cucd = grp.find(".cucd")[0];
  var cuscd = grp.find(".cuscd")[0];
  var cunm = grp.find(".cunm")[0];
  var spnm = grp.find(".spnm")[0];
  $(vseq).val(selVseq);
  $(viseq).val(selViseq);
  $(cuiseq).val(selCuiseq);
  $(cucd).val(selCucd);
  $(cuscd).val(selCuscd);
  $(cunm).val(selCunm);
  $(spnm).val(selSpnm);

  af241x1.initial();

  common.setValue($("#topPrgTitle"), common.getValue($("#af241x1OrgTitle")));
  $(vseq).trigger("afterSelectaf241x1");
});

$("#popaf241x1Close").on("popClose", function (e) {
  if (e.bubbles == false) {
    return false;
  }
  common.setValue($("#topPrgTitle"), common.getValue($("#af241x1OrgTitle")));
  af241x1.initial();
  af241x1.close();
});

$(document).on("keydown", "#af241x1Detail .selectRow", function (e) {
  if (e.keyCode == 32) {
    $("#popaf241x1Select").trigger("click");
    return false;
  }
});

var af241x1 = {
  close: function () {
    this.initial();
    popup.closePopup("af241x1");
  },

  appendRow: function (d) {
    var n = stringUtil.nvl;
    var row =
      ' <div class="col wp-100 detail"> ' +
      '   <div class="col wp-100"> ' +
      '     <a href="#" class="line"> </a> ' +
      '     <div class="item wd-S inputReadOnly cucd">' + n(d.cucd) + '</div>' +
      '     <div class="item wd-S inputReadOnly cuscd">' + n(d.cuscd) + '</div>' +
      '     <div class="item wd-M inputReadOnly cunm longnm ">' + n(d.cunm) + '</div>' +
      '     <div class="item wd-M inputReadOnly spnm longnm ">' + n(d.spnm) + '</div>' +
      '     <div class="item wd-SS inputReadOnly spktcd">' + n(d.spktcd) + '</div>' +
      '     <div class="item wd-M inputReadOnly spnm longnm">' + n(d.spnm) + '</div>' +
      '     <div class="item wd-100 inputReadOnly vseq">' + n(d.vseq) + '</div>' +
      '     <div class="item wd-date inputReadOnly dat1">' + n(d.dat1) + '</div>' +
      '     <div class="item wd-date inputReadOnly dat2">' + n(d.dat2) + '</div>' +
      '     <div class="item wd-number inputReadOnly amt number">' + common.addFigure(n(d.amt)) + '</div>' +
      '     <div class="item wd-S inputReadOnly ksts blue">' + n(d.ksts) + '</div>' +
      '     <input type="hidden" class="viseq" value="' + n(d.viseq) + '"> '+
      '     <input type="hidden" class="cuiseq" value="' + n(d.cuiseq) + '"> '+
      '   </div> ' +
      '   <hr> ' +
      ' </div> ';

    return row;
  },

  deldetail: function () {
    var el = document.getElementById("af241x1Detail");
    var n = el.cloneNode(false);
    el.parentNode.replaceChild(n, el);
  },

  initial: function () {
    var main = $(".af241x1").find(".header_popaf241x1");
    common.clearInputData(main);
    common.clearAllError();

    var el = document.getElementById("af241x1Detail");
    var n = el.cloneNode(false);
    el.parentNode.replaceChild(n, el);

    $("#af241x1Dat1s").focus();

  },
}
</script>
