import OHeader from '@/components/o-header'
import OSticky from '@/components/o-sticky'
import OEmpty from '@/components/o-empty'
import dayjs from 'dayjs'
import isBetween from 'dayjs/plugin/isBetween'
dayjs.extend(isBetween)
import {
  Cell,
  Icon,
  List,
  showToast,
  Image,
  DropdownMenu,
  DropdownItem,
  Button,
  Calendar,
  CellGroup
} from 'vant'
import OFullRefresh from '@/components/o-full-refresh'
import { defineComponent, reactive, ref, onMounted, nextTick } from 'vue'
import { state as globalState } from '@/state'
import { useRouter } from 'vue-router'
import styles from './index.module.less'
import request from '@/helpers/request'
import iconTeacher from '@common/images/icon_teacher.png'
import { courseEmnu } from '@/constant'
export default defineComponent({
  name: 'exercise-record',
  setup() {
    const platformApi = ref(globalState.platformApi)
    const router = useRouter()
    const state = reactive({
      showSearchStatus: true,
      showPopoverTime: false,
      actions: [] as any,
      subjects: [] as any
    })
    const forms = reactive({
      startTime: dayjs().day(1).format('YYYY-MM-DD'),
      endTime: dayjs().day(7).format('YYYY-MM-DD'),
      orchestraId: '',
      orchestraName: '',
      courseType: '',
      courseTypeName: '',
      coursewareErr: null,
      coursewareErrName: '',
      keyword: '',
      page: 1,
      rows: 20
    })
    const refreshing = ref(false)
    const loading = ref(false)
    const finished = ref(false)
    const showContact = ref(true)
    const list = ref([])
    const getList = async () => {
      try {
        if (refreshing.value) {
          forms.page = 1
          list.value = []
          refreshing.value = false
        }
        const { endTime, startTime, ...re } = forms
        const res = await request.post(`${platformApi.value}/courseSchedule/coursewareErrPage`, {
          data: { ...re, endTime: endTime + ' 23:59:59', startTime: startTime + ' 00:00:00' }
        })

        if (list.value.length > 0 && res.data.pages === 1) {
          return
        }

        list.value = list.value.concat(res.data.rows || [])
        finished.value = res.data.current >= res.data.pages
        showContact.value = list.value.length > 0
        forms.page = res.data.current + 1
        // console.log(showContact.value, ' showContact.value ')
        loading.value = false
      } catch (e: any) {
        // console.log(e, 'e')
        const message = e.message
        showToast(message)
        showContact.value = false
        finished.value = true
      }
    }
    onMounted(() => {
      getList()
      getOrchestraList()
      // getSubjects()
    })

    const onBack = () => {
      router.go(-1)
    }

    const checkSort = (val: any) => {
      console.log(val, 'val')
      forms.coursewareErr = val.value
      forms.coursewareErrName = val.name
    }

    const checkOrchestra = (val: any) => {
      forms.orchestraId = val.value
      forms.orchestraName = val.name
    }

    const checkSubject = (val: any) => {
      forms.courseType = val.value
      forms.courseTypeName = val.name
    }
    const getOrchestraList = async () => {
      try {
        const res = await request.post(`${platformApi.value}/orchestra/page`, {
          data: { page: 1, rows: 9999, status: 'DONE' }
        })
        state.actions = res.data.rows.map((item: any) => {
          return {
            name: item.name,
            value: item.id as string
          }
        })
      } catch (e: any) {
        const message = e.message
        showToast(message)
      }
    }

    // const getSubjects = async () => {
    //   try {
    //     const res = await request.post(`${platformApi.value}/subjectBasicConfig/page`, {
    //       data: { page: 1, rows: 9999 }
    //     })
    //     state.subjects = res.data.rows.map((item) => {
    //       return {
    //         name: item.courseTypeName,
    //         value: item.courseType as string
    //       }
    //     })
    //   } catch (e: any) {
    //     const message = e.message
    //     showToast(message)
    //   }
    // }
    const onRefresh = () => {
      finished.value = false
      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      loading.value = true
      getList()
    }

    // 名称
    const formatLength = (name: string) => {
      if (name.length > 11) {
        const fristName = name.substring(0, 6)
        const lastName = name.substring(name.length - 5, name.length)
        return fristName + '...' + lastName
      } else {
        return name
      }
    }

    const formatTime2Mins = (second: number) => {
      if (second) {
        const first = Math.floor(second / 60)
        const last = second % 60
        return (first >= 10 ? first : '0' + first) + ':' + (last >= 10 ? last : '0' + last)
      } else {
        return '00:00'
      }
    }

    const dropdownMenuRef = ref()
    const dropdownItemRef = ref()
    // 重置
    const onSearchReset = () => {
      forms.startTime = dayjs().day(1).format('YYYY-MM-DD')
      forms.endTime = dayjs().day(7).format('YYYY-MM-DD')
      forms.orchestraId = ''
      forms.orchestraName = ''
      forms.courseType = ''
      forms.courseTypeName = ''
      forms.coursewareErr = null
      forms.coursewareErrName = ''
      dropdownItemRef.value?.toggle()
      refreshing.value = true
      getList()
    }

    // 搜索
    const onSearchConfirm = () => {
      dropdownItemRef.value?.toggle()
      refreshing.value = true
      getList()
    }

    return () => (
      <div class={[!showContact.value ? 'emptyRootContainer' : '', styles.exerciseRecord]}>
        <OSticky
          position="top"
          background="#F8F8F8"
          onGetHeight={(height: any) => {
            document.documentElement.style.setProperty('--header-height', height + 'px')
          }}
        >
          <OHeader border={false}>
            {{
              right: () => (
                <DropdownMenu
                  class={styles.searchMore}
                  closeOnClickOverlay={false}
                  closeOnClickOutside={false}
                  ref={dropdownMenuRef}
                >
                  <DropdownItem title="筛选" v-model={state.showSearchStatus} ref={dropdownItemRef}>
                    <div class={styles.searchContainer}>
                      {state.actions.length > 0 && (
                        <>
                          <div class={styles.searchTitle}>乐团</div>
                          <div class={[styles.searchTypeGroup, styles.searchTypeFlex]}>
                            {state.actions.map((item: any) => (
                              <div
                                class={[
                                  styles.searchTypeItem,
                                  item.value === forms.orchestraId && styles['is-active']
                                ]}
                                onClick={() => checkOrchestra(item)}
                              >
                                {formatLength(item.name)}
                              </div>
                            ))}
                          </div>
                        </>
                      )}

                      <div class={styles.searchTitle}>时间段</div>
                      <div class={[styles.searchTypeGroup, styles.searchTypeFlex2]}>
                        <div
                          class={styles.searchTypeItem}
                          onClick={() => (state.showPopoverTime = true)}
                        >
                          {forms.startTime}
                        </div>
                        <div
                          class={styles.searchTypeItemLine}
                          onClick={() => (state.showPopoverTime = true)}
                        ></div>
                        <div
                          class={styles.searchTypeItem}
                          onClick={() => (state.showPopoverTime = true)}
                        >
                          {forms.endTime}
                        </div>
                      </div>
                      <div class={styles.searchTitle}>课程类型</div>
                      <div class={[styles.searchTypeGroup, styles.searchTypeFlex1]}>
                        {Object.keys(courseEmnu).map((subject: any) => (
                          <div
                            class={[
                              styles.searchTypeItem,
                              subject === forms.courseType && styles['is-active']
                            ]}
                            onClick={() => {
                              checkSubject({
                                name: courseEmnu[subject],
                                value: subject
                              })
                            }}
                          >
                            {courseEmnu[subject]}
                          </div>
                        ))}
                      </div>
                      <div class={styles.searchTitle}>课件使用状态</div>
                      <div class={[styles.searchTypeGroup, styles.searchTypeFlex]}>
                        {[
                          {
                            name: '正常',
                            value: false
                          },
                          {
                            name: '异常',
                            value: true
                          }
                        ].map((item: any) => (
                          <div
                            class={[
                              styles.searchTypeItem,
                              forms.coursewareErr === item.value && styles['is-active']
                            ]}
                            onClick={() => checkSort(item)}
                          >
                            {item.name}
                          </div>
                        ))}
                      </div>
                    </div>
                    <div class={[styles.searchMoreGroup, 'van-hairline--top']}>
                      <Button type="default" block round onClick={onSearchReset}>
                        重置
                      </Button>
                      <Button type="primary" block round onClick={onSearchConfirm}>
                        查询
                      </Button>
                    </div>
                  </DropdownItem>
                </DropdownMenu>
              )
            }}
          </OHeader>
          <div
            style={{
              backgroundColor: '#fff'
            }}
          >
            <div class={styles.searchPreview}>
              <div
                class={styles.searchPreviewItem}
                onClick={() => {
                  dropdownItemRef.value?.toggle()
                  nextTick(() => {
                    ;(
                      document.querySelectorAll('.van-dropdown-item--down')[0] as any
                    ).style.top = `calc(var(--van-nav-bar-height) + ${globalState.navBarHeight}px`
                  })
                }}
              >
                {forms.startTime}~{forms.endTime}
              </div>
              {forms.orchestraId && (
                <div class={styles.searchPreviewItem}>
                  {formatLength(forms.orchestraName)}
                  <Icon
                    name="cross"
                    class={styles.cross}
                    onClick={(e: any) => {
                      forms.orchestraId = ''
                      forms.orchestraName = ''
                      e.stopPropagation()
                      refreshing.value = true
                      getList()
                    }}
                  />
                </div>
              )}
              {forms.courseType && (
                <div class={styles.searchPreviewItem}>
                  {forms.courseTypeName}
                  <Icon
                    name="cross"
                    class={styles.cross}
                    onClick={(e: any) => {
                      forms.courseType = ''
                      forms.courseTypeName = ''
                      e.stopPropagation()
                      refreshing.value = true
                      getList()
                    }}
                  />
                </div>
              )}
              {forms.coursewareErr != null && (
                <div class={styles.searchPreviewItem}>
                  {forms.coursewareErrName}
                  <Icon
                    name="cross"
                    class={styles.cross}
                    onClick={(e: any) => {
                      forms.coursewareErr = null
                      forms.coursewareErrName = ''
                      e.stopPropagation()
                      refreshing.value = true
                      getList()
                    }}
                  />
                </div>
              )}
            </div>
          </div>
        </OSticky>
        {showContact.value ? (
          <OFullRefresh
            v-model:modelValue={refreshing.value}
            onRefresh={onRefresh}
            style={{
              minHeight: `calc(100vh - var(--header-height))`
            }}
          >
            <List
              loading-text=" "
              // v-model:loading={loading.value}
              finished={finished.value}
              style={{
                paddingTop: '12px'
              }}
              finished-text=" "
              immediateCheck={false}
              onLoad={getList}
            >
              {list.value.map((item: any) => (
                <CellGroup inset class={styles.coursewareGroup}>
                  <Cell class={styles.top1}>
                    {{
                      title: () => (
                        <>
                          {dayjs(item.startTime).format('YYYY-MM-DD HH:mm')}~
                          {dayjs(item.endTime).format('HH:mm')}
                        </>
                      ),
                      value: () => <>{formatLength(item.orchestraName)}</>
                    }}
                  </Cell>
                  <Cell class={styles.top2} center>
                    {{
                      icon: () => (
                        <Image
                          class={styles.userLogo}
                          src={item.teacherAvatar || iconTeacher}
                          fit="cover"
                        />
                      ),
                      title: () => (
                        <div class={styles.userInfo}>
                          <p class={[styles.userName, 'van-ellipsis']}>{item.teacherName}</p>
                          <p class={[styles.courseName, 'van-ellipsis']}>
                            {['INSTRUMENTAL_ENSEMBLE', 'EUPHONIUM_SINGLE'].includes(item.type)
                              ? courseEmnu[item.type]
                              : courseEmnu[item.type] + '单技课'}
                          </p>
                        </div>
                      ),
                      value: () => (
                        <div class={styles.courseCount}>
                          <p class={styles.courseRange}>
                            <span
                              style={{
                                color:
                                  item.coursewarePlayTime >= item.adviseStudyTimeSecond
                                    ? '#333'
                                    : '#f44541'
                              }}
                            >
                              {formatTime2Mins(item.coursewarePlayTime)}
                            </span>
                            /{formatTime2Mins(item.adviseStudyTimeSecond)}
                          </p>
                          <p class={styles.courseRangeTips}>使用时长/标准时长</p>
                        </div>
                      )
                    }}
                  </Cell>
                </CellGroup>
              ))}
            </List>
          </OFullRefresh>
        ) : (
          <OEmpty tips="暂无课件记录" />
        )}

        <Calendar
          v-model:show={state.showPopoverTime}
          firstDayOfWeek={1}
          showConfirm={false}
          type="range"
          maxRange={7}
          minDate={new Date('2023-02-27')}
          defaultDate={[dayjs(forms.startTime).toDate(), dayjs(forms.endTime).toDate()]}
          style={{
            height: '70%'
          }}
          onSelect={(item: any) => {
            forms.startTime = ''
            forms.endTime = ''
            // console.log(
            //   item,
            //   dayjs(item[0]).format('YYYY-MM-DD'),
            //   'item',
            //   dayjs(item[0]).day(0).format('YYYY-MM-DD'),
            //   dayjs(item[0]).day(),
            //   dayjs(item[0]).subtract(6, 'day').format('YYYY-MM-DD')
            // )

            if (!dayjs(item[0]).isBetween(dayjs(forms.startTime), dayjs(forms.endTime))) {
              const week = dayjs(item[0]).day()
              if (week === 0) {
                // 星期天
                forms.startTime = dayjs(item[0]).subtract(6, 'day').format('YYYY-MM-DD')
                forms.endTime = dayjs(item[0]).format('YYYY-MM-DD')
              } else if (week === 1) {
                // 星期一
                forms.startTime = dayjs(item[0]).format('YYYY-MM-DD')
                forms.endTime = dayjs(item[0]).add(6, 'day').format('YYYY-MM-DD')
              } else {
                forms.startTime = dayjs(item[0])
                  .subtract(week - 1, 'day')
                  .format('YYYY-MM-DD')
                forms.endTime = dayjs(item[0])
                  .add(7 - week, 'day')
                  .format('YYYY-MM-DD')
              }
            }
            state.showPopoverTime = false
          }}
        />
      </div>
    )
  }
})
