<template>
 
  <!-- 列表 -->
  <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="用户ID" align="center" prop="userId" /> -->
      <el-table-column label="提现渠道" align="center" prop="withdrawalType">
         <template #default="scope">
          <el-tag v-if="scope.row.withdrawalType === 1">微信</el-tag>
          <el-tag v-else-if="scope.row.withdrawalType === 2">支付宝</el-tag>
          <el-tag v-else-if="scope.row.withdrawalType === 3">银行卡</el-tag>
        </template>
      
      </el-table-column>
      <el-table-column label="提现总金额" align="center" prop="amountTotal"  >
        <template #default="scope">
          {{ fenToYuan(scope.row.amountTotal) }}
        </template>
      </el-table-column>
      <el-table-column label="到账金额" align="center" prop="amount" >
        <template #default="scope">
          {{ fenToYuan(scope.row.amount) }}
        </template>
      </el-table-column>
      <el-table-column label="消费分" align="center" prop="withdrawConsumption" >
        <template #default="scope">
          {{ pointsToPoint(scope.row.withdrawConsumption) }}
        </template>
      </el-table-column>
      <el-table-column label="提现账号" align="center" prop="withdrawalAccount" />
      <el-table-column label="提现账号名称" align="center" prop="accountName" />
      <el-table-column label="提现状态" align="center" prop="status" >
        <template #default="scope">
          {{ formatStatus(scope.row.status) }}
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />

    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <ApplicationForWithdrawalForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { fenToYuan,pointsToPoint } from '@/utils'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { ApplicationForWithdrawalApi, ApplicationForWithdrawalVO } from '@/api/system/distri/withdrawal'
import ApplicationForWithdrawalForm from './ApplicationForWithdrawalForm.vue'

/** 提现申请记录 列表 */
defineOptions({ name: 'ApplicationForWithdrawal' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<ApplicationForWithdrawalVO[]>([]) // 列表的数据
// 列表的总页数
const total = ref(0)
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  userId: undefined,
  withdrawalType: undefined,
  amount: undefined,
  withdrawalAccount: undefined,
  status: undefined,
  applicationForWithdrawalChannelId: undefined,
  createTime: []
})
const props = defineProps({
	row: {
    type: Object,
    default: () => ({})
  }
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

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

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

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

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ApplicationForWithdrawalApi.deleteApplicationForWithdrawal(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ApplicationForWithdrawalApi.exportApplicationForWithdrawal(queryParams)
    download.excel(data, '提现申请记录.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}
function formatStatus(status){
  if(!status){
    return '提现中'
  }else if(status === 1){
    return '提现成功'
  }else{
    return '提现失败'
  }
}
/** 初始化 **/
onMounted(() => {
  queryParams.applicationForWithdrawalChannelId = props.row.id
  getList();
})
</script>