//===================================================================*/
/* イベント
//===================================================================*/
//　F2 表示
$(document).on("click", ".popafm08xFunc2:not('.noevent')", function (e) {
  $('#popafm08xSearch').trigger('click');
});
//　F5 クリア
$(document).on("click", ".popafm08xFunc5:not('.noevent')", function (e) {
  afm08x.initial();
});
//　F9 決定
$(document).on("click", ".popafm08xFunc9:not('.noevent')", function (e) {
  $("#popafm08xSelect").trigger("click");
});
//　F12 メニュー/戻る
$(document).on("click", ".popafm08xFunc12:not('.noevent')", function (e) {
  $('#popafm08xClose').trigger('popClose');
});

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

  var grp = $me.closest(".chgcdGrp");
  var chgcd = grp.find(".chgcd")[0];

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

  common.setValue($("#afm08xOrgTitle"), common.getValue($("#topPrgTitle")));
  common.setValue($("#topPrgTitle"),"社員検索");


  var id = common.getId( $(chgcd) );
  $("#afm08xCd").data("popObjId", id);

  afm08x.initial();
  $("#afm08xNm").focus();
  common.setDetailHeight($("#afm08xDetail"));

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

$(document).on("click", ".cmdChgcd", function (e) {
  dspPopupAfm08x($(this));
});
$(document).on("keyup", ".popChgcd", function (e) {
  if (e.keyCode == 40) {
    dspPopupAfm08x($(this));
  }
});

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

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

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

  var sendData = {};
  sendData["popmode"] = "afm08x";
  sendData["nm"] = $("#afm08xNm").val();
  sendData["cd"] = $("#afm08xCd").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", "#popafm08xSearch", function (e, data) {
  var list = JSON.parse(data)

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

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

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

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

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

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

  var sel = $("#afm08xDetail .selectRow");
//  var selChgcd = $("#afm08xDetail .selectRow .chgcd").text();

  var grp = popup.closePopup("afm08x");
  var chgcd = grp.find(".chgcd")[0];
  //値をセット
  common.setValue(grp.find(".chgcd"), common.getValue($(sel).find(".chgcd")));
  common.setValue(grp.find(".chgnm"), common.getValue($(sel).find(".chgnm")));


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

  afm08x.initial();

  common.setValue($("#topPrgTitle"), common.getValue($("#afm08xOrgTitle")));
  $(chgcd).trigger("afterSelectAfm08x");
  common.clearError($(chgcd));
});

$("#popafm08xClose").on("popClose", function (e) {
  if (e.bubbles == false) {
    return false;
  }
  var grp = popup.closePopup("afm08x");
  grp.find(".chgcd")[0].focus();

  common.setValue($("#topPrgTitle"), common.getValue($("#afm08xOrgTitle")));
  afm08x.initial();
});

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

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

  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 ' + n(d.readcolor) + ' chgcd">' + n(d.chgcd) + '</div>' +
    '     <div class="item wd-L ' + n(d.readcolor) + ' chgnm">' + n(d.chgnm) + '</div>' +
    '     <div class="item wd-M ' + n(d.readcolor) + ' chgknm">' + n(d.chgknm) + '</div>' +
    '   </div> ' +
    ' </div> ';


    return row;
  },

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

  initial: function () {
    $("#afm08xNm").val("");
    $("#afm08xCd").val("");
    $('input:checkbox[name="afm08xStp"]').prop("checked",false);

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

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

  },
}
