<template>
  <a-page-header :title="getPageTitle()" @back="() => $router.go(-1)">
    <template #extra>
      <Extra />
    </template>
    <a-card :bordered="false" :body-style="{ padding: '16px' }">
      <ele-pro-table
        ref="tableRef"
        row-key="goodsId"
        :columns="columns"
        :datasource="datasource"
        :customRow="customRow"
        v-model:selection="selection"
        tool-class="ele-toolbar-form"
        class="sys-org-table"
      >
        <template #toolbar>
          <search
            @search="reload"
            :selection="selection"
            :navigationList="navigationList"
            @add="openEdit"
            @remove="removeBatch"
            @batchMove="openMove"
            @import="openImport"
            @imageImport="openImageImport"
            @export="handleExport"
          />
        </template>
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'name'">
            <a-space class="flex items-center cursor-pointer">
              <a-image
                :src="record.image"
                v-if="record.image"
                :preview="false"
                :width="50"
              />
              <span class="text-gray-700 font-bold">{{ record.name }}</span>
            </a-space>
          </template>
          <template v-if="column.key === 'recommend'">
            <a-space @click.stop="onRecommend(record)">
              <span
                v-if="record.recommend === 1"
                class="ele-text-success cursor-pointer"
                ><CheckOutlined
              /></span>
              <span v-else class="ele-text-placeholder cursor-pointer"
                ><CloseOutlined
              /></span>
            </a-space>
          </template>
          <template v-if="column.key === 'status'">
            <a-tag v-if="record.status === 0" color="green">已上架</a-tag>
            <a-tag v-if="record.status === 1" color="orange">待上架</a-tag>
            <a-tag v-if="record.status === 2" color="purple">待审核</a-tag>
            <a-tag v-if="record.status === 3" color="red">审核不通过</a-tag>
          </template>
          <template v-if="column.key === 'action'">
            <a-space>
              <a @click.stop="openEdit(record)">修改</a>
              <a-divider type="vertical" />
              <a-popconfirm
                title="确定要删除此记录吗？"
                @confirm.stop="remove(record)"
              >
                <a class="ele-text-danger">删除</a>
              </a-popconfirm>
            </a-space>
          </template>
        </template>
      </ele-pro-table>
    </a-card>

    <!-- 编辑弹窗 -->
    <ShopGoodsEdit
      v-model:visible="showEdit"
      :navigationList="navigationList"
      :data="current"
      @done="reload"
    />

    <!-- 导入弹窗 -->
    <ShopGoodsImport
      v-model:visible="showImport"
      @done="reload"
    />

    <ShopGoodsImageImport
      v-model:visible="showImageImport"
      @done="reload"
    />
  </a-page-header>
</template>

<script lang="ts" setup>
  import { createVNode, ref } from 'vue';
  import { message, Modal } from 'ant-design-vue';
  import {
    ExclamationCircleOutlined,
    CheckOutlined,
    CloseOutlined
  } from '@ant-design/icons-vue';
  import type { EleProTable } from 'ele-admin-pro';
  import { toTreeData } from 'ele-admin-pro';
  import { toDateString } from 'ele-admin-pro';
  import type {
    DatasourceFunction,
    ColumnItem
  } from 'ele-admin-pro/es/ele-pro-table/types';
  import Search from './components/search.vue';
  import ShopGoodsEdit from './components/shopGoodsEdit.vue';
  import Extra from '@/views/shop/shopGoods/components/extra.vue';
  import ShopGoodsImport from './components/shop-goods-import.vue';
  import ShopGoodsImageImport from './components/shop-goods-image-import.vue';
  import { listShopGoods, pageShopGoods, removeShopGoods, removeBatchShopGoods, updateShopGoods } from '@/api/shop/shopGoods';
  import type { ShopGoods, ShopGoodsParam } from '@/api/shop/shopGoods/model';
  import { getPageTitle } from '@/utils/common';
  import { ShopGoodsCategory } from '@/api/shop/shopGoodsCategory/model';
  import { listShopGoodsCategory } from '@/api/shop/shopGoodsCategory';
  import { utils, writeFile } from 'xlsx';
  import dayjs from 'dayjs';

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

  // 表格选中数据
  const selection = ref<ShopGoods[]>([]);
  // 当前编辑数据
  const current = ref<ShopGoods | null>(null);
  // 分类数据
  const navigationList = ref<ShopGoodsCategory[]>();
  // 是否显示编辑弹窗
  const showEdit = ref(false);
  // 是否显示导入弹窗
  const showImport = ref(false);
  // 是否显示图片导入弹窗
  const showImageImport = ref(false);
  // 是否显示批量移动弹窗
  const showMove = ref(false);
  // 加载状态
  const loading = ref(true);

  // 表格数据源
  const datasource: DatasourceFunction = ({
    page,
    limit,
    where,
    orders,
    filters
  }) => {
    if (filters) {
      where.status = filters.status;
    }
    return pageShopGoods({
      ...where,
      ...orders,
      page,
      limit
    });
  };

  // 表格列配置
  const columns = ref<ColumnItem[]>([
    {
      title: 'ID',
      dataIndex: 'goodsId',
      key: 'goodsId',
      align: 'center',
      width: 90
    },
    {
      title: '商品',
      dataIndex: 'name',
      key: 'name',
      width: 300
    },
    // {
    //   title: '编号',
    //   dataIndex: 'code',
    //   key: 'code',
    //   align: 'center',
    // },
    {
      title: '价格',
      dataIndex: 'price',
      key: 'price',
      align: 'center',
      customRender: ({ text }) => `￥${text}`
    },
    {
      title: '销量',
      dataIndex: 'sales',
      key: 'sales',
      align: 'center'
    },
    {
      title: '库存',
      dataIndex: 'stock',
      key: 'stock',
      align: 'center'
    },
    {
      title: '推荐',
      dataIndex: 'recommend',
      key: 'recommend',
      align: 'center'
    },
    {
      title: '状态',
      dataIndex: 'status',
      key: 'status',
      align: 'center'
    },
    // {
    //   title: '备注',
    //   dataIndex: 'comments',
    //   key: 'comments',
    //   align: 'center',
    // },
    {
      title: '排序号',
      dataIndex: 'sortNumber',
      key: 'sortNumber',
      align: 'center',
      width: 120
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      key: 'createTime',
      align: 'center',
      sorter: true,
      ellipsis: true,
      width: 180,
      customRender: ({ text }) => toDateString(text, 'yyyy-MM-dd HH:mm:ss')
    }
    // {
    //   title: '操作',
    //   key: 'action',
    //   width: 180,
    //   fixed: 'right',
    //   align: 'center',
    //   hideInSetting: true
    // }
  ]);

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

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

  /* 打开批量移动弹窗 */
  const openMove = () => {
    showMove.value = true;
  };

  /* 打开导入弹窗 */
  const openImport = () => {
    showImport.value = true;
  };

  /* 打开图片导入弹窗 */
  const openImageImport = () => {
    showImageImport.value = true;
  };

  /* 导出商品列表 */
  const handleExport = async () => {
    if (loading.value) {
      return;
    }
    loading.value = true;
    message.loading('正在准备导出数据...', 0);
    try {
      const array: (string | number)[][] = [
        [
          '商品ID',
          '商品名称',
          '价格',
          '销量',
          '库存',
          '状态',
          '推荐',
          '创建时间'
        ]
      ];

      const list = await listShopGoods({});
      if (!list || list.length === 0) {
        message.warning('没有数据可以导出');
        loading.value = false;
        return;
      }

      list.forEach((goods: ShopGoods) => {
        const statusMap = ['已上架', '待上架', '待审核', '审核不通过'];
        array.push([
          `${goods.goodsId || ''}`,
          `${goods.name || ''}`,
          `￥${goods.price || 0}`,
          `${goods.sales || 0}`,
          `${goods.stock || 0}`,
          statusMap[goods.status || 0] || '',
          goods.recommend === 1 ? '是' : '否',
          `${goods.createTime || ''}`
        ]);
      });

      const sheetName = `商品列表${dayjs(new Date()).format('YYYYMMDD')}`;
      const workbook = {
        SheetNames: [sheetName],
        Sheets: {}
      };
      const sheet = utils.aoa_to_sheet(array);
      workbook.Sheets[sheetName] = sheet;

      sheet['!cols'] = [
        { wch: 10 },
        { wch: 30 },
        { wch: 12 },
        { wch: 10 },
        { wch: 10 },
        { wch: 12 },
        { wch: 8 },
        { wch: 20 }
      ];

      message.destroy();
      message.loading('正在生成Excel文件...', 0);

      setTimeout(() => {
        writeFile(workbook, `${sheetName}.xlsx`);
        loading.value = false;
        message.destroy();
        message.success(`成功导出 ${list.length} 条记录`);
      }, 1000);
    } catch (error: any) {
      loading.value = false;
      message.destroy();
      message.error(error.message || '导出失败，请重试');
    }
  };

  /* 删除单个 */
  const remove = (row: ShopGoods) => {
    const hide = message.loading('请求中..', 0);
    removeShopGoods(row.goodsId)
      .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 = message.loading('请求中..', 0);
        removeBatchShopGoods(selection.value.map((d) => d.goodsId))
          .then((msg) => {
            hide();
            message.success(msg);
            reload();
          })
          .catch((e) => {
            hide();
            message.error(e.message);
          });
      }
    });
  };

  /* 查询 */
  const query = () => {
    loading.value = true;
    // 加载分类数据
    if (!navigationList.value) {
      listShopGoodsCategory({}).then((res) => {
        navigationList.value = toTreeData({
          data: res?.map((d) => {
            d.value = d.categoryId;
            d.label = d.title;
            return d;
          }),
          idField: 'categoryId',
          parentIdField: 'parentId'
        });
      });
    }
  };

  const onRecommend = (row: ShopGoods) => {
    updateShopGoods({
      ...row,
      recommend: row.recommend == 1 ? 0 : 1
    }).then((msg) => {
      message.success(msg);
      reload();
    });
  };

  /* 自定义行属性 */
  const customRow = (record: ShopGoods) => {
    return {
      // 行点击事件
      onClick: () => {
        openEdit(record);
      },
      // 行双击事件
      onDblclick: () => {
        // openEdit(record);
      }
    };
  };
  query();
</script>

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

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