<script>
//===================================================================*/
/* イベント
//===================================================================*/
//　F2 表示
$(document).on("click", ".popaf203x2Func2:not('.noevent')", function (e) {
  $('#popaf203x2Search').trigger('click');
});
//　F5 クリア
$(document).on("click", ".popaf203x2Func5:not('.noevent')", function (e) {
  af203x2.initial();
});
//　F9 決定
$(document).on("click", ".popaf203x2Func9:not('.noevent')", function (e) {
  $("#popaf203x2Select").trigger("click");
});
//　F12 メニュー/戻る
$(document).on("click", ".popaf203x2Func12:not('.noevent')", function (e) {
  $('#popaf203x2Close').trigger('popClose');
});

$(document).on("focus", "#af203x2Dummy", function(e) {
  $(".popaf203x2Func2").focus();
});

$(document).on("click", ".cmdAf203x2", function (e) {
  dspPopupAfm2002x($(this));
});

$(document).on("keyup", ".popAf203x2", function (e) {
  if (e.keyCode == 40) {
    dspPopupAfm2002x($(this));
  }
});

function dspPopupAfm2002x($me) {
    //var grp = $(".contents.af2000x .header .af203x2popup");
    var grp = $me.closest(".af203x2popup");
    var frmh = grp.closest(".header");

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

    af203x2.initial();

    $("#af203x2Cucd").val(frmh.find(".cucd").val());
    $("#af203x2Cuscd").val(frmh.find(".cuscd").val());
    $("#af203x2Cunm").val(frmh.find(".cunm").val());
    $("#af203x2Spnm").val(frmh.find(".spnm").val());
    $("#af203x2Vseq").val(frmh.find(".vseq").val());


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

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


	  $('#popaf203x2Search').trigger('click');
    setTimeout(function(){
        $("#af203x2Cucd").focus();
    },100);

}


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

  if(!stringUtil.isEmpty($("#af203x2Jdats").val()) && !stringUtil.isEmpty($("#af203x2Jdate").val())) {
    let wjdats = dateUtil.getYYYYMMDDDate($("#af203x2Jdats").val());
    let wjdate = dateUtil.getYYYYMMDDDate($("#af203x2Jdate").val());

    if(wjdats > wjdate) {
      common.addError($("#af203x2Jdate"), "正しい範囲を入力してください。", null, true);
      common.addError($("#af203x2Jdats"), "正しい範囲を入力してください。", null, true);
      alert("エラーがあります");
      return false;
    }
  }


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

  var sendData = {};

  sendData["popmode"] = "af203x2";
  sendData["cucd"] = $("#af203x2Cucd").val();
  sendData["cuscd"] = $("#af203x2Cuscd").val();
  sendData["vseq"] = $("#af203x2Vseq").val();
  sendData["jdats"] = $("#af203x2Jdats").val();
  sendData["jdate"] = $("#af203x2Jdate").val();
  sendData["chgcd"] = $("#af203x2Chgcd").val();
  sendData["spktcd"] = $("#af203x2Spktcd").val();
  sendData["mode"] = $("#af203x2Mode").val();

  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", "#popaf203x2Search", function (e, data) {
  var list = JSON.parse(data);

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

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

    if(i == 0) {
      $("#af203x2Tamt").val(common.addFigure(list[i].tamt));
      $("#af203x2Tamt2").val(common.addFigure(list[i].tamt2));
      $("#af203x2Cnt").val(common.addFigure(list[i].cnt));
    }

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

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

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

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

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

  var sel = $("#af203x2Detail .selectRow");
  /*
  var selSadat = $("#af203x2Detail .selectRow .sadat").val();
  var selMcd = $("#af203x2Detail .selectRow .mcd").text();
  var selMnm = $("#af203x2Detail .selectRow .mnm").text();
  */
  var selVseq = $("#af203x2Detail .selectRow .vseq").text();
  var selCucd = $("#af203x2Detail .selectRow .cucd").text();
  var selCuscd = $("#af203x2Detail .selectRow .cuscd").text();
  var selCuiseq = $("#af203x2Detail .selectRow .cuiseq").val();

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

  if (grp.hasClass("af222x1popup")) {  //売上入力
    var vseq = grp.find(".cvseq")[0];
  } else {
    var vseq = grp.find(".vseq")[0];
  }
  var cucd = frmh.find(".cucd")[0];
  var cuscd = frmh.find(".cuscd")[0];
  var cuiseq = frmh.find(".cuiseq")[0];
  $(vseq).val(selVseq);
  $(cucd).val(selCucd);
  $(cuscd).val(selCuscd);
  $(cuiseq).val(selCuiseq);

  af203x2.initial();

  common.setValue($("#topPrgTitle"), common.getValue($("#af203x2OrgTitle")));
  $(vseq).trigger("afterSelectaf203x2");
  $(cucd).trigger("afterSelectaf203x2");
});

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

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

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

  appendRow: function (d) {
    var n = stringUtil.nvl;
    var row =
      ' <div class="col wp-100 detail"> ' +
      '   <hr> ' +
      '   <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 longnm cunm">' + n(d.cunm) + '</div>' +
      '     <div class="item wd-190 inputReadOnly longnm spnm">' + n(d.spnm) + '</div>' +
      '     <div class="item wd-090 inputReadOnly vseq">' + n(d.vseq) + '</div>' +
      '     <div class="item wd-050 inputReadOnly gdflgnm center">' + n(d.gdflgnm) + '</div>' +
      '     <div class="item wd-S inputReadOnly jdat" style="width:72px;">' + n(d.jdat) + '</div>' +
      '     <div class="item wd-S inputReadOnly climit" style="width:72px;">' + n(d.climit) + '</div>' +
      '     <div class="item wd-S inputReadOnly amt number">' + common.addFigure(n(d.amt)) + '</div>' +
      '     <div class="item wd-S inputReadOnly amt2 number">' + common.addFigure(n(d.amt2)) + '</div>' +
      '     <div class="item wd-100 inputReadOnly chgnm longnm">' + n(d.chgnm) + '</div>' +
      '     <div class="item wd-100 inputReadOnly upchgnm longnm">' + n(d.upchgnm) + '</div>' +
      '     <div class="item wd-030 inputReadOnly spktcd">' + n(d.spktcd) + '</div>' +
      '     <input type="hidden" class="cuiseq" value="' + n(d.cuiseq) + '"> '+
      '   </div> ' +
      ' </div> ';

    return row;
  },

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

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

    //common.setValue(main.find(".cls1"),'0');
    common.setValue(main.find(".cls1"),'ALL'); {*22.10.17 区分初期表示全てに変更 by SE-2464*}

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

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

  },
}
</script>
