<template>
  <ContentWrap v-hasPermi="['pay:currency-recharge:query']">
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="100px"
    >
      <el-form-item label="支付订单编号" prop="payOrderId">
        <el-input
          v-model="queryParams.payOrderId"
          placeholder="请输入支付订单编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="支付退款编号" prop="payRefundId">
        <el-input
          v-model="queryParams.payRefundId"
          placeholder="请输入支付退款编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="是否已支付" prop="payStatus">
        <el-select
          v-model="queryParams.payStatus"
          placeholder="请选择是否已支付"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="(val, i) in payStatus"
            :key="i"
            :label="val.label"
            :value="val.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="支付时间" prop="payTime">
        <el-date-picker
          v-model="queryParams.payTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="用户实际到余额" align="center" prop="totalPrice">
        <template #default="scope">
          {{ fenToYuan(scope.row.totalPrice) }}
        </template>
      </el-table-column>
      <el-table-column label="实际支付金额" align="center" prop="payPrice">
        <template #default="scope">
          {{ fenToYuan(scope.row.payPrice) }}
        </template>
      </el-table-column>
      <el-table-column label="钱包赠送金额" align="center" prop="bonusPrice">
        <template #default="scope">
          {{ fenToYuan(scope.row.bonusPrice) }}
        </template>
      </el-table-column>
      <el-table-column label="是否已支付" align="center" prop="payStatus">
        <template #default="scope">
          {{ scope.row.payStatus ? '已支付': '未支付' }}
        </template>
      </el-table-column>
      <el-table-column label="支付成功的支付渠道" align="center" prop="payChannelCode">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.PAY_CHANNEL_CODE" :value="scope.row.payChannelCode" />
        </template>
      </el-table-column>
      <el-table-column label="支付成功的外部订单号" align="center" prop="payOrderChannelOrderNo" />
      <el-table-column label="退款状态" align="center" prop="refundStatus">
        <template #default="scope">
          <dict-tag v-if="scope.row.payStatus && scope.row.refundStatus !== 0" :type="DICT_TYPE.PAY_REFUND_STATUS" :value="scope.row.refundStatus" />
          <span v-else></span>
        </template>
      </el-table-column>
      <el-table-column
        label="订单支付时间"
        align="center"
        prop="payTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button v-if="scope.row.payStatus && !scope.row.refundStatus" link type="danger" @click="handleRefund(scope.row)">发起退款</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
</template>

<script lang="ts" setup>
import { dateFormatter } from '@/utils/formatTime'
import { DICT_TYPE } from '@/utils/dict'
import { fenToYuan } from '@/utils'
import { getWalletRechargePage, refundWalletRechargeOrder } from '@/api/pay/wallet/rechargeRecord'
defineOptions({ name: 'CurrencyRechargeList' })

const message = useMessage() // 消息弹窗
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  payTime: null,
  payStatus: null,
  payRefundId: null,
  payOrderId: null,
  packageId: null,
  currencyId: null,
  payChannelCode: null
})
const payStatus = ref([
  { label: '已支付', value: true },
  { label: '未支付', value: false }
])
const queryFormRef = ref()
const list = ref([]) // 列表的数据

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await getWalletRechargePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 退款按钮操作 */
const handleRefund = async (row: any) => {
  const id = row.id
  try {
    await message.confirm('是否确认退款编号为"' + id + '"的订单?')
    await refundWalletRechargeOrder(id)
    await getList()
    message.success('发起退款成功！')
  } catch {}
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

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

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>