<template>
  <view class="page-wrapper global-wrapper bgF2">
    <JHeaderTitle title="进件管理" bgColor="#f2f2f2" />
    <JSearchInput place="搜索用户名、用户号、进件单号" ref="searchInfo" @search="search" @resetSearch="resetSearch" />
    <ApplyHeader @selected="selected" />
    <block v-for="v in useDataResult.dataList" :key="v.applyId">
      <ApplyCard
        :ifName="v.ifName"
        :icon="v.icon"
        :bgColor="v.bgColor"
        :createdAt="v.createdAt"
        :mchFullName="v.mchFullName"
        :mchNo="v.mchNo"
        :applyId="v.applyId"
        :state="v.state"
        :applyErrorInfo="v.applyErrorInfo"
        :ifCode="v.ifCode"
        @click="toPage(v.ifCode, v.state, v.ifName, v.applyId, v.applyErrorInfo, v.mchNo, v.channelVar1,v.isvNo,v.autoConfigMchAppId,v.range)"
        @getNewState="getNewState(v)"
      />
      <view></view>
    </block>
    <jeepayListNull :isShow="true" :list="useDataResult.dataList.length" />
    <view class="block"></view>
    <view class="apply-footer ButtonBor">
      <JButton pd="30rpx  30rpx 50rpx 30rpx" pdTop="0" @HandleTouch="goToApply">发起进件</JButton>
    </view>
	
	<!-- 选择支付 接口 -->
	<JeepayPopupListSelect
	  ref="selectIncomingRef"
	  title='请选择场景类型'
	  :reqTableDataFunc="reqTableIncomingDataByIfcodeFunc"
	  :fields="{ key: 'range', left: 'name' }"
	  @confirm="confirmIncoming"
	>
	</JeepayPopupListSelect>
  </view>
</template>

<script setup>
import { ref } from 'vue'
import { onLoad, onBackPress, onShow } from '@dcloudio/uni-app'
import { $applyList, $getMchApplymentChannelState } from '@/http/apiManager.js'
import useGetList from '@/hooks/useList.js'
import JHeaderTitle from '@/components/newComponents/JHeaderTitle/JHeaderTitle'
import JSearchInput from '@/components/newComponents/JSearchInput/JSearchInput'
import jeepayListNull from '@/components/jeepayListNull/jeepayListNull.vue'
import JeepayPopupListSelect from '@/components/JeepayPopupConfirm/JeepayPopupListSelect.vue'
import JButton from '@/components/newComponents/JButton/JButton'
import ApplyHeader from './components/ApplyHeader.vue'
import ApplyCard from './components/ApplyCard.vue'
const { useDataResult, getList } = useGetList({
  requestFun: $applyList,
  params: { state: '' },
})
const searchInfo = ref(null)
const params = ref({
  state: '',
  mch: '',
})
const selectIncomingRef = ref()
const search = (data) => {
  params.value.mch = data
  getList(params.value)
}
const resetSearch = (data) => {
  params.value.mch = ''
  getList(params.value)
}
const selected = (val) => {
  params.value.state = val
  getList(params.value)
}
const goToApply = () => {
	console.log(selectIncomingRef,'selectIncomingRefselectIncomingRef')
	selectIncomingRef.value.open()
  // uni.navigateTo({
  //   url: './selectChannel',
  // })
}
function toPage(code, state, name, applyId, src, mchNo, channelVar1,isvNo,autoConfigMchAppId,range) {
	console.log(code, state, name, applyId, src, mchNo, channelVar1,isvNo,autoConfigMchAppId,range);
  let isView = state == 0 || state == 3 || state == 8
  uni.navigateTo({ url: `./applyDetailWeb?applyId=${applyId}&isView=${isView ? 0 : 1}&ifCode=${code}&autoConfigMchAppId=${autoConfigMchAppId}&mchNo=${mchNo}&range=${range}&isvNo=${isvNo}&applyId=${applyId}` })
}

function toH5ApplyOptionPage(applyId, configPage) {
  uni.navigateTo({ url: `./applyDetailH5ApplyOption?applyId=${applyId}&configPage=${configPage}` })
}
// 获取进件最新状态
const getNewState = (val) => {
  $getMchApplymentChannelState(val.applyId)
    .then(({ bizData }) => {
      val.state = bizData.state
      uni.showToast({
        title: '获取最新结果成功',
        icon: 'none',
        mask: true,
      })
    })
    .catch((err) => {
      uni.showToast({
        title: '获取最新结果失败请稍后重试',
        icon: 'none',
        mask: true,
      })
    })
}
onBackPress(() => {
  if (searchInfo.value.searchText !== '' || params.value.mch !== '') {
    searchInfo.value.searchText = ''
    params.value.state = ''
    params.value.mch = ''
    getList(params.value)
    return true
  } else {
    return false
  }
})

function confirmIncoming(selected){
	if(!selected){
		ak.infoBox.showToast('请选择场景类型')
		return false;
	}
	selectIncomingRef.value.close()
	uni.navigateTo({
	  url: './selectChannel?range='+selected.range,
	  
	})
	ak.go.to('PAGES_APPLYMENT_SELECETDPAY', { range: selected.range })
}

function reqTableIncomingDataByIfcodeFunc(){
	const incomingList = [
		{range:0,name:"线下场景"},
		{range:1,name:"线上场景"}
	];
	return Promise.resolve({ bizData: { records: incomingList, hasNext: false }})
}
</script>

<style lang="scss" scoped>
.page-wrapper {
  .block {
    height: 210rpx;
  }
  .apply-footer {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
  }
}
</style>
