<template>
    <x-search-bar class="mb-8-2">
        <template #default="{ gutter, colSpan }">
            <a-form :label-col="{ style: { width: '100px' } }" :model="searchFormData" layout="inline">
                <a-row :gutter="gutter">
                    <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 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 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="'status' === column.dataIndex">
                            <a-tag v-if="record.status === 'enabled'" :color="'green'">启用</a-tag>
                            <a-tag v-if="record.status === 'disabled'" :color="'red'">停用</a-tag>
                        </template>
                        <template v-if="'action' === column.key">
                            <x-action-button @click="$refs.editDialogRef.handleEdit(record)">
                                <a-tooltip>
                                    <template #title> {{ $t('pages.system.user.edit') }}</template>
                                    <edit-outlined /> </a-tooltip></x-action-button>
                            <x-action-button @click="handleDetail(record)">
                                <a-tooltip>
                                    <template #title>字典数据</template>
                                    <plus-outlined /> </a-tooltip></x-action-button>
                            <x-action-button @click="handleDelete(record)">
                                <a-tooltip>
                                    <template #title>{{ $t('pages.system.delete') }}</template>
                                    <delete-outlined style="color: #ff4d4f" /> </a-tooltip></x-action-button>
                        </template>
                    </template>
                </a-table>
            </a-card>
        </a-col>
    </a-row>
    <a-drawer v-model:open="open" class="custom-class" root-class-name="root-class-name" :root-style="{ color: 'blue' }"
        style="color: red" title="字典数据" placement="right" @after-open-change="afterOpenChange" width="800">
         <x-action-bar class="mb-8-2">
                    <a-button type="primary" @click="$refs.editDetailDialogRef.handleCreate()">
                        <template #icon>
                            <plus-outlined></plus-outlined>
                        </template>
                        添加数据
                    </a-button>
                </x-action-bar>
         <a-table :columns="detailColumns" :data-source="listDetailData" :loading="detailLoading" >
             
                    <template #bodyCell="{ column, record }">
                         <template v-if="'status' === column.key">
                            <a-tag v-if="record.status === 'enabled'" :color="'green'">启用</a-tag>
                            <a-tag v-if="record.status === 'disabled'" :color="'red'">停用</a-tag>
                        </template>
                        <template v-if="'action' === column.key">
                            <x-action-button @click="$refs.editDetailDialogRef.handleEdit(record)">
                                <a-tooltip>
                                    <template #title> {{ $t('pages.system.user.edit') }}</template>
                                    <edit-outlined /> </a-tooltip></x-action-button>
                            <x-action-button @click="handleDetailDelete(record)">
                                <a-tooltip>
                                    <template #title>{{ $t('pages.system.delete') }}</template>
                                    <delete-outlined style="color: #ff4d4f" /> </a-tooltip></x-action-button>
                        </template>
                    </template>
                </a-table>
    </a-drawer>
    <edit-dialog ref="editDialogRef" @ok="onOk"></edit-dialog>
    <edit-dialog-detail ref="editDetailDialogRef" @ok="onOkDetail" :categoryCode="currentCode"></edit-dialog-detail>
</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 { statusUserTypeEnum } from '@/enums/system'
import { usePagination } from '@/hooks'
import { useRoute, useRouter } from 'vue-router'
import EditDialog from './components/EditDialog.vue'
import EditDialogDetail from './components/EditDialogDetail.vue'
import { PlusOutlined, EditOutlined, DeleteOutlined } from '@ant-design/icons-vue'
import { useI18n } from 'vue-i18n'
defineOptions({
    name: 'dictionary',
})
const open = ref(false);
const { t } = useI18n() // 解构出t方法
const router = useRouter()
const columns = [
    { title: '字典名称', dataIndex: 'name' },
    { title: '字典类型', dataIndex: 'code' },
    { title: '备注', dataIndex: 'introduction', key: 'introduction' },
    { title: '状态', dataIndex: 'status', key: 'status' },

    { title: t('button.action'), key: 'action', fixed: 'right', width: 130 },
]
const currentCode = ref('');
const detailLoading = ref(false);
const listDetailData = ref([]);
const detailColumns = [
    { title: '字典标签', dataIndex: 'introduction',width:200 },
    { title: '字典值', dataIndex: 'dval',width:80 },
    { title: '备注', dataIndex: 'remark', key: 'remark' },
    { title: '状态', dataIndex: 'status', key: 'status',width:100 },
    { title: t('button.action'), key: 'action', fixed: 'right', width: 130 },
]
const { listData, loading, showLoading, hideLoading, paginationState, resetPagination, searchFormData } =
    usePagination()

const editDialogRef = ref()
const editDetailDialogRef = ref()
getPageList()
/**
 * 获取用户列表
 * @returns {Promise<void>}
 */
async function getPageList() {
    try {
        showLoading()
        const { pageSize, current } = paginationState
        const { success, data, total } = await apis.dict
            .getProjectList({
                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 handleDelete({ id }) {
    Modal.confirm({
        title: t('pages.system.user.delTip'),
        content: t('button.confirm'),
        okText: t('button.confirm'),
        onOk: () => {
            return new Promise((resolve, reject) => {
                ; (async () => {
                    try {
                        const { success } = await apis.dict.delItem(id).catch(() => {
                            throw new Error()
                        })
                        if (config('http.code.success') === success) {
                            resolve()
                            message.success(t('component.message.success.delete'))
                            await getPageList()
                        }
                    } catch (error) {
                        reject()
                    }
                })()
            })
        },
    })
}
/**
 * 删除
 */
function handleDetailDelete({ id }) {
    Modal.confirm({
        title: t('pages.system.user.delTip'),
        content: t('button.confirm'),
        okText: t('button.confirm'),
        onOk: () => {
            return new Promise((resolve, reject) => {
                ; (async () => {
                    try {
                        const { success } = await apis.dict.delDetailItem(id).catch(() => {
                            throw new Error()
                        })
                        if (config('http.code.success') === success) {
                            resolve()
                            message.success(t('component.message.success.delete'))
                            await getDetailList(currentCode.value)
                        }
                    } catch (error) {
                        reject()
                    }
                })()
            })
        },
    })
}
function handleDetail(record) {
    // router.push({ path: '/system/dictionary/detail', query: { id: record.id } })
    open.value = true;
    currentCode.value=record.code;
    getDetailList(record.code)
}
function getDetailList(code) {
    try {
        detailLoading.value = true
        apis.dict
            .getDetailCode({ categoryCode: code })
            .then((res) => {
                if (config('http.code.success') === res.success) {
                    listDetailData.value = res.data
                }
                 detailLoading.value = false
            })
            .catch(() => {
                throw new Error()
            })
    } catch (error) {
        detailLoading.value = false
    }
}
/**
 * 分页
 */
function onTableChange({ current, pageSize }) {
    paginationState.current = current
    paginationState.pageSize = pageSize
    getPageList()
}

/**
 * 搜索
 */
function handleSearch() {
    resetPagination()
    getPageList()
}
/**
 * 重置
 */
function handleResetSearch() {
    searchFormData.value = {}
    resetPagination()
    getPageList()
}
/**
 * 编辑完成
 */
async function onOk() {
    await getPageList()
}
async function onOkDetail() {
    if(currentCode.value){
        await getDetailList(currentCode.value)
    }
}
function afterOpenChange(open) {
    console.log(open);
  }
</script>

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