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

            <el-card class="box-card !border-none my-[10px] table-search-wrap" shadow="never">
                <el-form :inline="true" :model="campusTable.searchParam" ref="searchFormRef">
                    <el-form-item :label="t('campusName')" prop="campus_name">
                        <el-input v-model="campusTable.searchParam.campus_name" :placeholder="t('campusNamePlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('campusAddress')" prop="campus_address">
                        <el-input v-model="campusTable.searchParam.campus_address" :placeholder="t('campusAddressPlaceholder')" />
                    </el-form-item>
                    
                    <el-form-item :label="t('campusStatus')" prop="campus_status">
                        <el-select class="w-[280px]" v-model="campusTable.searchParam.campus_status" clearable :placeholder="t('campusStatusPlaceholder')">
                            <el-option label="全部" value=""></el-option>
                            <el-option
                                v-for="(item, index) in campus_statusList"
                                :key="index"
                                :label="item.name"
                                :value="item.value"
                            />
                        </el-select>
                    </el-form-item>
                    

                    <el-form-item>
                        <el-button type="primary" @click="loadCampusList()">{{ 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="campusTable.data" size="large" v-loading="campusTable.loading">
                    <template #empty>
                        <span>{{ !campusTable.loading ? t('emptyData') : '' }}</span>
                    </template>
                    <el-table-column prop="campus_name" :label="t('campusName')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="campus_address" :label="t('campusAddress')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column :label="t('campusStatus')" min-width="180" align="center" :show-overflow-tooltip="true">
                        <template #default="{ row }">
                            <div v-for="(item, index) in campus_statusList">
                                <div v-if="item.value == row.campus_status">{{ item.name }}</div>
                            </div>
                        </template>
                    </el-table-column>
                    
                    <el-table-column prop="create_time" :label="t('createTime')" 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="campusTable.page" v-model:page-size="campusTable.limit"
                        layout="total, sizes, prev, pager, next, jumper" :total="campusTable.total"
                        @size-change="loadCampusList()" @current-change="loadCampusList" />
                </div>
            </div>

            <edit ref="editCampusDialog" @complete="loadCampusList" />
        </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 { getCampusList, deleteCampus } from '@/app/api/campus'
import { img } from '@/utils/common'
import { ElMessageBox,FormInstance } from 'element-plus'
import Edit from '@/app/views/campus/components/campus-edit.vue'
import { useRoute } from 'vue-router'
const route = useRoute()
const pageName = route.meta.title;

let campusTable = reactive({
    page: 1,
    limit: 10,
    total: 0,
    loading: true,
    data: [],
    searchParam:{
      "campus_name":"",
      "campus_address":"",
      "campus_status":""
    }
})

const searchFormRef = ref<FormInstance>()

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

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


/**
 * 获取校区列表
 */
const loadCampusList = (page: number = 1) => {
    campusTable.loading = true
    campusTable.page = page

    getCampusList({
        page: campusTable.page,
        limit: campusTable.limit,
         ...campusTable.searchParam
    }).then(res => {
        campusTable.loading = false
        campusTable.data = res.data.data
        campusTable.total = res.data.total
    }).catch(() => {
        campusTable.loading = false
    })
}
loadCampusList()

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

/**
 * 添加校区
 */
const addEvent = () => {
    editCampusDialog.value.setFormData()
    editCampusDialog.value.showDialog = true
}

/**
 * 编辑校区
 * @param data
 */
const editEvent = (data: any) => {
    editCampusDialog.value.setFormData(data)
    editCampusDialog.value.showDialog = true
}

/**
 * 删除校区
 */
const deleteEvent = (id: number) => {
    ElMessageBox.confirm(t('campusDeleteTips'), t('warning'),
        {
            confirmButtonText: t('confirm'),
            cancelButtonText: t('cancel'),
            type: 'warning',
        }
    ).then(() => {
        deleteCampus(id).then(() => {
            loadCampusList()
        }).catch(() => {
        })
    })
}

    

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