<template>
  <div class="page">
    <!--    <a-page-header :ghost="false" title="订单管理">-->
    <!--      <div class="ele-text-secondary"> 订单管理，电池订单管理模块。 </div>-->
    <!--    </a-page-header>-->
    <div class="ele-body">
      <a-card :bordered="false">
        <!-- 表格 -->
        <ele-pro-table
          ref="tableRef"
          row-key="orderId"
          :columns="columns"
          :datasource="datasource"
          v-model:selection="selection"
          tool-class="ele-toolbar-form"
          :scroll="{ x: 2000 }"
          class="sys-org-table"
          :striped="true"
        >
          <template #toolbar>
            <search
              @search="reload"
              :selection="selection"
              @add="openEdit"
              @remove="removeBatch"
              @advanced="openAdvanced"
            />
          </template>
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'orderName'">
              <a-tooltip title="查看详情">
                <a href="#" @click="openInfo(record)">{{ record.orderName }}</a>
              </a-tooltip>
            </template>
            <template v-if="column.key === 'payPrice'">
              <p class="ele-text-warning">￥{{ record.payPrice }}</p>
              <p class="ele-text-placeholder"
                >(含运费：￥{{ record.expressPrice }})</p
              >
            </template>
            <template v-if="column.key === 'goods'">
              <template v-if="record.equipment">
                <p class="ele-text">{{ record.equipment.equipmentName }}</p>
                <p class="ele-text">{{ record.equipment.batteryModel }}</p>
                <p class="ele-text">{{ record.equipment.equipmentCode }}</p>
              </template>
            </template>
            <template v-if="column.key === 'payMethod'">
              <a-tag v-if="record.payMethod === '10'" color="orange"
                >余额支付</a-tag
              >
              <a-tag v-if="record.payMethod === '20'" color="green"
                >微信支付</a-tag
              >
              <a-tag v-if="record.payMethod === '30'" color="blue"
                >支付宝</a-tag
              >
              <a-tag v-if="record.payMethod === '40'" color="purple"
                >通联支付</a-tag
              >
            </template>
            <template v-if="column.key === 'deliveryType'">
              <span v-if="record.deliveryType === 10">快递配送</span>
              <span v-if="record.deliveryType === 20">门店自提</span>
            </template>
            <template v-if="column.key === 'payStatus'">
              <div class="ele-text-placeholder">
                付款状态：
                <a-tag v-if="record.payStatus === 10">未付款</a-tag>
                <a-tag v-if="record.payStatus === 20" color="green"
                  >已付款</a-tag
                >
              </div>
              <div class="ele-text-placeholder">
                发货状态：
                <a-tag v-if="record.deliveryStatus === 10">未发货</a-tag>
                <a-tag v-if="record.deliveryStatus === 20" color="success"
                  >已发货</a-tag
                >
              </div>
              <div class="ele-text-placeholder">
                收货状态：
                <a-tag v-if="record.receiptStatus === 10">未收货</a-tag>
                <a-tag v-if="record.receiptStatus === 20" color="success"
                  >已收货</a-tag
                >
              </div>
            </template>
            <template v-if="column.key === 'comments'">
              <a-tooltip :title="record.comments">
                {{ record.comments }}
              </a-tooltip>
            </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="red">禁用</a-tag>
            </template>
            <template v-if="column.key === 'orderSource'">
              <span v-if="record.orderSource === 10" color="green"
                >普通订单</span
              >
              <span v-if="record.orderSource === 20" color="red">禁用</span>
              <span v-if="record.orderSource === 30" color="red">禁用</span>
            </template>
            <template v-if="column.key === 'nickname'">
              <a-tooltip :title="`用户ID：${record.userId}`">
                <a-avatar :src="record.avatar" size="small" />
                <span style="padding-left: 4px">{{ record.nickname }}</span>
              </a-tooltip>
            </template>
            <template v-if="column.key === 'createTime'">
              {{ record.createTime }}
            </template>

            <template v-if="column.key === 'action'">
              <a-button @click="openInfo(record)">详情</a-button>
            </template>
          </template>
        </ele-pro-table>
      </a-card>
    </div>
    <!-- 编辑弹窗 -->
    <Delivery v-model:visible="deliveryEdit" :data="current" @done="reload" />
    <!--    &lt;!&ndash; 编辑弹窗 &ndash;&gt;-->
    <!--    <order-edit v-model:visible="showEdit" :data="current" @done="reload" />-->
    <!-- 用户详情 -->
    <!--    <order-info v-model:visible="showInfo" :data="current" @done="reload" />-->
  </div>
</template>

<script lang="ts" setup>
  import { createVNode, ref } from 'vue';
  import { message, Modal } from 'ant-design-vue';
  import {
    // PlusOutlined,
    // DeleteOutlined,
    ExclamationCircleOutlined
  } from '@ant-design/icons-vue';
  import type { EleProTable } from 'ele-admin-pro';
  import type {
    DatasourceFunction,
    ColumnItem
  } from 'ele-admin-pro/es/ele-pro-table/types';
  import { toDateString } from 'ele-admin-pro';
  import Search from './components/search.vue';
  import Delivery from './components/delivery.vue';
  // import OrderEdit from './components/order-edit.vue';
  // import OrderInfo from './components/order-info.vue';
  import { pageOrderRefund, removeOrderRefund, removeBatchOrderRefund } from '@/api/order/refund';
  import type { OrderRefund, OrderRefundParam } from '@/api/order/refund/model';
  // import { useUserStore } from '@/store/modules/user';
  // import { getDictionaryOptions } from '@/utils/common';

  // 当前用户信息
  // const userStore = useUserStore();
  // const loginUser = computed(() => userStore.info ?? {});
  // const orderType = localStorage.getItem('orderType');
  const props = defineProps<{
    activeKey?: boolean;
    data?: any;
  }>();

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

  /* 获取字典数据 */
  // const payMethodDict = getDictionaryOptions('payMethod');

  // 表格列配置
  const columns = ref<ColumnItem[]>([
    {
      key: 'index',
      width: 48,
      align: 'center',
      fixed: 'left',
      hideInSetting: true,
      customRender: ({ index }) => index + (tableRef.value?.tableIndex ?? 0)
    },
    {
      title: '操作',
      key: 'action',
      width: 100,
      align: 'center',
      hideInSetting: true
    },
    {
      title: '订单号',
      dataIndex: 'orderId',
      key: 'orderId'
    },
    {
      title: '买家',
      dataIndex: 'nickname',
      key: 'nickname',
      align: 'center'
    },
    {
      title: '所属商户',
      dataIndex: 'merchantName',
      sorter: true
    },
    {
      title: '实付款(元)',
      dataIndex: 'payPrice',
      key: 'payPrice',
      ellipsis: true
    },
    {
      title: '支付方式',
      key: 'payMethod',
      dataIndex: 'payMethod',
      filters: [
        {
          text: '余额支付',
          value: 10
        },
        {
          text: '微信支付',
          value: 20
        },
        {
          text: '支付宝',
          value: 30
        },
        {
          text: '通联支付',
          value: 40
        }
      ],
      filterMultiple: false
    },
    {
      title: '配送方式',
      key: 'deliveryType',
      dataIndex: 'deliveryType',
      filters: [
        {
          text: '快递配送',
          value: 10
        },
        {
          text: '门店自提',
          value: 20
        }
      ],
      filterMultiple: false
    },
    {
      title: '订单来源',
      key: 'orderSource',
      dataIndex: 'orderSource',
      filters: [
        {
          text: '普通订单',
          value: 10
        },
        {
          text: '砍价订单',
          value: 20
        },
        {
          text: '秒杀订单',
          value: 30
        }
      ],
      filterMultiple: false
    },
    {
      title: '备注',
      dataIndex: 'comments',
      key: 'comments',
      ellipsis: true
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      key: 'createTime',
      sorter: true,
      customRender: ({ text }) => toDateString(text)
    },
    {
      title: '交易状态',
      key: 'payStatus',
      dataIndex: 'payStatus',
      fixed: 'right',
      align: 'center',
      filters: [
        {
          text: '未付款',
          value: 10
        },
        {
          text: '已付款',
          value: 20
        }
      ],
      filterMultiple: false
    }
  ]);

  // 表格选中数据
  const selection = ref<OrderRefund[]>([]);
  // 当前编辑数据
  const current = ref<OrderRefund | null>(null);
  // 是否显示资产详情
  const showInfo = ref(false);
  // 是否显示编辑弹窗
  const showEdit = ref(false);
  const deliveryEdit = ref(false);
  // 是否显示高级搜索
  const showAdvancedSearch = ref(false);

  // 表格数据源
  const datasource: DatasourceFunction = ({
    page,
    limit,
    where,
    orders,
    filters
  }) => {
    // 搜索条件
    if (filters.payMethod) {
      where.payMethod = filters.payMethod;
    }
    if (filters.deliveryType) {
      where.deliveryType = filters.deliveryType;
    }
    if (filters.payStatus) {
      where.payStatus = filters.payStatus;
    }
    if (filters.orderSource) {
      where.orderSource = filters.orderSource;
    }
    where.tenantId = localStorage.getItem('tenantId');
    where.isRenew = 0;
    console.log('isRenew');
    return pageOrderRefund({
      ...where,
      ...orders,
      page,
      limit
    });
  };

  /* 搜索 */
  const reload = (where?: OrderRefundParam) => {
    console.log(where, '....>>>>111');
    selection.value = [];
    tableRef?.value?.reload({ where: where });
  };

  const openDelivery = (row?: OrderRefund) => {
    current.value = row ?? null;
    deliveryEdit.value = true;
  };

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

  /* 打开用户详情弹窗 */
  const openInfo = (row?: OrderRefund) => {
    current.value = row ?? null;
    showInfo.value = true;
  };

  /* 打开高级搜索 */
  const openAdvanced = () => {
    showAdvancedSearch.value = !showAdvancedSearch.value;
  };

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

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

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

<style lang="less" scoped>
  p {
    line-height: 0.8;
  }
  .sys-org-table :deep(.ant-table-body) {
    overflow: auto !important;
    overflow: overlay !important;
  }

  .sys-org-table :deep(.ant-table-pagination.ant-pagination) {
    padding: 0 4px;
    margin-bottom: 0;
  }
</style>
