<template>
	<view class="container">
		<view class="form">
			<u--form :model="form" ref="uForm" :rules="rules" label-width="170rpx">
				<u-gap height="10" bgColor="#f3f3f3"></u-gap>
				<view class="block">
					<u-form-item label="提现金额" prop="amount" borderBottom>
						<u-input v-model="form.amount" maxlength="20" :disabled="isEdit"
							:placeholder="`可提现金额￥${form.money ? form.money : 0}`" />
					</u-form-item>
					<u-form-item label="服务费" prop="serviceFee" borderBottom v-if="form.amount > 0">
						<span class="service-fee">本次提现服务费￥{{ form.amount * 0.04 }}, 实际到账￥{{form.amount - (form.amount * 0.04)}}</span>
					</u-form-item>
				</view>
				<u-gap height="10" bgColor="#f3f3f3"></u-gap>
				<view class="block">
					<u-form-item label="打款方式" prop="payType" borderBottom @click="showPayType">
						<span class="service-fee">{{ payType }}</span>
					</u-form-item>
					<u-form-item label="支付宝姓名" prop="alipayName" borderBottom v-if="payType == '支付宝'">
						<u-input v-model="form.alipayName" maxlength="20" :disabled="isEdit" placeholder="支付宝姓名" />
					</u-form-item>
					<u-form-item label="支付宝账号" prop="alipayAccount" borderBottom v-if="payType == '支付宝'">
						<u-input v-model="form.alipayAccount" maxlength="20" :disabled="isEdit" placeholder="支付宝账号" />
					</u-form-item>
					<u-form-item label="银行开户名" prop="bankName" borderBottom v-if="payType == '银行卡'">
						<u-input v-model="form.bankName" maxlength="20" :disabled="isEdit" placeholder="银行开户名" />
					</u-form-item>
					<u-form-item label="银行开户名" prop="bankAccount" borderBottom v-if="payType == '银行卡'">
						<u-input v-model="form.bankAccount" maxlength="20" :disabled="isEdit" placeholder="银行卡开户名" />
					</u-form-item>
					<u-form-item label="银行卡号" prop="bankCard" borderBottom v-if="payType == '银行卡'">
						<u-input v-model="form.bankCard" maxlength="20" :disabled="isEdit" placeholder="银行卡号" />
					</u-form-item>
				</view>
				<u-gap height="10" bgColor="#f3f3f3"></u-gap>
				<view class="block">
					<view class="desc-title">提现说明：</view>
					<view class="desc">提交成功后可<text @click="$push('sub_pages/withdraw/log/log')">点击这里</text>查看打款进度</view>
					<!-- <view class="photo">
						<image src="https://file.wsdns.cn/20230619/29a8c790211a46529fbbb66ee8d223d5.png"
							mode="widthFix"></image>
					</view> -->
				</view>
			</u--form>
		</view>
		<view class="btn-wrapper">
			<u-button :text="submitText" color="linear-gradient(to bottom, #010002, #681752)" :disabled="isEdit"
				shape="circle" @click="handleSubmit()"></u-button>
		</view>
	</view>
</template>

<script>
	import * as MerchantApi from '@/api/merchant.js'

	const userId = uni.getStorageSync('userId')

	export default {
		data() {
			return {
				form: {
					amount: undefined,
					alipayName: '',
					alipayAccount: '',
					payType: 20, // 打款方式 10微信 20支付宝 30银行卡
				},
				payType: '支付宝',
				fileList1: [],
				disabled: false,
				// 临时图片 (用于上传)
				tempFile: null,
				isEdit: false,
				rules: {
					'alipayName': {
						type: 'string',
						required: true,
						message: '请填写支付宝姓名',
						trigger: ['blur', 'change']
					},
					'alipayAccount': {
						type: 'string',
						required: true,
						message: '请填写支付宝账号',
						trigger: ['blur', 'change']
					},
					'amount': {
						type: 'string',
						required: true,
						message: '请填写提现金额',
						trigger: ['blur', 'change']
					}
				},
				submitText: '提交申请'
			}
		},
		onLoad() {
			this.getData()
		},
		onReady() {
				//如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
		    	this.$refs.uForm.setRules(this.rules)
		    },
		methods: {
			getData() {
				const app = this
				MerchantApi.listMerchant({
					userId
				}).then(res => {
					if (res.data) {
						app.form = res.data[0]
					}
				})
			},
			showPayType() {
				const app = this
				if(this.isEdit == true){
					return false;
				}
				const itemList = ['支付宝','银行卡'];
				uni.showActionSheet({
					itemList,
					success: ({tapIndex}) => {
						app.payType = itemList[tapIndex]
					}
				})
			},
			// 新增图片
			async afterRead(event) {
				console.log("event: ", event);
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result.url,
						thumb: result.thumbUrl
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					UploadApi.uploadFile({
						filePath: url,
						fileType: 'image',
						name: 'file',
						header: {
							Authorization: uni.getStorageSync('AccessToken')
						},
					}).then(result => {
						setTimeout(() => {
							resolve(result.data)
						}, 1000)
					}).catch(err => {
						console.log("err: ", err);
					})
				})
			},
			// 删除图片
			deletePic(event) {
				if (this.isEdit == true) {
					return false;
				}
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 确认修改
			async handleSubmit() {
				const app = this
				const { form,payType } = this
				if(form.amount > form.money){
					app.$error('可提现金额不足')
					return false
				}
				if(form.amount == 0){
					app.$error('提现金额不正确')
					return false
				}
				if(payType == '支付宝'){
					app.form.payType = 20
				}
				if(payType == '银行卡'){
					app.form.payType = 30
				}
				if (app.disabled === true) return
				uni.showLoading({
					mask: true,
					title: "正在验证"
				})
				app.$refs.uForm.validate().then(res => {
					MerchantApi.addWithdraw(app.form).then(result => {
						app.$success('提交成功')
						this.getData()
					
					}).catch(err => {
						app.$error(err.message)
					}).finally(() => {
						uni.hideLoading()
					})
				}).catch(errors => {
					uni.$u.toast('校验失败')
				})
				
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #f3f3f3;
	}

	.container {

		.form {
			background-color: #ffffff;
		}

		.block {
			margin: auto;
			width: 700rpx;
			font-size: 28rpx;

			.upload {
				width: 360rpx;
				margin: 0 auto;
				padding: 20rpx 0;
				justify-content: center;

				.id-card-upload {
					display: flex;
					flex-direction: column;
					justify-content: center;
					align-items: center;
					text-align: center;
					margin: 10rpx auto;
					color: #cccccc;
					border: 5rpx dashed #d9d9d9;
					border-radius: 20rpx;
					width: 360rpx;
					height: 220rpx;

					.photograph {
						width: 60rpx;
					}
				}
			}

			.desc-title {
				line-height: 3rem;
			}

			.desc {
				color: #999999;
				padding-bottom: 20rpx;
				text {
					color: #7f006f;
				}
			}

		}
	}

	.btn-wrapper {
		background: none;
		width: 360rpx;
		margin: 50rpx auto;
		// display: flex;
		// align-items: center;
		padding: 0 20rpx;
	}
	.service-fee{
		line-height: 2rem;
		color: #7f006f;
	}
	.pay-type{
		line-height: 2rem;
	}
</style>