<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('addCourse') }}
        </el-button>
		

      </div>

      <el-card
        class="box-card !border-none my-[10px] table-search-wrap"
        shadow="never"
      >
        <el-form
          :inline="true"
          :model="courseTable.searchParam"
          ref="searchFormRef"
        >
          <el-form-item :label="t('courseName')" prop="course_name">
            <el-input
              v-model="courseTable.searchParam.course_name"
              :placeholder="t('courseNamePlaceholder')"
            />
          </el-form-item>
          <el-form-item :label="t('courseType')" prop="course_type">
            <el-select
              v-model="courseTable.searchParam.course_type"
              :placeholder="t('courseTypePlaceholder')"
            >
              <el-option
                v-for="item in courseTypeList"
                :key="item.value"
                :label="item.name"
                :value="item.name"
              />
            </el-select>
          </el-form-item>
          

          <el-form-item>
            <el-button type="primary" @click="loadCourseList()">{{
              t('search')
            }}</el-button>
            <el-button @click="resetForm(searchFormRef)">{{
              t('reset')
            }}</el-button>
			
			
			<el-button type="primary" @click="dictData">
			  添加课程类型
			</el-button>
          </el-form-item>
        </el-form>
      </el-card>

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

    <!--      <el-table-column
            prop="course_type"
            :label="t('courseType')"
            min-width="120"
            :show-overflow-tooltip="true"
          /> -->
		  
		  
		  <el-table-column :label="t('courseName')" min-width="120" align="center" :show-overflow-tooltip="true">
		      <template #default="{ row }">
		          <div v-for="(item, index) in courseTypeList">
		              <div v-if="item.value == row.course_type">{{ item.name }}</div>
		          </div>
		      </template>
		  </el-table-column>

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

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

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

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

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

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

          <el-table-column
            prop="remarks"
            :label="t('remarks')"
            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>
			  
			  <el-button type="primary" link @click="contractEvent(row)">关联合同</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="mt-[16px] flex justify-end">
          <el-pagination
            v-model:current-page="courseTable.page"
            v-model:page-size="courseTable.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="courseTable.total"
            @size-change="loadCourseList()"
            @current-change="loadCourseList"
          />
        </div>
      </div>

      <edit ref="editCourseDialog" @complete="loadCourseList" />
	  <contract ref="contractDialog" @complete="loadCourseList" />
	  
	  <dict ref="dictDialog" @complete="loadCourseList" />
	  
    </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 { getCourseList, deleteCourse } from '@/app/api/course'
import { img } from '@/utils/common'
import { ElMessageBox, FormInstance } from 'element-plus'
import Edit from '@/app/views/course/components/course-edit.vue'
import Contract from '@/app/views/course/components/change_course.vue'
import dict from '@/app/views/dict/components/dict.vue'
import { useRoute } from 'vue-router'
const route = useRoute()
const pageName = route.meta.title

let courseTable = reactive({
  page: 1,
  limit: 10,
  total: 0,
  loading: true,
  data: [],
  searchParam: {
    course_name: '',
    course_type: '',
    duration: '',
    session_count: '',
    single_session_count: '',
    price: '',
    internal_reminder: '',
    customer_reminder: '',
    remarks: '',
  },
})
// const courseTypeList = useDictionary('course_type')

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

const dictData = () => {
  dictDialog.value.setFormData({id:22,name:'课程类型'})
}




const courseTypeList = ref([])
const getcourseTypeList = async () => {
  courseTypeList.value = await (
    await useDictionary('course_type')
  ).data.dictionary
}
getcourseTypeList()

const searchFormRef = ref<FormInstance>()

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

// 字典数据

/**
 * 获取课程列表
 */
const loadCourseList = (page: number = 1) => {
  courseTable.loading = true
  courseTable.page = page

  getCourseList({
    page: courseTable.page,
    limit: courseTable.limit,
    ...courseTable.searchParam,
  })
    .then((res) => {
      courseTable.loading = false
      courseTable.data = res.data.data
      courseTable.total = res.data.total
    })
    .catch(() => {
      courseTable.loading = false
    })
}
loadCourseList()

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

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

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


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

const contractEvent = (data: any) => {
  contractDialog.value.setFormData(data)
  contractDialog.value.showDialog = true
}

/**
 * 删除课程
 */
const deleteEvent = (id: number) => {
  ElMessageBox.confirm(t('courseDeleteTips'), t('warning'), {
    confirmButtonText: t('confirm'),
    cancelButtonText: t('cancel'),
    type: 'warning',
  }).then(() => {
    deleteCourse(id)
      .then(() => {
        loadCourseList()
      })
      .catch(() => {})
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
  loadCourseList()
}
</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>
