<template>
  <view class="page">
    <u-navbar title="添加收款账户" :border-bottom="false" :background="bgc" back-icon-color="#333" title-color='#333' 
      title-size='34' height='36' id="navbar"></u-navbar>

    <view class="container">
      <!-- 账户类型选择 -->
     <!-- <view class="section-card">
        <view class="section-title">选择收款方式</view>
        <view class="type-options">
          <view v-for="(item,index) in zhanghulist" :key="index"
            class="type-option" 
            :class="{active: activeType === item.apiType}"
            @click="btnxz(item)">
            <u-icon :name="item.picture" size="44" color="#409EFF"></u-icon>
            <view class="option-text">
              <text class="option-name">{{item.outName}}</text>
              <text class="option-desc">{{item.remark}}</text>
            </view>
            <u-icon v-if="activeType === item.apiType" name="checkmark" size="32" color="#409EFF"></u-icon>
          </view>
        </view>
      </view> -->

      <!-- 银行卡绑定表单 -->
      <view class="section-card" v-if="activeType == 'BANK' || activeType == 'PUBLIC_BANK'">
        <view class="section-title">绑定银行卡信息</view>
        <!-- 开户行输入（仅对公银行卡显示） -->
        <view class="form-item" v-if="activeType == 'PUBLIC_BANK'">
          <view class="form-label">开户行名称</view>
          <u-input v-model="bankForm.bankName" placeholder="请输入开户行名称" border="none" class="custom-input"></u-input>
        </view>
        
        <view class="form-item">
          <view class="form-label">持卡人姓名</view>
          <u-input v-model="bankForm.name" placeholder="请输入持卡人姓名" border="none" class="custom-input"></u-input>
        </view>
        
        <view class="form-item">
          <view class="form-label">银行卡号</view>
          <u-input v-model="bankForm.no" placeholder="请输入银行卡号" border="none" type="number" class="custom-input"></u-input>
        </view>
        
        <view class="form-item">
          <view class="form-label">身份证号</view>
          <u-input v-model="bankForm.idCard" placeholder="请输入身份证号" border="none" class="custom-input"></u-input>
        </view>
        
        <view class="form-item" style="border-bottom: none;">
          <view class="form-label">手机号</view>
          <u-input v-model="bankForm.mobile" placeholder="请输入手机号" border="none" class="custom-input"></u-input>
        </view>
      </view>
      
      <!-- 二维码收款设置 -->
      <view class="section-card" v-if="activeType == 'QR_CODE'">
        <view class="section-title">上传收款二维码</view>
        <view class="qrcode-upload">
          <view class="upload-area" @click="chooseImage">
            <u-icon name="plus" size="60" color="#C0C4CC" v-if="!bankForm.no"></u-icon>
            <image :src="bankForm.no" mode="aspectFit" v-else class="qrcode-img"></image>
            <text class="upload-tip" v-if="!bankForm.no">点击上传二维码</text>
          </view>
        </view>
        <view class="form-item" style="margin-top: 20rpx;">
          <view class="form-label">收款人姓名</view>
          <u-input v-model="bankForm.name" placeholder="请输入收款人姓名" border="none" class="custom-input"></u-input>
        </view>
        <view class="form-item">
          <view class="form-label">收款人身份证号</view>
          <u-input v-model="bankForm.idCard" placeholder="请输入收款人身份证号" border="none" class="custom-input"></u-input>
        </view>
        <view class="form-item" style="border-bottom: none;">
          <view class="form-label">收款人手机号</view>
          <u-input v-model="bankForm.mobile" placeholder="请输入收款人手机号" border="none" class="custom-input"></u-input>
        </view>
      </view>
	  <!-- 太米收款设置 -->
	  <view class="section-card" v-if="activeType == 'TM_WALLET'">
	    <view class="section-title">绑定银行卡信息</view>
	    <view class="form-item">
	      <view class="form-label">持卡人姓名</view>
	      <u-input v-model="bankForm.name" placeholder="请输入持卡人姓名" border="none" class="custom-input"></u-input>
	    </view>
	    <view class="form-item">
	      <view class="form-label">银行卡号</view>
	      <u-input v-model="bankForm.no" placeholder="请输入银行卡号" border="none" type="number" class="custom-input"></u-input>
	    </view>
	    <view class="form-item">
	      <view class="form-label">身份证号</view>
	      <u-input v-model="bankForm.idCard" placeholder="请输入身份证号" border="none" class="custom-input"></u-input>
	    </view>
	    <view class="form-item" style="border-bottom: none;">
	      <view class="form-label">手机号</view>
	      <u-input v-model="bankForm.mobile" placeholder="请输入手机号" border="none" class="custom-input"></u-input>
	    </view>
	  </view>

      <!-- 提交按钮 -->
      <view class="submit-btn">
        <u-button 
          type="primary" 
          shape="circle" 
          @click="submitForm"
          class="custom-button"
          :disabled="!activeType"
        >
          添加
        </u-button>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      bgc: {
        backgroundColor: "#fff",
      },
      activeType: '', // bank/qrcode
      bankForm: {
		channelId:'', // 银行卡类型：BANK-私人银行卡，PUBLIC_BANK-对公银行卡，QR-二维码收款
        name: '',
        idCard: '',
        mobile: '',
		no:'',
        bankName: '' // 开户行名称
      },
	  token: '', // 添加七牛云token
	  upurl: 'https://api.ccttiot.com', // 添加上传域名
	  zhanghulist:[],
	  activeTypeid:''
    }
  },
  onShow() {
	
  },
  onLoad(e) {
  	this.activeType = e.activeType
	this.activeTypeid = e.id 
	this.getqudao()
	this.getQiniuToken()
  },
  methods: {
	  btnxz(item){
		this.activeType = item.apiType 
		this.activeTypeid = item.id 
	  },
	  // 获取渠道列表
	  getqudao(){
		  this.$u.get(`/app/withdrawChannel/listSimple?appId=${this.$store.state.appid}`).then(res =>{
			  if(res.code == 200){
				  this.zhanghulist = res.data
			  }
		  })
	  },
	  // 获取七牛云token
	  getQiniuToken() {
	  	this.$u.get("/common/qiniuToken").then((res) => {
	  		if (res.code == 200) {
	  			this.token = res.data
	  		}
	  	})
	  },
	  // 选择图片
	  chooseImage() {
	  	uni.chooseImage({
	  		count: 1,
	  		sizeType: ['compressed'],
	  		sourceType: ['album', 'camera'],
	  		success: (res) => {
	  			this.uploadImage(res.tempFilePaths[0])
	  		}
	  	})
	  },
	  // 上传图片
	  uploadImage(filePath) {
	  	uni.showLoading({
	  		title: '上传中...'
	  	})
	  	const math = 'static/' + this.$u.guid(20)
	  	uni.uploadFile({
	  		url: 'https://up-z2.qiniup.com',
	  		filePath: filePath,
	  		name: 'file',
	  		formData: {
	  			token: this.token,
	  			key: 'bike/img/' + math
	  		},
	  		success: (res) => {
	  			const response = JSON.parse(res.data)
	  			if (response.key) {
	  				this.bankForm.no = this.upurl + '/' + response.key
	  				uni.showToast({
	  					title: '上传成功',
	  					icon: 'success'
	  				})
	  			} else {
	  				uni.showToast({
	  					title: '上传失败',
	  					icon: 'none'
	  				})
	  			}
	  		},
	  		fail: () => {
	  			uni.showToast({
	  				title: '上传失败',
	  				icon: 'none'
	  			})
	  		},
	  		complete: () => {
	  			uni.hideLoading()
	  		}
	  	})
	  },
    submitForm() {
		this.bankForm.channelId = this.activeTypeid
		if(this.activeType == 'WECHAT'){
			let that = this
			wx.login({
				success(res) {
					if (res.code) {
						that.$u.put(`/bst/account/bindByJsCode?jsCode=${res.code}&channelId=${that.activeTypeid}`).then((resp) => {
							if (resp.code == 200) {
								uni.hideLoading()
								uni.showToast({
								  title: '添加成功',
								  icon: 'success',
								  duration:3000
								})
								setTimeout(() => {
								    uni.navigateBack()
								}, 1500)
							}else{
								uni.showToast({
								  title: resp.msg,
								  icon: 'none',
								  duration:3000
								})
							}
						})
					}
				},
			})
			return
		}
		uni.showLoading({ title: '添加中...' })
		// 银行卡类型保持用户的选择：BANK 或 PUBLIC_BANK
		let data = {
			...this.bankForm
		}
	   this.$u.post(`/bst/account`,data).then((res) => {
	   	    if (res.code == 200) {
				uni.hideLoading()
				uni.showToast({
				  title: '添加成功',
				  icon: 'success',
				  duration:3000
				})
	   	    	setTimeout(() => {
	   	    	    uni.navigateBack()
	   	    	}, 1500)
	   	    }else{
				uni.showToast({
				  title: res.msg,
				  icon: 'none',
				  duration:3000
				})
			}
	   })
    },
  }
}
</script>

<style lang="scss">
page {
  background: #F5F7FA;
  font-family: -apple-system, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Microsoft YaHei', sans-serif;
}
.container {
  padding: 30rpx 24rpx;
  min-height: calc(100vh - 100rpx);
}

/* 通用卡片样式 */
.section-card {
  background: #fff;
  border-radius: 24rpx;
  padding: 40rpx 36rpx;
  margin-bottom: 30rpx;
  box-shadow: 0 2rpx 16rpx rgba(0, 0, 0, 0.04);
  border: 1rpx solid #F0F2F5;
}

/* 通用标题样式 */
.section-title {
  font-size: 34rpx;
  font-weight: 600;
  color: #303133;
  margin-bottom: 36rpx;
  padding-bottom: 20rpx;
  border-bottom: 2rpx solid #F5F7FA;
  position: relative;
}
.section-title::after {
  content: '';
  position: absolute;
  bottom: -2rpx;
  left: 0;
  width: 60rpx;
  height: 2rpx;
  background: linear-gradient(90deg, #409EFF 0%, #5CB8FF 100%);
  border-radius: 2rpx;
}

/* 账户类型选择 */
.type-options {
  display: flex;
  flex-direction: column;
  gap: 16rpx;
}
.type-option {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 24rpx;
  background: #F7F9FC;
  border-radius: 16rpx;
  transition: all 0.2s ease-in-out;
}
.type-option.active {
  background: #EEF5FF;
  border: 1rpx solid #409EFF;
}
.type-option:active {
  transform: scale(0.98);
}
.option-text {
  display: flex;
  flex-direction: column;
  margin-left: 20rpx;
  flex: 1;
}
.option-name {
  font-size: 28rpx;
  color: #333;
  font-weight: 400;
}
.option-desc {
  font-size: 24rpx;
  color: #999;
  margin-top: 4rpx;
}

/* 表单样式 */
.form-item {
  margin-bottom: 20rpx;
  padding-bottom: 24rpx;
  padding-top: 8rpx;
  border-bottom: 1rpx solid #F0F2F5;
  transition: all 0.3s ease;
}
.form-item:last-child {
  margin-bottom: 0;
}
.form-label {
  font-size: 26rpx;
  color: #606266;
  font-weight: 500;
  margin-bottom: 20rpx;
  display: flex;
  align-items: center;
  position: relative;
  padding-left: 8rpx;
}
.form-label::before {
  content: '';
  position: absolute;
  left: 0;
  width: 4rpx;
  height: 24rpx;
  background: linear-gradient(180deg, #409EFF 0%, #5CB8FF 100%);
  border-radius: 2rpx;
}
.custom-input {
  font-size: 30rpx;
  color: #303133;
  height: 48rpx;
  line-height: 48rpx;
  padding: 8rpx 0;
  placeholder {
    color: #C0C4CC;
    font-size: 28rpx;
  }
}

/* 二维码上传 */
.qrcode-upload {
    display: flex;
    justify-content: center;
    margin: 20rpx 0 40rpx 0;
}
.upload-area {
    width: 240rpx;
    height: 240rpx;
    background: linear-gradient(135deg, #F7F9FC 0%, #EEF5FF 100%);
    border-radius: 20rpx;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    position: relative;
    border: 2rpx dashed #D3D4D6;
    transition: all 0.3s ease;
}
.upload-area:active {
    transform: scale(0.98);
    border-color: #409EFF;
    background: linear-gradient(135deg, #EEF5FF 0%, #E1F0FF 100%);
}
.qrcode-img {
    width: 100%;
    height: 100%;
    border-radius: 18rpx;
    object-fit: cover;
    border: 2rpx solid #E4E7ED;
}
.upload-tip {
    margin-top: 20rpx;
    font-size: 26rpx;
    color: #909399;
    font-weight: 400;
}

/* 提交按钮 */
.submit-btn {
    margin-top: 50rpx;
    margin-bottom: 30rpx;
}
.custom-button {
    height: 96rpx;
    font-size: 34rpx;
    font-weight: 600;
    background: linear-gradient(135deg, #409EFF 0%, #5CB8FF 100%);
    border-radius: 48rpx;
    box-shadow: 0 8rpx 24rpx rgba(64, 158, 255, 0.25);
    transition: all 0.3s ease;
}
.custom-button::after {
    border: none;
}
.custom-button:active {
    transform: translateY(2rpx);
    box-shadow: 0 4rpx 12rpx rgba(64, 158, 255, 0.2);
}
.custom-button:disabled {
    background: linear-gradient(135deg, #C0C4CC 0%, #D3D4D6 100%);
    box-shadow: none;
    transform: none;
}
</style>