<template>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true" @row-click="openForm($event)" row-class-name="row">
      <el-table-column label="序号" align="center" prop="id" />
      <!-- <el-table-column label="提现渠道类型" align="center" prop="withdrawalType" /> -->
      <el-table-column label="渠道" align="center" prop="withdrawalName" />
      <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="count" />
      <!-- ,已完成是1，未完成为0 -->
      <el-table-column label="状态" align="center" prop="status" >
        <template #default="scope">
          {{ formatStatus(scope.row.status)  }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="提现渠道记录id" align="center" prop="ptDailyWithdrawalId" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      /> -->
      <el-table-column label="操作" align="center" width="180px">
        <template #default="scope">
          <el-button
            size='small'
            type="primary"
            @click="handleImport($event,scope.row)"
            v-if="scope.row.excelStatus && scope.row.status !== 1"
            plain
            v-hasPermi="['distri:application-for-withdrawal-channel:import']"
          >
            导入凭证
          </el-button>
          
          <el-button
            size='small'
            type="success"
            v-if="isExport"
            plain
            @click="handleExport($event,scope.row.id)"
            v-hasPermi="['distri:application-for-withdrawal-channel:export']"
          >
            导出
          </el-button>
          
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

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

</template>

<script setup lang="ts">
import { fenToYuan,pointsToPoint } from '@/utils'
import download from '@/utils/download'
import { ApplicationForWithdrawalChannelApi, ApplicationForWithdrawalChannelVO } from '@/api/system/distri/withdrawalchannel'
import ApplicationForWithdrawalChannelForm from './ApplicationForWithdrawalChannelForm.vue'
import { unionWith } from 'lodash-es';
import ImportPayMent from './ImportPayMent.vue'

/** 提现渠道记录 列表 */
defineOptions({ name: 'ApplicationForWithdrawalChannel' })

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

const loading = ref(true) // 列表的加载中
const list = ref<ApplicationForWithdrawalChannelVO[]>([]) // 列表的数据
// 列表的总页数
const total = ref(0)
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  withdrawalType: undefined,
  withdrawalName: undefined,
  amount: undefined,
  count: undefined,
  status: undefined,
  ptDailyWithdrawalId: undefined,
  id:undefined
  // createTime: [],
})
const props = defineProps({
  row: {
    type: Object,
    default: () => ({})
  }
})

const isExport = computed(() => {
  const currentTime = new Date().getTime()
  const endTime = new Date(props.row.endTime).getTime()
  // console.log(currentTime,endTime)
  return currentTime > endTime
})

const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ApplicationForWithdrawalChannelApi.getApplicationForWithdrawalChannelPage(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 ApplicationForWithdrawalChannelApi.deleteApplicationForWithdrawalChannel(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

// 导入
const importRef = ref()
const handleImport = async (event, row) => {
  event.stopPropagation()
  importRef.value.open(event, row)
}
/** 导出按钮操作 */
const handleExport = async (event,id) => {
  event.stopPropagation();
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ApplicationForWithdrawalChannelApi.exportApplicationForWithdrawalChannel({id})
    download.excel(data, '提现渠道记录.xls')
  } catch {
  } finally {
    exportLoading.value = false;
    await getList();
  }
}
function formatStatus(status){
  if(!status){
    return '待转账'
  }else if(status === 1){
    return '转账成功'
  }else if(status === 2){
    return '部分转账成功'
  }else{
    return '转账失败'
  }
}
/** 初始化 **/
onMounted(() => {
  queryParams.ptDailyWithdrawalId = props.row.id
  getList()
})
</script>