<template>
	<view class="content" v-show="menus">
		<menuModel v-for="(menu ,index) in modelMenu" :key="index" :title="menu.name" :class="{'line-block':index!=0}">
			<view class="uni-flex align-top" name="slot">
				<menuItem 
					v-for="item in menu.list"
					:key="item.id"
					:item ="item"
					:doCout="toDoMsg ? toDoMsg[item.id] : 0"
					:active="1"
					@handleClick="toPage"
					
				 ></menuItem>
			</view>
		</menuModel>
	<!-- 	<view class="home-model">
			<div class="model-title">
				<text class="text">综合办公</text>
			</div>
			<div class="model-content">
				<menuItem v-for="item in modelMenu.zhbg"
					:key="item.id"
					:item ="item"
					:doCout="toDoMsg[item.id]"
					:active="menus[item.id]==1"
					@tap="toPage(item)"
				 ></menuItem>
				<view class="model-c-menu" hover-class="navigator-hover" v-for="item in modelMenu.zhbg"  :key="item.id" @tap="toPage(item)">

					<image :src="menus[item.id]==1?item.imagePath:item.noImagePath" mode="aspectFill"></image>

					<text class="ico-t-count" v-if="toDoMsg[item.id]>0">{{toDoMsg[item.id]> 99 ? '99+' : toDoMsg[item.id]}}</text>

					<text class="menu-text" :class="{'noFn': !menus[item.id] || menus[item.id]==0}">{{item.name}}</text>
				</view>
				
			</div>
		</view>
		<view class="line-block"></view>
		<view class="home-model">
			<div class="model-title">
				<text class="text">业务审批</text>
			</div>
			<div class="model-content">
				<menuItem v-for="item in modelMenu.ywsp"
					:key="item.id"
					:item ="item"
					:doCout="toDoMsg[item.id]"
					:active="menus[item.id]"
					@tap="toPage(item)"
				 ></menuItem>
				<view class="model-c-menu" v-for="item in yewuMode" hover-class="navigator-hover" :key="item.id" @tap="toPage(item)">

					<image :src="menus[item.id]==1?item.imagePath:item.noImagePath" mode="aspectFill"></image>

					<text class="ico-t-count" v-if="toDoMsg[item.id]>0">{{toDoMsg[item.id]> 99 ? '99+' : toDoMsg[item.id]}}</text>
					<text class="menu-text" :class="{'noFn': !menus[item.id] || menus[item.id]==0}">{{item.name}}</text>
				</view>

			</div>
		</view>
		<view class="line-block"></view>
		<view class="home-model">
			<div class="model-title">
				<text class="text">现场管理</text>
			</div>
			<div class="model-content">
				<menuItem v-for="item in modelMenu.xcgl"
					:key="item.id"
					:item ="item"
					:doCout="toDoMsg[item.id]"
					:active="menus[item.id]"
					@tap="toPage(item)"
				 >
				 
				 </menuItem>
				 
				<view class="model-c-menu" v-for="item in sceneMode" hover-class="navigator-hover" :key="item.id" @tap="toPage(item)">

					<image :src="menus[item.id]==1?item.imagePath:item.noImagePath" mode="aspectFill"></image>
					<text class="ico-t-count" v-if="toDoMsg[item.id]>0">{{toDoMsg[item.id]> 99 ? '99+' : toDoMsg[item.id]}}</text>

					<text class="menu-text" :class="{'noFn': !menus[item.id] || menus[item.id]==0}">{{item.name}}</text>
				</view>
				<view class="model-c-menu" @tap="scanCode()">

					<image src="/static/images/index/saoyisao.png" mode="aspectFill" v-if="user.Membership_Id"></image>
					<image src=" /static/images/index/saoyisao-no.png" mode="aspectFill" v-else></image>

					<text class="menu-text" :class="{'noFn': !user.Membership_Id}">扫一扫</text>
				</view>
				
			</div>
		</view>
		<view class="line-block"></view>
		<view class="home-model">
			<div class="model-title">
				<text class="text">商业经营</text>
			</div>
			<div class="model-content">
				
				<view class="model-c-menu" v-for="item in managementMode" hover-class="navigator-hover" :key="item.id" @tap="toPage(item)">

					<image :src="menus[item.id]==1?item.imagePath:item.noImagePath" mode="aspectFill"></image>

					<text class="ico-t-count" v-if="toDoMsg[item.id]>0">{{toDoMsg[item.id]> 99 ? '99+' : toDoMsg[item.id]}}</text>

					<text class="menu-text" :class="{'noFn': !menus[item.id] || menus[item.id]==0}">{{item.name}}</text>
				</view>

			</div>
		</view> -->
	</view>
</template>

<script>
	import {
		mapState,
		
		mapMutations,
		mapActions
	} from 'vuex'
	import modelMenu from'./components/menus.js'
	import menuModel from'./components/menuModel.vue'
	import menuItem from'./components/menuItem.vue'
	export default {
		data() {
			return {
				modelMenu: null,
				membershipType: null
			}
		},
		components:{
			menuModel,
			menuItem
		},
		onLoad() {
			this.loadMenuSetting()
		},
		computed: {
			...mapState({
				user: (state)=>{
					return {
						UserId: state.userData.UserId,
						UserName: state.userData.UserName,
						Membership_Phone: state.userData.Membership_Phone,
						Membership_Id: state.userData.Membership_Id,
						Membership_Type: state.userData.Membership_Type
					}
				},
				toDoMsg: (state)=>{
					return state.toDoMsg
				},
				menus: (state)=> {
					return state.userData.AuthorityInfo
				}
			}),
			
		},
		methods: {
			...mapActions(['updateUser']),
			scanCode() {
				let _this = this
				uni.scanCode({
					onlyFromCamera: true,
					success: function(res) {
						console.log('条码类型：' + res.scanType)
						console.log('条码内容：' + res.result)
						if (res.scanType.indexOf("QR_CODE") > -1) {

							let result = res.result.toLocaleLowerCase()
							if (result.indexOf('http://eshangtech.com:8000/mbwa/index.html') > -1) {
								let mbwaId = res.result.split('?')[1]
								mbwaId = mbwaId.split('=')[1]
								
								_this.$util.toNextRoute('navigateTo', "/pages/mbwa/mbwaUpload?mbwaID=" + mbwaId)
								
							} else if (res.result.indexOf("http") > -1) {
								_this.$util.toNextRoute('navigateTo', "/pages/webview/webview?src=" +
									encodeURIComponent(res.result))
							} else {
								let invoice = res.result.split(",")
								// console.log(invoice.length)
								if (invoice.length == 9) {
									_this.$util.toNextRoute('navigateTo', "/pages/invoice/invoice?invoice=" + invoice)
								} else {
									uni.showModal({
										title: "温馨提示",
										content: "不识别该二维码，请扫描发票或商品条形码",
										showCancel: false,
										confirmText: "确定"
									})
								}
							}
						} else if (res.scanType.indexOf("EAN_13") > -1) {
							this.$util.toNextRoute('navigateTo', "/pages/commodtity/commodtity?commodtity=" + res.result)
						} else {
							uni.showModal({
								title: "温馨提示",
								content: "不识别该二维码，请扫描发票或商品条形码",
								showCancel: false,
								confirmText: "确定"
							})
						}
					}
				})

			},
			recordBrowsing(item) { // 点击业务模块事件
				let _this = this
				if (!item.id) {
					uni.showModal({
						title: ' 温馨提示',
						content: '该模块正在开发，尽请期待',
					})
				// } else if (_this.menus && (_this.menus[item.id] == 1 ||
				// 	this.user.Membership_Type === 9999)) { // 记录用户访问业务事件
				} else {
					this.$util.toNextRoute('navigateTo', item.homeUrl)
				// } else if (this.user.Membership_Type !== 9999) {
				// 	uni.showToast({
				// 		title: '您没有该模块权限',
				// 		icon: 'none'
				// 	})
				}
			},
			toPage(item) {

				let _this = this
				
				if (!this.user.Membership_Phone) {
					uni.showModal({
						title: ' 温馨提示',
						content: '您还没有登录，快去登录吧!',
						success(res) {
							if (res.confirm) {
								_this.$util.toNextRoute('navigateTo', '/pages/register/register')
							}
						}
					})

				}else if(item.id=='d5a9e3ef-3699-448f-abcc-819179df60bd') { // 如果是扫一扫功能
					this.scanCode()
				} else if (this.user.UserId == '') {
					uni.showModal({
						title: ' 温馨提示',
						content: '您的账户未绑定综管平台账户，请先绑定以获取业务权限',
						success(res) {
							if (res.confirm) {
								_this.$util.toNextRoute('navigateTo', '/pages/bindingBack/bindingBack')
							}
						}
					})
				} else {
					this.recordBrowsing(item)
				}
			},
			async loadMenuSetting(){ //加载首页菜单配置
				this.membershipType = this.user.Membership_Type
				let res = await this.$request.$webGet("EShangApiMain/APPManage/GetWeChatAppletMenuList",{
					ModuleStatus: this.membershipType === 9999 ? '1,2' : '1',
					AppletMenuStatus: this.membershipType === 9999 ? '1,2' : '1'
				})
				this.modelMenu = res.Result_Data.List
        console.log('modelMenu',this.modelMenu)
			}
		},
		onReady() {
		},
		onShow() {
		},
		onPullDownRefresh() {
			let _this = this
			this.updateUser()
			this.loadMenuSetting()
			setTimeout(function() {
				uni.stopPullDownRefresh()
			}, 1000)
		},
		onTabItemTap(e) {
			this.$util.addUserBehavior({
				outtoRoute: '/pages/index/index',
				intoRoute: '/pages/userCenter/userCenter'
			})
		}
	}
</script>
<style scoped>
	.line-block {
		border-top: 13rpx solid  #F5F5F5;
		
	}

	.content {
		display: flex;
		flex-direction: column;
		justify-content: center;
	}
	.uni-flex {
		align-items: center;
		flex-wrap: wrap;
	}

</style>
