import { defineComponent, onMounted, reactive } from 'vue';
import { NAvatar, NButton, NForm, NFormItem, NSpace, NTag } from 'naive-ui';
import Pagination from '@/components/pagination';
import { courseSchedulePage } from '../api';
import { useRoute } from 'vue-router';
import CDatePicker from '/src/components/CDatePicker';
import styles from './classRecord.module.less';
import teacherIcon from '@components/layout/images/teacherIcon.png';
import dayjs from 'dayjs';
import { getTimes } from '/src/utils';
import TheEmpty from '/src/components/TheEmpty';
export default defineComponent({
  name: 'class-record',
  setup() {
    const route = useRoute();
    const nowTime = dayjs().format('YYYY-MM-DD');
    const state = reactive({
      searchForm: {
        createTimer: [
          dayjs(nowTime).subtract(1, 'month').valueOf(),
          dayjs(nowTime).valueOf()
        ] as any
      },
      loading: false,
      pagination: {
        page: 1,
        rows: 10,
        pageTotal: 0
      },
      tableList: [] as any
    });
    const search = () => {
      state.pagination.page = 1;
      getList();
    };

    const onReset = () => {
      state.searchForm = {
        createTimer: [
          dayjs(nowTime).subtract(1, 'month').valueOf(),
          dayjs(nowTime).valueOf()
        ] as any
      };
      search();
    };
    const getList = async () => {
      state.loading = true;
      try {
        console.log(
          state.searchForm.createTimer,
          'state.searchForm.createTimer'
        );
        const { data } = await courseSchedulePage({
          classGroupId: route.query.id,
          ...getTimes(
            state.searchForm.createTimer,
            ['startTime', 'endTime'],
            'YYYY-MM-DD'
          ),
          ...state.pagination
        });
        const rows = data.rows || [];
        rows.forEach((row: any) => {
          row.lessonCourseware = row.lessonCoursewareJson
            ? JSON.parse(row.lessonCoursewareJson)
            : {};
        });
        state.tableList = rows;
        state.pagination.pageTotal = data.total;
        state.loading = false;
      } catch (e) {
        state.loading = false;
      }
    };
    onMounted(() => {
      getList();
    });
    return () => (
      <div>
        <div class={styles.searchList}>
          <NForm label-placement="left" inline>
            <NFormItem>
              <CDatePicker
                v-model:value={state.searchForm.createTimer}
                timerValue={state.searchForm.createTimer}
                separator={'至'}
                type="daterange"></CDatePicker>
            </NFormItem>

            <NFormItem>
              <NSpace justify="end">
                <NButton type="primary" class="searchBtn" onClick={search}>
                  搜索
                </NButton>
                <NButton
                  type="primary"
                  ghost
                  class="resetBtn"
                  onClick={onReset}>
                  重置
                </NButton>
              </NSpace>
            </NFormItem>
          </NForm>
        </div>
        <div class={styles.tableWrap}>
          <NSpace>
            {state.tableList.map((item: any) => (
              <div class={styles.tableContainer}>
                <div class={styles.header}>
                  <div class={styles.time}>
                    {dayjs(item.startTime).format('YYYY-MM-DD HH:mm')}
                  </div>
                  {route.query.name && (
                    <NTag type="primary" class={styles.ntag} strong>
                      {route.query.name}
                    </NTag>
                  )}
                </div>
                <div class={styles.content}>
                  <NAvatar
                    class={styles.navatar}
                    round
                    src={item.teacherAvatar || teacherIcon}
                  />
                  <div class={styles.userInfo}>
                    <h2>{item.teacherName}</h2>
                    <p>
                      {item.lessonCourseware.lessonCoursewareName} |{' '}
                      {item.lessonCourseware.lessonCoursewareDetailName}|{' '}
                      {
                        item.lessonCourseware
                          .lessonCoursewareKnowledgeDetailName
                      }
                    </p>
                  </div>
                </div>
              </div>
            ))}
          </NSpace>
          {state.tableList.length <= 0 && <TheEmpty />}

          <Pagination
            v-model:page={state.pagination.page}
            v-model:pageSize={state.pagination.rows}
            v-model:pageTotal={state.pagination.pageTotal}
            onList={getList}
            sync
            saveKey="classRecord-key"
          />
        </div>
      </div>
    );
  }
});
