<script>
import { Date, add, cmp, timedelta } from '@silane/datetime';
import { computed } from 'vue';

import { inject, keys as injectionKeys } from '../../../injection';


export default {
  name: 'PageMasterTimetableIndex',
  setup() {
    const resourceStore = inject(injectionKeys.resourceStore);
    const generalTimetableScope = inject(injectionKeys.generalTimetableScope);

    const resources = computed(
      () => resourceStore.resources.value.filter(x => !x.disabled)
    );
    const todayCropped = computed(() => {
      const today = Date.today();
      if(cmp(today, generalTimetableScope.dateRange.value[0]) < 0) {
        return generalTimetableScope.dateRange.value[0];
      }
      if(cmp(generalTimetableScope.dateRange.value[1], today) <= 0) {
        return add(
          generalTimetableScope.dateRange.value[1], timedelta({ days: -1 })
        );
      }
      return today;
    });

    return {
      resources, todayCropped, currentClassroom: computed(
        () => generalTimetableScope.classroom.value
      ),
    };
  },
}
</script>

<template>
<h1>Index</h1>
<p>
  <router-link
    :to="`./datetimeline/${todayCropped.isoFormat()}`"
  >日別時間割表</router-link>
</p>
<p>
  <router-link to="./events/index">イベント一覧</router-link>
</p>
<p>
  <router-link to="./resourcehourcopy">可能時間曜日別一括複製</router-link>
</p>
<p>
  <router-link to="./eventintegrity">整合性チェック</router-link>
</p>
<p>
  <router-link to="./timelineprint">時間割表印刷</router-link>
</p>
<section>
  <h1>教室</h1>
  <router-link
    :to="`./resources/${currentClassroom}/resourcehours`"
  >可能時間（開室時間）入力</router-link>
</section>
<section>
  <h1>講師</h1>
  <table v-if="resources.some(x => x.type === 'teacher')">
    <thead>
      <tr><th>氏名</th><th>リンク</th></tr>
    </thead>
    <tbody>
      <tr
        v-for="resource of resources.filter(x => x.type === 'teacher')"
        :key="resource.id"
      >
        <td>
          <ruby style="margin-inline-end: .5em;">
            {{ resource.lastName }}
            <rp>(</rp><rt>{{ resource.lastNameKana }}</rt><rp>)</rp>
          </ruby>
          <ruby>
            {{ resource.firstName }}
            <rp>(</rp><rt>{{ resource.firstNameKana }}</rt><rp>)</rp>
          </ruby>
        </td>
        <td>
          <router-link
            :to="`./resources/${resource.id}/index`"
          >詳細</router-link> <router-link
            :to="`./resources/${resource.id}/timeline`"
          >時間割表</router-link> <router-link
            :to="`./resources/${resource.id}/resourcehours`"
          >可能時間入力</router-link> <router-link
            :to="`./resources/${resource.id}/lessons`"
          >授業入力</router-link> <router-link
            :to="`./resources/${resource.id}/timelineeventinput`"
          >時間割表イベント入力</router-link>
        </td>
      </tr>
    </tbody>
  </table>
  <p v-else>
    講師が一人も登録されていません。
    <router-link to="/master/config/teacher">ここ</router-link>から登録してください。
  </p>
</section>
<section>
  <h1>生徒</h1>
  <table v-if="resources.some(x => x.type === 'student')">
    <thead>
      <tr><th>学年</th><th>氏名</th><th>リンク</th></tr>
    </thead>
    <tbody>
      <tr
        v-for="resource of resources.filter(x => x.type === 'student')"
        :key="resource.id"
      >
        <td>{{ resource.grade?.label ?? '[無し]' }}</td>
        <td>
          <ruby style="margin-inline-end: 1em;">
            {{ resource.lastName }}
            <rp>(</rp><rt>{{ resource.lastNameKana }}</rt><rp>)</rp>
          </ruby>
          <ruby>
            {{ resource.firstName }}
            <rp>(</rp><rt>{{ resource.firstNameKana }}</rt><rp>)</rp>
          </ruby>
        </td>
        <td>
          <router-link
            :to="`./resources/${resource.id}/index`"
          >詳細</router-link> <router-link
            :to="`./resources/${resource.id}/timeline`"
          >時間割表</router-link> <router-link
            :to="`./resources/${resource.id}/resourcehours`"
          >可能時間入力</router-link> <router-link
            :to="`./resources/${resource.id}/timelineeventinput`"
          >時間割表イベント入力</router-link>
        </td>
      </tr>
    </tbody>
  </table>
  <p v-else>
    生徒が一人も登録されていません。
    <router-link to="/master/config/student">ここ</router-link>から登録してください。
  </p>
</section>
</template>
