<template>
	<view :class="[AppTheme]" class="all_box">
		<view class="top_back bg-primary"></view>
		<view class="bottom_back"></view>
		<view class="apply_box" v-if="!ifgetsh && !insuccess">  
			<view class="apply_box_title  text-primary">分销商申请</view>
			<!-- <view class="apply_box_oor"></view> -->
			<input type="text" v-model="name" placeholder-style="font-size:26rpx" placeholder="请输入真实姓名" />
			<input type="idcard" v-model="percard" placeholder-style="font-size:26rpx" placeholder="请输入身份证号码" />
			<input type="number" v-model="phone" placeholder-style="font-size:26rpx" placeholder="请输入联系电话" />
			<view class=" ino_button ino_button_a bg-linear-gradient" @click="nowjoin()">立即申请</view>
			<view class="agreement">
				点击立即申请，即表示同意
				<text class="ana_p1" @click="openagreement()">《分销政策》</text>
			</view>
		</view>
		<view class="apply_box" v-if="ifgetsh">
			<view class="apply_box_title  text-primary">分销 申请</view>
			<view class="apply_box_oor bg-primary "></view>
			<view class="ino_button">已申请成功</view>
		</view>
		<view class="apply_box" v-if="insuccess">
			<view class="apply_box_title  text-primary">分销 申请</view>
			<view class="apply_box_oor bg-primary"></view>
			<view class="ino_button">已申请,请耐心等待审批</view>
		</view>
	</view>
</template>
<script>
	import mallApi from '@/api/mall/index.js';
	export default {
		data() {
			return {
				primary:this.$theme.primary,
				settingFile:getApp().globalData.siteinfo,
				percard: '',
				phone: '',
				name: '',
				ifgetsh: false,
				insuccess: false
			};
		},
		onLoad() {
			mallApi.fxindex({}).then(res => {
				// 做判断
				if (res.status === 200) {
					this.insuccess = res.data.insuccess;
					this.ifgetsh =  res.data.ifgetsh;
				}
			});
		},
		methods: {
			nowjoin() {
				let that = this;
				let pain = that.IdCodeValid(that.percard);
				if (uni.getStorageSync('userInfo').isfx === 0) {
					var re = /^[\u4e00-\u9fa5]+$/;
					if (re.test(that.name)) {
						if (pain.pass) {
							var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
							if (myreg.test(that.phone)) {
								let params = {
									fx_name: that.name,
									idno: that.percard,
									fx_mobile: that.phone
								};
								mallApi.dionation(params).then(res => {
									// 做判断
									if (res.status === 200) {
										this.insuccess = true;
									}
								});
							} else {
								that.$api.msg('手机号格式错误');
								that.phone = '';
							}
						} else {
							that.$api.msg(pain.msg);
							that.percard = '';
						}
					} else {
						that.$api.msg('请输入正确的姓名');
					}
				} else {
					this.$api.msg('暂时无法申请');
				}
			},
			//打开分销协议
			openagreement() {
				uni.navigateTo({ url: '/pagesA/pages/distribution/policy/policy' })
			},
			//验证身份证
			IdCodeValid(code) {
				//身份证号合法性验证
				//支持15位和18位身份证号
				//支持地址编码、出生日期、校验位验证
				var city = {
					11: '北京',
					12: '天津',
					13: '河北',
					14: '山西',
					15: '内蒙古',
					21: '辽宁',
					22: '吉林',
					23: '黑龙江 ',
					31: '上海',
					32: '江苏',
					33: '浙江',
					34: '安徽',
					35: '福建',
					36: '江西',
					37: '山东',
					41: '河南',
					42: '湖北 ',
					43: '湖南',
					44: '广东',
					45: '广西',
					46: '海南',
					50: '重庆',
					51: '四川',
					52: '贵州',
					53: '云南',
					54: '西藏 ',
					61: '陕西',
					62: '甘肃',
					63: '青海',
					64: '宁夏',
					65: '新疆',
					71: '台湾',
					81: '香港',
					82: '澳门',
					91: '国外 '
				};
				var row = {
					pass: true,
					msg: '验证成功'
				};
				if (!code || !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|[xX])$/.test(code)) {
					row = {
						pass: false,
						msg: '身份证号格式错误'
					};
				} else if (!city[code.substr(0, 2)]) {
					row = {
						pass: false,
						msg: '身份证号地址编码错误'
					};
				} else {
					//18位身份证需要验证最后一位校验位
					if (code.length == 18) {
						code = code.split('');
						//∑(ai×Wi)(mod 11)
						//加权因子
						var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
						//校验位
						var parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
						var sum = 0;
						var ai = 0;
						var wi = 0;
						for (var i = 0; i < 17; i++) {
							ai = code[i];
							wi = factor[i];
							sum += ai * wi;
						}
						if (parity[sum % 11] != code[17].toUpperCase()) {
							row = {
								pass: false,
								msg: '身份证号校验位错误'
							};
						}
					}
				}
				return row;
			}
		}
	};
</script>
<style lang="scss" scoped>
	.all_box {
		.top_back {
			width: 100%;
			height: 280rpx;
		}

		.bottom_back {
			background-color: #f1f1f1;
			// height: calc(100vh - 200rpx);
		}

		.apply_box {
			position: fixed;
			left: 50%;
			top: 8%;
			transform: translate(-50%, 0);
			background-color: #ffffff;
			border-radius: 20rpx;
			width: 605rpx;
			padding: 80rpx 0 130rpx 0;
            margin-top: 60rpx;
			.apply_box_title {
				font-size: 30rpx;
				font-weight: 600;
				width: 100%;
				text-align: center;
			}
			.ino_button_a{
				color: #fff;
			}

			.apply_box_oor {
				margin: 15rpx auto 65rpx auto;
				height: 7.5rpx;
				width: 45rpx;
				// background-color: #34a049;
				border-radius: 5rpx 5rpx 0 0;
			}

			input {
				margin: 40rpx 40rpx;
			}

			.ino_button {
				font-size: 30rpx;
				padding: 25rpx 0;
				border-radius: 35rpx;
				text-align: center;
				width: 540rpx;
				margin: 0 auto;
			}

			.agreement {
				font-size: 24rpx;
				color: #000000;
				margin: 30rpx 40rpx 0 40rpx;

				.ana_p1 {
					color: #2375f4;
				}
			}
		}

		.inantext {
			background-color: #ffffff;
			width: 700rpx;
			height: 400rpx;
			padding-top: 55rpx;
			text-align: center;
		}
	}
</style>
