<route lang="json5" type="page">
{
  style: {
    navigationBarTitleText: '银行卡列表',
    navigationBarBackgroundColor: '#fff',
  },
}
</route>

<template>
  <view class="withdrawal-type-page min-h-screen pb-100">
    <template v-if="list.length > 0">
      <wd-swipe-action v-for="item in list" :key="item.id">
        <view
          class="flex items-center justify-between py-15 text-14 shadow-[0_0_10px_rgba(10,68,245,0.1)] rounded-10 m-15 p-15"
          @click="onSelectType(item)"
        >
          <image
            class="w-45 h-45"
            src="@/static/images/invest/unionpay-icon.png"
            mode="aspectFit"
          />
          <view class="flex-1 ml-15">
            <view class="text-16 font-bold">{{ item.kaihuhang }}</view>
            <view class="mt-10 text-14 text-#889BC6">{{ item.banknum }}</view>
          </view>
          <wd-icon v-if="isSelect" name="arrow-right" size="14px"></wd-icon>
        </view>
        <template #right>
          <view
            class="px-15 mr-15 rounded-10 bg-#ff000040 h-full flex items-center text-14 text-red-600"
            @click="onDelete(item)"
          >
            删除
          </view>
        </template>
      </wd-swipe-action>
    </template>
    <template v-else>
      <wd-status-tip image="content" tip="暂无内容" />
    </template>
    <view class="fixed bottom-0 left-0 right-0 pb-safe">
      <button class="text-16 btn-gradient text-white rounded-full m-15" @click="onAddType">
        添加收款方式
      </button>
    </view>
  </view>
</template>

<script setup lang="ts">
import { deleteBankCardAPI, getBankCardAPI } from '@/service'
import { showModal } from '@/utils'
import { onLoad } from '@dcloudio/uni-app'

const isSelect = ref(false)
onLoad((options) => {
  if (options.type === 'select') {
    isSelect.value = true
  }
})

const onSelectType = (item) => {
  if (isSelect.value) {
    uni.$emit('withdrawal-type', item)
    uni.navigateBack()
  } else {
    // uni.navigateTo({
    //   url: '/pages-sub/withdrawal-type/edit?id=1',
    // })
  }
}

const onAddType = () => {
  uni.navigateTo({
    url: '/pages-sub/withdrawal-type/edit',
  })
}

const onDelete = async (item) => {
  await showModal('确认删除？')
  await deleteBankCardAPI({
    id: item.id,
  })
  await showModal('删除成功')
  loadData()
}

const loadData = async () => {
  list.value = (await getBankCardAPI({
    limit: 1000,
    page: 1,
  }).then((res) => res.data)) as any
}

const list = ref([])
onShow(async () => {
  loadData()
})
</script>
