<template>
  <div class="page">
    <div class="ele-body">
      <a-card :bordered="false" :body-style="{ padding: '16px' }">
        <ele-pro-table
          ref="tableRef"
          row-key="orderId"
          :columns="columns"
          :datasource="datasource"
          :customRow="customRow"
          tool-class="ele-toolbar-form"
          class="sys-org-table"
        >
          <template #toolbar>
            <search
              @search="reload"
              :selection="selection"
              @add="openEdit"
              @remove="removeBatch"
              @batchMove="openMove"
            />
          </template>
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'image'">
              <a-image :src="record.image" :width="50" />
            </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>
                <a @click="openEdit(record)">修改</a>
                <a-divider type="vertical" />
                <a-popconfirm
                  title="确定要删除此记录吗？"
                  @confirm="remove(record)"
                >
                  <a class="ele-text-danger">删除</a>
                </a-popconfirm>
              </a-space>
            </template>
          </template>
        </ele-pro-table>
      </a-card>

      <!-- 编辑弹窗 -->
      <OrderEdit v-model:visible="showEdit" :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 } from '@ant-design/icons-vue';
  import type { EleProTable } 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 OrderEdit from './components/orderEdit.vue';
  import { pageOrder, removeOrder, removeBatchOrder } from '@/api/booking/order';
  import type { Order, OrderParam } from '@/api/booking/order/model';

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

  // 表格选中数据
  const selection = ref<Order[]>([]);
  // 当前编辑数据
  const current = ref<Order | null>(null);
  // 是否显示编辑弹窗
  const showEdit = 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 pageOrder({
      ...where,
      ...orders,
      page,
      limit
    });
  };

  // 表格列配置
  const columns = ref<ColumnItem[]>([
    {
      title: '',
      dataIndex: 'orderId',
      key: 'orderId',
      align: 'center',
      width: 90,
    },
    {
      title: '订单类型，0商城订单 1预定订单 2会员卡',
      dataIndex: 'type',
      key: 'type',
      align: 'center',
    },
    {
      title: '订单号',
      dataIndex: 'orderNo',
      key: 'orderNo',
      align: 'center',
    },
    {
      title: '下单渠道，0小程序预定 1俱乐部训练场 3活动订场',
      dataIndex: 'channel',
      key: 'channel',
      align: 'center',
    },
    {
      title: '微信支付订单号',
      dataIndex: 'transactionId',
      key: 'transactionId',
      align: 'center',
    },
    {
      title: '微信退款订单号',
      dataIndex: 'refundOrder',
      key: 'refundOrder',
      align: 'center',
    },
    {
      title: '场馆id用于权限判断',
      dataIndex: 'merchantId',
      key: 'merchantId',
      align: 'center',
    },
    {
      title: '商户名称',
      dataIndex: 'merchantName',
      key: 'merchantName',
      align: 'center',
    },
    {
      title: '商户编号',
      dataIndex: 'merchantCode',
      key: 'merchantCode',
      align: 'center',
    },
    {
      title: '用户id',
      dataIndex: 'uid',
      key: 'uid',
      align: 'center',
    },
    {
      title: '使用的优惠券id',
      dataIndex: 'cid',
      key: 'cid',
      align: 'center',
    },
    {
      title: '使用的会员卡id',
      dataIndex: 'vid',
      key: 'vid',
      align: 'center',
    },
    {
      title: '关联管理员id',
      dataIndex: 'aid',
      key: 'aid',
      align: 'center',
    },
    {
      title: '核销管理员id',
      dataIndex: 'adminId',
      key: 'adminId',
      align: 'center',
    },
    {
      title: 'IC卡号',
      dataIndex: 'code',
      key: 'code',
      align: 'center',
    },
    {
      title: '真实姓名',
      dataIndex: 'name',
      key: 'name',
      align: 'center',
    },
    {
      title: '手机号码',
      dataIndex: 'phone',
      key: 'phone',
      align: 'center',
    },
    {
      title: '订单总额',
      dataIndex: 'totalPrice',
      key: 'totalPrice',
      align: 'center',
    },
    {
      title: '减少的金额，使用VIP会员折扣、优惠券抵扣、优惠券折扣后减去的价格',
      dataIndex: 'reducePrice',
      key: 'reducePrice',
      align: 'center',
    },
    {
      title: '实际付款',
      dataIndex: 'payPrice',
      key: 'payPrice',
      align: 'center',
    },
    {
      title: '用于统计',
      dataIndex: 'price',
      key: 'price',
      align: 'center',
    },
    {
      title: '价钱，用于积分赠送',
      dataIndex: 'money',
      key: 'money',
      align: 'center',
    },
    {
      title: '退款金额',
      dataIndex: 'refundMoney',
      key: 'refundMoney',
      align: 'center',
    },
    {
      title: '教练价格',
      dataIndex: 'coachPrice',
      key: 'coachPrice',
      align: 'center',
    },
    {
      title: '教练id',
      dataIndex: 'coachId',
      key: 'coachId',
      align: 'center',
    },
    {
      title: '1微信支付，2积分，3支付宝，4现金，5POS机，6VIP月卡，7VIP年卡，8VIP次卡，9IC月卡，10IC年卡，11IC次卡，12免费，13VIP充值卡，14IC充值卡，15积分支付，16VIP季卡，17IC季卡',
      dataIndex: 'payType',
      key: 'payType',
      align: 'center',
    },
    {
      title: '1已付款，2未付款',
      dataIndex: 'payStatus',
      key: 'payStatus',
      align: 'center',
    },
    {
      title: '1已完成，2未使用，3已取消，4退款申请中，5退款被拒绝，6退款成功，7客户端申请退款',
      dataIndex: 'orderStatus',
      key: 'orderStatus',
      align: 'center',
    },
    {
      title: '优惠类型：0无、1抵扣优惠券、2折扣优惠券、3、VIP月卡、4VIP年卡，5VIP次卡、6VIP会员卡、7IC月卡、8IC年卡、9IC次卡、10IC会员卡、11免费订单、12VIP充值卡、13IC充值卡、14VIP季卡、15IC季卡',
      dataIndex: 'couponType',
      key: 'couponType',
      align: 'center',
    },
    {
      title: '二维码地址，保存订单号，支付成功后才生成',
      dataIndex: 'qrcode',
      key: 'qrcode',
      align: 'center',
    },
    {
      title: '优惠说明',
      dataIndex: 'couponDesc',
      key: 'couponDesc',
      align: 'center',
    },
    {
      title: 'vip月卡年卡、ic月卡年卡回退次数',
      dataIndex: 'returnNum',
      key: 'returnNum',
      align: 'center',
    },
    {
      title: 'vip充值回退金额',
      dataIndex: 'returnMoney',
      key: 'returnMoney',
      align: 'center',
    },
    {
      title: '预约详情开始时间数组',
      dataIndex: 'startTime',
      key: 'startTime',
      align: 'center',
    },
    {
      title: '是否已开具发票：1已开发票，2未开发票，3不能开具发票',
      dataIndex: 'isInvoice',
      key: 'isInvoice',
      align: 'center',
    },
    {
      title: '下单时间',
      dataIndex: 'createTime',
      key: 'createTime',
      align: 'center',
      sorter: true,
      ellipsis: true,
      customRender: ({ text }) => toDateString(text, 'yyyy-MM-dd')
    },
    {
      title: '',
      dataIndex: 'updateTime',
      key: 'updateTime',
      align: 'center',
    },
    {
      title: '付款时间',
      dataIndex: 'payTime',
      key: 'payTime',
      align: 'center',
    },
    {
      title: '退款时间',
      dataIndex: 'refundTime',
      key: 'refundTime',
      align: 'center',
    },
    {
      title: '申请退款时间',
      dataIndex: 'refundApplyTime',
      key: 'refundApplyTime',
      align: 'center',
    },
    {
      title: '过期时间',
      dataIndex: 'expirationTime',
      key: 'expirationTime',
      align: 'center',
    },
    {
      title: '对账情况：1=已对账；2=未对账；3=已对账，金额对不上；4=未查询到该订单',
      dataIndex: 'checkBill',
      key: 'checkBill',
      align: 'center',
    },
    {
      title: '备注',
      dataIndex: 'comments',
      key: 'comments',
      align: 'center',
    },
    {
      title: '排序号',
      dataIndex: 'sortNumber',
      key: 'sortNumber',
      align: 'center',
    },
    {
      title: '是否删除, 0否, 1是',
      dataIndex: 'deleted',
      key: 'deleted',
      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 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">
  export default {
    name: 'Order'
  };
</script>

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