<template>
	<view class="notice">
		<view class="notice-bd">
			<view class="a">
				<image @click="uploadImage()" v-if="avatar_id" :src="avatar_url"></image>
				<image @click="uploadImage()" v-else src="https://www.amiami.com.cn/static/caigou/avatar.png"></image>
			</view>
			<view class="a">
				<view class="item">
					<view class="l">真实姓名</view>
					<input type="text" v-model="username" placeholder="请输入真实姓名" />
				</view>
				<view class="item">
					<view class="l">身份证号</view>
					<input type="text" v-model="card_no" placeholder="请输入身份证号" />
				</view>
				<view class="item">
					<view class="l">手机号码</view>
					<input type="number" v-model="mobile" placeholder="请输入手机号码" />
				</view>
				<view class="item">
					<view class="l">验证码</view>
					<input type="number" v-model="mobile_code" placeholder="请输入验证码" />
					<view class="code" @click="openPage(4)">{{countdown == 60?'获取验证码':countdown+'s'}}</view>
				</view>
				<view class="item">
					<view class="l">经营地址</view>
					<picker style="flex: 1;" mode="multiSelector" @change="multiChange" @columnchange="columnChange" value="{{multiIndex}}" :range="multiArray" range-key="name">
						<view class="pbox" :style="{'color': cityInfo.province?'#303030':'gray' }">
							{{cityInfo.province?cityInfo.province+cityInfo.city+cityInfo.area:'请选择您的实际经营地区'}} 
						</view>
					</picker>
					<image src="https://www.amiami.com.cn/static/caigou/icon-arrow.png"></image>
				</view>
				<view class="item">
					<view class="l">主营品类</view>
					<input type="text" disabled v-model="business" placeholder="可多选" />
				</view>
				<view class="items">
					<view class="li" v-for="(a,i) in list" :key="i" :class="a.isClick?'li-on':''" @click="tabItem(i)">{{a.name}}</view>
				</view>
				<view class="tips">
					<view class="p">说明:</view>
					<view class="p">1、根据相关法规要求，电子商务经营者应当依法办理市场主体登记，我们需收集您的身份证信息以做市场主体登记使用，如您未参与经营活动，无需提交身份认证。</view>
					<view class="p">2、为了您的账户和资金安全，请务必保证账号是您本人使用请勿替他人实名认证，因此而造成的损失需自己承担。</view>
				</view>
				<view class="rule" @click="toPay()">
					<image v-if="toggle" src="@/static/invoice/select.png"></image>
					<image v-else src="@/static/invoice/select-on.png"></image>
					我已阅读并同意<text @click.stop="openPage(2)">《用户服务协议》</text><text @click.stop="openPage(3)">《隐私政策》</text>
				</view>
			</view>
		</view>
		<view class="notice-fd">
			<view class="btn" @click="openPage(1)">下一步</view>
		</view>
		<addShuiyin />
	</view>
</template>

<script>
	import * as AddressApi from '@/api/address'
	import * as newFunApi from '@/api/newFun'
	import Config from '@/core/config'
	import * as CaptchaApi from "@/api/captcha";
	export default {
		data() {
			return {
				list: [{
					name: "大小家电",
					isClick: false,
					index: 0
				},{
					name: "家具家居",
					isClick: false,
					index: 1
				},{
					name: "家装建材",
					isClick: false,
					index: 2
				},{
					name: "3C数码",
					isClick: false,
					index: 3
				},{
					name: "日用百货",
					isClick: false,
					index: 4
				},{
					name: "生鲜超市",
					isClick: false,
					index: 5
				},{
					name: "母婴用品",
					isClick: false,
					index: 6
				},{
					name: "美妆护肤",
					isClick: false,
					index: 7
				},{
					name: "社区团购",
					isClick: false,
					index: 8
				},{
					name: "餐饮酒店",
					isClick: false,
					index: 9
				},{
					name: "文体户外",
					isClick: false,
					index: 10
				},{
					name: "物业房产",
					isClick: false,
					index: 11
				},{
					name: "菜鸟驿站",
					isClick: false,
					index: 12
				},{
					name: "其它",
					isClick: false,
					index: 13
				}],
				cityInfo: {
					province: '',
					city: '',
					area: ''
				},
				avatar_id: "",
				avatar_url: "",
				multiIndex: [0, 0],
				multiArray: [],
				multiData: [],
				index: 0,
				toggle: true,
				card_front_img_id: "",
				card_front_img_url: "",
				card_back_img_id: "",
				card_back_img_url: "",
				"address": "", 
				"sex": "", 
				"nation": "", 
				"birthday": "", 
				"username": "", 
				"card_no": "",
				license_img_id: "",
				license_img_url: "",
				door_img_id: "",
				door_img_url: "",
				company_name: "",
				credit_code: "",
				province_id: "",
				city_id: "",
				form: {
					name: '',
					phone: '',
					region: [],
					detail: ''
				},
				showCountDown: true,
				countdown: 60,
			};
		},
		onLoad() {
			if(uni.getStorageSync("orcInfo") && uni.getStorageSync("orcInfo").door_img_id){
				console.log(uni.getStorageSync("orcInfo"))
				this.avatar_id = uni.getStorageSync("orcInfo").avatar_id
				this.avatar_url = uni.getStorageSync("orcInfo").avatar_url
				this.mobile = uni.getStorageSync("orcInfo").mobile
				this.mobile_code = uni.getStorageSync("orcInfo").mobile_code
				
				if(uni.getStorageSync("orcInfo") && uni.getStorageSync("orcInfo").cityInfo){
					this.form.region = uni.getStorageSync("orcInfo").region
					this.cityInfo = uni.getStorageSync("orcInfo").cityInfo
				}
				if(uni.getStorageSync("orcInfo") && uni.getStorageSync("orcInfo").business){
					let business = uni.getStorageSync("orcInfo").business.split(",");
					this.business = uni.getStorageSync("orcInfo").business
					const that = this;
					business.map(a=>{
						that.list.map(b=>{
							if(a == b.name){
								b.isClick = true
							}
						})
					})
				}
				this.address = uni.getStorageSync("orcInfo").address
				this.sex = uni.getStorageSync("orcInfo").sex
				this.nation = uni.getStorageSync("orcInfo").nation
				this.birthday = uni.getStorageSync("orcInfo").birthday
				this.username = uni.getStorageSync("orcInfo").username
				this.card_no = uni.getStorageSync("orcInfo").card_no
				this.invalid_date = uni.getStorageSync("orcInfo").invalid_date
				this.issuance_date = uni.getStorageSync("orcInfo").issuance_date
				this.issuance_office = uni.getStorageSync("orcInfo").issuance_office
				this.card_front_img_id = uni.getStorageSync("orcInfo").card_front_img_id
				this.card_front_img_url = uni.getStorageSync("orcInfo").card_front_img_url
				this.card_back_img_id = uni.getStorageSync("orcInfo").card_back_img_id
				this.card_back_img_url = uni.getStorageSync("orcInfo").card_back_img_url
				this.license_img_id = uni.getStorageSync("orcInfo").license_img_id
				this.license_img_url = uni.getStorageSync("orcInfo").license_img_url
				this.door_img_id = uni.getStorageSync("orcInfo").door_img_id
				this.door_img_url = uni.getStorageSync("orcInfo").door_img_url
				this.company_name = uni.getStorageSync("orcInfo").company_name
				this.credit_code = uni.getStorageSync("orcInfo").credit_code
			}
			this.getProvinceAll();
		},
		methods: {
			//上传照片
			uploadImage(index) {
				const that = this
				// 选择图片
				uni.chooseImage({
				  count: 1,
				  sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
				  sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
				  success(res) {
					console.log(res)
					uni.uploadFile({
						url: (Config.get('apiUrl')+'upload/image').replace("index.php?s=/",""), // 仅为示例，非真实的接口地址
						filePath: res.tempFiles[0].path,
						name: 'file',
						 header: {
							'content-type': 'multipart/form-data',
							'Storeid': uni.getStorageSync('Store').storeInfo.store_id,
							'Access-Token': uni.getStorageSync('AccessToken'),
							'platform': "MP-WEIXIN",
						},
						formData: {
							'Storeid': uni.getStorageSync('Store').storeInfo.store_id,
							'Access-Token': uni.getStorageSync('AccessToken'),
							'platform': "MP-WEIXIN",
						},
						success: (res1) => {
							const {status,data,message} = JSON.parse(res1.data);
							setTimeout(()=>{
							  if(status == 200){
								uni.showToast({
								  title: '上传成功',
								})
								that.avatar_url = data.fileInfo.external_url
								that.avatar_id = data.fileInfo.file_id
							  }else{
								uni.showToast({
								  icon: 'none',
								  title: msg,
								})
							  }
							},1500)
						},
						fail: (err) => {
							console.log(err)
						}
					});
				  }
				});
				
			},
			async getProvinceAll() {
				let {
					status,
					message,
					data
				} = await AddressApi.province();
				if (status == 200) {
					this.multiData = data.list;
					var multiIndex = this.multiIndex;
					this.multiArray = [
						this.multiData,
						this.multiData[0].children,
						// this.multiData[0].children[0].children
					]
				}
			},
			// 获取多列的索引
			multiChange: function(e) {
				this.multiIndex = e.detail.value;
				let a_name = this.multiArray[0][e.detail.value[0]].name;
				let a_id = this.multiArray[0][e.detail.value[0]].id;
				let b_name = this.multiArray[1][e.detail.value[1]].name;
				let b_id = this.multiArray[1][e.detail.value[1]].id;
				// let c_name = this.multiArray[2][e.detail.value[2]].name;
				// let c_id = this.multiArray[2][e.detail.value[2]].id;
				this.cityInfo.province = a_name;
				this.cityInfo.city = b_name;
				// this.cityInfo.area = c_name;
				this.form.region = [{
					value: a_id,
					label: a_name
				}, {
					value: b_id,
					label: b_name
				},
				// {
				// 	value: c_id,
				// 	label: c_name
				// }
				];
			},
			
			// 选择列
			columnChange: function(e) {
				var _this = this;
				var columnIndex = e.detail.column;
				var columnValue = e.detail.value;
				_this.multiIndex[columnIndex] = columnValue;
				this.multiArray = [
					_this.multiData,
					_this.multiData[_this.multiIndex[0]].children,
					// _this.multiData[_this.multiIndex[0]].children[_this.multiIndex[1]].children
				]
			},
			tabItem(i){
				this.list[i].isClick = !this.list[i].isClick;
				let arr = [];
				this.list.map(a=>{
					if(a.isClick){
						arr.push(a.name)
					}
				})
				this.business = arr.join(",")
			},
			openPage(index) {
				if(index == 1){
					if(this.toggle == true){
						uni.showToast({
							icon: "none",
							title: "请勾选用户协议"
						})
						return ;
					}
					if (!this.avatar_id) {
					  uni.showToast({
						icon:"none",
					    title: "请上传用户头像",
					  });
					  return;
					}
					if (!this.username) {
					  uni.showToast({
						icon:"none",
					    title: "真实姓名不能为空！",
					  });
					  return;
					}
					if (!this.card_no) {
					  uni.showToast({
						icon:"none",
					    title: "身份证号不能为空！",
					  });
					  return;
					}
					if (!/^1[3456789]\d{9}$/.test(this.mobile)) {
					  uni.showToast({
						icon:"none",
					    title: "请输入正确的手机号",
					  });
					  return;
					}
					if (!this.mobile_code) {
					  uni.showToast({
						icon:"none",
					    title: "验证码不能为空！",
					  });
					  return;
					}
					this.province_id = this.form.region[0].value
					this.city_id = this.form.region[1].value
					if (!this.province_id) {
					  uni.showToast({
						icon:"none",
					    title: "经营地址不能为空！",
					  });
					  return;
					}
					if (!this.business) {
					  uni.showToast({
						icon:"none",
					    title: "主营品类不能为空！",
					  });
					  return;
					}
					let orcInfo = uni.getStorageSync("orcInfo");
					orcInfo.province_id = this.province_id
					orcInfo.region = this.form.region
					orcInfo.cityInfo = this.cityInfo
					orcInfo.city_id = this.city_id
					orcInfo.business = this.business
					orcInfo.mobile = this.mobile
					orcInfo.username = this.username
					orcInfo.card_no = this.card_no
					orcInfo.avatar_id = this.avatar_id
					orcInfo.avatar_url = this.avatar_url
					orcInfo.mobile_code = this.mobile_code
					uni.setStorageSync("orcInfo",orcInfo)
					uni.navigateTo({
						url: "/pages/news/caigou/notice6"
					})
				}else if(index == 2 || index == 3){
					uni.navigateTo({
					  url: "/pages/news1/text?pageFlag=" + (index-1),
					});
				}else if(index == 4){
					if(this.toggle == true){
						uni.showToast({
							icon: "none",
							title: "请勾选用户协议"
						})
						return ;
					}
					if (!/^1[3456789]\d{9}$/.test(this.mobile)) {
					  uni.showToast({
						icon:"none",
					    title: "请输入正确的手机号",
					  });
					  return;
					}
					this.toSms();
				}
				
			},
			toSms() {
				const that = this;
				if(that.showCountDown == false){
					return ;
				}
				clearInterval(that.countDownFun);
				that.showCountDown = !that.showCountDown;
				CaptchaApi.sendSmsCaptcha({
				    mobile: that.mobile
				}).then((res) => {
				  if (res.status == 200) {
							that.countDownFun = setInterval(() => {
							  if (that.countdown >= 1) {
								that.countdown = that.countdown - 1;
							  } else {
								that.showCountDown = false;
								that.countdown = 60;
								clearInterval(that.countDownFun);
							  }
							}, 1000);
				    uni.showToast({
							  icon:"none",
				      title: res.message,
				    });
				  } else {
					that.showCountDown = true
				    uni.showToast({
					  icon:"none",
				      title: res.message,
				    });
				  }
				});
			},
			toPay() {
				this.toggle = !this.toggle
			}
		}
	}
</script>

<style lang="scss" scoped>
.notice{
	background-color: #fff;
	padding-bottom: 150rpx;
	&-bd{
		width: 700rpx;
		margin: 0 auto;
		text-align: justify;
		border-radius: 10rpx;
		padding: 0 45rpx;
		box-sizing: border-box;
		.a{
			padding: 20rpx 0;
			.title{
				font-size: 36rpx;
				color: #222222;
				text-align: center;
			}
			.desc{
				width: 550rpx;
				font-size: 28rpx;
				color: #333333;
				line-height: 48rpx;
				text-align: center;
				margin: 0 auto;
				margin-top: 20rpx;
			}
			image{
				width: 120rpx;
				height: 120rpx;
				display: block;
				margin: 50rpx auto 0;
			}
			.item{
				padding: 20rpx 0;
				display: flex;
				align-items: center;
				border-bottom: 1px solid #E9E9E9;
				.l{
					font-size: 28rpx;
					color: #222222;
					height: 50rpx;
					line-height: 50rpx;
					width: 200rpx;
				}
				input{
					flex: 1;
					font-size: 28rpx;
					color: #222222;
					height: 50rpx;
					line-height: 50rpx;
				}
				.code{
					font-weight: 400;
					font-size: 28rpx;
					color: #F34A40;
				}
				image{
					width: 11rpx;
					height: 20rpx;
					margin: 0;
				}
			}
			.items{
				padding: 20rpx 0 0;
				overflow: hidden;
				.li{
					min-width: 126rpx;
					line-height: 50rpx;
					background: #F8F6F6;
					border-radius: 10rpx;
					text-align: center;
					margin-bottom: 20rpx;
					margin-right: 15rpx;
					float: left;
					font-size: 24rpx;
					color: #333333;
					&-on{
						color: #FFFFFF;
						background: #F34A40;
					}
				}
			}
			.tips{
				width: 100%;
				min-height: 246rpx;
				background: #FFF6F5;
				border-radius: 10rpx;
				padding: 20rpx;
				box-sizing: border-box;
				margin: 10rpx auto;
				line-height: 50rpx;
			}
			.rule{
				display: flex;
				align-items: center;
				font-size: 24rpx;
				color: #333333;
				padding: 20rpx 0;
				image{
					width: 30rpx;
					height: 30rpx;
					margin: 0;
					margin-right: 10rpx;
				}
				text{
					color: #F34A40;
				}
			}
		}
	}
	&-fd{
		height: 140rpx;
		width: 100%;
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 22;
		background-color: #FFFFFF;
		display: flex;
		justify-content: center;
		align-items: center;
		.btn{
			width: 662rpx;
			height: 90rpx;
			line-height: 90rpx;
			background: #F34A40;
			border-radius: 90rpx;
			text-align: center;
			font-size: 32rpx;
			color: #FFFFFF;
			&-on{
				background-color: #ccc;
			}
		}
	}
}
</style>
