<template>
	<view>
		<u-card :border="false" padding="30" box-shadow="0px 1px 10px rgba(0,0,0,0.2)" border-radius="20" :show-foot="false">
			<view slot="head">
				<view class="u-flex">
					<view style="width: 8rpx;height: 34rpx;background: #0292FD;margin-right:20rpx;"></view>
					基本信息
				</view>
			</view>
			<view slot="body">
				<u-form :model="form" ref="uForm">
					<u-form-item prop="store">
						<view style="margin-right: 10rpx;">
							<u-icon name="home" size="36"></u-icon>
						</view>
						<u-input v-model="form.store" placeholder="请输入门店名称" />
					</u-form-item>
					<u-form-item prop="company">
						<view style="margin-right: 10rpx;">
							<u-icon name="home" size="36"></u-icon>
						</view>
						<u-input v-model="form.company" placeholder="请输入公司名称" :disabled="true" />
					</u-form-item>
					<u-form-item prop="name">
						<view style="margin-right: 10rpx;">
							<u-icon name="account" size="36"></u-icon>
						</view>
						<u-input v-model="form.name" placeholder="请输入联系人" />
					</u-form-item>
					<u-form-item prop="phone">
						<view style="margin-right: 10rpx;">
							<u-icon name="phone" size="36"></u-icon>
						</view>
						<u-input v-model="form.phone" placeholder="请输入联系电话" type="number" />
					</u-form-item>
					<u-form-item prop="code">
						<view style="margin-right: 10rpx;">
							<u-icon name="lock" size="36"></u-icon>
						</view>
						<u-input v-model="form.code" placeholder="请输入验证码" type="number" />
						<view class="" slot="right">
							<u-button size="mini" type="primary" @click="getCode" shape="circle" :loading="codeLoading">{{codeText}}</u-button>
						</view>
					</u-form-item>
<!-- 					<u-form-item prop="region">
						<view style="margin-right: 10rpx;">
							<u-icon name="map" size="36"></u-icon>
						</view>
						<u-input :select-open="pickerShow" v-model="form.region" placeholder="点击选择省市区" @click="pickerShow = true"
						 :disabled="true"></u-input>
						<view class="" slot="right">
							<u-icon name="arrow-right" color="#666666" size="36"></u-icon>
						</view>
					</u-form-item> -->
					<u-form-item prop="region">
						<view style="margin-right: 10rpx;">
							<u-icon name="map" size="36"></u-icon>
						</view>
						<u-input placeholder="点击选择省市区" v-model="form.region" :disabled="true" @click="inMap"></u-input>
						<view class="" slot="right">
							<u-icon name="arrow-right" color="#666666" size="36"></u-icon>
						</view>
					</u-form-item>
					<u-form-item prop="addressInfo">
						<view style="margin-right: 10rpx;">
							<u-icon name="map" size="36"></u-icon>
						</view>
						<u-input placeholder="点击选择详细地址" v-model="form.addressInfo" :disabled="true" @click="inMap"></u-input>
						<view class="" slot="right">
							<u-icon name="arrow-right" color="#666666" size="36"></u-icon>
						</view>
					</u-form-item>
				</u-form>
				<u-verification-code ref="uCode" @change="codeChange"></u-verification-code>
				<u-picker mode="region" v-model="pickerShow" @confirm="regionConfirm" :default-region="defaultRegion"></u-picker>
			</view>
		</u-card>
		<view style="margin: 30rpx;">
			<u-button @click="nextStep" type="primary">下一步</u-button>
		</view>
		<u-modal v-model="modelshow" :content="modelcontent" title="温馨提示"></u-modal>
	</view>
</template>

<script>
	import permision from "@/js_sdk/wa-permission/permission.js"
	import {
		request
	} from '../../common/request/request'
	require("promise.prototype.finally").shim()
	export default {
		data() {
			return {
				codeText: '',
				pickerShow: false,
				defaultRegion: [],
				form: {
					store: '',
					company: '',
					name: "",
					phone: "",
					code: '',
					region: '',
					latitude: '',
					longitude: '',
					province: "",
					city: "",
					district: "",
					addressInfo: "",
				},
				rules: {
					store: [{
						required: true,
						message: '请输入门店名称',
						trigger: 'blur,change'
					},
					{
						// 此为同步验证，可以直接返回true或者false，如果是异步验证，稍微不同，见下方说明
						validator: (rule, value, callback) => {
							// 调用uView自带的js验证规则，详见：https://www.uviewui.com/js/test.html
							return this.$u.test.chinese(value);
						},
						message: '门店名称必须为中文',
						// 触发器可以同时用blur和change，二者之间用英文逗号隔开
						trigger: ['change','blur'],
					},
					],
					company: [{
						required: true,
						message: '请输入公司名称',
						trigger: 'blur,change'
					}],
					name: [{
						required: true,
						message: '请输入联系人',
						trigger: 'blur,change'
					},{
						min: 2,
						max: 10,
						message: '姓名长度在2到10个字符',
						trigger: ['change','blur'],
					},
					{
						// 此为同步验证，可以直接返回true或者false，如果是异步验证，稍微不同，见下方说明
						validator: (rule, value, callback) => {
							// 调用uView自带的js验证规则，详见：https://www.uviewui.com/js/test.html
							return this.$u.test.chinese(value);
						},
						message: '姓名必须为中文',
						// 触发器可以同时用blur和change，二者之间用英文逗号隔开
						trigger: ['change','blur'],
					}],
					phone: [{
						required: true,
						message: '请输入联系电话',
						trigger: 'blur,change'
					},
					{
						validator: (rule, value, callback) => {
							// 调用uView自带的js验证规则，详见：https://www.uviewui.com/js/test.html
							return this.$u.test.mobile(value);
						},
						message: '手机号码不正确',
						// 触发器可以同时用blur和change，二者之间用英文逗号隔开
						trigger: ['change','blur'],
					}],
					code: [{
						required: true,
						message: '请输入验证码',
						trigger: 'blur,change'
					},
					{
						min: 6,
						max: 6,
						message: '验证码为6位数',
						trigger: 'change',
					},
					{
						type: 'number',
						message: '验证码只能为数字',
						trigger: ['change','blur'],
					}],
					region: [{
						required: true,
						message: '请选择地区',
						trigger: 'change',
					}],
					addressInfo: [{
						required: true,
						message: '请选择地区',
						trigger: 'change',
					}],
				},
				dataUrl: "",
				codeLoading: false,
				modelshow: false,
				modelcontent: "如果店名和联系人与营业执照不一致，请重新上传清晰度更高的营业执照"
			};
		},
		created() {
			uni.$on('licenseData', (data) => {
				if(data.company.words!="无"){
					this.form.store = data.company.words
				}
				this.form.company = data.company.words
				if(data.company.words!="无"){
					this.form.name = data.person.words
				}
				
			})
			uni.$on('addressData', (data) => {
				this.form.latitude = data.latitude
				this.form.longitude = data.longitude
				this.address=data.address
				this.form.region=this.address.province+"-"+this.address.city+"-"+this.address.district
				this.form.addressInfo =this.address.addressInfo
			})
			uni.$on('dataUrl', (data) => {
				this.dataUrl = data
			})
			uni.getSystemInfo({
				success: res => {
					this.SystemInfo = res
					console.log(res)
				}
			});
			if (this.SystemInfo.platform == 'ios') {
				this.judgeIosPermission('location')
				uni.showModal({
					title: "温馨提示",
					content: "如果店名和联系人与营业执照不一致，请重新上传清晰度更高的营业执照？",
					confirmText: "确认",
					showCancel: false
				})
			} else {
				this.requestAndroidPermission('android.permission.ACCESS_FINE_LOCATION')
				this.modelshow = true
			}

		},
		methods: {
			judgeIosPermission(permisionID) {
				let result = permision.judgeIosPermission(permisionID)
				if (!result) {
					this.getLocation()
				} else {
					this.getLocation()
				}
			},
			async requestAndroidPermission(permisionID) {
				let result = await permision.requestAndroidPermission(permisionID)
				if (result == 1) {
					this.getLocation()
				} else if (result == 0) {
					this.getLocation()
				} else {
					this.getLocation()
				}
			},
			getLocation() {
				let that = this
				uni.getLocation({
					type: 'wgs84',
					geocode: true,
					success: function(res) {
						switch (res.address.province) {
							case "北京市":
								{
									that.city = "直辖市"
									break;
								}
							case "天津市":
								{
									that.city = "直辖市"
									break;
								}
							case "上海市":
								{
									that.city = "直辖市"
									break;
								}
							case "重庆市":
								{
									that.city = "直辖市"
									break;
								}
							default:
								{
									that.city = "不是直辖市"
									break;
								}
						}
						if (that.city == "直辖市") {
							let RegionData = res.address.province + "," + that.city + "," + res.address.district
							that.defaultRegion = RegionData.split(",");
						} else {
							let RegionData = res.address.province + "," + res.address.city + "," + res.address.district
							that.defaultRegion = RegionData.split(",");
						}
					}
				});
			},
			nextStep() {
				this.$refs.uForm.validate(valid => {
					if (valid) {

						this.$u.route({
							url: 'pages/login/register_3',
						})
						uni.$emit("formData", {
							formData: this.form,
							dataUrl: this.dataUrl
						})
					} else {
						console.log('验证失败');
					}

				});
			},
			codeChange(text) {
				this.codeText = text;
			},
			getCode() {
				if (this.form.phone) {
					this.codeLoading = true
					request({
						url: '/sailun/appStoreBasicInfo/sendCode',
						method: 'post',
						data: {
							"phoneNumber": parseInt(this.form.phone),
							"opreaType": "0"
						},
					}).then(res => {
						console.log(res)
						if (res.data.code == 0) {
							if (this.$refs.uCode.canGetCode) {
								this.$refs.uCode.start();
							} else {
								this.$u.toast('倒计时结束后再发送');
							}
						}
						if (res.data.code == 500) {
							this.$u.toast(res.data.msg);
						}
					}).catch(err => {
						console.log(err)
					}).finally(() => {
						this.codeLoading = false
						// 通知验证码组件内部开始倒计时
					})
				} else {
					this.$u.toast('请输入手机号');
				}

			},
			regionConfirm(e) {
				// this.form.region = e.province.label + '-' + e.city.label + '-' + e.area.label;
				// this.form.province = e.province.label;
				// this.form.city = e.city.label;
				// this.form.district = e.area.label;
			},
			inMap() {
				this.$u.route({
					url: 'pages/login/register_map'
				})
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		}
	}
</script>

<style lang="scss" scoped>

</style>
