<template>
  <div class="page">
    <div class="ele-body">
      <a-card :bordered="false">
        <!-- 表格 -->
        <ele-pro-table
          ref="tableRef"
          row-key="orderRefundId"
          :columns="columns"
          :datasource="datasource"
          v-model:selection="selection"
          tool-class="ele-toolbar-form"
          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 === 'expirationDay'">
              {{ record.expirationDay }}天
            </template>
            <template v-if="column.key === 'batteryModel'">
              {{ record.batteryModel }}
            </template>
            <template v-if="column.key === 'equipmentCode'">
              {{ record.equipmentCode }}
            </template>

            <template v-if="column.key === 'merchantName'">
              {{ record.merchantName }}
            </template>

            <template v-if="column.key === 'payStatus'">
              <span v-if="record.payStatus === 20" color="green">已付</span>
              <span v-if="record.payStatus === 10" color="red">未付</span>
            </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 === 'createTime'">
              {{ record.createTime }}
            </template>
            <template v-if="column.key === 'isAdminRenew'">
              <span v-if="record.isAdminRenew === 1">后台续费</span>
              <span v-else>-</span>
            </template>
          </template>
        </ele-pro-table>
      </a-card>
    </div>
    <!-- 编辑弹窗 -->
    <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';
  import { 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 Edit from './components/edit.vue';
  import {
    pageOrderRefund,
    removeOrderRefund,
    removeBatchOrderRefund
  } from '@/api/order/refund';
  import {listOrderPay, pageOrderPay} from '@/api/order';
  import type { OrderRefund, OrderRefundParam } from '@/api/order/refund/model';

  const props = defineProps<{
    activeKey?: boolean;
    data?: any;
  }>();

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

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

  // 表格列配置
  const columns = ref<ColumnItem[]>([


    {
      title: '订单号',
      dataIndex: 'orderNo',
      key: 'orderNo',
      sorter: true
    },
    {
      title: '关联订单号',
      dataIndex: 'rentOrderNo',
      key: 'rentOrderNo',
      sorter: true
    },
    {
      title: '电池型号',
      dataIndex: 'batteryModel',
      key: 'batteryModel'
    },
    {
      title: '电池编号',
      dataIndex: 'equipmentCode',
      key: 'equipmentCode'
    },
    {
      title: '所属商户',
      dataIndex: 'merchantName',
      key: 'merchantName',
      sorter: true
    },
    {
      title: '到期时间',
      dataIndex: 'expirationTime',
      key: 'expirationTime'
    },
    {
      title: '剩余时间',
      key: 'expirationDay',
      dataIndex: 'expirationDay'
    },
    {
      title: '客户实名',
      dataIndex: 'realName',
      key: 'realName'
    },
    {
      title: '订单金额',
      dataIndex: 'payPrice',
      key: 'payPrice'
    },
    {
      title: '续费情况',
      dataIndex: 'isAdminRenew',
      key: 'isAdminRenew'
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      key: 'createTime',
      customRender: ({ text }) => toDateString(text)
    }
  ]);

  // 表格选中数据
  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;
    where.payStatus=20;
    console.log('isRenew');
    return pageOrderPay({
      ...where,
      ...orders,
      page,
      limit,
      payStatus: 20,
    });
  };

  /* 搜索 */
  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.orderRefundId)
      .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.orderRefundId))
          .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>
