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

            <el-card class="box-card !border-none my-[10px] table-search-wrap" shadow="never">
                <el-form :inline="true" :model="coursesTable.searchParam" ref="searchFormRef">
                    <el-form-item :label="t('name')" prop="name">
                        <el-input v-model="coursesTable.searchParam.name" :placeholder="t('namePlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('targetGroup')" prop="target_group">
                        <el-input v-model="coursesTable.searchParam.target_group" :placeholder="t('targetGroupPlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('tastePrice')" prop="taste_price">
                        <el-input v-model="coursesTable.searchParam.taste_price" :placeholder="t('tastePricePlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('price')" prop="price">
                        <el-input v-model="coursesTable.searchParam.price" :placeholder="t('pricePlaceholder')" />
                    </el-form-item>
                    
                    <el-form-item :label="t('isAutomaticSigning')" prop="is_automatic_signing">
                        <el-select class="w-[280px]" v-model="coursesTable.searchParam.is_automatic_signing" clearable :placeholder="t('isAutomaticSigningPlaceholder')">
                            <el-option label="全部" value=""></el-option>
                            <el-option
                                v-for="(item, index) in is_automatic_signingList"
                                :key="index"
                                :label="item.name"
                                :value="item.value"
                            />
                        </el-select>
                    </el-form-item>
                    
                    
                    <el-form-item :label="t('status')" prop="status">
                        <el-select class="w-[280px]" v-model="coursesTable.searchParam.status" clearable :placeholder="t('statusPlaceholder')">
                            <el-option label="全部" value=""></el-option>
                            <el-option
                                v-for="(item, index) in statusList"
                                :key="index"
                                :label="item.name"
                                :value="item.value"
                            />
                        </el-select>
                    </el-form-item>
                    

                    <el-form-item>
                        <el-button type="primary" @click="loadCoursesList()">{{ 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="coursesTable.data" size="large" v-loading="coursesTable.loading">
                    <template #empty>
                        <span>{{ !coursesTable.loading ? t('emptyData') : '' }}</span>
                    </template>
                    <el-table-column prop="name" :label="t('name')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="description" :label="t('description')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="target_group" :label="t('targetGroup')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="duration" :label="t('duration')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="taste_price" :label="t('tastePrice')" 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 :label="t('isAutomaticSigning')" min-width="180" align="center" :show-overflow-tooltip="true">
                        <template #default="{ row }">
                            <div v-for="(item, index) in is_automatic_signingList">
                                <div v-if="item.value == row.is_automatic_signing">{{ item.name }}</div>
                            </div>
                        </template>
                    </el-table-column>
                    
                    <el-table-column :label="t('status')" min-width="180" align="center" :show-overflow-tooltip="true">
                        <template #default="{ row }">
                            <div v-for="(item, index) in statusList">
                                <div v-if="item.value == row.status">{{ 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="coursesTable.page" v-model:page-size="coursesTable.limit"
                        layout="total, sizes, prev, pager, next, jumper" :total="coursesTable.total"
                        @size-change="loadCoursesList()" @current-change="loadCoursesList" />
                </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 { getCoursesList, deleteCourses } from '@/addon/zhjw/api/courses'
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 coursesTable = reactive({
    page: 1,
    limit: 10,
    total: 0,
    loading: true,
    data: [],
    searchParam:{
      "name":"",
      "description":"",
      "target_group":"",
      "taste_price":"",
      "price":"",
      "is_automatic_signing":"",
      "status":""
    }
})

const searchFormRef = ref<FormInstance>()

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

// 字典数据
    const is_automatic_signingList = ref([] as any[])
    const is_automatic_signingDictList = async () => {
    is_automatic_signingList.value = await (await useDictionary('is_radio')).data.dictionary
    }
    is_automatic_signingDictList();
    const statusList = ref([] as any[])
    const statusDictList = async () => {
    statusList.value = await (await useDictionary('config_status')).data.dictionary
    }
    statusDictList();


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

    getCoursesList({
        page: coursesTable.page,
        limit: coursesTable.limit,
         ...coursesTable.searchParam
    }).then(res => {
        coursesTable.loading = false
        coursesTable.data = res.data.data
        coursesTable.total = res.data.total
    }).catch(() => {
        coursesTable.loading = false
    })
}
loadCoursesList()

const router = useRouter()

/**
 * 添加课程管理
 */
const addEvent = () => {
    router.push('/courses/courses_edit')
}

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

/**
 * 删除课程管理
 */
const deleteEvent = (id: number) => {
    ElMessageBox.confirm(t('coursesDeleteTips'), t('warning'),
        {
            confirmButtonText: t('confirm'),
            cancelButtonText: t('cancel'),
            type: 'warning',
        }
    ).then(() => {
        deleteCourses(id).then(() => {
            loadCoursesList()
        }).catch(() => {
        })
    })
}

    

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