//===================================================================*/
/* イベント
//===================================================================*/
//　F2 表示
$(document).on("click", ".popafm02x1Func2:not('.noevent')", function (e) {
  $('#popafm02x1Search').trigger('click');
});
//　F5 クリア
$(document).on("click", ".popafm02x1Func5:not('.noevent')", function (e) {
  afm02x1.initial();
});
//　F9 決定
$(document).on("click", ".popafm02x1Func9:not('.noevent')", function (e) {
  $("#popafm02x1Select").trigger("click");
});
//　F12 メニュー/戻る
$(document).on("click", ".popafm02x1Func12:not('.noevent')", function (e) {
  $('#popafm02x1Close').trigger('popClose');
});

$(document).on("focus", "#afm02x1Dummy", function(e) {
  $(".popafm02x1Func2").focus();
});

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

  var grp = $me.closest(".janGrp");
  var inpjan = grp.find(".inpjan")[0];

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

  common.setValue($("#afm02x1OrgTitle"), common.getValue($("#topPrgTitle")));
  common.setValue($("#topPrgTitle"),"商品選択");

  var id = common.getId( $(inpjan) );
  $("#afm02x1Cd").data("popObjId", id);

  /*結果を残す ------------------------------------------------------------*/
  var $noclear = $(inpjan).data("noclear"); //0=クリアする, 1=クリアしない
  if(!stringUtil.isValid($noclear)) {
    $noclear = "0";
  }
  /*結果を残す ------------------------------------------------------------*/
  if ($noclear == "0") {
    afm02x1.initial();
  } else {
    $("#afm02x1Gdjnm").focus();
  }

  $("#afm02x1Gdjnm").focus();
  common.setDetailHeight($("#afm02x1Detail"));

  var $stpflg = $me.data("stpflg"); //0=廃止可(デフォルト) 1=廃止不可 2=マスタ無し可能
  console.log($stpflg);
  if(!stringUtil.isValid($stpflg)) {
    $stpflg = '0';
  }
  console.log($stpflg);
  common.setValue("#afm02x1Stpflg",$stpflg);

  //名称取得で複数結果があったら検索画面を開く
  console.log($me.data("multiJan"));
  console.log(common.getValue($me));
  if($me.data("multiJan") == '1') {
    console.log("multiJan : "+common.getValue($me));
    common.setValue($("#afm02x1Jan"),common.getValue($me));
    $me.data("multiJan","");
    common.setValue($me,"");
    doSerach = true;
  }
  if(doSerach) {
    $("#popafm02x1Search").trigger("click");
  }

}

$(document).on("openAfm02x1", ".popJan", function(e) {
  dspPopupafm02x1($(this));
});

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

  var $stpflg = common.getValue("#afm02x1Stpflg");

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

  var sendData = {};
  sendData["popmode"] = "afm02x1";
  sendData["jan"] = $("#afm02x1Jan").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", "#popafm02x1Search", function (e, data) {
  var list = JSON.parse(data)

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

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

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

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

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

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

  var sel = $("#afm02x1Detail .selectRow");
/*
  var selJan = $("#afm02x1Detail .selectRow .jan").text();
  var selVjan = $("#afm02x1Detail .selectRow .vjan").text();
  var selIjan = $("#afm02x1Detail .selectRow .ijan").text();
  var selGdjnm = $("#afm02x1Detail .selectRow .gdjnm").text();
  var selGdjstnd = $("#afm02x1Detail .selectRow .gdjstnd").text();
*/

  var grp = popup.closePopup("afm02x1");
  var inpjan = grp.find(".inpjan")[0];  //入力項目
  //値をセット
  common.setValue(grp.find(".jan"), common.getValue($(sel).find(".jan")));
  common.setValue(grp.find(".vjan"), common.getValue($(sel).find(".vjan")));
  common.setValue(grp.find(".ijan"), common.getValue($(sel).find(".ijan")));
  common.setValue(grp.find(".gdjnm"), common.getValue($(sel).find(".gdjnm")));
  common.setValue(grp.find(".gdjnms"), common.getValue($(sel).find(".gdjnms")));
  common.setValue(grp.find(".gdjstnd"), common.getValue($(sel).find(".gdjstnd")));
  common.setValue(grp.find(".gdjstnds"), common.getValue($(sel).find(".gdjstnds")));
  common.setValue(grp.find(".gdcls9"), common.getValue($(sel).find(".gdcls9")));
  common.setValue(grp.find(".gdcls9nm"), common.getValue($(sel).find(".gdcls9nm")));

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

  /*結果を残す ------------------------------------------------------------*/
  var $noclear = $(inpjan).data("noclear"); //0=クリアする, 1=クリアしない
  if(!stringUtil.isValid($noclear)) {
    $noclear = "0";
  }
  /*結果を残す ------------------------------------------------------------*/
  if ($noclear == "0") {
    afm02x1.initial();
  }

  common.setValue($("#topPrgTitle"), common.getValue($("#afm02x1OrgTitle")));
  $(inpjan).trigger("afterSelectAfm02x1");
  $(inpjan).trigger("afterSelectAfm02x");
  common.clearError($(inpjan));
});

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

  /*結果を残す ------------------------------------------------------------*/
  var grp = popup.closePopup("afm02x1");
  var jan = grp.find(".jan")[0];
  var $noclear = $(jan).data("noclear"); //0=クリアする, 1=クリアしない
  if(!stringUtil.isValid($noclear)) {
    $noclear = "0";
  }
  /*結果を残す ------------------------------------------------------------*/
  if ($noclear == "0") {
    afm02x1.initial();
  }
  common.setValue($("#topPrgTitle"), common.getValue($("#afm02x1OrgTitle")));

  //afm02x1.close();
});

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

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

  appendRow: function (d) {
    var n = stringUtil.nvl;
    if(d.gdstp == null) {
      var row =
        ' <div class="col wp-100 detail"> ' +
        '   <hr> ' +
        '   <div class="col wp-100"> ' +
        '     <a href="#" class="line"> </a> ' +
        '     <div class="item wd-jan inputReadOnly jan">' + n(d.jan) + '</div>' +
        '     <div class="item wd-kbn inputReadOnly gdcls9nm center">' + n(d.gdcls9nm) + '</div>' +
        '     <div class="item wd-gdjnm inputReadOnly gdjnm ">' + n(d.gdjnm) + '</div>' +
        /*
        '     <div class="item wd-090 inputReadOnly gdacst number">' + common.addFigure(n(d.gdacst)) + '</div>' +
        '     <div class="item wd-090 inputReadOnly gdsal number">' + common.addFigure(n(d.gdsal)) + '</div>' +
        '     <div class="item wd-100 inputReadOnly gdbcst number">' + common.addFigure(n(d.gdbcst),2) + '</div>' +
        '     <div class="item wd-100 inputReadOnly gdcst number">' + common.addFigure(n(d.gdcst),2) + '</div>' +
        '     <div class="item wd-S inputReadOnly mcd">' + n(d.mcd) + '</div>' +
        '     <div class="item wd-230 inputReadOnly mnm">' + n(d.mnm) + '</div>' +
        */
        '     <input type="hidden" class="ijan" value="' + n(d.ijan) + '" /> ' +
        '     <input type="hidden" class="vjan" value="' + n(d.vjan) + '" /> ' +
        '     <input type="hidden" class="gdjnms" value="' + n(d.gdjnms) + '" /> ' +
        '     <input type="hidden" class="gdjstnd" value="' + n(d.gdjstnd) + '" /> ' +
        '     <input type="hidden" class="gdjstnds" value="' + n(d.gdjstnds) + '" /> ' +
        '     <input type="hidden" class="gdcls9" value="' + n(d.gdcls9) + '" /> ' +
        '   </div> ' +
        ' </div> ';
    } else {
      var row =
      ' <div class="col wp-100 detail"> ' +
      '   <hr> ' +
      '   <div class="col wp-100"> ' +
      '     <a href="#" class="line"> </a> ' +
      '     <div class="item wd-jan inputReadOnly jan" style="background-color:#FFC8F4;">' + n(d.jan) + '</div>' +
      '     <div class="item wd-kbn inputReadOnly gdcls9nm center" style="background-color:#FFC8F4;">' + n(d.gdcls9nm) + '</div>' +
      '     <div class="item wd-LL inputReadOnly gdjnm longnm" style="background-color:#FFC8F4;">' + n(d.gdjnm) + '</div>' +
        /*
      '     <div class="item wd-090 inputReadOnly gdacst number" style="background-color:#FFC8F4;">' + common.addFigure(n(d.gdacst)) + '</div>' +
      '     <div class="item wd-090 inputReadOnly gdsal number" style="background-color:#FFC8F4;">' + common.addFigure(n(d.gdsal)) + '</div>' +
      '     <div class="item wd-100 inputReadOnly gdbcst number" style="background-color:#FFC8F4;">' + common.addFigure(n(d.gdbcst),2) + '</div>' +
      '     <div class="item wd-100 inputReadOnly gdcst number" style="background-color:#FFC8F4;">' + common.addFigure(n(d.gdcst),2) + '</div>' +
      '     <div class="item wd-S inputReadOnly mcd" style="background-color:#FFC8F4;">' + n(d.mcd) + '</div>' +
      '     <div class="item wd-230 inputReadOnly mnm" style="background-color:#FFC8F4;">' + n(d.mnm) + '</div>' +
        */
      '     <input type="hidden" class="ijan" value="' + n(d.jan) + '" /> ' +
      '     <input type="hidden" class="vjan" value="' + n(d.vjan) + '" /> ' +
      '     <input type="hidden" class="gdjnms" value="' + n(d.gdjnms) + '" /> ' +
      '     <input type="hidden" class="gdjstnd" value="' + n(d.gdjstnd) + '" /> ' +
      '     <input type="hidden" class="gdjstnds" value="' + n(d.gdjstnds) + '" /> ' +
      '     <input type="hidden" class="gdcls9" value="' + n(d.gdcls9) + '" /> ' +
      '   </div> ' +
      ' </div> ';
    }

    return row;
  },

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

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

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

    $("#afm02x1Gdjnm").focus();
  },
}
