
(function () {
  "use strict";

  // ------------------- 詳細画面表示時の処理 -------------------
  kintone.events.on('app.record.detail.show', function (event) {
    // 「登園/帰園」ボタン
    const area = kintone.app.record.getSpaceElement('header-clocking-btn-area');

    const clockIn = createBtn('clock-in', '登園', dateToFieldInDetailIn('登園時刻'));
    const clockOut = createBtn('clock-out', '帰園', dateToFieldInDetail('帰園時刻'));
    area.appendChild(clockIn);
    area.appendChild(clockOut);

    hideSpaceField(['clock-in-btn-area', 'clock-out-btn-area']);

    // 「伝達事項」
    if (!isBaby(event.record['学年'].value)) {
      kintone.app.record.setFieldShown('伝達事項', false);
    }

    return event;
  });

  function isBaby(grade) {
    return grade === "０歳児" || grade === "１歳児";
  }

  function dateToFieldInDetail(fieldCode) {
    return async function (e) {
      await new KintoneRestAPIClient().record.updateRecord({
        app: kintone.app.getId(),
        id: kintone.app.record.getId(),
        record: {
          [fieldCode]: {
            value: getCurrentTime()
          }
        }
      });
      location.reload();
    }
  }

  //PVC追加
  function dateToFieldInDetailIn(fieldCode) {
    return async function (e) {
      await new KintoneRestAPIClient().record.updateRecord({
        app: kintone.app.getId(),
        id: kintone.app.record.getId(),
        record: {
          [fieldCode]: {
            value: getCurrentTime()
          },
          出欠: {
            value: "出席"
          }
        }
      });
      location.reload();
    }
  }

  // ------------------- 印刷画面表示時の処理 -------------------
  kintone.events.on('app.record.print.show', (event) => {
    // 「登園/帰園」ボタン
    hideSpaceField(['clock-in-btn-area', 'clock-out-btn-area']);

    // 「伝達事項」
    if (!isBaby(event.record['学年'].value)) {
      kintone.app.record.setFieldShown('伝達事項', false);
    }
    return event;
  });

  // ------------------- 編集画面表示時の処理 -------------------
  kintone.events.on(['app.record.create.show', 'app.record.edit.show'], function (event) {
    // 「登園/帰園」ボタン
    const clockIn = createBtn('clock-in', '登園', dateToFieldInEditIn('登園時刻','出欠'));
    kintone.app.record.getSpaceElement('clock-in-btn-area').appendChild(clockIn);
    const clockOut = createBtn('clock-out', '帰園', dateToFieldInEdit('帰園時刻'));
    kintone.app.record.getSpaceElement('clock-out-btn-area').appendChild(clockOut);

    // 「伝達事項」
    if (!isBaby(event.record['学年'].value)) {
      kintone.app.record.setFieldShown('伝達事項', false);
    }
    return event;
  });

  function dateToFieldInEdit(fieldCode) {
    return function (e) {
      var record = kintone.app.record.get();
      record['record'][fieldCode]['value'] = getCurrentTime();
      kintone.app.record.set(record);
    }
  }

  //PVC追加
  function dateToFieldInEditIn(fieldCode1,fieldCode2) {
    return function (e) {
      var record = kintone.app.record.get();
      record['record'][fieldCode1]['value'] = getCurrentTime();
      record['record'][fieldCode2]['value'] = "出席";
      kintone.app.record.set(record);
    }
  }

  function getCurrentTime() {
    const now = new Date();
    const hours = String(now.getHours()).padStart(2, '0');
    const minutes = String(now.getMinutes()).padStart(2, '0');
    return `${hours}:${minutes}`;
  }
})();