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

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

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

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


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

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

  // ------------------- 編集画面保存時の処理 -------------------
  kintone.events.on(['mobile.app.record.create.submit', 'mobile.app.record.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);
  }

})();