<template>
  <view class="container">
    <view class="input-group">
      <text>提现金额</text>
      <input type="number" v-model="amount" placeholder="请输入提现金额" />
    </view>
    <button type="primary" @click="handleWithdraw" :disabled="loading">
      {{ loading ? '处理中...' : '提现到微信' }}
    </button>
    
    <!-- 确认弹窗 -->
    <uni-popup ref="popup" type="center">
      <view class="popup-content">
        <text>请在微信中完成提现确认</text>
        <button @click="openWechatConfirm">立即前往微信</button>
        <button @click="copyConfirmLink" type="default">复制确认链接</button>
      </view>
    </uni-popup>
  </view>
</template>

<script>
export default {
  data() {
    return {
      amount: '',
      loading: false,
      confirmData: null,
      confirmUrl: ''
    }
  },
  methods: {
    async handleWithdraw() {
      if (!this.amount || this.amount <= 0) {
        uni.showToast({ title: '请输入有效金额', icon: 'none' });
        return;
      }
      
      this.loading = true;
      
      try {
        // const res = await this.$http.post('/api/withdraw', {
        //   amount: this.amount * 100 // 转为分
        // });
        let res = {
    "status": 1,
    "data": "{\"create_time\":\"2025-05-23T12:01:40+08:00\",\"out_bill_no\":\"202505231126000002\",\"package_info\":\"ABBQO+oYAAABAAAAAACYSTcDBCM+7RZzJPMvaBAAAADnGpepZahT9IkJjn90+1qg4zDQwD3P8jHzAsKItXzHZ9bVt0U6Iij9P3cUmhIlhy0wyH+RIr+WsvjmT1l8wnpCFjaHG19hQMsvHQDeKrYk+qQQeaE=\",\"state\":\"WAIT_USER_CONFIRM\",\"transfer_bill_no\":\"1330007506535022505230016728873933\"}"
}
        if (res.status === 1) {
          // 解析data字段
          const data = JSON.parse(res.data);
          this.confirmData = data;
          
          // 生成微信确认链接
          this.confirmUrl = this.generateWechatConfirmUrl(data);
		  uni.requestMerchantTransfer({
			mchId: '1710280179',
			package: data.package_info,
			appId: 'wx5b1c0c8d01ded35d',
			openId: 'orh_Q600WOMuQiu9uBGJr2MLL4-g'
		  })
          
          // 显示确认弹窗
          // this.$refs.popup.open();
		  console.log(this.confirmUrl)
        } else {
          uni.showToast({ title: '提现申请失败', icon: 'none' });
        }
      } catch (error) {
        uni.showToast({ title: error.message || '网络错误', icon: 'none' });
      } finally {
        this.loading = false;
      }
    },
    
    generateWechatConfirmUrl(data) {
      // 实际项目中这里应该使用后端提供的专门接口生成确认链接
      // 以下是示例逻辑：
      return `https://pay.weixin.qq.com/confirm-transfer?package=${encodeURIComponent(data.package_info)}`;
    },
    
    openWechatConfirm() {
      // 尝试直接调起微信
      if (plus.os.name === 'Android') {
        plus.runtime.openURL(this.confirmUrl, (err) => {
          // 调起失败显示备用方案
          this.showAlternativeOptions();
        });
      } else {
        // iOS使用通用链接
        uni.navigateTo({
          url: `/pages/webview/webview?url=${encodeURIComponent(this.confirmUrl)}`
        });
      }
    },
    
    copyConfirmLink() {
      uni.setClipboardData({
        data: this.confirmUrl,
        success: () => {
          uni.showToast({ title: '链接已复制', icon: 'success' });
        }
      });
    },
    
    showAlternativeOptions() {
      uni.showActionSheet({
        itemList: ['在浏览器打开', '复制链接'],
        success: (res) => {
          if (res.tapIndex === 0) {
            plus.runtime.openURL(this.confirmUrl);
          } else {
            this.copyConfirmLink();
          }
        }
      });
    }
  }
}
</script>

<style>
.container {
  padding: 20px;
}
.input-group {
  margin-bottom: 20px;
}
.popup-content {
  background: #fff;
  padding: 20px;
  border-radius: 10px;
  text-align: center;
}
</style>