<!--
 * @Author: your name
 * @Date: 2021-11-16 13:37:51
 * @LastEditTime: 2022-07-19 09:21:02
 * @LastEditors: jinxin hgjinx@163.com
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \orico_ops_admin\src\views\login\index.vue
-->
<template>
	<el-main></el-main>
</template>
<style lang="scss">
.el-main {
	height: 100vh;
}
</style>
<script>
import { ElLoading } from 'element-plus';
import { setSignedIn } from '@/utils/auth';
import { mapActions, mapState } from 'vuex';

// import store from '@/store';

export default {
	name: 'Login',
	data() {
		return {};
	},
	created() {
		// let Base64 = require('js-base64').Base64
		// ElLoading.service({ fullscreen: true })
		const code = this.$route.query.code;
		// const c = this.$route.query.c;

		// if (c) {
		// 	localStorage.setItem('companyId', c);
		// }

		// const url = this.$route.query.u
		// console.log('url', url)
		// url ? console.log('decode url', Base64.decode(url)) : false
		// localStorage.setItem('LoginIn', true)
		if (typeof code === 'undefined') {
			const redirectURL = encodeURIComponent(window.location.href);
			// console.log(buildRedirectURL(redirectURL))
			setTimeout(() => {
				location.href = buildRedirectURL(redirectURL);
			}, 1000);
		} else {
			this.$http
				.showLoading(false)
				.JWT(false)
				.LoginIn()
				.get('Login/code', { code: code })
				.then((r) => {
					if (r.status === 200) {
						this.$http
							.get('SysConfig/GetOrganizations')
							.then((req) => {
								if (req.status === 200 && req.data) {
									let theid =
										r.data.userInfo.identity === 1
											? r.data.userInfo.orgId
											: r.data.userInfo.identity === 2
											? r.data.userInfo.supplierId
											: r.data.userInfo.identity === 3
											? r.data.userInfo.customerId
											: null;
									this.setOrgIdArr(req.data);
									this.setOrgid(theid);
									this.setOrgDeptId(theid);
									localStorage.setItem(
										'store',
										JSON.stringify(this.$store.state)
									);
									this.getStatus();
								}
							});
						setSignedIn(); // 设为已登录
						this.setUserInfo(r.data.userInfo);
						localStorage.setItem(
							'userToken',
							r.data.accessToken.tokenType +
								' ' +
								r.data.accessToken.token
						);
						console.log(r.data.userInfo.companyId, '是这里吗？');
						localStorage.setItem(
							'companyId',
							r.data.userInfo.companyId
						);
						// 登录时清除页签
						this.$store.state.storeData.lock = true;
						if (this.$store.state.topNavTag.tags != undefined) {
							this.$store.state.topNavTag.tags.splice(
								1,
								this.$store.state.topNavTag.tags.length
							);
						}

						setTimeout(
							() => this.$router.push({ path: '/' }),
							1000
						);
					} else {
						const redirectURL = encodeURIComponent(
							window.location.protocol +
								'//' +
								window.location.host +
								window.location.pathname
						);
						setTimeout(
							() =>
								(location.href = buildRedirectURL(redirectURL)),
							1500
						);
					}
				});
		}
	},
	methods: {
		...mapActions({
			setUserInfo: 'user/setUserInfo',
			setOrgIdArr: 'user/setorgIdArr',
			setOrgid: 'user/setorgId',
			setOrgDeptId: 'org/setorgId',
		}),
		initSelectArray(data) {
			let arr = [];
			if (!data || JSON.stringify(data) === '{}') {
				console.error('initSelectArray方法接受一个对象并转为数组');
				return [];
			}
			for (let key in data) {
				let obj = {};
				obj.value = key;
				obj.label = data[key];
				arr.push(obj);
			}
			return arr;
		},

		//获取审批流的select
		getStatus() {
			//防止上次账号的缓存
			this.$store.dispatch('examine/deleteAll');
			this.$store.dispatch('examine/deleteStaffAllList');

			const { examineStatus } = this.$store.state.storeData;
			if (examineStatus && JSON.stringify(examineStatus) !== '{}') {
				return;
			}

			this.$http.get('ApprovalFlow/status').then((res) => {
				if (res.status === 200) {
					res.data.useOrderType = this.initSelectArray(
						res.data.useOrderType
					);
					res.data.approvalWayType = this.initSelectArray(
						res.data.approvalWayType
					);
					res.data.method = this.initSelectArray(res.data.method);
					//approvalResult
					res.data.approvalResult = this.initSelectArray(
						res.data.approvalResult
					);
					this.$store.dispatch('examine/setExamine', res.data);
				}
			});
		},
	},
	computed: {
		...mapState({
			orgIdArr: (state) => (state ? state.user.orgIdArr : null),
			orgDeptId: (state) => (state ? state.org.orgDeptId : null),
		}),
	},
};
// 组装重定向链接
const buildRedirectURL = (redirectURL) => {
	const loginURL = import.meta.env.VITE_APP_SSO_LOGINURL;
	const APPID = import.meta.env.VITE_APP_SSO_APPID;
	return (
		loginURL +
		'?app_id=' +
		APPID +
		'&redirect_url=' +
		redirectURL +
		'&response_type=code'
	);
};
</script>
