//===================================================================*/
/* イベント
//===================================================================*/
//　F2 表示
$(document).on("click", ".popafm03xFunc2:not('.noevent')", function (e) {
  $('#popafm03xSearch').trigger('click');
});
//　F5 クリア
$(document).on("click", ".popafm03xFunc5:not('.noevent')", function (e) {
  afm03x.initial();
});
//　F9 決定
$(document).on("click", ".popafm03xFunc9:not('.noevent')", function (e) {
  $("#popafm03xSelect").trigger("click");
});
//　F12 メニュー/戻る
$(document).on("click", ".popafm03xFunc12:not('.noevent')", function (e) {
  $('#popafm03xClose').trigger('popClose');
});

$(document).on("focus", "#afm03xDummy", function(e) {
  $(".popafm03xFunc2").focus();
});

function dspPopupAfm03x($me) {  //表示
  if ($me.attr("readonly") == "readonly" || $me.attr("readonly") == true) {
    return true
  }
console.log("dspPopupAfm03x");

  var grp = $me.closest(".cucdGrp");
  var cucd = grp.find(".cucd")[0];

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

  common.setValue($("#afm03xOrgTitle"), common.getValue($("#topPrgTitle")));
  common.setValue($("#topPrgTitle"),"得意先検索");

  var id = common.getId( $(cucd) );
  $("#afm03xCd").data("popObjId", id);

  afm03x.initial();
  $("#afm03xNm").focus();
  common.setDetailHeight($("#afm03xDetail"));


  //$("#popafm03xSearch").trigger("click");
}
$(document).on("click", ".cmdCucd", function (e) {

  var $me = $(this);
  var grp = $me.closest(".cucdGrp");
  var cucd = $(grp.find(".cucd")[0]).val();
  var $cuscd = grp.find(".cuscd")[0];
  var $cmdCuscd = grp.find(".cmdCuscd")[0];

  console.log(cucd);
  console.log($cuscd);
  console.log($cmdCuscd);
  console.log($cuscd==null);
  console.log($cmdCuscd==null);
  console.log($cuscd!=null);
  console.log($cmdCuscd!=null);

  //得意先が埋まっていて、得意先項目がグループ内にあるかつ、得意先検索ボタンがないなら、店舗検索を上げる
  if(!stringUtil.isEmpty(cucd) && $cuscd != null && $cmdCuscd == null){
    console.log("flg1");
    return true;
  }

  dspPopupAfm03x($(this));
});

$(document).on("keyup", ".popCucd", function (e) {
  if (e.keyCode == 40) {

    var $me = $(this);
    var grp = $me.closest(".cucdGrp");
    var cucd = $(grp.find(".cucd")[0]).val();
    var $cuscd = grp.find(".cuscd")[0];
    var $cmdCuscd = grp.find(".cmdCuscd")[0];

    console.log(cucd);
    console.log($cuscd);
    console.log($cmdCuscd);
    console.log($cuscd==null);
    console.log($cmdCuscd==null);
    console.log($cuscd!=null);
    console.log($cmdCuscd!=null);
    
    if(!stringUtil.isEmpty(cucd) && $cuscd != null && $cmdCuscd == null){  
    console.log("flg1");
    //if($cuscd != null && !stringUtil.isEmpty(cucd)){  //店舗がグループ内にあり、得意先が埋まっていれば店舗検索
      return true;
    }
  
    dspPopupAfm03x($(this));
  }
});

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


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

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

  var sendData = {};
  sendData["popmode"] = "afm03x";
  sendData["cnm"] = $("#afm03xCnm").val();
  sendData["ccd"] = $("#afm03xCcd").val();
  sendData["zip"] = $("#afm03xZip").val();
  sendData["add"] = $("#afm03xAdd").val();
  sendData["tel"] = $("#afm03xTel").val();
  sendData["fax"] = $("#afm03xFax").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", "#popafm03xSearch", function (e, data) {
  var list = JSON.parse(data)

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

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

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

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

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

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

  var sel = $("#afm03xDetail .selectRow");
/*
  var selCucd = $("#afm03xDetail .selectRow .cucd").text();
  var selCuscd = $("#afm03xDetail .selectRow .cuscd").text();
*/

  var grp = popup.closePopup("afm03x");
  var cucd = grp.find(".cucd")[0];
  var eventcucd = grp.find(".eventcucd")[0];
  //値をセット
  common.setValue(grp.find(".cucd"), common.getValue($(sel).find(".cucd")));
  common.setValue(grp.find(".cunm"), common.getValue($(sel).find(".cunm")));
  common.setValue(grp.find(".cuknm"), common.getValue($(sel).find(".cuknm")));


//  $(cucd).change();
  $(eventcucd).focus();

  afm03x.initial();

  common.setValue($("#topPrgTitle"), common.getValue($("#afm03xOrgTitle")));
  $(eventcucd).trigger("afterSelectAfm03x");
  common.clearError($(eventcucd));
});

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

  var grp = popup.closePopup("afm03x");
  grp.find(".popCucd")[0].focus();

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

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

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

  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) + ' cucd">' + n(d.cucd) + '</div>' +
    '     <div class="item wd-L   ' + n(d.readcolor) + ' cunm ">' + n(d.cunm) + '</div>' +
    '     <div class="item wd-150 ' + n(d.readcolor) + ' cuknm ">' + n(d.cuknm) + '</div>' +
    '     <div class="item wd-070 ' + n(d.readcolor) + ' cuzip ">' + n(d.cuzip) + '</div>' +
    '     <div class="item wd-LL  ' + n(d.readcolor) + ' cuad longnm">' + n(d.cuad) + '</div>' +
    '     <div class="item wd-100 ' + n(d.readcolor) + ' cutel">' + n(d.cutel) + '</div>' +
    '     <div class="item wd-100 ' + n(d.readcolor) + ' cufax">' + n(d.cufax) + '</div>' +
    '   </div> ' +
    ' </div> ';

    return row;
  },

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

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

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

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

  },
}
