//===================================================================*/
/* イベント
//===================================================================*/
//　F2 表示
$(document).on("click", ".popafm05xFunc2:not('.noevent')", function (e) {
  $('#popafm05xSearch').trigger('click');
});
//　F5 クリア
$(document).on("click", ".popafm05xFunc5:not('.noevent')", function (e) {
  afm05x.initial();
});
//　F9 決定
$(document).on("click", ".popafm05xFunc9:not('.noevent')", function (e) {
  $("#popafm05xSelect").trigger("click");
});
//　F12 メニュー/戻る
$(document).on("click", ".popafm05xFunc12:not('.noevent')", function (e) {
  $('#popafm05xClose').trigger('popClose');
});

$(document).on("focus", "#afm05xDummy", function(e) {
  $(".popafm05xFunc2").focus();
});

function dspPopupAfm05x($me) {  //表示
  if ($me.attr("readonly") == "readonly" || $me.attr("readonly") == true) {
    return true
  }

  var grp = $me.closest(".ktktcdGrp");
  var ktktcd = grp.find(".ktktcd")[0];

  console.log("openPop_afm05x");
  popup.openPopup("afm05x", $(grp));
  common.setValue($("#afm05xOrgTitle"), common.getValue($("#topPrgTitle")));
  common.setValue($("#topPrgTitle"),"倉庫検索");

  var $ktcls = $(ktktcd).data("ktcls"); //0=全て可(デフォルト) 1=見ない 2=未処理在庫倉庫不可
  if(!stringUtil.isValid($ktcls)) {
    $ktcls = '0';
  }
  $("#afm05xKtcls").val($ktcls);


  var id = common.getId( $(ktktcd) );
  $("#afm05xCd").data("popObjId", id);

  afm05x.initial();
  $("#afm05xNm").focus();
  common.setDetailHeight($("#afm05xDetail"));

  //$("#popafm05xSearch").trigger("click");
}

$(document).on("click", ".cmdKtktcd", function (e) {
  dspPopupAfm05x($(this));
});

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

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

  if($('input:checkbox[name="afm05xStp"]:checked').val() == '1') {
    var $stpflg = '1';                          //1=廃止OK
  } else {
    var $stpflg = '0';
  }

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

  var sendData = {};
  sendData["popmode"] = "afm05x";
  sendData["nm"] = $("#afm05xNm").val();
  sendData["zip"] = $("#afm05xZip").val();
  sendData["add"] = $("#afm05xAdd").val();
  sendData["tel"] = $("#afm05xTel").val();
  sendData["fax"] = $("#afm05xFax").val();
  sendData["cd"] = $("#afm05xCd").val();
  sendData["ktcls"] = $("#afm05xKtcls").val();
  sendData['stpflg'] = $stpflg;

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

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

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

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

  $("#afm05xDetail .detail").first().find("a").focus();
});

$(document).on("regError", "#popafm05xSearch", function (e, data) {
  alert(data.msg);
});

$(document).on("dblclick", "#afm05xDetail .selectRow", function (e) {
  hideToolTip();
  $("#popafm05xSelect").trigger("click");
});

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

  var sel = $("#afm05xDetail .selectRow");
//  var selKtktcd = $("#afm05xDetail .selectRow .ktktcd").text();

  var grp = popup.closePopup("afm05x");
  var ktktcd = grp.find(".ktktcd")[0];
  //値をセット
  common.setValue(grp.find(".ktktcd"), common.getValue($(sel).find(".ktktcd")));
  common.setValue(grp.find(".ktktnm"), common.getValue($(sel).find(".ktktnm")));
  common.setValue(grp.find(".ktktnk"), common.getValue($(sel).find(".ktktnk")));
  common.setValue(grp.find(".ktcls"), common.getValue($(sel).find(".ktcls")));

//  $(ktktcd).change();
  $(ktktcd).focus();

  afm05x.initial();

  common.setValue($("#topPrgTitle"), common.getValue($("#afm05xOrgTitle")));
  common.clearError($(ktktcd));
  $(ktktcd).trigger("afterSelectAfm05x");
});

$("#popafm05xClose").on("popClose", function (e) {
  if (e.bubbles == false) {
    return false;
  }

  var grp = popup.closePopup("afm05x");
  grp.find(".ktktcd")[0].focus();

  common.setValue($("#topPrgTitle"), common.getValue($("#afm05xOrgTitle")));
  afm05x.initial();
  //afm05x.close();
});

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

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

  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-SS  ' + n(d.readcolor) + ' ktktcd">' + n(d.ktktcd) + '</div>' +
    '     <div class="item wd-L   ' + n(d.readcolor) + ' ktktnm">' + n(d.ktktnm) + '</div>' +
    '     <div class="item wd-M   ' + n(d.readcolor) + ' ktktnk">' + n(d.ktktnk) + '</div>' +
    '     <div class="item wd-S   ' + n(d.readcolor) + ' ktzip">' + n(d.ktzip) + '</div>' +
    '     <div class="item wd-LL  ' + n(d.readcolor) + ' ktad longnm">' + n(d.ktad) + '</div>' +
    '     <div class="item wd-120 ' + n(d.readcolor) + ' kttel">' + n(d.kttel) + '</div>' +
    '     <div class="item wd-120 ' + n(d.readcolor) + ' ktfax">' + n(d.ktfax) + '</div>' +
    '     <input type="hidden" class="ktcls" value="' +  n(d.ktcls) + '">' +
    '   </div> ' +
    ' </div> ';

    return row;
  },

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

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

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

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

  },
}
