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

            <el-card class="box-card !border-none my-[10px] table-search-wrap" shadow="never">
                <el-form :inline="true" :model="timetablesTable.searchParam" ref="searchFormRef">
                    
                    <el-form-item :label="t('classId')" prop="class_id">
                        <el-select class="w-[280px]" v-model="timetablesTable.searchParam.class_id" clearable :placeholder="t('classIdPlaceholder')">
                           <el-option
                                       v-for="(item, index) in classIdList"
                                       :key="index"
                                       :label="item['name']"
                                       :value="item['id']"
                                   />
                        </el-select>
                    </el-form-item>
                    
                    
                    <el-form-item :label="t('cycle')" prop="cycle">
                        <el-select class="w-[280px]" v-model="timetablesTable.searchParam.cycle" clearable :placeholder="t('cyclePlaceholder')">
                            <el-option label="全部" value=""></el-option>
                            <el-option
                                v-for="(item, index) in cycleList"
                                :key="index"
                                :label="item.name"
                                :value="item.value"
                            />
                        </el-select>
                    </el-form-item>
                    

                    <el-form-item>
                        <el-button type="primary" @click="loadTimetablesList()">{{ 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="timetablesTable.data" size="large" v-loading="timetablesTable.loading">
                    <template #empty>
                        <span>{{ !timetablesTable.loading ? t('emptyData') : '' }}</span>
                    </template>
                    <el-table-column prop="class_id_name" :label="t('classId')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column :label="t('cycle')" min-width="180" align="center" :show-overflow-tooltip="true">
                        <template #default="{ row }">
                            <div v-for="(item, index) in cycleList">
                                <div v-if="item.value == row.cycle">{{ item.name }}</div>
                            </div>
                        </template>
                    </el-table-column>
                    

                    <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="timetablesTable.page" v-model:page-size="timetablesTable.limit"
                        layout="total, sizes, prev, pager, next, jumper" :total="timetablesTable.total"
                        @size-change="loadTimetablesList()" @current-change="loadTimetablesList" />
                </div>
            </div>

            
        </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 { getTimetablesList, deleteTimetables, getWithClassesList } from '@/addon/zhjw/api/timetables'
import { img } from '@/utils/common'
import { ElMessageBox,FormInstance } from 'element-plus'
import { useRouter } from 'vue-router'
import { useRoute } from 'vue-router'
const route = useRoute()
const pageName = route.meta.title;

let timetablesTable = reactive({
    page: 1,
    limit: 10,
    total: 0,
    loading: true,
    data: [],
    searchParam:{
      "class_id":"",
      "cycle":""
    }
})

const searchFormRef = ref<FormInstance>()

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

// 字典数据
    const cycleList = ref([] as any[])
    const cycleDictList = async () => {
    cycleList.value = await (await useDictionary('cycle')).data.dictionary
    }
    cycleDictList();


/**
 * 获取课表管理列表
 */
const loadTimetablesList = (page: number = 1) => {
    timetablesTable.loading = true
    timetablesTable.page = page

    getTimetablesList({
        page: timetablesTable.page,
        limit: timetablesTable.limit,
         ...timetablesTable.searchParam
    }).then(res => {
        timetablesTable.loading = false
        timetablesTable.data = res.data.data
        timetablesTable.total = res.data.total
    }).catch(() => {
        timetablesTable.loading = false
    })
}
loadTimetablesList()

const router = useRouter()

/**
 * 添加课表管理
 */
const addEvent = () => {
    router.push('/timetables/timetables_edit')
}

/**
 * 编辑课表管理
 * @param data
 */
const editEvent = (data: any) => {
    router.push('/timetables/timetables_edit?id='+data.id)
}

/**
 * 删除课表管理
 */
const deleteEvent = (id: number) => {
    ElMessageBox.confirm(t('timetablesDeleteTips'), t('warning'),
        {
            confirmButtonText: t('confirm'),
            cancelButtonText: t('cancel'),
            type: 'warning',
        }
    ).then(() => {
        deleteTimetables(id).then(() => {
            loadTimetablesList()
        }).catch(() => {
        })
    })
}

    
    const classIdList = ref([])
    const setClassIdList = async () => {
    classIdList.value = await (await getWithClassesList({})).data
    }
    setClassIdList()

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