<script>
//===================================================================*/
/* イベント
//===================================================================*/
//　F2 表示
$(document).on("click", ".popaf244x1Func2:not('.noevent')", function (e) {
  $('#popaf244x1Search').trigger('click');
});
//　F5 クリア
$(document).on("click", ".popaf244x1Func5:not('.noevent')", function (e) {
  af244x1.initial();
});
//　F9 決定
$(document).on("click", ".popaf244x1Func9:not('.noevent')", function (e) {
  $("#popaf244x1Select").trigger("click");
});
//　F12 メニュー/戻る
$(document).on("click", ".popaf244x1Func12:not('.noevent')", function (e) {
  $('#popaf244x1Close').trigger('popClose');
});

$(document).on("focus", "#af244x1Dummy", function(e) {
  $(".popaf244x1Func2").focus();
});

$(document).on("click", ".cmdAf244x1", function (e) {
  dspPopupAfm244x1($(this), "0");
});
$(document).on("click", ".cmdAf241x1_w", function (e) { {*伝票重複*}
  $(".popaf241x1Func5").addClass("noevent");
  dspPopupAfm241x1($(this), "2");
});

$(document).on("keyup", ".popAf244x1", function (e) {
  if (e.keyCode == 40) {
    dspPopupAfm244x1($(this), "0");
  }
});

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

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

    af244x1.initial();

    $("#af244x1ChkSsbat").val("1"); //1固定

    $("#af244x1Vseq").val($(grp).find(".vseq244").val()); //af244x用 同一伝票番号複数あった時

    common.setValue($("#af244x1OrgTitle"), common.getValue($("#topPrgTitle")));
    common.setValue($("#topPrgTitle"),"伝票番号検索");
    common.setValue($("#af244x1Pmode"),pmode);

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

    if (!stringUtil.isEmpty(common.getValue($("#af244x1Vseq")))) {
      $('#popaf244x1Search').trigger('click');
    }
}

function setPopupAfm244x1() {
  var grp = popup.closePopup("af244x1");
  var frmh = grp.closest(".header");

  var sel = $("#af244x1Detail .selectRow");
  var selVseq = common.getValue($("#af244x1Detail .selectRow .vseq"));
  var selViseq = common.getValue($("#af244x1Detail .selectRow .viseq"));
  var selMcd= common.getValue($("#af244x1Detail .selectRow .mcd"));
  var selMnm = common.getValue($("#af244x1Detail .selectRow .mnm"));
  var selSpktcd= common.getValue($("#af244x1Detail .selectRow .spktcd"));
  var selSpktnm = common.getValue($("#af244x1Detail .selectRow .spktnm"));
  var selSbat2 = common.getValue($("#af244x1Detail .selectRow .sbat2"));

  var vseq = grp.find(".vseq")[0];
  var viseq = grp.find(".viseq")[0];
  var mcd = grp.find(".mcd")[0];
  var mnm = grp.find(".mnm")[0];
  var spktcd = grp.find(".spktcd")[0];
  var spktnm = grp.find(".spktnm")[0];
  $(vseq).val(selVseq);
  $(viseq).val(selViseq);
  $(mcd).val(selMcd);
  $(mnm).val(selMnm);
  $(spktcd).val(selSpktcd);
  $(spktnm).val(selSpktnm);

  af244x1.initial();

  common.setValue($("#topPrgTitle"), common.getValue($("#af244x1OrgTitle")));
  $(vseq).trigger("afterSelectaf244x1");
}

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

  common.clearFormError(top);


  var errflg = false;

  if(stringUtil.isEmpty($("#af244x1Mcd").val())
    &&(stringUtil.isEmpty($("#af244x1Dat1s").val()) || stringUtil.isEmpty($("#af244x1Dat1e").val()))) {
    common.addError($("#af244x1Mcd"), "いずれかを入力してください。", null, true);
    common.addError($("#af244x1Dat1s"), "いずれかを入力してください。", null, true);
    common.addError($("#af244x1Dat1e"), "いずれかを入力してください。", null, true);
    errflg = true;
  }

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


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

  var sendData = {};

  sendData["popmode"] = "af244x1";
  sendData["mcd"] = $("#af244x1Mcd").val();
  sendData["vseq"] = $("#af244x1Vseq").val();
  sendData["dat1s"] = $("#af244x1Dat1s").val();
  sendData["dat1e"] = $("#af244x1Dat1e").val();
  sendData["spktcd"] = $("#af244x1Spktcd").val();
  if ($("#af244x1Pmode").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", "#popaf244x1Search", function (e, data) {
  var list = JSON.parse(data);

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

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

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

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

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

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

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

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

  var sel = $("#af244x1Detail .selectRow");
  var selSbat2 = common.getValue($("#af244x1Detail .selectRow .sbat2"));

  if (common.getValue($("#af244x1ChkSsbat")) == "1") {    {*締め済みチェック*}
    var sendData = {};
    sendData["sbat2"] = selSbat2;
    common.postData(
        "./chkSsbat2.php"
      , sendData
      , function (data) {
        if (data == "1") {
          alert("締め済み伝票のため修正できません。");
        } else {
          setPopupAfm244x1();
        }
      }
      , function (error) {
        alert(error.msg);
      }
    );
  } else {
    setPopupAfm244x1();
  }

});

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

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

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

  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-100 inputReadOnly vseq">' + n(d.vseq) + '</div>' +
      '     <div class="item wd-number inputReadOnly number cnt">' + n(d.cnt) + '</div>' +
      '     <div class="item wd-number inputReadOnly number amt">' + n(d.amt) + '</div>' +
      '     <div class="item wd-date inputReadOnly dat">' + n(d.dat) + '</div>' +
      '     <div class="item wd-S inputReadOnly mcd">' + n(d.mcd) + '</div>' +
      '     <div class="item wd-M inputReadOnly mnm longnm ">' + n(d.mnm) + '</div>' +
      '     <input type="hidden" class="spktcd" value="' + n(d.spktcd) + '"> '+
      '     <input type="hidden" class="spktnm" value="' + n(d.spktnm) + '"> '+
      '     <input type="hidden" class="sbat2" value="' + n(d.sbat2) + '"> '+
      '   </div> ' +
      '   <hr> ' +
      ' </div> ';

    return row;
  },

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

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

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

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

  },
}
</script>
