<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="status">
                            <a-select v-model:value="searchFormData.status" allowClear>
                                <a-select-option v-for="item of nbdMutualStatus.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" bordered="true" :loading="loading"
                    :pagination="paginationState" :scroll="{ x: 1000 }" @change="onTableChange">
                    <template #bodyCell="{ column, record }">
                        <template v-if="'avator' === column.dataIndex">
                            <a-image :width="60"
                                :src="record.avator ? config('http.apiBasic') + record.avator : errImg" />
                        </template>

                        <template v-if="'status' === column.dataIndex">
                            <a-tag :color="nbdMutualStatus.getColor(record.status)">{{
                                nbdMutualStatus.getName(record.status)
                            }}</a-tag>
                        </template>
                        <template v-if="'createdAt' === column.dataIndex">
                            <span>{{ dayjs(record.createdAt).format('YYYY-MM-DD HH:mm:ss') }}</span>
                        </template>
                        <template v-if="'title' === column.dataIndex">
                            <a-tooltip :title="record.title">
                                <span>{{ record.title }}</span>
                            </a-tooltip>
                        </template>
                        <template v-if="'content' === column.dataIndex">
                            <a-tooltip :title="record.content">
                                <span>{{ record.content }}</span>
                            </a-tooltip>
                        </template>
                        <template v-if="'remark' === column.dataIndex">
                            <a-tooltip :title="record.remark">
                                <span>{{ record.remark }}</span>
                            </a-tooltip>
                        </template>
                        <template v-if="'action' === column.key">
                            <x-action-button @click="auditHandleEdit(record, 'audit')" v-if="record.status === 1">
                                <a-tooltip :title="'审核'">
                                    <i class='iconfont icon-shenhe' style='font-size:14px;color:#faad14'></i>
                                </a-tooltip>
                            </x-action-button>
                            <x-action-button @click="$refs.editDialogRef.handleEdit(record)">
                                <a-tooltip :title="'详情'">
                                    <i class="iconfont icon-xiangqing" style='font-size:16px;'></i>
                                </a-tooltip>
                            </x-action-button>
                            <x-action-button @click="handleDelete(record)">
                                <a-tooltip :title="'评论列表'">
                                    <i class="iconfont icon-lishi" style='font-size:16px;color:#666666'></i>
                                </a-tooltip>
                            </x-action-button>
                        </template>
                    </template>
                </a-table>
            </a-card>
        </a-col>
    </a-row>
    <a-modal v-model:open="open" :title="'审核'" @ok="handleClick">
        <a-card class="mb-8-2">
            <a-form-item label="审核" name="">
                <a-radio-group v-model:value="auditStatus">
                    <a-radio :value="2">通过</a-radio>
                    <a-radio :value="99">不通过</a-radio>
                </a-radio-group>
            </a-form-item>
            <a-form-item label="备注" name="">
                <a-textarea :placeholder="'请输入审核备注'" v-model:value="remark"
                    :auto-size="{ minRows: 2, maxRows: 5 }"></a-textarea>
            </a-form-item>
        </a-card>
    </a-modal>
    <a-modal v-model:open="historyOpen" destroyOnClose  :title="'历史记录'" @ok="historyOpen = false">
        <a-spin :spinning="spining">
            <a-card class="mb-8-2">
                <p style="background-color: #f9f9f9;padding: 10px;border-radius: 5px;box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);">{{ nbdMutualAidInfo.content }}</p>
                <div v-if="nbdMutualAidInfo.histories&&nbdMutualAidInfo.histories.length > 0" >
                    <comment-item v-for="comment in nbdMutualAidInfo.histories" :key="comment.id" :comment="comment" :parentName="comment.pusherName"/>
                </div>
                <div v-else>暂无评论记录...</div>
            </a-card>
        </a-spin>
    </a-modal>
    <edit-dialog ref="editDialogRef" @ok="onOk"></edit-dialog>
</template>

<script setup>
import { message, Modal } from 'ant-design-vue'
import { ref } from 'vue'
import apis from '@/apis'
import { config } from '@/config'
import { usePagination, useSpining } from '@/hooks'
import { CommentItem } from './components/CommentItem.js';
import errImg from '@/assets/blankSpace.png'
import EditDialog from './components/EditDialog.vue'
import { PlusOutlined, EditOutlined, DeleteOutlined } from '@ant-design/icons-vue'
import { useI18n } from 'vue-i18n'
import { nbdMutualStatus } from '@/enums/index.js'

import dayjs from 'dayjs'
defineOptions({
    name: 'nbdMutuaAid',
})
const { t } = useI18n() // 解构出t方法
const columns = [
    { title: '用户名称', dataIndex: 'customerName', key: 'customerName', width: 120 },
    { title: '求助标题', dataIndex: 'title', width: 120, align: 'center', ellipsis: true },
    { title: '状态', dataIndex: 'status', width: 120, align: 'center' },
    { title: '创建时间', dataIndex: 'createdAt', width: 160, align: 'center' },
    { title: '内容', dataIndex: 'content', align: 'center', ellipsis: true },
    { title: '备注', dataIndex: 'remark', align: 'center' },
    { title: t('button.action'), key: 'action', fixed: 'right', width: 140, align: 'center' },
]
const historyList = ref([])
const historyOpen = ref(false)
const { listData, loading, showLoading, hideLoading, paginationState, resetPagination, searchFormData } = usePagination()
const { spining, showSpining, hideSpining } = useSpining()
const open = ref(false)
const auditStatus = ref(2)
const remark = ref('')
const editDialogRef = ref()
const currentInfo = ref({})
const nbdMutualAidInfo = ref({})
getPageList()
/**
 * 获取用户列表
 * @returns {Promise<void>}
 */

async function getPageList() {
    try {
        showLoading()
        const { pageSize, current } = paginationState
        const { success, data, total } = await apis.nbdMutuaAid
            .getDataList({
                pageSize,
                current: current,
                ...searchFormData.value,
            })
            .catch(() => {
                throw new Error()
            })
        hideLoading()
        if (config('http.code.success') === success) {
            //筛选type的值80对应about
            listData.value = data
            paginationState.total = total
        }
    } catch (error) {
        hideLoading()
    }
}
const auditHandleEdit = (obj = {}, type) => {
    currentInfo.value = obj
    if (type === 'audit') {
        open.value = true
    } else {
        // imgOpen.value = true
    }

}
const handleClick = async () => {
    try {
        const params = {
            ...currentInfo.value,
            status: auditStatus.value,
            remark: remark.value
        }
        const result = await apis.nbdMutuaAid.updateMenu(currentInfo.value.id, params).catch(() => {
            throw new Error(error)
        })
        if (config('http.code.success') === result?.success) {
            message.success('已审核')
            open.value = false
            getPageList()
        }
    } catch (error) {
        message.error({ content: error.message })
        hideLoading()
    }
}
/**
 * 评论列表
 */
const handleDelete = async ({ id }) => {
    try {
        historyOpen.value = true
        showSpining()
        const { success, data } = await apis.nbdMutuaAid.getList(id).catch(() => {
            throw new Error()
        })
        if (config('http.code.success') === success) {
            nbdMutualAidInfo.value = { ...data }
            hideSpining()
        }
    } catch (error) {
        hideSpining()
        message.error(error.message)
    }
}

/**
 * 分页
 */
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()
}
</script>

<style lang="less" scoped>
/* 全局评论样式 */
.comment {
  margin-bottom: 15px;
  padding: 15px;
  border-radius: 8px;
  background-color: #f9f9f9;
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

.comment-header {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  margin-right: 10px;
  object-fit: cover;
}

.username {
  font-weight: bold;
  margin-right: 10px;
}

.time {
  color: #888;
  font-size: 0.85em;
}

.comment-content {
  line-height: 1.5;
  color: #333;
}

.replies {
  margin-top: 15px;
  margin-left: 30px;
  padding-left: 15px;
  border-left: 2px solid #e0e0e0;
}
</style>
