<template>
  <div class="main-container">
    <el-card class="box-card !border-none" shadow="never">
      <div class="flex justify-between items-center">
        <span class="text-lg">{{ pageName }}</span>
        <el-button type="primary" @click="addEvent">
          {{ t('addCourseSchedule') }}
        </el-button>
      </div>

      <el-card
        class="box-card !border-none my-[10px] table-search-wrap"
        shadow="never"
      >
        <el-form
          :inline="true"
          :model="courseScheduleTable.searchParam"
          ref="searchFormRef"
        >
          <el-form-item :label="t('campusId')" prop="campus_id">
            <el-input
              v-model="courseScheduleTable.searchParam.campus_id"
              :placeholder="t('campusIdPlaceholder')"
            />
          </el-form-item>
          <el-form-item :label="t('venueId')" prop="venue_id">
            <el-input
              v-model="courseScheduleTable.searchParam.venue_id"
              :placeholder="t('venueIdPlaceholder')"
            />
          </el-form-item>
          <el-form-item :label="t('courseDate')" prop="course_date">
            <el-input
              v-model="courseScheduleTable.searchParam.course_date"
              :placeholder="t('courseDatePlaceholder')"
            />
          </el-form-item>
          <el-form-item :label="t('timeSlot')" prop="time_slot">
            <el-input
              v-model="courseScheduleTable.searchParam.time_slot"
              :placeholder="t('timeSlotPlaceholder')"
            />
          </el-form-item>
          <el-form-item :label="t('courseId')" prop="course_id">
            <el-input
              v-model="courseScheduleTable.searchParam.course_id"
              :placeholder="t('courseIdPlaceholder')"
            />
          </el-form-item>
          <el-form-item :label="t('coachId')" prop="coach_id">
            <el-input
              v-model="courseScheduleTable.searchParam.coach_id"
              :placeholder="t('coachIdPlaceholder')"
            />
          </el-form-item>
          <el-form-item :label="t('participants')" prop="participants">
            <el-input
              v-model="courseScheduleTable.searchParam.participants"
              :placeholder="t('participantsPlaceholder')"
            />
          </el-form-item>
          <el-form-item :label="t('studentIds')" prop="student_ids">
            <el-input
              v-model="courseScheduleTable.searchParam.student_ids"
              :placeholder="t('studentIdsPlaceholder')"
            />
          </el-form-item>
          <el-form-item
            :label="t('availableCapacity')"
            prop="available_capacity"
          >
            <el-input
              v-model="courseScheduleTable.searchParam.available_capacity"
              :placeholder="t('availableCapacityPlaceholder')"
            />
          </el-form-item>
          <el-form-item :label="t('status')" prop="status">
            <el-input
              v-model="courseScheduleTable.searchParam.status"
              :placeholder="t('statusPlaceholder')"
            />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="loadCourseScheduleList()">{{
              t('search')
            }}</el-button>
            <el-button @click="resetForm(searchFormRef)">{{
              t('reset')
            }}</el-button>
          </el-form-item>
        </el-form>
      </el-card>

      <div class="mt-[10px]">
        <el-table
          :data="courseScheduleTable.data"
          size="large"
          v-loading="courseScheduleTable.loading"
        >
          <template #empty>
            <span>{{
              !courseScheduleTable.loading ? t('emptyData') : ''
            }}</span>
          </template>
          <el-table-column
            prop="campus_id"
            :label="t('campusId')"
            min-width="120"
            :show-overflow-tooltip="true"
          />

          <el-table-column
            prop="venue_id"
            :label="t('venueId')"
            min-width="120"
            :show-overflow-tooltip="true"
          />

          <el-table-column
            prop="course_date"
            :label="t('courseDate')"
            min-width="120"
            :show-overflow-tooltip="true"
          />

          <el-table-column
            prop="time_slot"
            :label="t('timeSlot')"
            min-width="120"
            :show-overflow-tooltip="true"
          />

          <el-table-column
            prop="course_id"
            :label="t('courseId')"
            min-width="120"
            :show-overflow-tooltip="true"
          />

          <el-table-column
            prop="coach_id"
            :label="t('coachId')"
            min-width="120"
            :show-overflow-tooltip="true"
          />

          <el-table-column
            prop="participants"
            :label="t('participants')"
            min-width="120"
            :show-overflow-tooltip="true"
          />

          <el-table-column
            prop="student_ids"
            :label="t('studentIds')"
            min-width="120"
            :show-overflow-tooltip="true"
          />

          <el-table-column
            prop="available_capacity"
            :label="t('availableCapacity')"
            min-width="120"
            :show-overflow-tooltip="true"
          />

          <el-table-column
            prop="status"
            :label="t('status')"
            min-width="120"
            :show-overflow-tooltip="true"
          />

          <el-table-column
            :label="t('operation')"
            fixed="right"
            min-width="120"
          >
            <template #default="{ row }">
              <el-button type="primary" link @click="editEvent(row)">{{
                t('edit')
              }}</el-button>
              <el-button type="primary" link @click="deleteEvent(row.id)">{{
                t('delete')
              }}</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="mt-[16px] flex justify-end">
          <el-pagination
            v-model:current-page="courseScheduleTable.page"
            v-model:page-size="courseScheduleTable.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="courseScheduleTable.total"
            @size-change="loadCourseScheduleList()"
            @current-change="loadCourseScheduleList"
          />
        </div>
      </div>

      <edit ref="editCourseScheduleDialog" @complete="loadCourseScheduleList" />
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, watch } from 'vue'
import { t } from '@/lang'
import { useDictionary } from '@/app/api/dict'
import {
  getCourseScheduleList,
  deleteCourseSchedule,
} from '@/app/api/course_schedule'
import { img } from '@/utils/common'
import { ElMessageBox, FormInstance } from 'element-plus'
import Edit from '@/app/views/course_schedule/components/course-schedule-edit.vue'
import { useRoute } from 'vue-router'
const route = useRoute()
const pageName = route.meta.title

let courseScheduleTable = reactive({
  page: 1,
  limit: 10,
  total: 0,
  loading: true,
  data: [],
  searchParam: {
    campus_id: '',
    venue_id: '',
    course_date: '',
    time_slot: '',
    course_id: '',
    coach_id: '',
    participants: '',
    student_ids: '',
    available_capacity: '',
    status: '',
  },
})

const searchFormRef = ref<FormInstance>()

// 选中数据
const selectData = ref<any[]>([])

// 字典数据

/**
 * 获取课程安排列表
 */
const loadCourseScheduleList = (page: number = 1) => {
  courseScheduleTable.loading = true
  courseScheduleTable.page = page

  getCourseScheduleList({
    page: courseScheduleTable.page,
    limit: courseScheduleTable.limit,
    ...courseScheduleTable.searchParam,
  })
    .then((res) => {
      courseScheduleTable.loading = false
      courseScheduleTable.data = res.data.data
      courseScheduleTable.total = res.data.total
    })
    .catch(() => {
      courseScheduleTable.loading = false
    })
}
loadCourseScheduleList()

const editCourseScheduleDialog: Record<string, any> | null = ref(null)

/**
 * 添加课程安排
 */
const addEvent = () => {
  editCourseScheduleDialog.value.setFormData()
  editCourseScheduleDialog.value.showDialog = true
}

/**
 * 编辑课程安排
 * @param data
 */
const editEvent = (data: any) => {
  editCourseScheduleDialog.value.setFormData(data)
  editCourseScheduleDialog.value.showDialog = true
}

/**
 * 删除课程安排
 */
const deleteEvent = (id: number) => {
  ElMessageBox.confirm(t('courseScheduleDeleteTips'), t('warning'), {
    confirmButtonText: t('confirm'),
    cancelButtonText: t('cancel'),
    type: 'warning',
  }).then(() => {
    deleteCourseSchedule(id)
      .then(() => {
        loadCourseScheduleList()
      })
      .catch(() => {})
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
  loadCourseScheduleList()
}
</script>

<style lang="scss" scoped>
/* 多行超出隐藏 */
.multi-hidden {
  word-break: break-all;
  text-overflow: ellipsis;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
</style>
