<script>
//===================================================================*/
/* イベント
//===================================================================*/
//　F9 Excel
$(document).on("click", ".popaf032mFunc9:not('.noevent')", function (e) {
  $('#af032mExcel').trigger('printExcel');
});
//　F12 メニュー/戻る
$(document).on("click", ".popaf032mFunc12:not('.noevent')", function (e) {
  $('#af032mClose').trigger('popClose');
});

//表示
$(document).on("click", ".popup-af032m", function (e) {

    popup.openPopup("af032m", "");
    af032m.initial();
    $("#af032m").focus();
});

$("#af032mExcel").on("printExcel", function (e) {
  var errflg = false;
  common.clearFormError(".af032m")

  if(!stringUtil.isEmpty($("#af032mCuscds").val()) && !stringUtil.isEmpty($("#af032mCuscde").val())) {
    if($("#af032mCuscds").val() > $("#af032mCuscde").val()) {
      errflg = true;
      common.addError($("#af032mCuscds"), "正しい範囲を入力してください。", null, true);
      common.addError($("#af032mCuscde"), "正しい範囲を入力してください。", null, true);
    }
  }
  if(!stringUtil.isEmpty($("#af032mDats").val()) && !stringUtil.isEmpty($("#af032mDate").val())) {
    let wdats = dateUtil.getYYYYMMDDDate($("#af032mDats").val());
    let wdate = dateUtil.getYYYYMMDDDate($("#af032mDate").val());
    if(wdats > wdate) {
      common.addError($("#af032mDate"), "正しい範囲を入力してください。", null, true);
      common.addError($("#af032mDats"), "正しい範囲を入力してください。", null, true);
      errflg = true;
    }
  }

  if(errflg) {
    alert("エラーがあります");
  } else if( window.confirm('【店舗マスタExcel出力】をします。よろしいですか？') ){
    let param2 = { "method": "GetWseqMjan" };
    let excelid = "AF032M";
    $.ajax({
      type: "GET",
      url: "../../api/getWseq.php",
      data: param2,
      dataType: "json",
      scriptCharset: 'utf-8',
      success: function(data, status, xhr) {
        var wurl = "{$_REPORT_URL}" + "AF999JE?WSEQ=" + JSON.parse(data.data)
                + "&IREP=" + excelid
                + "&CHGCD=" + "{$_SYSTEM_NAME_EN_CHGCD}"
                + "&ACCD=" + "{$_SYSTEM_NAME_EN_ACCD}"
                + "&DBNO=" + "{$_SYSTEM_NO}"
                + "&JOKEN1=" + $("#af032mCucd").val()
                + "&JOKEN2=" + $("#af032mCuscds").val()
                + "&JOKEN3=" + $("#af032mCuscde").val()
                + "&JOKEN4=" + $("#af032mCshcd").val()
                + "&JOKEN5=" + common.getValue($("#af032mSpcat"))
                + "&JOKEN6=" + $("#af032mUdats").val()
                + "&JOKEN7=" + $("#af032mUdate").val();
        window.open(wurl, excelid+JSON.parse(data.data));
      }
    });
  }

});

$(document).on("change","#af032mCuscds, #af032mCuscde", function(e) {
  common.clearError("#af032mCuscds");
  common.clearError("#af032mCuscde");
});

//店舗取得用に、それぞれのgrpにcucdを埋める
$(document).on("change","#af032mCucd",function(e){
  var frm = $(".af032m .header_popaf032m");
  var me = $(this).val();
  var grp = $(this).closest(".cucdGrp");
  frm.find(".memocucd").val(me);
  frm.find(".memocunm").val(grp.find(".cunm").val());
});
$(document).on("afterChangeCucd","#af032mCucd",function(e) {
  var frm = $(".af032m .header_popaf032m");
  var me = $(this).val();
  var grp = $(this).closest(".cucdGrp");
  frm.find(".memocucd").val(me);
  frm.find(".memocunm").val(grp.find(".cunm").val());
});
$(document).on("afterSelectAfm03x","#af032mCucd",function(e) {
  var frm = $(".af032m .header_popaf032m");
  var me = $(this).val();
  var grp = $(this).closest(".cucdGrp");
  frm.find(".memocucd").val(me);
  frm.find(".memocunm").val(grp.find(".cunm").val());
});
//得意先をクリアしたら、店舗もクリア
$(document).on("change","#af032mCucd",function(e) {
  if(stringUtil.isEmpty($(this).val())) {
    var frm = $(".af032m .header_popaf032m");
    frm.find(".memocucd").val("");
    frm.find(".memocucd").trigger("change");
  }
});

$(document).on("focus","#af032mDummy", function(e) {
  $(".popaf032mFunc9").focus();
});

$("#af032mClose").on("popClose", function (e) {
  if (e.bubbles == false) {
    return false;
  }
  af032m.initial();
  af032m.close();
});

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

  initial: function () {
    common.clearInputData($(".af032m")); //検索画面データのクリア
    $("#af032mPattern0").show();

    $("#af032mCucd").focus();
    $('input:radio[name="af032mFlg"]').val(['E']);
  },
}
</script>
