<template>
  <div class="page">
    <a-card>
      <search
        @search="reload"
        :selection="selection"
        @add="openEdit"
        @remove="removeBatch"
        @batchMove="openMove"
      />
    </a-card>
    <div class="ele-body">
      <a-card
        title="订单列表"
        :bordered="false"
        :body-style="{ padding: '16px' }"
      >
        <ele-pro-table
          ref="tableRef"
          row-key="orderId"
          :columns="columns"
          :datasource="datasource"
          :customRow="customRow"
          :scroll="{ x: 800 }"
          :toolbar="false"
          tool-class="ele-toolbar-form"
          class="sys-org-table"
        >
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'image'">
              <a-image :src="record.image" :width="50" />
            </template>
            <template v-if="column.key === 'payType'">
              <template v-if="record.payStatus == 1">
                <a-tag v-if="record.payType == 0">余额支付</a-tag>
                <a-tag v-if="record.payType == 1"
                  ><WechatOutlined class="tag-icon" />微信支付</a-tag
                >
                <a-tag v-if="record.payType == 2">积分</a-tag>
                <a-tag v-if="record.payType == 3"
                  ><AlipayCircleOutlined class="tag-icon" />支付宝</a-tag
                >
                <a-tag v-if="record.payType == 4"
                  ><IdcardOutlined class="tag-icon" />现金</a-tag
                >
                <a-tag v-if="record.payType == 5"
                  ><IdcardOutlined class="tag-icon" />POS机</a-tag
                >
                <a-tag v-if="record.payType == 6"
                  ><IdcardOutlined class="tag-icon" />VIP月卡</a-tag
                >
                <a-tag v-if="record.payType == 7"
                  ><IdcardOutlined class="tag-icon" />VIP年卡</a-tag
                >
                <a-tag v-if="record.payType == 8"
                  ><IdcardOutlined class="tag-icon" />VIP次卡</a-tag
                >
                <a-tag v-if="record.payType == 9"
                  ><IdcardOutlined class="tag-icon" />IC月卡</a-tag
                >
                <a-tag v-if="record.payType == 10"
                  ><IdcardOutlined class="tag-icon" />IC年卡</a-tag
                >
                <a-tag v-if="record.payType == 11"
                  ><IdcardOutlined class="tag-icon" />IC次卡</a-tag
                >
                <a-tag v-if="record.payType == 12"
                  ><IdcardOutlined class="tag-icon" />免费</a-tag
                >
                <a-tag v-if="record.payType == 13"
                  ><IdcardOutlined class="tag-icon" />VIP充值卡</a-tag
                >
                <a-tag v-if="record.payType == 14"
                  ><IdcardOutlined class="tag-icon" />IC充值卡</a-tag
                >
                <a-tag v-if="record.payType == 15"
                  ><IdcardOutlined class="tag-icon" />积分支付</a-tag
                >
                <a-tag v-if="record.payType == 16"
                  ><IdcardOutlined class="tag-icon" />VIP季卡</a-tag
                >
                <a-tag v-if="record.payType == 17"
                  ><IdcardOutlined class="tag-icon" />IC季卡</a-tag
                >
                <a-tag v-if="record.payType == 18">代付</a-tag>
                <a-tag v-if="record.payType == 19">门店余额</a-tag>
                <a-tag v-if="record.payType == 20">门店套餐</a-tag>
              </template>
              <template v-else>
                <span></span>
              </template>
            </template>
            <template v-if="column.key === 'couponType'">
              <a-tag v-if="record.couponType == 0">无</a-tag>
              <a-tag v-if="record.couponType == 1" color="blue"
                ><IdcardOutlined class="tag-icon" />抵扣优惠券</a-tag
              >
              <a-tag v-if="record.couponType == 2" color="blue"
                ><IdcardOutlined class="tag-icon" />折扣优惠券</a-tag
              >
              <a-tag v-if="record.couponType == 3" color="blue"
                ><IdcardOutlined class="tag-icon" />VIP月卡</a-tag
              >
              <a-tag v-if="record.couponType == 4" color="blue"
                ><IdcardOutlined class="tag-icon" />VIP年卡</a-tag
              >
              <a-tag v-if="record.couponType == 5" color="blue"
                ><IdcardOutlined class="tag-icon" />VIP次卡</a-tag
              >
              <a-tag v-if="record.couponType == 6" color="blue"
                ><IdcardOutlined class="tag-icon" />VIP会员卡</a-tag
              >
              <a-tag v-if="record.couponType == 7" color="blue"
                ><IdcardOutlined class="tag-icon" />IC月卡</a-tag
              >
              <a-tag v-if="record.couponType == 8" color="blue"
                ><IdcardOutlined class="tag-icon" />IC年卡</a-tag
              >
              <a-tag v-if="record.couponType == 9" color="blue"
                ><IdcardOutlined class="tag-icon" />IC次卡</a-tag
              >
              <a-tag v-if="record.couponType == 10" color="blue"
                ><IdcardOutlined class="tag-icon" />IC会员卡</a-tag
              >
              <a-tag v-if="record.couponType == 11" color="blue"
                ><IdcardOutlined class="tag-icon" />免费订单</a-tag
              >
              <a-tag v-if="record.couponType == 12" color="blue"
                ><IdcardOutlined class="tag-icon" />VIP充值卡</a-tag
              >
              <a-tag v-if="record.couponType == 13" color="blue"
                ><IdcardOutlined class="tag-icon" />IC充值卡</a-tag
              >
              <a-tag v-if="record.couponType == 14" color="blue"
                ><IdcardOutlined class="tag-icon" />VIP季卡</a-tag
              >
              <a-tag v-if="record.couponType == 15" color="blue"
                ><IdcardOutlined class="tag-icon" />IC季卡</a-tag
              >
            </template>
            <template v-if="column.key === 'payStatus'">
              <a-tag v-if="record.payStatus == 1" color="green"
                ><CheckOutlined class="tag-icon" />已付款</a-tag
              >
              <a-tag v-if="record.payStatus == 0" color="error"
                ><CloseOutlined class="tag-icon" />未付款</a-tag
              >
              <a-tag v-if="record.payStatus == 3" color="cyan"
                ><CoffeeOutlined class="tag-icon" />未付款,占场中</a-tag
              >
            </template>
            <template v-if="column.key === 'orderInfo'">
              {{ record.orderInfoList }}
            </template>
            <template v-if="column.key === 'orderStatus'">
              <span v-if="record.orderStatus == 0" class="ele-text-primary"
                >未使用</span
              >
              <span v-if="record.orderStatus == 2" class="ele-text-placeholder"
                >已完成</span
              >
              <span v-if="record.orderStatus == 1" class="ele-text-success"
                >已完成</span
              >
              <span v-if="record.orderStatus == 3" class="ele-text-placeholder"
                >已取消</span
              >
              <span v-if="record.orderStatus == 4" class="ele-text-warning"
                >退款申请中</span
              >
              <span v-if="record.orderStatus == 5" class="ele-text-danger"
                >退款被拒绝</span
              >
              <span v-if="record.orderStatus == 6" class="ele-text-heading"
                >退款成功</span
              >
              <span v-if="record.orderStatus == 7" class="ele-text-warning"
                >客户端申请退款</span
              >
            </template>
            <template v-if="column.key === 'isInvoice'">
              <a-tag v-if="record.isInvoice == 0">未开</a-tag>
              <a-tag v-if="record.isInvoice == 1" color="green">已开</a-tag>
              <a-tag v-if="record.isInvoice == 2">不能开</a-tag>
            </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 === 'action'">
              <a-space>
                <template v-if="record.type === 0 && record.deliveryStatus === 10 && record.deliveryType === 0 && record.payStatus === 1">
                  <a @click="openDelivery(record)">发货</a>
                  <a-divider type="vertical" />
                </template>
                <a @click="openEdit(record)">详情</a>
                <a-divider type="vertical" />
                <a-dropdown>
                  <a class="ant-dropdown-link" @click.prevent>
                    更多
                    <DownOutlined />
                  </a>
                  <template #overlay>
                    <a-menu>
                      <a-menu-item>
                        <a href="javascript:;">打印小票</a>
                      </a-menu-item>
                      <a-menu-item>
                        <a href="javascript:;">直接退款</a>
                      </a-menu-item>
                      <a-menu-item>
                        <a href="javascript:;">订单备注</a>
                      </a-menu-item>
                    </a-menu>
                  </template>
                </a-dropdown>
              </a-space>
            </template>
          </template>
        </ele-pro-table>
      </a-card>
      <!-- 编辑弹窗 -->
      <OrderInfo v-model:visible="showEdit" :data="current" @done="reload" />
      <!-- 发货弹窗 -->
      <Delivery v-model:visible="showDelivery" :data="current" @done="reload" />
    </div>
  </div>
</template>

<script lang="ts" setup>
  import { createVNode, ref } from 'vue';
  import { message, Modal } from 'ant-design-vue';
  import {
    ExclamationCircleOutlined,
    CheckOutlined,
    CloseOutlined,
    AlipayCircleOutlined,
    DownOutlined
  } from '@ant-design/icons-vue';

  import { EleProTable, 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 OrderInfo from './components/orderInfo.vue';
  import Delivery from './components/delivery.vue';
  import { pageOrder, removeOrder, removeBatchOrder } from '@/api/shop/order';
  import type { Order, OrderParam } from '@/api/shop/order/model';
  import { formatNumber } from 'ele-admin-pro/es';
  import { getMerchantId } from '@/utils/merchant';

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

  // 表格选中数据
  const selection = ref<Order[]>([]);
  // 当前编辑数据
  const current = ref<Order | null>(null);
  // 是否显示编辑弹窗
  const showEdit = ref(false);
  // 是否显示发货弹窗
  const showDelivery = ref(false);
  // 是否显示批量移动弹窗
  const showMove = ref(false);
  // 加载状态
  const loading = ref(true);

  // 表格数据源
  const datasource: DatasourceFunction = ({
    page,
    limit,
    where,
    orders,
    filters
  }) => {
    if (filters) {
      where.status = filters.status;
    }
    // 商城订单
    // where.type = 0;
    // where.sceneType = 'showOrderInfo';
    where.merchantId = getMerchantId();
    return pageOrder({
      ...where,
      ...orders,
      page,
      limit
    });
  };

  // 表格列配置
  const columns = ref<ColumnItem[]>([
    {
      title: '订单号',
      dataIndex: 'orderId',
      key: 'orderId',
      width: 90
    },
    {
      title: '姓名',
      dataIndex: 'realName',
      key: 'realName',
      align: 'center'
    },
    {
      title: '手机号',
      dataIndex: 'phone',
      key: 'phone',
      width: 120,
      align: 'center'
    },
    {
      title: '总额',
      dataIndex: 'totalPrice',
      key: 'totalPrice',
      align: 'center',
      customRender: ({ text }) => `￥${formatNumber(text)}`
    },
    {
      title: '减少金额',
      dataIndex: 'reducePrice',
      key: 'reducePrice',
      align: 'center',
      customRender: ({ text }) => `￥${formatNumber(text)}`
    },
    {
      title: '实付金额',
      dataIndex: 'payPrice',
      key: 'payPrice',
      align: 'center',
      customRender: ({ text }) => `￥${formatNumber(text)}`
    },
    {
      title: '支付方式',
      dataIndex: 'payType',
      key: 'payType',
      align: 'center'
    },
    {
      title: '付款状态',
      dataIndex: 'payStatus',
      key: 'payStatus',
      align: 'center'
    },
    {
      title: '付款时间',
      dataIndex: 'payTime',
      key: 'payTime',
      align: 'center',
      customRender: ({ text }) => toDateString(text, 'yyyy-MM-dd HH:mm:ss')
    },
    {
      title: '订单状态',
      dataIndex: 'orderStatus',
      key: 'orderStatus',
      align: 'center'
    },
    {
      title: '订单类型',
      dataIndex: 'type',
      key: 'type',
      align: 'center',
      customRender: ({ text }) =>
        ['商品', '外卖'][text]
    },
    {
      title: '物流类型',
      dataIndex: 'deliveryType',
      align: 'center',
      customRender: ({ text }) =>
        ['快递/配送', '自提'][text]
    },
    // {
    //   title: '申请退款时间',
    //   dataIndex: 'refundApplyTime',
    //   key: 'refundApplyTime',
    //   align: 'center',
    //   customRender: ({ text }) => toDateString(text, 'yyyy-MM-dd HH:mm:ss')
    // },
    // {
    //   title: '备注',
    //   dataIndex: 'comments',
    //   key: 'comments',
    //   align: 'center'
    // },
    {
      title: '操作',
      key: 'action',
      width: 180,
      fixed: 'right',
      align: 'center',
      hideInSetting: true
    }
  ]);

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

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

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

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

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

  /* 查询 */
  const query = () => {
    loading.value = true;
  };

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

<script lang="ts">
  import * as MenuIcons from '@/layout/menu-icons';
  export default {
    name: 'Order',
    components: MenuIcons
  };
</script>
<style lang="less" scoped>
  .tag-icon {
    padding-right: 6px;
  }
</style>
