<script>
//===================================================================*/
/* イベント
//===================================================================*/
//　F8 CXV
$(document).on("click", ".popaf02mFunc8:not('.noevent')", function (e) {
  do_CSV();
});
//　F9 Excel
$(document).on("click", ".popaf02mFunc9:not('.noevent')", function (e) {
  do_Excel('E');
});
//　F10 確認用Excel
$(document).on("click", ".popaf02mFunc10:not('.noevent')", function (e) {
  do_Excel('K');
});
//　F12 メニュー/戻る
$(document).on("click", ".popaf02mFunc12:not('.noevent')", function (e) {
  $('#af02mClose').trigger('popClose');
});

//表示
$(document).on("click", ".popup-af02m", function (e) {
    popup.openPopup("af02m", "");
    af02m.initial();
    $("#af02m").focus();
});


function do_Excel(cls) {
  var errflg = false;
  let msg = "";

  var format = common.getValue($('input:radio[name="af02mFormat"]:checked'));
  console.log(format);

  if(errflg) {
    alert("エラーがあります");
  } else if ( window.confirm('【商品マスタExcel出力】をします。よろしいですか？') ){
    let param2 = { "method": "GetWseqMjan" };

    var excelid = "";
    if(cls == 'K') {
      excelid = "AF02M2";
    } else {
      if(format == '0') { //全て
        excelid = "AF02M";
      } else {
        excelid = "AF02M3";
      }
    }

    $.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}"
                + "&KTKTCD=" + "{$_SYSTEM_NAME_EN_LGKTKTCD}"
                + "&DBNO=" + "{$_SYSTEM_NO}"
                + "&JOKEN1=" + $("#af02mJans").val()
                + "&JOKEN2=" + $("#af02mJane").val()
                + "&JOKEN3=" + $("#af02mMcds").val()
                + "&JOKEN4=" + $("#af02mMcde").val()
                + "&JOKEN10=" + $("#af02mGdcls9").val();
        window.open(wurl, excelid+JSON.parse(data.data));
      }
    });
  }

}

function do_CSV() {
  var errflg = false;
  let msg = "";

  if(errflg) {
    alert("エラーがあります");
  } else if ( window.confirm('【商品マスタCSV出力】をします。よろしいですか？') ){
    let param2 = { "method": "GetWseqMjan" };

    var csvid = "AFM02C";

    $.ajax({
          type: "GET",
          url: "../../api/getWseq.php",
          data: param2,
          dataType: "json",
          scriptCharset: 'utf-8',
          success: function(data, status, xhr) {
            var wurl = "{$_REPORT_URL}" + "AF999C?WSEQ=" + JSON.parse(data.data)
                  + "&CSVID=" + csvid 
                  + "&CHGCD=" + "{$_SYSTEM_NAME_EN_CHGCD}"
                  + "&KTKTCD=" + "{$_SYSTEM_NAME_EN_LGKTKTCD}"
                  + "&VEND=" + "{$_SYSTEM_NAME_EN_VEND}"
                  + "&ACCD=" + "{$_SYSTEM_NAME_EN_ACCD}"
                  + "&DBNO=" + "{$_SYSTEM_NO}"
                  + "&JOKEN1=" + $("#af02mJans").val()
                  + "&JOKEN2=" + $("#af02mJane").val()
                  + "&JOKEN3=" + $("#af02mMcds").val()
                  + "&JOKEN4=" + $("#af02mMcde").val()
                  + "&JOKEN10=" + $("#af02mGdcls9").val();
          window.open(wurl, csvid+JSON.parse(data.data));
        }
    });
  }
}

$(document).on("focus","#af02mDummy", function(e) {
  $(".popaf02mFunc9").focus();
});

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

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

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

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