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

            <el-card class="box-card !border-none my-[10px] table-search-wrap" shadow="never">
                <el-form :inline="true" :model="campusesTable.searchParam" ref="searchFormRef">
                    <el-form-item :label="t('name')" prop="name">
                        <el-input v-model="campusesTable.searchParam.name" :placeholder="t('namePlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('coordinate')" prop="coordinate">
                        <el-input v-model="campusesTable.searchParam.coordinate" :placeholder="t('coordinatePlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('address')" prop="address">
                        <el-input v-model="campusesTable.searchParam.address" :placeholder="t('addressPlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('contactPerson')" prop="contact_person">
                        <el-input v-model="campusesTable.searchParam.contact_person" :placeholder="t('contactPersonPlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('contactPhone')" prop="contact_phone">
                        <el-input v-model="campusesTable.searchParam.contact_phone" :placeholder="t('contactPhonePlaceholder')" />
                    </el-form-item>
                    
                    <el-form-item :label="t('status')" prop="status">
                        <el-select class="w-[280px]" v-model="campusesTable.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="loadCampusesList()">{{ 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="campusesTable.data" size="large" v-loading="campusesTable.loading">
                    <template #empty>
                        <span>{{ !campusesTable.loading ? t('emptyData') : '' }}</span>
                    </template>
                    <el-table-column prop="name" :label="t('name')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="coordinate" :label="t('coordinate')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="address" :label="t('address')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="contact_person" :label="t('contactPerson')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="contact_phone" :label="t('contactPhone')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <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="campusesTable.page" v-model:page-size="campusesTable.limit"
                        layout="total, sizes, prev, pager, next, jumper" :total="campusesTable.total"
                        @size-change="loadCampusesList()" @current-change="loadCampusesList" />
                </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 { getCampusesList, deleteCampuses } from '@/addon/zhjw/api/campuses'
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 campusesTable = reactive({
    page: 1,
    limit: 10,
    total: 0,
    loading: true,
    data: [],
    searchParam:{
      "name":"",
      "coordinate":"",
      "address":"",
      "contact_person":"",
      "contact_phone":"",
      "status":""
    }
})

const searchFormRef = ref<FormInstance>()

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

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


/**
 * 获取校区管理列表
 */
const loadCampusesList = (page: number = 1) => {
    campusesTable.loading = true
    campusesTable.page = page

    getCampusesList({
        page: campusesTable.page,
        limit: campusesTable.limit,
         ...campusesTable.searchParam
    }).then(res => {
        campusesTable.loading = false
        campusesTable.data = res.data.data
        campusesTable.total = res.data.total
    }).catch(() => {
        campusesTable.loading = false
    })
}
loadCampusesList()

const router = useRouter()

/**
 * 添加校区管理
 */
const addEvent = () => {
    router.push('/campuses/campuses_edit')
}

/**
 * 编辑校区管理
 * @param data
 */
const editEvent = (data: any) => {
    router.push('/campuses/campuses_edit?id='+data.id)
}

/**
 * 删除校区管理
 */
const deleteEvent = (id: number) => {
    ElMessageBox.confirm(t('campusesDeleteTips'), t('warning'),
        {
            confirmButtonText: t('confirm'),
            cancelButtonText: t('cancel'),
            type: 'warning',
        }
    ).then(() => {
        deleteCampuses(id).then(() => {
            loadCampusesList()
        }).catch(() => {
        })
    })
}

    

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