
(function () {
  "use strict";
  const FIELD_CODE = "ユニークキー";

  // ------------------- 詳細/印刷/編集画面表示時の処理 -------------------
  kintone.events.on(['app.record.detail.show', 'app.record.print.show'], function (event) {
    hideField(FIELD_CODE);
    return event;
  });

  // ------------------- 編集画面表示時の処理 -------------------
  kintone.events.on(['app.record.edit.show', 'app.record.create.show'], function (event) {
    const targetFieldEl = kintone.app.record.getSpaceElement("before-unique-key").parentElement.nextSibling;
    targetFieldEl.querySelector('.input-constraints-cybozu').style.display = 'none';

    event.record[FIELD_CODE]['value'] = "<自動計算：出席番号＋学年+クラス+名前>";
    disableField(event.record, FIELD_CODE);
    return event;
  });


  function hideField(fieldCode) {
    kintone.app.record.setFieldShown(fieldCode, false);
  }

  function disableField(record, fieldCode) {
    record[fieldCode]['disabled'] = true;
  }

  // ------------------- 編集画面保存時の処理 -------------------
  kintone.events.on(['app.record.create.submit', 'app.record.edit.submit', 'app.record.index.edit.submit'], function (event) {
    event.record[FIELD_CODE]['value'] = getUniqueKey(event.record);
    return event;
  });

  function getUniqueKey(record) {
    return (record['出席番号']['value'] + '_' + record['学年']['value'] + '_' + record['クラス']['value'] + '_' + record['名前']['value']).substring(0, 64);
  }

})();