<template>
	<view class="container">
		<u-cell-group class="form-info" :border="false">
			<u-field label="商家名称" placeholder="请输入商家名称" label-width="180" v-model="shopInfo.name" disabled></u-field>
			<u-cell-item title="请上传商家图标" :arrow="false">
				<view slot="label">
					<image class="shop-img" mode="aspectFill" :src="item.url" v-for="(item, index) in defaultList1" :key="index" v-if="!edit"></image>
					<u-upload :action="uploadUrl" :file-list="defaultList1" :form-data="uploadData" @on-success="logoUploadSuccess"
					 @on-error="uploadError" max-count="1" v-if="edit"></u-upload>
				</view>
			</u-cell-item>
			<u-cell-item title="请上传店铺主图" :arrow="false">
				<view slot="label">
					<image class="shop-img" mode="aspectFill" :src="item.url" v-for="(item, index) in defaultList2" :key="index" v-if="!edit"></image>
					<u-upload :action="uploadUrl" :file-list="defaultList2" :form-data="uploadData" @on-success="uploadSuccess"
					 @on-error="uploadError" @on-remove="uploadRemove" v-if="edit"></u-upload>
				</view>
			</u-cell-item>
			<u-cell-item title="请上传身份证正反面" :arrow="false">
				<view slot="label" class="id-card-box">
					<image class="id-card" mode="aspectFill" :src="shopInfo.idCardCopyFilePath"></image>
					<image class="id-card" mode="aspectFill" :src="shopInfo.idCardNationalFilePath"></image>
				</view>
			</u-cell-item>
			<u-cell-item title="请上传营业执照照片" :arrow="false">
				<view slot="label" class="license-box">
					<image class="license-img" mode="aspectFill" :src="shopInfo.businessLicenseCopyFilePath"></image>
				</view>
			</u-cell-item>
			<u-field label="法人姓名" placeholder="请输入法人姓名" label-width="180" v-model="shopInfo.duty" disabled></u-field>
			<u-cell-item title="所在城市" @click="regionShow = true">
				<text v-show="shopInfo.companyAddressProvince">{{shopInfo.companyAddressProvince}}-{{shopInfo.companyAddressCity}}-{{shopInfo.companyAddressDistrict}}</text>
			</u-cell-item>
			<u-field label="联系方式" placeholder="请输入联系方式" label-width="180" v-model="shopInfo.contactTel"></u-field>
			<u-field label="收款人姓名" placeholder="请输入收款人姓名" label-width="180" v-model="shopInfo.collectionName" disabled></u-field>
			<u-field label="户名" placeholder="请输入户名" label-width="180" v-model="shopInfo.bankAccountName" disabled></u-field>
			<u-field label="银行名称" placeholder="请输入银行名称" label-width="180" v-model="shopInfo.bankAllName" disabled></u-field>
			<u-field label="银行卡号" placeholder="请输入银行卡号" label-width="180" v-model="shopInfo.bankNumber" disabled></u-field>
		</u-cell-group>
		<view class="form-handle">
			<u-button type="success" shape="circle" :ripple="true" @click="edit = true" class="handle-custom" v-if="!edit">编辑</u-button>
			<u-button type="success" shape="circle" :ripple="true" @click="submitData" class="handle-custom" v-else>提交</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 {
				edit: false,
				shopInfo: {
					name: '',
					logo: '',
					idCardCopyFilePath: '',
					idCardNationalFilePath: '',
					businessLicenseCopyFilePath: '',
					duty: '',
					companyAddressProvince: '',
					companyAddressCity: '',
					companyAddressDistrict: '',
					contactTel: '',
					collectionName: '',
					bankAccountName: '',
					bankAllName: '',
					bankNumber: '',
				},
				regionShow: false,
				uploadData: {
					folderId: 0,
				},
				uploadUrl: '',
				fileList: [],
				defaultList1: [],
				defaultList2: [],
			}
		},
		onLoad(options) {
			this.uploadUrl = API.uploadFile
			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
				}]
				this.defaultList2 = res.data.storeImgUrl.split(',').map(site => {
					return {
						url: site
					}
				})
			}).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
			},
			//  文件上传失败回调
			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]) {
						required = false
					}
				}
				if (!required) {
					this.$refs.uTips.show({
						title: '请填写必填项',
						type: 'warning',
					})
					return false
				}
				if (this.fileList.length <=0) {
					this.$refs.uTips.show({
						title: '店铺主页图至少上传一张',
						type: 'warning',
					})
					return false
				}
				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.switchTab({
							url: '/pages/user/index'
						})
					}, 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;
			}

			/deep/.u-cell_title {
				color: #333333;
			}

			.shop-img {
				width: 90px;
				height: 90px;
				float: left;
				margin-right: 10px;
			}

			.id-card-box {
				display: flex;

				.id-card {
					width: 164px;
					height: 140px;
					box-sizing: border-box;
					margin-right: 14px;
					border-radius: 6px;
				}
			}

			.license-box {

				.license-img {
					width: 345px;
					height: 145px;
				}

			}

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

		.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;
			// }
		}
	}
</style>
