<template>
	<view class="wrap">
		<view v-if="!isShowAlert">
			<view class="comp" @click='show0=true'>
				<view>{{compName}}</view>
				<u-icon name="arrow-right" color="" v-if="compList.length>1"></u-icon>
				<u-picker @confirm="compChange" range-key='compName' mode="selector" v-model="show0" :range="compList"
					v-if="compList.length>1"></u-picker>
			</view>
			<view class='title'>仓库管理</view>
			<view class="dropdown">
				<view class="left" @click='show1=true'>
					<view>{{warehouseName}}</view>
					<u-icon name="arrow-right" color=""></u-icon>
					<u-picker @confirm="warehousechange" range-key='warehouseName' mode="selector" v-model="show1"
						:range="warehouseList"></u-picker>
				</view>
				<view class="right" @click='show2=true'>
					<view>{{binNumber}}仓位</view>
					<u-icon name="arrow-right" color=""></u-icon>
					<u-picker @confirm="warehouseCWchange" range-key='binNumber' mode="selector" v-model="show2"
						:range="warehouseCWList"></u-picker>
				</view>
			</view>

			<view class="cu-list grid" :class="['col-' + gridCol,gridBorder?'':'no-border']">
				<view class="cu-item" v-for="(item,index) in gridList" :key="index" @click="gridClick(item, index)"
					v-if="index<gridCol*2">
					<view :class="['cuIcon-' + item.cuIcon,'text-' + item.color]">
						<image :src="item.src" class="sign"></image>
					</view>

					<text>{{item.name}}</text>
				</view>
			</view>
		</view>
		<u-modal v-model="isShowAlert" :title-style="{fontSize: '18px',fontWeight:'500'}"
			:content-style="{fontSize: '14px',fontWeight:'400'}" confirm-color='#22C572' confirm-text='立即开通'
			title='您尚未开通ERP业务' showCancelButton='false' :content="content" @confirm="goOpenService"></u-modal>
	</view>
</template>
<script>
	import {
		mapState
	} from 'vuex';
	import helper from '@/common/helper.js';
	export default {
		components: {

		},
		data() {
			return {
				show0: false,
				show1: false,
				show2: false,
				isShowAlert: false,
				compName: '',
				compList: [],
				content: '易粮易运ERP系统包含合同管理、仓库管理、运输管理、结算管理等多个模块，可为粮企提供粮食贸易的全流程服务，平台诚邀您的加入。',
				warehouseName: '暂无仓库',
				binNumber: '暂无',
				compId: '',
				warehouseId: "",
				positionId: "",
				warehouseList: [],
				warehouseCWList: [],
				allWarehouse: [],
				gridCol: 4,
				gridBorder: false,
				personCharge:'',
				gridList: [{
						num: 0,
						name: '入库',
						// icon: 'cuIcon-apps',
						src: '../../static/img/erp/yaoqingyonghu@3x.png',
						tips: 0,
						url: '/pages/erp/warehousing/warehousing',
						show: true
					},
					{
						num: 1,
						name: '待完善入库',
						// icon: 'cuIcon-calendar',
						src: '../../static/img/erp/shougouzhijian@3x.png',
						tips: 0,
						url: '/pages/erp/improvedWrehousing/improvedWrehousing',
						show: true
					},
					{
						num: 2,
						name: '出库',
						// icon: 'cuIcon-copy',
						src: '../../static/img/erp/shougoujianjin@3x.png',
						tips: 0,
						url: '/pages/erp/exWarehousing/exWarehousing',
						show: true
					},
					{
						num: 3,
						name: '待完善出库',
						// icon: 'cuIcon-edit',
						src: '../../static/img/erp/shougouzhijian@3x.png',
						tips: 0,
						url: `/pages/erp/improvedExWaehousing/improvedExWaehousing`,
						show: true
					},
					// {
					// 	num: 4,
					// 	name: '开通业务',
					// 	// icon: 'cuIcon-edit',
					// 	src: '../../static/img/erp/shougouzhijian@3x.png',
					// 	tips: 0,
					// 	url: `/pages/erp/openService/openService`,
					// 	show: true
					// },
					// {
					// 	num: 5,
					// 	name: '联系客服',
					// 	// icon: 'cuIcon-edit',
					// 	src: '../../static/img/erp/shougouzhijian@3x.png',
					// 	tips: 0,
					// 	url: `/pages/erp/contactCustomerService/contactCustomerService`,
					// 	show: true
					// }
				],
			}
		},

		onLoad() {},
		// #ifndef MP
		onNavigationBarButtonTap(e) {
			const index = e.index;
			if (index === 0) {
				this.navTo('/pages/set/set');
			} else if (index === 1) {
				// #ifdef APP-PLUS
				const pages = getCurrentPages();
				const page = pages[pages.length - 1];
				const currentWebview = page.$getAppWebview();
				currentWebview.hideTitleNViewButtonRedDot({
					index
				});
				// #endif
				uni.navigateTo({
					url: '/pages/notice/notice'
				})
			}
		},
		// #endif
		computed: {
			...mapState(['hasLogin', 'userInfo']),
		},
		onShow() {
			this.$api.doRequest('get', '/commonUser/api/checkSession').then(res => {
				console.log("checkSession", res)
				if (res.data.data == "INVALID") {
					uni.showModal({
						title: '登录提示',
						content: 'Session过期需要重新登录，是否立即登录？',
						showCancel: true,
						confirmText: '登录',
						success: (e) => {
							if (e.confirm) {
								uni.navigateTo({
									url: '/pages/public/login'
								})
							}
						},
						fail: () => {},
						complete: () => {}
					})
				}
			})
			this.$api.doRequest('get', '/openServiceInfo/selectCommonCompany', {
				phone: this.userInfo.phone
			}).then(res => {
				if (res.data.code == 200) {
					if (res.data.data.length == 0) {
						this.isShowAlert = true
					} else {
						this.isShowAlert = false
					}
					this.compList = res.data.data
					this.compName = res.data.data[0].compName
					this.init(res.data.data[0].compId)
				}
			})
		},
		methods: {
			getCompWarehouse() {},
			goOpenService() {
				if (!this.hasLogin) {
					url = '/pages/public/login';
				}
				uni.navigateTo({
					url: '/pages/erp/openService/openService'
				})
			},
			/**
			 * 统一跳转接口,拦截未登录路由
			 * navigator标签现在默认没有转场动画，所以用view
			 */
			navTo(url) {
				if (!this.hasLogin) {
					url = '/pages/public/login';
				}
				uni.navigateTo({
					url
				})
			},
			gridClick(item, index) {
				var that = this
				if (!this.hasLogin) {
					uni.showModal({
						title: '登录提示',
						content: '您尚未登录，是否立即登录？',
						showCancel: true,
						confirmText: '登录',
						success: (e) => {
							if (e.confirm) {
								uni.navigateTo({
									url: '/pages/public/login'
								})
							}
						},
						fail: () => {},
						complete: () => {}
					})
				} else {
					if (item.url) {
						helper.erpWarehouse = {
							warehouseName: this.warehouseName,
							binNumber: this.binNumber,
							compId: this.compId,
							warehouseId: this.warehouseId,
							baseId: this.baseId,
							allWarehouse: this.allWarehouse,
							warehouseCWList: this.warehouseCWList,
							positionId: this.positionId,
							personCharge:this.personCharge
						}
						uni.navigateTo({
							url: item.url
						})
					}
				}
			},
			confirmWarehouse() {},
			confirmPositon() {},
			compChange(e) {
				this.compName = this.compList[e[0]].compName
				this.init(this.compList[e[0]].compId)
			},
			init(compId) {
				console.log(this.userInfo)
				this.$api.doRequest('get', '/warehouseBaseInfo/selectWarehouseSelf', {
					compId: compId,
				}).then(res => {
					if (res.data.data.length != 0) {
						console.log('res', res.data.data)
						let _showData = uni.getStorageSync("erpSelectWarehous")
						let _showCWData = uni.getStorageSync("erpSelectWarehousCW")
						this.allWarehouse = res.data.data
						this.warehouseList = res.data.data;
						if (_showData) {
							this.warehouseName = _showData.warehouseName
							this.compId = _showData.compId
							this.agent = _showData.agent
							this.binNumber = _showCWData.binNumber
							this.warehouseCWList = _showData.positionInfos
							this.warehouseId = _showData.id
							this.baseId = _showCWData.baseId
							this.positionId = _showCWData.id
							this.personCharge = _showData.personCharge
						} else {
							this.allWarehouse = res.data.data
							this.warehouseName = res.data.data[0].warehouseName
							this.warehouseList = res.data.data;
							this.compId = res.data.data[0].compId
							this.agent = res.data.data[0].agent
							this.binNumber = res.data.data[0].positionInfos[0].binNumber
							this.warehouseCWList = res.data.data[0].positionInfos
							this.warehouseId = res.data.data[0].id
							this.baseId = res.data.data[0].positionInfos[0].baseId
							this.positionId = res.data.data[0].positionInfos[0].id
							this.personCharge = res.data.data[0].personCharge
						}

					} else {
						this.warehouseList = []
						this.warehouseCWList = []
						this.warehouseName = '暂无仓库'
						this.binNumber = '暂无'
					}
				})
			},
			makeBinNumber() {

			},
			warehousechange(e) {
				this.warehouseName = this.warehouseList[e[0]].warehouseName
				this.compId = this.warehouseList[e[0]].compId
				this.agent = this.warehouseList[e[0]].agent
				this.warehouseId = this.warehouseList[e[0]].id
				this.warehouseCWList = this.warehouseList[e[0]].positionInfos
				uni.setStorageSync('erpSelectWarehous', this.warehouseList[e[0]])
				this.warehouseCWchange([0])
			},
			warehouseCWchange(e) {
				this.binNumber = this.warehouseCWList[e[0]].binNumber
				this.baseId = this.warehouseCWList[e[0]].baseId
				this.positionId = this.warehouseCWList[e[0]].id
				uni.setStorageSync('erpSelectWarehousCW', this.warehouseCWList[e[0]])
			}

		}
	}
</script>
<style lang='scss' scoped>
	page {
		background: #F5F6FA;
	}

	.container {
		padding-top: 85px;
		padding-top: 35px;
		background-color: #F5F6FA;
		position: relative;
		width: 100vw;
		height: 100vh;
		overflow: hidden;
		background: url('~@/static/img/login/bg_slices/bg@3x.png');
		background-size: 100% 100%;
		margin: 0 auto;
	}

	.wrap {
		background: #fff;
		margin: 10px;
		border-radius: 10px;
		padding: 10px;

		.title {
			font-size: 16px;
			font-weight: 700;
		}
	}

	.cu-list {
		/* height: 483rpx;
		overflow-y: scroll; */
		background: transparent;
	}

	.cu-list.grid.no-border {
		padding: 0;
	}

	.grid {
		display: flex;
		align-items: center;
		flex-wrap: wrap;

		/* border-top: 2upx solid rgba(172,172,172,.2); */
		.grid-item-3 {
			box-sizing: border-box;
			width: calc(100% / 3);
			border-bottom: 2upx solid rgba(172, 172, 172, .2);
			border-right: 2upx solid rgba(172, 172, 172, .2);
			text-align: center;
			padding: 40upx 0;
			position: relative;

			/* view{
				font-size: $font-sm;
				margin-top: 16upx;
				color: $font-color-dark;
			} */
			.grid_icon {
				font-size: 48upx;
				margin-bottom: 18upx;
				color: #fa436a;
			}

			.tip_text {
				display: block;
				padding: 4upx 8upx;
				text-align: center;
				border-radius: 36upx;
				font-size: 24upx;
				background-color: #fa436a;
				color: rgba(255, 255, 255, 1);
				position: absolute;
				right: 6upx;
				top: 6upx;
			}
		}

		.grid-item-3:nth-child(3n + 3),
		.grid-item-4:nth-child(4n + 4) {
			border-right: none;
		}
	}

	.sign {
		width: 40px;
		height: 40px;
		top: 4px;
		margin-right: 6px;
	}

	.dropdown {
		display: flex;
		margin: 20rpx 0;
		justify-content: space-between;
		border-bottom: 1px solid #EEEEEE;
		padding-bottom: 20rpx;

		.left,
		.right {
			display: flex;
			align-items: center;
		}
	}

	.comp {
		display: flex;
		font-size: 36rpx;
		font-weight: 500;
		color: #333333;
		margin-bottom: 20rpx;
		padding-bottom: 20rpx;
		font-weight: 700;
		border-bottom: 1px solid #EEEEEE;
	}

	.-model__title {
		background: red;
		color: red;
	}
</style>
