<template>
  <div class="ele-body">
    <!-- 编辑弹窗 -->
    <comment-edit v-model:visible="showEdit" :data="current" @done="reload" />
  </div>
</template>

<script lang="ts" setup>
  import { createVNode, ref } from 'vue';
  import { message, Modal } from 'ant-design-vue/es';
  import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
  import type { EleProTable } from 'ele-admin-pro/es';
  import type {
    DatasourceFunction,
    ColumnItem
  } from 'ele-admin-pro/es/ele-pro-table/types';
  import { messageLoading, toDateString } from 'ele-admin-pro/es';
  import CommentEdit from './components/comment-edit.vue';
  import {
    pageComments,
    removeComment,
    removeComments
  } from '@/api/goods/comment';
  import type { Comment, CommentParam } from '@/api/goods/comment/model';

  // 表格实例
  const tableRef = ref<InstanceType<typeof EleProTable> | null>(null);

  // 表格列配置
  const columns = ref<ColumnItem[]>([
    {
      key: 'index',
      width: 48,
      align: 'center',
      fixed: 'left',
      hideInSetting: true,
      customRender: ({ index }) => index + (tableRef.value?.tableIndex ?? 0)
    },
    {
      title: '文件名称',
      dataIndex: 'goods',
      showSorterTooltip: false
    },
    {
      title: '发布者',
      dataIndex: 'user',
      showSorterTooltip: false
    },
    {
      title: '描述',
      dataIndex: 'score',
      sorter: true,
      showSorterTooltip: false
    },
    {
      title: '大小',
      dataIndex: 'content'
    },
    {
      title: '发布时间',
      dataIndex: 'createTime',
      sorter: true,
      showSorterTooltip: false,
      ellipsis: true,
      customRender: ({ text }) => toDateString(text)
    },
    {
      title: '操作',
      key: 'action',
      width: 200,
      align: 'center'
    }
  ]);

  // 表格选中数据
  const selection = ref<Comment[]>([]);

  // 当前编辑数据
  const current = ref<Comment | null>(null);

  // 是否显示编辑弹窗
  const showEdit = ref(false);
  const type = ref('goodsName');
  const searchText = ref('');
  const score = ref(0);

  const handleTabs = (e) => {
    score.value = e.target.value;
  };

  // 表格数据源
  const datasource: DatasourceFunction = ({ page, limit, where, orders }) => {
    if (type.value == 'goodsName') {
      where.goodsName = searchText.value;
    }
    if (type.value == 'goodsCode') {
      where.goodsCode = searchText.value;
    }
    if (type.value == 'userId') {
      where.userId = searchText.value;
    }
    return pageComments({ ...where, ...orders, page, limit });
  };

  /* 搜索 */
  const reload = (where?: CommentParam) => {
    selection.value = [];
    tableRef?.value?.reload({ page: 1, where });
  };

  /* 打开编辑弹窗 */
  const openEdit = (row?: Comment) => {
    current.value = row ?? null;
    showEdit.value = true;
  };

  /* 删除单个 */
  const remove = (row: Comment) => {
    const hide = messageLoading('请求中..', 0);
    removeComment(row.commentId)
      .then((msg) => {
        hide();
        message.success(msg);
        reload();
      })
      .catch((e) => {
        hide();
        message.error(e.message);
      });
  };

  /* 批量删除 */
  const removeBatch = () => {
    if (!selection.value.length) {
      message.error('请至少选择一条数据');
      return;
    }
    Modal.confirm({
      title: '提示',
      content: '确定要删除选中的评价吗?',
      icon: createVNode(ExclamationCircleOutlined),
      maskClosable: true,
      onOk: () => {
        const hide = messageLoading('请求中..', 0);
        removeComments(selection.value.map((d) => d.commentId))
          .then((msg) => {
            hide();
            message.success(msg);
            reload();
          })
          .catch((e) => {
            hide();
            message.error(e.message);
          });
      }
    });
  };
</script>

<script lang="ts">
  export default {
    name: 'ShopComment'
  };
</script>
