<template>
	<view class="container">
		<u-cell-group class="form-info" :border="false">
		    <u-form :model="shopInfo" ref="shopInfo">
				<!-- <u-form-item label="商家名称" prop="name">
				  <u-input placeholder="请输入商家名称" label-width="180" v-model="shopInfo.name" /> -->
				  <u-field label="商家名称" required placeholder="请输入商家名称" label-width="180" v-model="shopInfo.name"></u-field>
				<!-- </u-form-item> -->
				<u-cell-item title="请上传商家图标" :arrow="false">
					<view slot="label">
						<u-upload :action="uploadUrl" :file-list="defaultList1" :form-data="uploadData" @on-success="logoUploadSuccess"
						 @on-error="uploadError" max-count="1"></u-upload>
					</view>
				</u-cell-item>
				<u-cell-item title="请上传店铺主图" :arrow="false" class="all-width">
					<view slot="label">
						<u-upload :action="uploadUrl" :file-list="defaultList2" :form-data="uploadData" @on-success="uploadSuccess"
						 @on-error="uploadError" @on-remove="uploadRemove" max-count="3"></u-upload>
					</view>
				</u-cell-item>
				<u-cell-item title="请上传身份证正反面" :arrow="false" class="all-width">
					<view slot="label" 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-cell-item>
				<u-cell-item title="请上传营业执照照片" required :arrow="false" class="all-width">
					<view slot="label" 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-cell-item>
				<u-field label="法人姓名" required placeholder="请输入法人姓名" label-width="180" v-model="shopInfo.duty"></u-field>
				<u-cell-item title="所在城市" required @click="regionShow = true">
					<text v-show="shopInfo.companyAddressProvince">{{shopInfo.companyAddressProvince}}-{{shopInfo.companyAddressCity}}-{{shopInfo.companyAddressDistrict}}</text>
				</u-cell-item>
				<!-- <u-form-item label="联系方式" prop="contactTel"> -->
				<u-field label="联系方式" required :error-message="errorMessage1" placeholder="请输入联系方式" label-width="180" v-model="shopInfo.contactTel"></u-field>
					<!-- <input type="number" placeholder="请输入联系方式" label-width="180" v-model="shopInfo.contactTel" /> -->
				<!-- </u-form-item> -->
				<u-field label="收款人姓名" required placeholder="请输入收款人姓名" label-width="180" v-model="shopInfo.collectionName"></u-field>
				<u-field label="户名" required placeholder="请输入户名" label-width="180" v-model="shopInfo.bankAccountName"></u-field>
				<u-field label="银行名称" required placeholder="请输入银行名称" label-width="180" v-model="shopInfo.bankAllName"></u-field>
				<u-field label="银行卡号" required :error-message="errorMessage2" placeholder="请输入银行卡号" label-width="180" v-model="shopInfo.bankNumber"></u-field>
			</u-form>
		</u-cell-group>
		<view class="form-handle">
			<u-button type="success" shape="circle" :ripple="true" @click="submitData" class="handle-custom">提交</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: ''
			}
		},
		// 必须要在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() {
				debugger
				let required = true
				for (let key in this.shopInfo) {
					if (!this.shopInfo[key]) {
						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 lang="less" scoped>
	page {
		width: 100%;
		height: 100%;
	}

	.container {
		width: 100%;
		height: 100%;
		float: left;
		overflow-y: auto;

		.form-info {
			width: 100%;
			float: left;

			/deep/.u-label-text {
				color: #333333;
				line-height: 40px;
				font-size: 15px;
			}

			/deep/.u-cell_title {
				color: #333333;
				line-height: 40px;
				font-size: 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;
			}
		}

		.form-handle {
			width: calc(100% - 30px);
			float: left;
			height: 40px;
			margin: 30px 15px 20px 15px;


			.handle-custom {
				background-color: #51A539;

				/deep/button {
					background-color: #56a83a;
				}

				/deep/.u-btn--success--disabled {
					background-color: #74bd60 !important;
				}
			}

			// /deep/.u-btn--success--disabled {
			// 	background-color: #999999 !important;
			// }
		}
	}

	.all-width {
		/deep/.u-cell_title {
			width: 100% !important;
		}
	}
</style>
