<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() {
			//区域
			// http://localhost:8080/?OrgId=b043b28b-bbc3-c452-6052-4fba1457abfa&createType=10&AREA_ID=b3f5af14-401d-45a5-beb8-5e9c845aae8f#/
			//组织 岗位
			// http://localhost:8080/?OrgId=b043b28b-bbc3-c452-6052-4fba1457abfa&createType=20&DEPARTMENT_ID=fa6fb6e3-dd43-fad3-6b8c-75ceeba3ca75&POST_ID=f3fb87a5-027c-0611-84a5-153a5e5a402a#/

			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) {
					if (paraString.indexOf('&') > -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]
							}
						})
					} else if (paraString.indexOf('OrgId') > -1) {
						this.OrgId = paraString.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: {
			// FM/DataSync/Sync
			menuClick(menuInfo) {
				var urlTo = menuInfo.url + '?OrgId=' + menuInfo.ORG_ID
				if (this.RISK_AREA_ID && this.RISK_AREA_ID != undefined && this.RISK_AREA_ID.length > 1)
					urlTo += '&RISK_AREA_ID=' + this.RISK_AREA_ID
				if (this.DEPARTMENT_ID && this.DEPARTMENT_ID != undefined && this.DEPARTMENT_ID.length > 1)
					urlTo += '&DEPARTMENT_ID=' + this.DEPARTMENT_ID
				if (this.POST_ID && this.POST_ID != undefined && this.POST_ID.length > 1)
					urlTo += '&POST_ID=' + this.POST_ID

				uni.navigateTo({
					url: urlTo
				})
			},
			getCaidan() {
				const json = {
					OrgId: this.OrgId
				}
				getMenu(json).then((res) => {
					//有区域或者岗位TYPE 20   扫二维码查看
					//只有OrgID 生成二维码 TYPE 20   生成二维码

					// this.baseList = res.data
					if (this.createType == '') {
						this.baseList = res.data
					} else {
						//createType  10 按区域显示  20 按岗位显示
						if (this.createType == 10 || this.createType == 20) {
							this.baseList = res.data.filter(e => e.TYPE == this.createType)
							if (this.baseList && this.baseList.length == 1) {
								this.menuClick(this.baseList[0]) //只有一个节点 直接跳转
							}
						} else {
							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.createType =10  按区域生成
				// this.createType =20  按岗位生成

				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()
				// this.Sync()
			},

			Sync() {
				var params = {}
				var listPost = []
				var listDepartment = []
				var listUser = []

				// listUser.push({
				// 	CODE: 'XLK100194',
				// 	DEPARTMENT_CODE: 'XLKXK0201',
				// 	POST_CODE: 'QQJSY13',
				// 	NAME: '马华满',
				// 	PHONE: '18950958648',
				// 	SEX: '1',
				// 	ID_CARD: '350822200005149876',
				// 	NATIONALITY: '汉',
				// 	BIRTHDAY: '2000-05-14',
				// 	POLITICALSTATUE: '群众',
				// 	JOBTITLE: '高工',
				// 	CULTURALLEVEL: '大学',
				// 	WORKDATE: '2000-05-14',
				// 	ENTRYTIME: '2019-05-14',
				// 	Nav_Works: [{
				// 		CODE: 'XLK100194',
				// 		STARTDATE: '2015-05-14',
				// 		ENDDATE: '2018-05-14',
				// 		WORKPLACE: '厦门科技',
				// 		POSTNAME: '程序员'
				// 	}, {
				// 		CODE: 'XLK100194',
				// 		STARTDATE: '2018-05-14',
				// 		ENDDATE: '2019-05-14',
				// 		WORKPLACE: '信息集团',
				// 		POSTNAME: '软件工程师'
				// 	}, {
				// 		CODE: 'XLK100194',
				// 		STARTDATE: '2019-05-14',
				// 		ENDDATE: '',
				// 		WORKPLACE: '中国软件',
				// 		POSTNAME: '开发经理'
				// 	}],
				// 	Nav_Skills: [],
				// 	// Nav_UserPhotoFiles
				// 	// Nav_IDCardFiles
				// 	// Nav_EducationFiles
				// 	// Nav_CertificateFiles
				// 	ENABLE_STATUS: '0',
				// 	MODIFY_TIME: '2025-05-14 10:23:23'
				// })

				// listUser.push({
				// 	CODE: 'XLK100194',
				// 	DEPARTMENT_CODE: 'XLKXK0201',
				// 	POST_CODE: 'QQJSY13',
				// 	NAME: '马华满',
				// 	PHONE: '18950958648',
				// 	SEX: '1',
				// 	ID_CARD: '350822200005149876',
				// 	NATIONALITY: '汉',
				// 	BIRTHDAY: '2000-05-13',
				// 	POLITICALSTATUE: '群众',
				// 	JOBTITLE: '高工',
				// 	CULTURALLEVEL: '大学',
				// 	WORKDATE: '2000-05-14',
				// 	ENTRYTIME: '2019-05-14',
				// 	Nav_Works: [{
				// 		CODE: 'XLK100194',
				// 		STARTDATE: '2015-05-14',
				// 		ENDDATE: '2018-05-14',
				// 		WORKPLACE: '厦门科技',
				// 		POSTNAME: '程序员'
				// 	}, {
				// 		CODE: 'XLK100194',
				// 		STARTDATE: '2018-05-14',
				// 		ENDDATE: '2019-05-14',
				// 		WORKPLACE: '信息集团',
				// 		POSTNAME: '软件工程师'
				// 	}, {
				// 		CODE: 'XLK100194',
				// 		STARTDATE: '2019-05-14',
				// 		ENDDATE: '',
				// 		WORKPLACE: '中国软件',
				// 		POSTNAME: '开发经理'
				// 	}],
				// 	Nav_Skills: [],
				// 	// Nav_UserPhotoFiles
				// 	// Nav_IDCardFiles
				// 	// Nav_EducationFiles
				// 	// Nav_CertificateFiles
				// 	ENABLE_STATUS: '0',
				// 	MODIFY_TIME: '2025-05-13 10:23:23'
				// })

				// listUser.push({
				// 	CODE: 'XLK100888',
				// 	DEPARTMENT_CODE: 'XLKKS',
				// 	POST_CODE: 'ZZAQY',
				// 	NAME: '王永发',
				// 	PHONE: '18750553001',
				// 	SEX: '1',
				// 	ID_CARD: '350822200005149876',
				// 	NATIONALITY: '汉',
				// 	BIRTHDAY: '1996-08-18',
				// 	POLITICALSTATUE: '群众',
				// 	JOBTITLE: '高工2',
				// 	CULTURALLEVEL: '研究生',
				// 	WORKDATE: '2000-08-18',
				// 	ENTRYTIME: '2019-08-18',
				// 	Nav_Works: [],
				// 	Nav_Skills: [{
				// 		NAME: '全国英语水平',
				// 		LEVEL: 'CET-4',
				// 	}],
				// 	// Nav_UserPhotoFiles
				// 	// Nav_IDCardFiles
				// 	// Nav_EducationFiles
				// 	// Nav_CertificateFiles
				// 	ENABLE_STATUS: '0',
				// 	MODIFY_TIME: '2025-05-08 10:23:23'
				// })

				// listDepartment.push({
				// 	CODE:'XLKXK0501',
				// 	NAME:'电工一班同步测试',
				// 	NUM:'10',
				// 	PARENT_CODE:'XLKXK05',
				// 	MODIFY_TIME:'2025-05-15 9:23:23',
				// 	ENABLE_STATUS:0,
				// 	DEPARTMENT_TYPE:2

				// })
				// listDepartment.push({
				// 	CODE:'XL123KXK05033',
				// 	NAME:'步测试新增2',
				// 	NUM:'12',
				// 	PARENT_CODE:'XLKXK05',
				// 	MODIFY_TIME:'2025-05-15 9:23:23',
				// 	ENABLE_STATUS:0,
				// 	DEPARTMENT_TYPE:2

				// })

				listPost.push({
					CODE: '000',
					NAME: 'HCM修改',
					STATUS: '1',
					MODIFY_TIME: '2025-05-08 10:23:23'
				})
				listPost.push({
					CODE: '2155',
					NAME: 'HCM同步新增',
					STATUS: '0',
					MODIFY_TIME: '2025-05-08 9:23:23'
				})
				params.ORG_ID = this.OrgId
				params.listPost = listPost
				params.listDepartment = listDepartment
				params.listUser = listUser
				return request.send({
					url: '/FM/DataSync/Sync',
					method: 'post',
					data: params,
				}).then(res => {
					// debugger
					// // this.selectorInfo.totalCount = res.TotalCount
					// if (res != undefined && res.Data.length > 0) {
					// 	return res.Data
					// } else {
					// 	uni.showToast({
					// 		title: '未获取到区域！',
					// 		icon: 'none'
					// 	})
					// }
				});

				// const url = config.serviceHost('/FM/DataSync/SyncUploadFile')
				// params.ORG_ID = this.OrgId
				// params.TYPE = "Nav_IDCardFiles"
				// params.CODE = "XLK100335"
				// uni.uploadFile({
				// 	url: url,
				// 	filePath: uni.getStorageSync('riskUpInfo')[0].filePath, //e.filePath,
				// 	name: 'file',
				// 	// data: params,
				// 	formData: params,
				// 	header: {
				// 		Tenant: '0001',
				// 		RequestType: '2'
				// 	},
				// 	success: (res) => {
				// 		if (res.statusCode == 200) {
				// 		}
				// 	},
				// 	fail: (err) => {
				// 		console.log(err)
				// 	}
				// })
			},

		}
	}
</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>