<template>
    <div class="post-lists">
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="客户名称">
                    <el-input class="w-[280px]" v-model="queryParams.account" clearable placeholder="请输入" @keyup.enter="resetPage" />
                </el-form-item>
                <el-form-item label="账号状态">
                    <el-select class="w-[280px]" v-model="queryParams.status">
                        <el-option label="全部" value />
                        <el-option label="正常" :value="0" />
                        <el-option label="停用" :value="1" />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <div>
                <el-button v-perms="['system:post:add']" type="primary" @click="handleAdd()">
                    <template #icon>
                        <icon name="el-icon-Plus" />
                    </template>
                    新增
                </el-button>
            </div>
            <el-table class="mt-4" size="large" v-loading="pager.loading" :data="pager.lists">
                <el-table-column label="客户名称" prop="clientName" min-width="100" />
                <el-table-column label="账号名额" prop="quota" min-width="100">
                    <template #default="{ row }">
                        <span class="text-primary">{{ row.quota }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="已使用" prop="quantity" />
                <el-table-column label="未使用" prop="">
                    <template #default="{ row }">
                        <span>{{ row.quota - row.quantity }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="账号状态" prop="status" width="180">
                    <template #default="{ row }">
                        <el-tag class="ml-2" :type="tagType(row.status)">
                            {{ parseStatusText(row.status) }}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="账号有效期" prop="" width="350">
                    <template #default="{ row }">
                        <span>{{ formatDate(row.startTime, 'YYYY-MM-DD HH:mm:ss') }}</span>
                        至
                        <span>{{ formatDate(row.expirationTime, 'YYYY-MM-DD HH:mm:ss') }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="180" fixed="right">
                    <template #default="{ row }">
                        <el-button type="primary" link @click="handleEdit(row)">编辑</el-button>
                        <el-button type="primary" link :disabled="isDisabled(row.status)" @click="handleChangeStatus(row)">
                            {{ btnText(row.status) }}
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>
<script lang="ts" setup name="post">
import EditPopup from './edit.vue'
import { usePaging } from '@/hooks/usePaging'
import { accountList, accountStatus } from '@/api/account_center'
import { StatusEnum } from '@/enums'
import feedback from '@/utils/feedback'
import dayjs from 'dayjs'

const statusMap: Record<StatusEnum, string> = {
    [StatusEnum.Normal]: '启用',
    [StatusEnum.Stop]: '停用',
    [StatusEnum.Expired]: '已过期'
}
const parseStatusText = computed(() => (status: StatusEnum) => statusMap[status])
const isDisabled = computed(() => (status: StatusEnum) => status == StatusEnum.Expired)
const btnText = computed(() => (status: StatusEnum) => status == StatusEnum.Stop ? '启用' : '停用')
const tagType = computed(() => (status: StatusEnum) => status == StatusEnum.Normal ? 'primary' : status == StatusEnum.Stop ? 'danger' : 'info')

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const queryParams = reactive({
    account: '',
    status: ''
})

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: accountList,
    params: queryParams
})

const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
}

const handleChangeStatus = async row => {
    const { id, status } = row
    const msg = status == StatusEnum.Normal ? '停用' : '启用'
    const statusVal = status == StatusEnum.Normal ? StatusEnum.Stop : StatusEnum.Normal
    await feedback.confirm(`确定要${msg}该账号？`)
    await accountStatus({ id, status: statusVal })
    feedback.msgSuccess(`${msg}成功`)
    getLists()
}
const formatDate = (value: string, format = 'YYYY-MM-DD HH:mm:ss') => {
    return dayjs(value).format(format)
}
getLists()
</script>
