<template>
	<view class="container">
		<u-form :model="shopInfo" ref="shopInfo" label-width="180">
			<u-form-item label="商家名称" prop="name" required>
				<u-input v-model="shopInfo.name" type="text" placeholder="请输入商家名称" />
			</u-form-item>
			<u-form-item label="请上传商家图标" placeholder="请上传商家图标" required label-position="top">
				<u-upload :action="uploadUrl" :file-list="defaultList1" :form-data="uploadData" @on-success="logoUploadSuccess"
				 @on-error="uploadError" max-count="1"></u-upload>
			</u-form-item>
			<u-form-item label="请上传店铺主图" placeholder="请上传店铺主图" required label-position="top">
				<u-upload :action="uploadUrl" :file-list="defaultList2" :form-data="uploadData" @on-success="uploadSuccess"
				 @on-error="uploadError" @on-remove="uploadRemove" max-count="3"></u-upload>
			</u-form-item>
			<u-form-item label="请上传身份证正反面" placeholder="请上传身份证正反面" required label-position="top">
				<view class="id-card-box">
					<u-upload :action="uploadUrl" :file-list="defaultList3" :form-data="uploadData" @on-success="uploadIdCardSuccess1"
					 @on-error="uploadError" :custom-btn="true" max-count="1" style="margin-right: 16px;" :style="{width: cWidth + 'px', height: (cWidth * 0.66 + 30) + 'px'}">
						<view slot="addBtn" class="id-card card-img1" :style="{width: cWidth + 'px', height: (cWidth * 0.66 + 30) + 'px', paddingTop: (cWidth * 0.66) + 'px', backgroundSize: '100% ' + (cWidth * 0.66) + 'px'}">拍摄正面</view>
					</u-upload>
					<u-upload :action="uploadUrl" :file-list="defaultList4" :form-data="uploadData" @on-success="uploadIdCardSuccess2"
					 @on-error="uploadError" :custom-btn="true" max-count="1" :style="{width: cWidth + 'px', height: (cWidth * 0.66 + 30) + 'px'}">
						<view slot="addBtn" class="id-card card-img2" :style="{width: cWidth + 'px', height: (cWidth * 0.66 + 30) + 'px', paddingTop: (cWidth * 0.66) + 'px', backgroundSize: '100% ' + (cWidth * 0.66) + 'px'}">拍摄反面</view>
					</u-upload>
				</view>
			</u-form-item>
			<u-form-item label="请上传营业执照照片" placeholder="请上传营业执照照片" label-position="top">
				<view class="license-box">
					<u-upload :action="uploadUrl" :file-list="defaultList5" :form-data="uploadData" @on-success="uploadLicenseSuccess"
					 @on-error="uploadError" :custom-btn="true" max-count="1" class="diy-upload">
						<image slot="addBtn" class="license-img" src="../../static/images/license-img.png"></image>
					</u-upload>
					<view class="license-text"> 营业执照</view>
				</view>
			</u-form-item>
			<u-form-item label="法人姓名" prop="duty" required>
				<u-input v-model="shopInfo.duty" type="text" placeholder="请输入法人姓名" />
			</u-form-item>
			<u-form-item label="所在城市" placeholder="请选择所在城市" right-icon="arrow-right" required @click.native="regionShow = true">
				<text v-show="shopInfo.companyAddressProvince">{{shopInfo.companyAddressProvince}}-{{shopInfo.companyAddressCity}}-{{shopInfo.companyAddressDistrict}}</text>
			</u-form-item>
			<u-form-item label="联系方式" prop="contactTel" required>
				<u-input v-model="shopInfo.contactTel" type="text" placeholder="请输入联系方式" />
			</u-form-item>
			<u-form-item label="收款人姓名" prop="collectionName" required>
				<u-input v-model="shopInfo.collectionName" type="text" placeholder="请输入收款人姓名" />
			</u-form-item>
			<u-form-item label="户名" prop="bankAccountName" required>
				<u-input v-model="shopInfo.bankAccountName" type="text" placeholder="请输入户名" />
			</u-form-item>
			<u-form-item label="银行名称" prop="bankAllName" required>
				<u-input v-model="shopInfo.bankAllName" type="text" placeholder="请输入银行名称" />
			</u-form-item>
			<u-form-item label="银行卡号" prop="bankNumber" required>
				<u-input v-model="shopInfo.bankNumber" type="text" placeholder="请输入银行卡号" />
			</u-form-item>
		</u-form>
		<view class="handle-fix-box">
			<u-button type="success" shape="circle" :ripple="true" :custom-style="customStyle" @click="submitData">提交</u-button>
		</view>
		<u-picker mode="region" v-model="regionShow" @confirm="setRegion"></u-picker>
		<u-top-tips ref="uTips"></u-top-tips>
	</view>
</template>

<script>
	const NET = require('@/utils/request')
	const API = require('@/config/api')
	export default {
		data() {
			return {
				cWidth: 0,
				type: '',
				shopInfo: {
					name: '',
					logo: '',
					idCardCopyFilePath: '',
					idCardNationalFilePath: '',
					businessLicenseCopyFilePath: '',
					duty: '',
					companyAddressProvince: '',
					companyAddressCity: '',
					companyAddressDistrict: '',
					contactTel: null,
					collectionName: '',
					bankAccountName: '',
					bankAllName: '',
					bankNumber: null,
				},
				regionShow: false,
				uploadData: {
					folderId: 0,
				},
				uploadUrl: '',
				fileList: [],
				defaultList1: [],
				defaultList2: [],
				defaultList3: [],
				defaultList4: [],
				defaultList5: [],
				rules: {
					name: [{
						required: true,
						message: '请输入姓名',
						// 可以单个或者同时写两个触发验证方式 
						trigger: 'blur',
					}]
				},
				errorMessage1: '',
				errorMessage2: '',
				customStyle: { //  底部悬浮按钮样式
					height: '40px',
					backgroundColor: '#56a83a'
				}
			}
		},
		// 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
		onReady() {
			// this.$refs.shopInfo.setRules(this.rules);
		},
		onLoad(options) {
			this.cWidth = (uni.upx2px(750) - 40) / 2;
			this.uploadUrl = API.uploadFile
			this.type = options.type
			if (options.type == 'edit') {
				NET.request(API.getShopRegisterInfo, {}, 'GET').then(res => {
					this.shopInfo = {
						name: res.data.name,
						logo: res.data.logo,
						idCardCopyFilePath: res.data.idCardCopyFilePath,
						idCardNationalFilePath: res.data.idCardNationalFilePath,
						businessLicenseCopyFilePath: res.data.businessLicenseCopyFilePath,
						duty: res.data.duty,
						companyAddressProvince: res.data.companyAddressProvince,
						companyAddressCity: res.data.companyAddressCity,
						companyAddressDistrict: res.data.companyAddressDistrict,
						contactTel: res.data.contactTel,
						collectionName: res.data.collectionName,
						bankAccountName: res.data.bankAccountName,
						bankAllName: res.data.bankAllName,
						bankNumber: res.data.bankNumber,
					}
					this.fileList = res.data.storeImgUrl.split(',')
					this.defaultList1 = [{
						url: res.data.logo,
						// url: "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603877862802&di=e1d4201e637fcd3e21a9b52fbfc1b771&imgtype=0&src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202005%2F25%2F20200525131219_ntnpg.thumb.400_0.jpeg"
					}]
					this.defaultList2 = res.data.storeImgUrl.split(',').map(site => {
						return {
							url: site
						}
					})
					this.defaultList3 = [{
						url: res.data.idCardCopyFilePath
					}]
					this.defaultList4 = [{
						url: res.data.idCardNationalFilePath
					}]
					this.defaultList5 = [{
						url: res.data.businessLicenseCopyFilePath
					}]
				}).catch(res => {
					this.$refs.uTips.show({
						title: '获取注册信息失败',
						type: 'warning',
					})
				})
			}
		},
		methods: {
			//  logo上传成功回调
			logoUploadSuccess(res, index, lists, name) {
				this.shopInfo.logo = res.data.url
				this.$refs.uTips.show({
					title: 'logo上传成功',
					type: 'success',
				})
				return true
			},
			//  文件上传成功回调
			uploadSuccess(res, index, lists, name) {
				this.fileList.push(res.data.url)
				this.$refs.uTips.show({
					title: '文件上传成功',
					type: 'success',
				})
				return true
			},
			//  身份证正面上传成功回调
			uploadIdCardSuccess1(res, index, lists, name) {
				this.shopInfo.idCardCopyFilePath = res.data.url
				this.$refs.uTips.show({
					title: '身份证正面照片上传成功',
					type: 'success',
				})
				return true
			},
			//  身份证反面上传成功回调
			uploadIdCardSuccess2(res, index, lists, name) {
				this.shopInfo.idCardNationalFilePath = res.data.url
				this.$refs.uTips.show({
					title: '身份证反面照片上传成功',
					type: 'success',
				})
				return true
			},
			//  营业执照上传成功回调
			uploadLicenseSuccess(res, index, lists, name) {
				this.shopInfo.businessLicenseCopyFilePath = res.data.url
				this.$refs.uTips.show({
					title: '营业执照照片上传成功',
					type: 'success',
				})
				return true
			},
			//  文件上传失败回调
			uploadError(res, index, lists, name) {
				this.$refs.uTips.show({
					title: '文件上传失败',
					type: 'warning',
				})
			},
			//  移除文件回调
			uploadRemove(index, lists, name) {
				this.fileList.splice(index, 1)
			},
			//  设置地址
			setRegion(data) {
				this.shopInfo.companyAddressProvince = data.province.label
				this.shopInfo.companyAddressCity = data.city.label
				this.shopInfo.companyAddressDistrict = data.area.label
			},
			//  提交
			submitData() {
				let required = true
				for (let key in this.shopInfo) {
					if (!this.shopInfo[key] && key != 'businessLicenseCopyFilePath') {
						required = false
					}
				}
				if (!required) {
					this.$refs.uTips.show({
						title: '请填写必填项',
						type: 'warning',
					})
					return false
				}
				if (!Number(this.shopInfo.contactTel)) {
					this.errorMessage1 = '请输入数字'
					return
				} else {
					this.errorMessage1 = ''
				}
				if (!Number(this.shopInfo.bankNumber)) {
					this.errorMessage2 = '请输入数字'
					return
				} else {
					this.errorMessage2 = ''
				}
				if (!this.fileList.length) {
					this.$refs.uTips.show({
						title: '请上传店铺主页图',
						type: 'warning',
					})
					return false
				}
				if (this.type == 'edit') {
					NET.request(API.editShopRegisterInfo, {
						...this.shopInfo,
						address: this.shopInfo.companyAddressProvince + '-' + this.shopInfo.companyAddressCity + '-' + this.shopInfo.companyAddressDistrict,
						companyAddress: this.shopInfo.companyAddressProvince + '-' + this.shopInfo.companyAddressCity + '-' + this.shopInfo
							.companyAddressDistrict,
						storeImgUrl: this.fileList.join(',')
					}, 'PUT').then(res => {
						this.$refs.uTips.show({
							title: '编辑成功',
							type: 'success',
						})
						setTimeout(() => {
							uni.redirectTo({
								url: '/pages/index/registerState'
							});
						}, 1000)
					}).catch(res => {
						this.$refs.uTips.show({
							title: '编辑失败',
							type: 'warning',
						})
					})
				} else {
					NET.request(API.submitShopRegisterInfo, {
						...this.shopInfo,
						address: this.shopInfo.companyAddressProvince + '-' + this.shopInfo.companyAddressCity + '-' + this.shopInfo.companyAddressDistrict,
						companyAddress: this.shopInfo.companyAddressProvince + '-' + this.shopInfo.companyAddressCity + '-' + this.shopInfo
							.companyAddressDistrict,
						storeImgUrl: this.fileList.join(',')
					}, 'POST').then(res => {
						this.$refs.uTips.show({
							title: '注册成功',
							type: 'success',
						})
						setTimeout(() => {
							uni.redirectTo({
								url: '/pages/index/registerState'
							});
						}, 1000)
					}).catch(res => {
						this.$refs.uTips.show({
							title: '注册失败',
							type: 'warning',
						})
					})
				}
			},
		},
	}
</script>
<style>
	page {
		width: 100%;
		height: 100%;
	}
</style>
<style lang="less" scoped>
	.container {
		width: 100%;
		float: left;
		padding: 0 15px 60px 15px;

		.id-card-box {
			display: flex;

			.id-card {
				height: 140px;
				margin-right: 10px;
				box-sizing: border-box;
				padding-top: 110px;
				background-color: #51A539;
				background-size: 100% 110px;
				background-position: center top;
				background-repeat: no-repeat;
				border-radius: 6px;
				font-size: 15px;
				font-family: PingFang SC;
				color: #FFFFFF;
				line-height: 30px;
				text-align: center;
			}

			.card-img1 {
				background-image: url(@/static/images/card-img1.png);
			}

			.card-img2 {
				background-image: url(@/static/images/card-img2.png);
			}

			/deep/.u-list-item {
				width: 100% !important;
				height: 110px !important;
			}
		}

		.license-box {
			width: 100%;
			height: 175px;
			position: relative;

			.diy-upload {
				width: 100%;
				height: 145px;
				position: absolute;
			}

			.license-img {
				width: 100%;
				height: 145px;
				position: absolute;
			}

			/deep/.u-list-item {
				width: 100% !important;
				height: 145px !important;
			}
		}

		.license-text {
			width: 100%;
			height: 30px;
			margin-top: 5px;
			font-size: 15px;
			font-family: PingFang SC;
			color: #656565;
			line-height: 30px;
			text-align: center;
			position: relative;
			top: 145px;
		}

		.handle-fix-box {
			width: 100%;
			box-sizing: border-box;
			padding: 10px 15px;
			position: fixed;
			left: 0;
			bottom: 0;
			height: 60px;
			background-color: #FFFFFF;
			z-index: 999;
		}
	}
</style>
