<template>
  <div class="ele-body">
    <a-card :bordered="false">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        row-key="commentId"
        :columns="columns"
        :datasource="datasource"
        v-model:selection="selection"
        :scroll="{ x: 800 }"
        cache-key="proShopCommentTable"
      >
        <template #toolbar>
          <a-space>
            <!--            <a-button type="primary" class="ele-btn-icon" @click="openEdit()">-->
            <!--              <template #icon>-->
            <!--                <plus-outlined />-->
            <!--              </template>-->
            <!--              <span>新建</span>-->
            <!--            </a-button>-->
            <a-button
              danger
              type="primary"
              class="ele-btn-icon"
              @click="removeBatch"
            >
              <template #icon>
                <delete-outlined />
              </template>
              <span>删除</span>
            </a-button>
            <a-radio-group :defaultValue="score" @change="handleTabs">
              <a-radio-button :value="0">全部</a-radio-button>
              <a-radio-button :value="10">好评</a-radio-button>
              <a-radio-button :value="20">中评</a-radio-button>
              <a-radio-button :value="30">差评</a-radio-button>
            </a-radio-group>
            <a-input-search
              allow-clear
              v-model:value="searchText"
              placeholder="请输入关键词"
              @search="reload"
              @pressEnter="reload"
            >
              <template #addonBefore>
                <a-select
                  v-model:value="type"
                  style="width: 100px; margin: -5px -12px"
                >
                  <a-select-option value="goodsName">商品名称</a-select-option>
                  <a-select-option value="goodsCode">商品编码</a-select-option>
                  <a-select-option value="userId">用户ID</a-select-option>
                </a-select>
              </template>
            </a-input-search>
          </a-space>
        </template>
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'action'">
            <a-space>
              <a @click="openEdit(record)">修改</a>
              <a-divider type="vertical" />
              <a-popconfirm
                placement="topRight"
                title="确定要删除此评价吗？"
                @confirm="remove(record)"
              >
                <a class="ele-text-danger">删除</a>
              </a-popconfirm>
            </a-space>
          </template>
        </template>
      </ele-pro-table>
    </a-card>
    <!-- 编辑弹窗 -->
    <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 {
    PlusOutlined,
    DeleteOutlined,
    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: 'isPicture'
    },
    {
      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>
