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

            <el-card class="box-card !border-none my-[10px] table-search-wrap" shadow="never">
                <el-form :inline="true" :model="studentCoursesTable.searchParam" ref="searchFormRef">
                    
                    <el-form-item :label="t('studentId')" prop="student_id">
                        <el-select class="w-[280px]" v-model="studentCoursesTable.searchParam.student_id" clearable :placeholder="t('studentIdPlaceholder')">
                           <el-option
                                       v-for="(item, index) in studentIdList"
                                       :key="index"
                                       :label="item['name']"
                                       :value="item['id']"
                                   />
                        </el-select>
                    </el-form-item>
                    
                    
                    <el-form-item :label="t('courseId')" prop="course_id">
                        <el-select class="w-[280px]" v-model="studentCoursesTable.searchParam.course_id" clearable :placeholder="t('courseIdPlaceholder')">
                           <el-option
                                       v-for="(item, index) in courseIdList"
                                       :key="index"
                                       :label="item['course_name']"
                                       :value="item['id']"
                                   />
                        </el-select>
                    </el-form-item>
                    

                    <el-form-item>
                        <el-button type="primary" @click="loadStudentCoursesList()">{{ 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="studentCoursesTable.data" size="large" v-loading="studentCoursesTable.loading">
                    <template #empty>
                        <span>{{ !studentCoursesTable.loading ? t('emptyData') : '' }}</span>
                    </template>
                    <el-table-column prop="student_id_name" :label="t('studentId')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="course_id_name" :label="t('courseId')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="total_hours" :label="t('totalHours')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="gift_hours" :label="t('giftHours')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="start_date" :label="t('startDate')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="end_date" :label="t('endDate')" 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="studentCoursesTable.page" v-model:page-size="studentCoursesTable.limit"
                        layout="total, sizes, prev, pager, next, jumper" :total="studentCoursesTable.total"
                        @size-change="loadStudentCoursesList()" @current-change="loadStudentCoursesList" />
                </div>
            </div>

            <edit ref="editStudentCoursesDialog" @complete="loadStudentCoursesList" />
        </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 { getStudentCoursesList, deleteStudentCourses, getWithStudentList, getWithCourseList } from '@/app/api/student_courses'
import { img } from '@/utils/common'
import { ElMessageBox,FormInstance } from 'element-plus'
import Edit from '@/app/views/student_courses/components/student-courses-edit.vue'
import { useRoute } from 'vue-router'
const route = useRoute()
const pageName = route.meta.title;

let studentCoursesTable = reactive({
    page: 1,
    limit: 10,
    total: 0,
    loading: true,
    data: [],
    searchParam:{
      "student_id":"",
      "course_id":""
    }
})

const searchFormRef = ref<FormInstance>()

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

// 字典数据
    

/**
 * 获取学员课程列表
 */
const loadStudentCoursesList = (page: number = 1) => {
    studentCoursesTable.loading = true
    studentCoursesTable.page = page

    getStudentCoursesList({
        page: studentCoursesTable.page,
        limit: studentCoursesTable.limit,
         ...studentCoursesTable.searchParam
    }).then(res => {
        studentCoursesTable.loading = false
        studentCoursesTable.data = res.data.data
        studentCoursesTable.total = res.data.total
    }).catch(() => {
        studentCoursesTable.loading = false
    })
}
loadStudentCoursesList()

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

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

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

/**
 * 删除学员课程
 */
const deleteEvent = (id: number) => {
    ElMessageBox.confirm(t('studentCoursesDeleteTips'), t('warning'),
        {
            confirmButtonText: t('confirm'),
            cancelButtonText: t('cancel'),
            type: 'warning',
        }
    ).then(() => {
        deleteStudentCourses(id).then(() => {
            loadStudentCoursesList()
        }).catch(() => {
        })
    })
}

    
    const studentIdList = ref([])
    const setStudentIdList = async () => {
    studentIdList.value = await (await getWithStudentList({})).data
    }
    setStudentIdList()
    const courseIdList = ref([])
    const setCourseIdList = async () => {
    courseIdList.value = await (await getWithCourseList({})).data
    }
    setCourseIdList()

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