<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="phone">
                            <a-input :placeholder="'请输入用户联系方式'" v-model:value="searchFormData.phone"></a-input>
                        </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">
                <x-action-bar class="mb-8-2">
                    <!-- <a-button v-action="'add'" type="primary" @click="$refs.editDialogRef.handleCreate()">
                        <template #icon>
                            <plus-outlined></plus-outlined>
                        </template>
                        添加图片
                    </a-button> -->
                </x-action-bar>
                <a-table :columns="columns" :data-source="listData" :loading="loading" :pagination="paginationState"
                    :scroll="{ x: 1000 }" @change="onTableChange">
                    <template #bodyCell="{ column, record }">
                        <template v-if="'banner_type' === column.key">
                            <!--状态-->
                            <a-tag v-if="record.banner_type == 1" color="processing">
                                首页轮播图
                            </a-tag>
                            <!--状态-->
                            <a-tag v-else color="processing">
                                营销活动图
                            </a-tag>
                        </template>

                        <template v-if="'is_recommend' === column.key">
                            <!--状态-->
                            <a-tag v-if="record.banner_type == true" color="processing">
                                启用
                            </a-tag>
                            <!--状态-->
                            <a-tag v-else color="processing">
                                禁用
                            </a-tag>
                        </template>


                        <template v-if="'createAt' === column.key">
                            {{ formatUtcDateTime(record.created_at) }}
                        </template>

                        <template v-if="'action' === column.key">
                            <!-- <x-action-button @click="$refs.editDialogRef.handleEdit(record)">
                                <a-tooltip>
                                    <template #title> {{ $t('pages.system.role.edit') }}</template>
                                    <edit-outlined />
                                </a-tooltip>
                            </x-action-button> -->
                            <x-action-button @click="handleRemove(record)">
                                封禁
                            </x-action-button>
                             <x-action-button @click="handleWater(record)">
                                流水
                            </x-action-button>
                              <x-action-button @click="handleHost(record)">
                                实例
                            </x-action-button>
                              <x-action-button @click="handleOrder(record)">
                                实例订单
                            </x-action-button>
                        </template>
                    </template>
                </a-table>
            </a-card>
        </a-col>
    </a-row>
 
    <edit-dialog ref="editDialogRef" @ok="onOk"></edit-dialog>
    <water-dialog ref="waterDialogRef" @ok="onOk"></water-dialog>
    <order-dialog ref="orderDialogRef" @ok="onOk"></order-dialog>
    <host-case-dialog ref="hostCaseDialogRef" @ok="onOk"></host-case-dialog>
</template>

<script setup>
import { message, Modal } from 'ant-design-vue'
import { ref } from 'vue'
import apis from '@/apis'
import { formatUtcDateTime } from '@/utils/util'
import { config } from '@/config'
import { statusTypeEnum } from '@/enums/system'
import { usePagination, useForm } from '@/hooks'
import EditDialog from './components/EditDialog.vue'
import WaterDialog from './components/WaterDialog.vue'
import OrderDialog from './components/OrderDialog.vue'
import HostCaseDialog from './components/HostCaseDialog.vue'
import { PlusOutlined, EditOutlined, DeleteOutlined } from '@ant-design/icons-vue'
import { useI18n } from 'vue-i18n'
import { disabledDict, authenticationTypeDict } from '@/enums/dict'

defineOptions({
    name: 'systemRole',
})
const { t } = useI18n() // 解构出t方法
const columns = [
    { title: '用户姓名', dataIndex: 'userName', fixed: 'left', width: 200 },
    { title: '用户联系方式', dataIndex: 'phone', width: 150 },
    {
        title: '状态',
        dataIndex: 'status',
        key: 'status',
        width: 150,
        customRender: ({ text }) => disabledDict.getLabel(text) || text,
    },
    {
        title: '账号类型',
        key: 'accountType',
        dataIndex: 'accountType',
        width: 120,
        customRender: ({ text }) => authenticationTypeDict.getLabel(text) || text,
    },
    { title: '余额', key: 'balance', dataIndex: 'balance', width: 120 },
    { title: '算力点', key: 'point', dataIndex: 'point', width: 120 },
    { title: t('button.action'), key: 'action', fixed: 'right', width: 250 },
]

const { listData, loading, showLoading, hideLoading, paginationState, searchFormData, resetPagination } =
    usePagination()
const { resetForm } = useForm()
const editDialogRef = ref()
const waterDialogRef = ref()
const orderDialogRef = ref()
const hostCaseDialogRef = ref()

getPageList()

/**
 * 获取用户列表
 * @returns {Promise<void>}
 */
async function getPageList() {
    try {
        showLoading()
        const { pageSize, current } = paginationState
        const { data, total } = await apis.userControl
            .getCustomersList({
                pageSize,
                current: current,
                ...searchFormData.value,
            })
            .catch(() => {
                throw new Error()
            })
        hideLoading()
        if (data.length > 0) {
            listData.value = data
            paginationState.total = total
        }
    } catch (error) {
        hideLoading()
    }
}

/**
 * 移除
 */
function handleRemove({ id, status }) {
    console.log(id, status)
    if( status == 'DISABLED' ){
        message.error('该用户已被禁用，请勿重复操作')
        return
    }
    Modal.confirm({
        title: '确认操作',
        content: '确认拉黑该用户？',
        okText: t('button.confirm'),
        onOk: () => {
             editDialogRef.value?.handleEdit(id)
        },
    })
}

// 流水
function handleWater({ id }) {
     waterDialogRef.value?.handleWater(id)
}

// 实例
function handleHost({ id }) {
    console.log(id)
    hostCaseDialogRef.value?.handleHost(id)
}   

// 订单
function handleOrder({ id }) {
    console.log(id)
    orderDialogRef.value?.handleOrder(id)
}   
/**
 * 分页
 */
function onTableChange({ current, pageSize }) {
    paginationState.current = current
    paginationState.pageSize = pageSize
    getPageList()
}

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

/**
 * 搜索
 */
function handleSearch() {
    // resetForm()
    // resetPagination()
    getPageList()
}

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

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