<template>
	<view class="content">
		<u-form>
			<view>

				<u-form-item label="公司名称" @click="handleChange()" label-width='150px' style="margin: 0 20px 0 30px;"
					prop="OrgId" borderBottom>
					<u--input disabledColor="#fff" v-model="NAME" placeholder="请选择公司名称" border="none"
						inputAlign="right"></u--input>
					<u-icon v-if="!OrgId||OrgId.length<1" style="margin-left: 4px;" slot="right"
						name="arrow-down"></u-icon>
				</u-form-item>
			</view>
			<view class="list">
				<u-grid :border="false" :col="2">
					<u-grid-item v-for="(baseListItem,baseListIndex) in baseList" :key="baseListIndex" bgColor="#fff">
						<u-icon :customStyle="{paddingTop:20+'rpx'}" :name="baseListItem.iconName" :size="42"
							@click="menuClick(baseListItem)" color="#304ffe"></u-icon>
						<text class="grid-text">{{baseListItem.name}}</text>
					</u-grid-item>
				</u-grid>
			</view>
		</u-form>


		<query-selector :show="selectorInfo.showPopup" :defaultChecked="selectorInfo.defaultChecked"
			:title="selectorInfo.title" :lists="selectorInfo.dataLists" @search="handleSearch" @select="handleSelected"
			@close="selectorInfo.showPopup=false" />
	</view>
</template>

<script>
	import {
		getMenu
	} from '../../services/apply'
	import config from '../../config/common'
	import request from '../../utils/request.js'
	export default {
		data() {
			return {
				DEPARTMENT_ID: '', //'B18F95F4-572A-E521-1412-FA8CE50DC98A',
				RISK_AREA_ID: '', //8EB0A653-E785-46D9-B65E-17F49E77121E',
				POST_ID: '',
				OrgId: '',
				NAME: '',
				createType: '',
				uni_app_web_local_api_url: config.uni_app_web_local_api_url,
				baseList: [],
				company: '',
				dataLists: [{
					name: '行洛坑',
					code: 'XLK',
					id: 'B043B28B-BBC3-C452-6052-4FBA1457ABFA',
					tenantId: '0001'
				}, {
					name: '都昌金鼎',
					code: 'DCJD',
					id: '8B3C41AA-51B1-7CE9-1879-248A038C1B5C',
					tenantId: 'A0000025'
				}, {
					name: '洛阳豫鹭',
					code: 'LYYL',
					id: 'D9871BA8-0EEC-9E4A-BB87-7D5A540D8913',
					tenantId: 'A0000024'
				}, {
					name: '广西博白',
					code: 'GXBB',
					id: '3EFD5276-632B-E379-9FF3-7A7546591FCA',
					tenantId: 'A0000028'
				}],
				selectorInfo: {
					showPopup: false,
					title: '检查人员',
					itemData: {},
					index: 0,
					dataLists: [],
					defaultText: '',
					defaultValue: '',
					name: '',
					defaultChecked: []
				},
			}
		},
		// onShow() {
		// 	const pages = getCurrentPages()
		// 	const currentPage = pages[pages.length - 1]
		// 	const Options = currentPage.options
		// 	uni.setStorageSync('Company', Options.Company ? Options.Company : 'XLK')
		// },
		onLoad(option) {
			if (option.OrgId && option.OrgId.length > 0) {
				this.OrgId = option.OrgId
				this.createType = option.createType
				this.RISK_AREA_ID = option.AREA_ID
				this.DEPARTMENT_ID = option.DEPARTMENT_ID
				this.POST_ID = option.POST_ID
			} else {
				var paraString = location.search; //.substring(1, location.search.length - 1);
				if (paraString && paraString.length > 1) {
					var paras = paraString.split("&");
					paras.forEach(e => {
						if (e.indexOf("OrgId") > -1) {
							this.OrgId = e.split('=')[1]
						} else if (e.indexOf("createType") > -1) {
							this.createType = e.split('=')[1]
						} else if (e.indexOf("AREA_ID") > -1) {
							this.RISK_AREA_ID = e.split('=')[1]
						} else if (e.indexOf("DEPARTMENT_ID") > -1) {
							this.DEPARTMENT_ID = e.split('=')[1]
						} else if (e.indexOf("POST_ID") > -1) {
							this.POST_ID = e.split('=')[1]
						}
					})
				}
			}
			// createType    '区域'10    '岗位'20
			if (this.OrgId && this.OrgId.length > 1) {
				this.dataLists.forEach(e => {
					if (this.OrgId.toUpperCase() == e.id) {
						this.NAME = e.name
						//初始化加载 存值
						uni.setStorageSync('tenantId', e.tenantId)
					}
				})
				this.getCaidan()
			}
		},

		//页面跳转到APP
		onNavigationBarButtonTap(e) {
			let urlTo = this.uni_app_web_local_api_url + ':9100/'
			//let urlTo ='http://localhost:8000/'  //本地是8000
			uni.navigateTo({
				url: '/pages/webview/webview?url=' + urlTo
			})
		},
		methods: {
			// click(name) {
			// 	this.baseList.map((item, index) => {
			// 		if (index == name) {
			// 			if (item.name == '新增用户') {
			// 				uni.navigateTo({
			// 					url: '/pages/apply/index'
			// 				})
			// 			}
			// 		}
			// 	})
			// },

			menuClick(menuInfo) {
				uni.navigateTo({
					url: menuInfo.url + '?OrgId=' + menuInfo.ORG_ID +
						'&RISK_AREA_ID=' + this.RISK_AREA_ID + '&DEPARTMENT_ID=' + this.DEPARTMENT_ID
				})
			},
			// goCamera() {
			// 	// uni.chooseImage({
			// 	// 	count: 6, //默认9
			// 	// 	sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
			// 	// 	sourceType: ['camera'],
			// 	// 	success: (res) => {
			// 	// 		const url = config.serviceHost('/PF/File/UploadFile')
			// 	// 		request.upload(url, res.tempFilePaths).then((result) => {
			// 	// 			if (result.IsSuccessful) {
			// 	// 				// TODO: 目前只支持一张照片
			// 	// 				store.commit('updateImageLists', [{
			// 	// 					IMG_FILE_ID: result.Data.imgFileID,
			// 	// 					IMG_FILE_PATH: result.Data.imgFilePath
			// 	// 				}]);
			// 	// 				uni.showActionSheet({
			// 	// 					title: '选择上报类型',
			// 	// 					itemList: ['随手拍', '隐患上报'],
			// 	// 					success: (e) => {
			// 	// 						if (e.tapIndex === 0) {
			// 	// 							uni.navigateTo({
			// 	// 								url: '/pages/camera/index'
			// 	// 							})
			// 	// 						} else {
			// 	// 							uni.navigateTo({
			// 	// 								url: '/pages/apply/subPages/riskReport'
			// 	// 							})
			// 	// 						}
			// 	// 					}
			// 	// 				})
			// 	// 			}
			// 	// 		}).catch((error) => {
			// 	// 			console.log(error)
			// 	// 		});
			// 	// 	},
			// 	// 	complete: function(com) {
			// 	// 		console.log('choose image complete', com)
			// 	// 	}
			// 	// })
			// },
			getCaidan() {
				const json = {
					OrgId: this.OrgId
				}
				getMenu(json).then((res) => {
					this.baseList = res.data
				}).catch((err) => {
					uni.showToast({
						title: '请求错误',
						icon: 'none'
					})
				})
			},

			//显示组件
			handleChange() {
				if (!this.OrgId || this.OrgId.length < 2) {
					this.selectorInfo.showPopup = true
					this.selectorInfo.title = '公司选择'
					this.handleSearch()
				}
			},

			//数据加载
			handleSearch(val) {
				this.selectorInfo.dataLists = this.dataLists
				// const orgId = uni.getStorageSync('orgId')
				// const json = initFilter(orgId, "", "", 0)
				// if (val) {
				// 	extendRule(json, 'NAME', 9, val);
				// }
				// OrderPagedUseful2(json).then(res => {
				// 	this.selectorInfo.dataLists = res.Data.map(i => {
				// 		return {
				// 			...i,
				// 			name: i.NAME,
				// 			code: i.CODE
				// 		}
				// 	})
				// })
			},

			handleSelected(e) {
				this.selectorInfo.showPopup = false
				this.OrgId = e.id
				this.NAME = e.name
				//公司选择 存值
				uni.setStorageSync('tenantId', e.tenantId)
				if (this.OrgId && this.OrgId.length > 1)
					this.getCaidan()
			}
		}
	}
</script>

<style>
	.content {
		/* background-color: #f4f7ff; */
		background-color: rgb(246, 250, 255);
		width: 100%;
		/* #ifndef APP-PLUS */
		height: calc(100vh - 44px - 50px); // 非APP平台下生效
		/* #endif */
		/* #ifdef APP-PLUS */
		height: calc(100vh); // APP平台下生效
		/* #endif */
	}

	.list {
		padding: 20px 40px;
	}

	.list .u-grid {
		justify-content: space-between !important;
	}

	.list .u-grid-item {
		padding: 20px 0px;
		/* margin: 0px 0px 20px 0px; */
		margin: 10px 0px;
		width: 47% !important;
		border-radius: 10px;
		box-shadow: 5px 5px 5px #ceccca;
	}

	.home-button {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		padding: 10px 36px;
		box-sizing: border-box;
		/* background: #f4f7ff; */
		background: rgb(246, 250, 255);
		z-index: 999;
	}

	.home-button .u-button {
		height: 50px;
	}

	.grid-text {
		font-size: 14px;
		color: #000;
		font-weight: bold;
		padding: 10rpx 0 20rpx 0rpx;
		/* #ifndef APP-PLUS */
		box-sizing: border-box;
		/* #endif */
	}
</style>