<template>
    <!-- <x-search-bar class="mb-8-2">
        <template #default="{ gutter, colSpan }">
            <a-form :model="searchFormData" layout="inline">
                <a-row :gutter="gutter">
                    <a-col v-bind="colSpan">
                        <a-form-item label="登录账号" name="username">
                            <a-input placeholder="请输入登录账号" v-model:value="searchFormData.username"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col v-bind="colSpan">
                        <a-form-item label="用户名称" name="name">
                            <a-input placeholder="请输入用户名称" v-model:value="searchFormData.name"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col v-bind="colSpan">
                        <a-form-item :label="'状态'" name="status">
                            <a-select v-model:value="searchFormData.status" allowClear>
                                <a-select-option v-for="item of userStatus.getAll()" :value="item.value">{{
                                    item.label }}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col class="align-right" v-bind="colSpan">
                        <a-space>
                            <a-button @click="handleResetSearch">{{ $t('button.reset') }}</a-button>
                            <a-button ghost type="primary" @click="handleSearch">
                                {{ $t('button.search') }}
                            </a-button>
                        </a-space>
                    </a-col>
                </a-row>
            </a-form>
        </template>
    </x-search-bar> -->
    <a-row :gutter="8" :wrap="false">
        <a-col flex="auto">
            <a-card type="flex">
                <a-table :columns="columns" :data-source="listData" :loading="loading" bordered="true"
                    :pagination="paginationState" :scroll="{ x: 1000 }" @change="onTableChange">
                    <template #bodyCell="{ column, record }">
                        <template v-if="column.dataIndex === 'avatar'">
                            <a-image :width="45" :src="record.avatar ? config('http.apiBasic') + record.avatar : errImg" />
                        </template>
                        <template v-if="'status' === column.dataIndex">
                            <a-tag :color="userStatus.getColor(record.status)">{{
                                userStatus.getName(record.status)
                            }}</a-tag>
                        </template>
                        <template v-if="'createdAt' === column.dataIndex">
                            {{ dayjs(record.createdAt).format('YYYY-MM-DD') }}
                        </template>
                        
                        <template v-if="'action' === column.key">
                            <x-action-button @click="auditHandleEdit(record)">
                                <a-tooltip>
                                    <template #title> {{ '编辑' }}</template>
                                    <edit-outlined />
                                </a-tooltip>
                            </x-action-button>
                        </template>
                    </template>
                </a-table>
            </a-card>
        </a-col>
    </a-row>
    <a-modal :open="modal.open" :title="'编辑'" :width="640" :confirm-loading="modal.confirmLoading" 
        ok-text="确认" cancel-text="取消" @ok="handleAuditEdit" @cancel="hideModal()">
        <a-card class="mb-8-2">
            <a-form-item :label="'编辑'" name="">
                <a-radio-group v-model:value="auditStatus"
                    :options="userStatus.getAll()"></a-radio-group>
            </a-form-item>
            <!-- <a-form-item :label="'备注'" name="">
                <a-textarea v-model:value="remark"></a-textarea>
            </a-form-item> -->
        </a-card>
    </a-modal>
    <add-dialog ref="addDialogRef" @ok="onOk"></add-dialog>
</template>

<script setup>
import { message, Modal } from 'ant-design-vue'
import { ref } from 'vue'
import apis from '@/apis'
import { config } from '@/config'
import { userStatus } from '@/enums/index.js'
import { usePagination,useModal } from '@/hooks'
import { PlusOutlined, EditOutlined, DeleteOutlined } from '@ant-design/icons-vue'

import { useI18n } from 'vue-i18n'
import dayjs from 'dayjs'
import errImg from '@/assets/blankSpace.png'

defineOptions({
    name: 'systemRole',
})
const { t } = useI18n() // 解构出t方法
const columns = [
    { title: '头像', dataIndex: 'avatar', key: 'avatar', with: 50, align: 'center' },
    { title: '用户名', dataIndex: 'name', key: 'name'},
    { title: '手机号', dataIndex: 'phone'},
    { title: '简介', dataIndex: 'introduce',align: 'center' },
    { title: '状态', key: 'status', dataIndex: 'status', width: 120, align: 'center' },
    { title: '创建时间', dataIndex: 'createdAt', width: 200, align: 'center' },
    { title: t('button.action'), key: 'action', fixed: 'right', width: 140, align: 'center' },
]
const { modal, showModal, hideModal, showLoading:auditShowLoading, hideLoading:auditHideLoading } = useModal()
const { listData, loading, showLoading, hideLoading, paginationState, searchFormData, resetPagination } = usePagination()
// const { resetForm } = useForm()
const addDialogRef = ref()
const currentForm=ref({})
const auditStatus=ref(2)
const remark=ref('')
getPageList()

/**
 * 获取公公告列表
 * @returns {Promise<void>}
 */
async function getPageList() {
    try {
        showLoading()
        const { pageSize, current } = paginationState
        const { success, data, total } = await apis.customer
            .getDataList({
                pageSize,
                current: current,
                ...searchFormData.value,
            })
            .catch(() => {
                throw new Error()
            })
        hideLoading()
        if (config('http.code.success') === success) {
            listData.value = data
            paginationState.total = total
        }
    } catch (error) {
        hideLoading()
    }
}

/**
 * 移除
 */
function handleRemove({ id }) {
    Modal.confirm({
        title: t('pages.system.role.delTip'),
        content: t('button.confirm'),
        okText: t('button.confirm'),
        onOk: () => {
            return new Promise((resolve, reject) => {
                ; (async () => {
                    try {
                        const { success } = await apis.customer.delNotices(id).catch(() => {
                            throw new Error()
                        })
                        if (config('http.code.success') === success) {
                            resolve()
                            message.success(t('component.message.success.delete'))
                            await getPageList()
                        }
                    } catch (error) {
                        reject()
                    }
                })()
            })
        },
    })
}
const auditHandleEdit = (params) => {
    currentForm.value = params
    auditStatus.value=params.status
    showModal()
}
const handleAuditEdit = async () => {
    try {
        auditShowLoading()
        const params = {
            ...currentForm.value,
            status: auditStatus.value,
        }
        console.log(params);
        const result = await apis.customer.updateMenu(currentForm.value.id, params).catch(() => {
            throw new Error()
        })
        auditHideLoading()
        if (config('http.code.success') === result?.success) {
            hideModal()
            getPageList()
            message.success('编辑成功')
        }
    } catch (error) {
        message.error(error.message)
        auditHideLoading()
    }
}
/**
 * 分页
 */
function onTableChange({ current, pageSize }) {
    paginationState.current = current
    paginationState.pageSize = pageSize
    getPageList()
}

/**
 * 重置
 */
function handleResetSearch() {
    searchFormData.value = {}
    resetPagination()
    getPageList()
}
/**
 * 搜索
 */
function handleSearch() {
    resetPagination()
    getPageList()
}

/**
 * 编辑完成
 */
async function onOk() {
    await getPageList()
}
</script>

<style lang="less" scoped></style>