<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label-width="100px" label="充值商品名" prop="outTradeNo">
              <el-input v-model="queryParams.subject" placeholder="请输入充值商品名" clearable @keyup.enter="handleQuery" style="width: 200px" />
            </el-form-item>
            <el-form-item label-width="100px" label="用户名/手机号" prop="subject">
              <el-input v-model="queryParams.userIds" placeholder="请输入用户名/手机号" clearable @keyup.enter="handleQuery" style="width: 200px" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
       <template #header>
        <el-row :gutter="10" class="mb8">
<!--          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['system:order:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['system:order:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['system:order:remove']">删除</el-button>
          </el-col>-->
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" border :data="orderList">
        <el-table-column label="编号" width="60" align="center">
          <template #default="{ $index }">
            {{ $index + 1 + (queryParams.pageNum - 1) * queryParams.pageSize }}
          </template>
        </el-table-column>
        <el-table-column label="充值商品名" align="center" prop="subject" />
        <el-table-column label="价格" align="center" prop="totalAmount" />
        <el-table-column label="说明" align="center" prop="productText" />
        <el-table-column label="关联道具" align="center" prop="relatedItemIdText" />
        <el-table-column label="用户ID" align="center" prop="userId" />
        <el-table-column label="用户手机号" align="center" prop="phone" />
        <el-table-column label="用户名" align="center" prop="userName" />
        <el-table-column label="充值时间" align="center" prop="payTime" />
        <el-table-column label="充值状态" align="center" prop="status">
          <template #default="{ row }">
            <el-tag :type="row.status === 'success' ? 'success' : 'info'" size="small">
              {{ row.status === 'success' ? '充值成功' : row.status }}
            </el-tag>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
  </div>
</template>

<script setup name="Order" lang="ts">
import { queryStatisticsPageList } from '@/api/system/business/order';
import { OrderVO, OrderQuery, OrderForm } from '@/api/system/business/order/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const orderList = ref<OrderVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const orderFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: OrderForm = {
  id: undefined,
  outTradeNo: undefined,
  subject: undefined,
  body: undefined,
  totalAmount: undefined,
  currency: undefined,
  userId: undefined,
  bizType: undefined,
  bizId: undefined,
  tradeNo: undefined,
  appId: undefined,
  sellerId: undefined,
  status: undefined,
  payTime: undefined,
  notifyTime: undefined,
  notifyType: undefined,
  tradeStatus: undefined,
  productId: undefined,
  relatedItemId: undefined
};
const data = reactive<PageData<OrderForm, OrderQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    outTradeNo: undefined,
    subject: undefined,
    body: undefined,
    totalAmount: undefined,
    currency: undefined,
    userId: undefined,
    bizType: undefined,
    bizId: undefined,
    tradeNo: undefined,
    appId: undefined,
    sellerId: undefined,
    status: undefined,
    payTime: undefined,
    notifyTime: undefined,
    notifyType: undefined,
    tradeStatus: undefined,
    productId: undefined,
    relatedItemId: undefined,
    params: {}
  },
  rules: {
    id: [{ required: true, message: '主键不能为空', trigger: 'blur' }],
    outTradeNo: [{ required: true, message: '商户订单号不能为空', trigger: 'blur' }],
    subject: [{ required: true, message: '商品标题不能为空', trigger: 'blur' }],
    totalAmount: [{ required: true, message: '订单金额不能为空', trigger: 'blur' }],
    userId: [{ required: true, message: '用户ID不能为空', trigger: 'blur' }],
    bizType: [{ required: true, message: '业务类型不能为空', trigger: 'change' }]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询支付订单列表 */
const getList = async () => {
  loading.value = true;
  const res = await queryStatisticsPageList(queryParams.value);
  orderList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  orderFormRef.value?.resetFields();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  //queryParams.value.flagType = '';
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

import { parseTime } from '@/utils/dateUtils';
/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'business/order/exportPayStatisticsPageList',
    {
      ...queryParams.value
    },
    `充值流水统计${parseTime(new Date(), '{y}{m}{d}{h}{i}{s}')}.xlsx`
  );
};

import { useRoute } from 'vue-router';
const route = useRoute();
onMounted(() => {
  const flagType = route.query.timeFilter;
  const startTime = route.query.startTime;
  const endTime = route.query.endTime;
  // 根据参数设置查询条件
  if (startTime) {
    queryParams.value.startTime = startTime as string; // 直接赋值字符串
  } else {
  }
  // 处理结束时间（如果需要）
  if (endTime) {
    queryParams.value.endTime = endTime as string;
  }
  if (flagType) {
    queryParams.value.flagType = flagType as string;
  }
  getList();
});

// 监听路由参数变化
watch(
  () => route.query,
  (newQuery, oldQuery) => {
    const flagType = route.query.timeFilter;
    const startTime = route.query.startTime;
    const endTime = route.query.endTime;
    // 根据参数设置查询条件
    if (startTime) {
      queryParams.value.startTime = startTime as string; // 直接赋值字符串
    } else {
    }
    // 处理结束时间（如果需要）
    if (endTime) {
      queryParams.value.endTime = endTime as string;
    }
    if (flagType) {
      queryParams.value.flagType = flagType as string;
    }
    getList();
  },
  { immediate: true } // 立即执行一次，确保初始参数被处理
);
</script>
