
<template>
	 <view class="enterp">
		<view class="head_text">{{enterPriseList.length <= 0 ? '请绑定企业以登录' : '请选择企业以登录'}}</view>
		<view class="line"></view>
		<view class="enter_list">
			<view class="enterp-box" v-for="(item,index) in enterPriseList" @click="toLogin(item)">
				<text class="iconfont icon-qiye" style="font-size: 40rpx; color: #2979FF;vertical-align: middle;"></text>
				<text style="vertical-align: middle; display: inline-block; margin-left: 10rpx;">{{item.enterpriseName}}</text>
			</view>
		</view>
		<view class="btn-view">
			<u-button type="primary" :custom-style="customStyle" @click="bindEnterprises">绑定企业</u-button>
		</view>
		
		<u-modal negative-top="200" :async-close="true" :show-title="false" :show-cancel-button="true" :mask-close-able="false" ref="uModal" :show="showModal"  @confirm="confirm" @cancel="cancel">
			<view class="slot-content ">
				<u-form label-width="140" :model="form">
					<u-form-item label="企业编号:" required>
						<u-input placeholder="请输入企业编号" :clearable="false" v-model="form.enterpriseCode"></u-input>
					</u-form-item>
					<u-form-item label="账 号 :" required>
						<u-input placeholder="请输入账号" :clearable="false" v-model="form.username"></u-input>
					</u-form-item>
					<u-form-item label="密 码 :" required>
						<u-input placeholder="请输入密码" type="password" :password-icon="false" :clearable="false" v-model="form.password"></u-input>
					</u-form-item>
				</u-form>
			</view>
		</u-modal>
	</view> 
</template>

<script>
	import encryption from '../../utils/util'
	import crypto from '../../utils/crypto.js'
	export default {
		data() {
			return {
				customStyle: {
					height: '100rpx',
					marginBottom: '20rpx',
					fontWeight: '700'
				},
				showModal: false,
				enterPriseList: [],
				user_info: {},
				enterpriseInfo: '',
				form: {
					enterpriseCode: '',
					username: '',
					password: '',
					phone: ''
				},
				wxUserInfo: ''
			};
		},
		
		onLoad(option) {
			console.log("有没又进来")
			this.user_info = uni.getStorageSync('userInfo')
			this.form.phone = this.user_info.username
			this.getEnterprise()
		},
		
		methods: {
			async getEnterprise () {
				const { data: res } = await this.$request({
					url: '/admin/enterprise/getSysEnterprisePage/' + this.user_info.phone,
					method: 'GET',
					data: {}
				})
				console.log(res)
				if (res.code == 0) {
					this.enterPriseList = res.data || []
				}
			},
			
			async toLogin (val) {
				const that = this
				uni.showLoading({
				    title: '正在登录中'
				});
				console.log(val)
				console.log(that.user_info.phone)
				that.enterpriseInfo = val
				setTimeout(async function  () {
					const { data: res } = await that.$request({
						url: '/admin/enterprise/login',
						method: 'POST',
						data: {
							enterpriseCode: that.enterpriseInfo.enterpriseCode,
							phone: that.user_info.phone
						}
					})
					
					console.log(res, 'resrsers')
					if (res.code == 0) {
						uni.setStorageSync('phone', that.user_info.phone)
						uni.setStorageSync('oldtoken', uni.getStorageSync('token'))
						uni.setStorageSync('token', res.data.access_token)
						uni.setStorageSync('enterpriseInfo', JSON.stringify(that.enterpriseInfo))
						getApp().globalData.tabIndex = 'approval'
								uni.switchTab({
									url: "/pages/approval/approvallist"
						})
						uni.showToast({
							title: '登录成功',
							icon: "none"
						})
					} else if (res.code == 1) {
						uni.showToast({
							title: '认证失败，请重新认证',
							icon: 'none',
							success: function (res) {
								setTimeout((function () {
								  // const url = "/pages/login/accountLogin/index?enterpriseCode=" + that.enterpriseInfo.enterpriseCode
								  // uni.navigateTo({
								  // 	url: url
								  // })
								  that.form.enterpriseCode = val.enterpriseCode
								  that.showModal = true
								}), 1000)
							}
						})
						
					}  else {
						uni.showToast({
							title: res.data.msg,
							icon: "none"
						})
					}
					uni.hideLoading()
				}, 1500);
			},
			
			bindEnterprises () {
				console.log('绑定')
				this.showModal = true
			},
			
			confirm () {
				const that = this
				that.$refs.uModal.clearLoading();
				console.log(that.form)
				if (that.form.enterpriseCode == '') {
					uni.showToast({
						title: '请输入企业编号',
						icon: 'none'
					})
				} else if (that.form.username == '') {
					uni.showToast({
						title: '请输入账号',
						icon: 'none'
					})
				} else if (that.form.password == '') {
					uni.showToast({
						title: '请输入密码',
						icon: 'none'
					})
				} else {
					uni.showLoading({
					    title: '正在绑定中'
					});
					
					 setTimeout(async function () {
						const user =crypto.encryption({
							data: that.form,
							key: 'pigxpigxpigxpigx',
							param: ['password'],
							source: 'mini'
						})
						
						const { data: res } = await that.$request({
							url: '/admin/enterprise/bindEnterpriseUserInfo',
							method: 'POST',
							data: user,
							header: {
								'Content-Type': 'application/json;charset=utf-8',
								'Authorization': uni.getStorageSync('token'),
								'TENANT-ID': uni.getStorageSync('tenantId')
							}
						})
						console.log('--------',res)
						if (res.code == 0) {
							uni.setStorageSync('tenantId', res.data.enterpriseInfo.tenantId)
							uni.setStorageSync('oldtoken', uni.getStorageSync('token'))
							uni.setStorageSync('token', res.data.access_token)
							uni.setStorageSync('enterpriseInfo', JSON.stringify(res.data.enterpriseInfo))
							getApp().globalData.tabIndex = 'approval'
								uni.switchTab({
									url: "/pages/approval/approvallist"
							})
							uni.showToast({
								title: '绑定登录成功',
								icon: "none",
								duration: 2500,
							})
						} else {
							uni.showToast({
								title: res.msg == null ? '企业绑定失败' : res.msg,
								icon: "none",
							})
						}
						// uni.hideLoading();
					}, 1000);
				}
				
			},
			
			cancel () {
				Object.keys(this.form).forEach(key => (this.form[key] = ""));
				this.showModal=false;
			},
			
			// 生成随机数字
			randomLenNum (len, date) {
			  let random = ''
			  random = Math.ceil(Math.random() * 100000000000000).toString().substr(0, len || 4)
			  if (date) random = random + Date.now()
			  return random
			}
		}
	}
</script>

<style lang="scss">
 .enterp {
	 text-align: center;
 }
 
 .head_text {
	 font-size: 40rpx;
	 margin: 100rpx 0 60rpx;

 }
 
 .line {
	 width: 30%;
	 height: 2rpx;
	 margin: auto;
	 background-color: #e6e6e6;
	 margin-bottom: 50rpx;
 }
 
 .enter_list {
	 width: 80%;
	 margin: 40rpx auto;
 }
 
 .enterp-box {
	 text-align: left;
	 height: 120rpx;
	 background-color: #FFFFFF;
	 line-height: 120rpx;
	 padding: 0 40rpx;
	 margin-bottom: 30rpx;
 }
 
 .btn-view {
	 width: 80%;
	 margin: 0 auto;
 }
 
 .slot-content {
   padding: 40rpx;
 }
 
 .u-form-item {
	 padding: 10rpx 0 !important;
 }
</style>
