<script>
//===================================================================*/
/* イベント
//===================================================================*/
//　F2 表示
$(document).on("click", ".popaf215x1Func2:not('.noevent')", function (e) {
  $('#popaf215x1Search').trigger('click');
});
//　F5 クリア
$(document).on("click", ".popaf215x1Func5:not('.noevent')", function (e) {
  af215x1.initial();
});
//　F9 決定
$(document).on("click", ".popaf215x1Func9:not('.noevent')", function (e) {
  $("#popaf215x1Select").trigger("click");
});
//　F12 メニュー/戻る
$(document).on("click", ".popaf215x1Func12:not('.noevent')", function (e) {
  $('#popaf215x1Close').trigger('popClose');
});

$(document).on("click", ".cmdAf215x1", function (e) {
  dspPopupAfm2003x($(this));
});

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


$(document).on("click", ".popup-af215x1", function (e) {
    var grp = $(".contents.af215x .header .af215x1popup");

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

    af215x1.initial();

    $("#af215x1Cucd").val(grp.find(".cucd").val());
    $("#af215x1Cuscd").val(grp.find(".cuscd").val());
    $("#af215x1Wseq").val(grp.find(".wseq").val());


    common.setValue($("#af215x1OrgTitle"), common.getValue($("#topPrgTitle")));
    common.setValue($("#topPrgTitle"),"複写店舗選択");

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


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

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

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

  var sendData = {};
  sendData["popmode"] = "af215x1";
  sendData["cucd"] = $("#af215x1Cucd").val();
  sendData["cuscd"] = $("#af215x1Cuscd").val();
  sendData["wseq"] = $("#af215x1Wseq").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", "#popaf215x1Search", function (e, data) {
  var list = JSON.parse(data);

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

  $("#af215x1Detail .detail").remove();
  var el = "";
  for (var i = 0; i < list.length; i++) {
    el += af215x1.appendRow(list[i]);
  }
  $("#af215x1Detail").append(el);

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

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

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

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

  var sendData = {};
  var header = {};
  header["cucd"] = common.getValue($("#af215x1Cucd"));
  header["cuscd"] = common.getValue($("#af215x1Cuscd"));
  header["wseq"] = common.getValue($("#af215x1Wseq"));

  var detailList = [];
  var detail = $("#af215x1Detail .detail");
  var cnt = 0;
  for(i = 0 ; i < detail.length ; i++) {
    var $me = $(detail[i]);
    if(!stringUtil.isEmpty(common.getValue($me.find(".cuscd"))) && $me.find(".copychk").prop("checked")) {
      if(!stringUtil.isEmpty(common.getValue($me.find(".vseq")))) {
        var buf = {};
        buf["cucd"] = common.getValue($me.find(".cucd"));
        buf["cuscd"] = common.getValue($me.find(".cuscd"));
        buf["vseq"] = common.getValue($me.find(".vseq"));
        detailList[cnt] = buf;
        cnt = cnt + 1;
      }
    }
  }

  if(cnt > 1) {
  
    sendData["header"] = header;
    sendData["data"] = detailList;

    var me = $(this);
    common.postData(
      "./chkCopyAfw81.php" //元画面の物を呼ぶ
      , sendData
      , function (data) {
        // 特殊処理があれば
        var obj = JSON.parse(data);
        let ret = obj.ret;

        if(ret == '0') {

          insCopyAfw81();

        } else {
          ret_detail = obj.data;
          
          for(j = 0 ; j < ret_detail.length ; j++) {
            if(ret_detail[j]["cnt"] == '0') {
              continue;
            }
            
            var detail = $("#af215x1Detail .detail");
            for(i = 0 ; i < detail.length ; i++) {
              var $me = $(detail[i]);

              if(common.getValue($me.find(".cucd"))  == ret_detail[j]["cucd"]
              && common.getValue($me.find(".cuscd")) == ret_detail[j]["cuscd"]
              && common.getValue($me.find(".vseq"))  == ret_detail[j]["vseq"]) {
                break;
              }
            }
          }
        }
      }
      , function (error) {
        alert(error.msg);
      }
    );
  } else {
    insCopyAfw81();
  }
});

function insCopyAfw81() {

  var header = {};
  header["cucd"] = common.getValue($("#af215x1Cucd"));
  header["cuscd"] = common.getValue($("#af215x1Cuscd"));
  header["wseq"] = common.getValue($("#af215x1Wseq"));

  var detailList = [];
  var detail = $("#af215x1Detail .detail");
  var cnt = 0;
  for(i = 0 ; i < detail.length ; i++) {
    var $me = $(detail[i]);
    if(!stringUtil.isEmpty(common.getValue($me.find(".cuscd"))) && $me.find(".copychk").prop("checked")) {
      var buf = {};
      buf["cucd"] = common.getValue($me.find(".cucd"));
      buf["cuscd"] = common.getValue($me.find(".cuscd"));
      buf["vseq"] = common.getValue($me.find(".vseq"));
      detailList[cnt] = buf;
      cnt = cnt + 1;
    }
  }
  var sendData = {};
  sendData["data"] = detailList;
  sendData["header"] = header;

  common.postData(
    "./insCopyAfw81.php" //元画面の物を呼ぶ
    , sendData
    , function (data) {
      // 特殊処理があれば
      var obj = JSON.parse(data);
      let ret = obj.ret;

      if(ret == '0') {

        common.setValue($("#topPrgTitle"), common.getValue($("#af215x1OrgTitle")));
        callFmAF215XCopy(); //元画面に記載
        af215x1.initial();
        af215x1.close();

      } else {
        alert("登録に失敗しました。");
      }
    }
    , function (error) {
      alert(error.msg);
    }
  );

}


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

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

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

  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> ' +
      '     <input type="checkbox" value="1" class="form-control item input-sm wd-020 copychk clear" ' + n(d.chkflg) + ' tabindex="-1" />' +
      '     <div class="item wd-S inputReadOnly cuscd">' + n(d.cuscd) + '</div>' +
      '     <div class="item wd-L inputReadOnly spnm">' + n(d.spnm) + '</div>' +
      '     <input type="hidden" class="cucd" value="' + n(d.cucd) + '"> '+
      '   </div> ' +
      ' </div> ';

    return row;
  },

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

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

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

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

  },
}
</script>
