<template>
	<view class="page">
		<view class="status_bar">
			<view class="top_view"></view>
		</view>
		<view class="wui_banner">
			<view class="banner_img">
				<image src="../../static/home-bg.png" mode=""></image>
			</view>
		</view>
		<view class="home-apps">
			<view class="home-apps-item" @click="fnScan">
				<view class="home-apps-item-img">
					<image src="../../static/icon-apps/app_icons6.png" mode=""></image>
				</view>
				<view class="text">
					<text>扫码</text>
				</view>
			</view>
			<view class="home-apps-item" v-for="(item, index) in baseList" :key="index" @click="fnNavigator(index)">
				<view class="home-apps-item-img">
					<image :src="item.img" mode=""></image>
				</view>
				<view class="text">
					<text>{{ item.title }}</text>
				</view>
			</view>
		</view>
		<u-modal
			:show="updateVersion.modalShow"
			title="温馨提示"
			:showConfirmButton="updateVersion.showConfirmButton"
			:showCancelButton="updateVersion.showCancelButton"
			:confirmText="updateVersion.confirmText"
			:cancelText="updateVersion.cancelText"
			@cancel="modalCancel"
			@confirm="modalConfirm"
		>
			<view style="text-align: center; color: #606266">
				<rich-text :nodes="updateVersion.modalContent"></rich-text>
			</view>
		</u-modal>

	</view>
</template>

<script>
import updateVersion from '../../utils/updateVersion'
import { setEntry, getIsUploadFace } from '../../api'

export default {
	mixins: [updateVersion],
	data() {
		return {
			baseList: [
				{
					img: require('../../static/icon-apps/app_icons.png'),
					title: '电子工牌',
					url: '/pages/electronic_work_card/index'
				},
				{
					img: require('../../static/icon-apps/app_icons2.png'),
					title: '服务单位 管理',
					url: '/pages/service_unit_management/index'
				},
				{
					img: require('../../static/icon-apps/app_icons3.png'),
					title: '我的信息',
					url: '/pages/mine/information/index'
				},
				{
					img: require('../../static/icon-apps/app_icons4.png'),
					title: '证书信息',
					url: '/pages/certificate_information/index'
				},
				{
					img: require('../../static/icon-apps/app_icons5.png'),
					title: '就职单位',
					url: '/pages/employed_by/index'
				},
				{
					img: require('../../static/icon-apps/app_icons7.png'),
					title: '高危作业',
					url: '/pages/eight_assignments/index'
				},
				{
					img: require('../../static/icon-apps/app_icons8.png'),
					title: '培训管理',
					url: '/pages/train_management/index'
				}
			]
		}
	},
	computed: {
		userInfo() {
			return this.$store.getters.getUserInfo
		}
	},
	onLoad() {
		this.fnUpdateVersion(false)
		this.getUserFaceCompleted()
	},
	methods: {
		async fnScan() {
			uni.scanCode({
				success: async (res) => {
					let obj = JSON.parse(res.result)
					let type = obj[0].CODE_TYPE
					if (type === '0') {
						let info = await setEntry({
							USER_ID: this.userInfo.USER_ID,
							CORPINFO_ID: obj[0].CORPINFO_ID,
							RELEVANT_UNIT_NAME: obj[0].RELEVANT_UNIT_NAME
						})
						if (info.code === 200) {
							uni.$u.toast('入职成功')
						} else {
							uni.$u.toast('入职失败,请联系管理员')
						}
					} else if (type === '1') {
						// type=1时跳转电子工牌页面
						uni.$u.route({
							url: '/pages/electronic_work_card/index',
							params: {
								USER_ID: obj[0].USER_ID,
								CODE_TYPE: obj[0].CODE_TYPE
							}
						})
					}
				}
			})
		},
		fnIsShow(index) {
			if (index === 5) return !!this.userInfo.CORPINFO_ID
			return true
		},
		fnNavigator(e) {
			uni.$u.route({
				url: this.baseList[e].url
			})
		},
		/**
		 * 获取是否已经完成上传人脸信息
		 */
		async getUserFaceCompleted() {
			const userFaceData = await getIsUploadFace()
			// userFaceData.authentication --> 0: 未完成人脸认证
			// userFaceData.authentication --> 1: 已完成人脸认证
			this.$store.dispatch('setVerification', userFaceData.authentication ? userFaceData.authentication : "0"); // 设置本地存储人脸检测验证状态
			const isPassedVerification = userFaceData.authentication === '0'
			if (isPassedVerification) {
				uni.showModal({
					title: '温馨提示',
					content: '为了能够得到更好的体验,我们会获取您个人信息,请完成人脸信息认证!',
					success: function (res) {
						if (res.confirm) {
							uni.$u.route({
								url: '/pages/train_management/face_authentication',
								params: {
									type: 'facial_input'
								}
							})
						}
						if (res.cancel) {
              uni.$u.toast("您可进入[我的]-[人脸认证]完成信息认证")
						}
					}
				})
			}
		}
	}
}
</script>

<style lang="scss" scoped>
.page {
	background-color: #f7f9ff;
	min-height: 100vh;
}
</style>