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

            <el-card class="box-card !border-none my-[10px] table-search-wrap" shadow="never">
                <el-form :inline="true" :model="serviceTable.searchParam" ref="searchFormRef">
                    <el-form-item :label="t('serviceName')" prop="service_name">
                        <el-input v-model="serviceTable.searchParam.service_name" :placeholder="t('serviceNamePlaceholder')" />
                    </el-form-item>
                    
                    <el-form-item :label="t('serviceType')" prop="service_type">
                        <el-select class="w-[280px]" v-model="serviceTable.searchParam.service_type" clearable :placeholder="t('serviceTypePlaceholder')">
                            <el-option label="全部" value=""></el-option>
                            <el-option
                                v-for="(item, index) in service_typeList"
                                :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="serviceTable.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="loadServiceList()">{{ 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="serviceTable.data" size="large" v-loading="serviceTable.loading">
                    <template #empty>
                        <span>{{ !serviceTable.loading ? t('emptyData') : '' }}</span>
                    </template>
                    <el-table-column prop="service_name" :label="t('serviceName')" 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 :label="t('serviceType')" min-width="180" align="center" :show-overflow-tooltip="true">
                        <template #default="{ row }">
                            <div v-for="(item, index) in service_typeList">
                                <div v-if="item.value == row.service_type">{{ item.name }}</div>
                            </div>
                        </template>
                    </el-table-column>
                    
                    <el-table-column prop="customer_feedback" :label="t('customerFeedback')" 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="serviceTable.page" v-model:page-size="serviceTable.limit"
                        layout="total, sizes, prev, pager, next, jumper" :total="serviceTable.total"
                        @size-change="loadServiceList()" @current-change="loadServiceList" />
                </div>
            </div>

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

let serviceTable = reactive({
    page: 1,
    limit: 10,
    total: 0,
    loading: true,
    data: [],
    searchParam:{
      "service_name":"",
      "service_type":"",
      "status":""
    }
})

const searchFormRef = ref<FormInstance>()

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

// 字典数据
    const service_typeList = ref([] as any[])
    const service_typeDictList = async () => {
    service_typeList.value = await (await useDictionary('service_type')).data.dictionary
    }
    service_typeDictList();
    const staff_reminderList = ref([] as any[])
    const staff_reminderDictList = async () => {
    staff_reminderList.value = await (await useDictionary('global_true_or_false')).data.dictionary
    }
    staff_reminderDictList();
    const customer_reminderList = ref([] as any[])
    const customer_reminderDictList = async () => {
    customer_reminderList.value = await (await useDictionary('global_true_or_false')).data.dictionary
    }
    customer_reminderDictList();
    const customer_confirmationList = ref([] as any[])
    const customer_confirmationDictList = async () => {
    customer_confirmationList.value = await (await useDictionary('global_true_or_false')).data.dictionary
    }
    customer_confirmationDictList();
    const statusList = ref([] as any[])
    const statusDictList = async () => {
    statusList.value = await (await useDictionary('SiteStatus')).data.dictionary
    }
    statusDictList();


/**
 * 获取服务列表
 */
const loadServiceList = (page: number = 1) => {
    serviceTable.loading = true
    serviceTable.page = page

    getServiceList({
        page: serviceTable.page,
        limit: serviceTable.limit,
         ...serviceTable.searchParam
    }).then(res => {
        serviceTable.loading = false
        serviceTable.data = res.data.data
        serviceTable.total = res.data.total
    }).catch(() => {
        serviceTable.loading = false
    })
}
loadServiceList()

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

/**
 * 添加服务
 */
const addEvent = () => {
    editServiceDialog.value.setFormData()
    editServiceDialog.value.showDialog = true
}

/**
 * 编辑服务
 * @param data
 */
const editEvent = (data: any) => {
    editServiceDialog.value.setFormData(data)
    editServiceDialog.value.showDialog = true
}

/**
 * 删除服务
 */
const deleteEvent = (id: number) => {
    ElMessageBox.confirm(t('serviceDeleteTips'), t('warning'),
        {
            confirmButtonText: t('confirm'),
            cancelButtonText: t('cancel'),
            type: 'warning',
        }
    ).then(() => {
        deleteService(id).then(() => {
            loadServiceList()
        }).catch(() => {
        })
    })
}

    

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