<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 === '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 === 'deliveryType'">
              <span v-if="record.deliveryType === 10">快递配送</span>
              <span v-if="record.deliveryType === 20">门店自提</span>
            </template>
            <template v-if="column.key === 'comments'">
              <a-tooltip :title="record.comments">
                {{ record.comments }}
              </a-tooltip>
            </template>
            <template v-if="column.key === 'auditStatus'">
              <a-tag v-if="record.auditStatus === 10" color="orange"
                >待审核</a-tag
              >
              <a-tag v-if="record.auditStatus === 20" color="green"
                >已同意</a-tag
              >
              <a-tag v-if="record.auditStatus === 30" 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-space>
                <a-button v-if="record.auditStatus == 10" @click="openEdit(record)">审核</a-button>
              </a-space>
            </template>
            <template v-if="column.key === 'updateTime'">
              <span v-if="record.auditStatus ===10">-</span>
              <span v-else>{{ record.updateTime }}</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 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[]>([
    {
      key: 'index',
      width: 48,
      align: 'center',
      fixed: 'left',
      hideInSetting: true,
      customRender: ({ index }) => index + (tableRef.value?.tableIndex ?? 0)
    },
    {
      title: '操作',
      key: 'action',
      width: 100,
      align: 'center',
      fixed: 'left',
      hideInSetting: true
    },
    {
      title: '订单号',
      dataIndex: 'orderNo',
      key: 'orderNo'
    },
    {
      title: '买家',
      dataIndex: 'nickname',
      key: 'nickname'
    },
    {
      title: '用户申请原因',
      dataIndex: 'applyDesc',
      key: 'applyDesc',
      ellipsis: true
    },
    {
      title: '拒绝原因',
      dataIndex: 'refuseDesc',
      key: 'refuseDesc',
      ellipsis: true
    },
    {
      title: '实际返还金额',
      dataIndex: 'refundMoney',
      key: 'refundMoney'
    },
    {
      title: '审核状态',
      key: 'auditStatus',
      dataIndex: 'auditStatus'
    },
    {
      title: '审核时间',
      dataIndex: 'updateTime',
      key: 'updateTime',
      customRender: ({ text }) => toDateString(text)
    },
    {
      title: '操作人',
      dataIndex: 'operator',
      key: 'operator'
    },
    {
      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;
    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.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>
