<template>
	<view class="wrap">
		<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>
		<u-modal v-model="isShowAlert" confirm-color='#22C572' confirm-text='立即开通' title='您尚未开通ERP业务'
			:content="content"></u-modal>
	</view>
</template>
<script>
	import {
		mapState
	} from 'vuex';
import helper from '@/common/helper.js'; 
	export default {
		components: {

		},
		data() {
			return {
				show1: false,
				show2: false,
				isShowAlert: false,
				content: '易粮易运ERP系统包含合同管理、仓库管理、运输管理、结算管理等多个模块，可为粮企提供粮食贸易的全流程服务，平台诚邀您的加入。',
				warehouseName: '暂无仓库',
				binNumber: '暂无',
				compId:'',
				warehouseList: [],
				warehouseCWList:[],
				gridCol: 4,
				gridBorder: false,
				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() {
			this.init()
		},
		// #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: () => {}
					})
				}
			})
		},
		methods: {
			/**
			 * 统一跳转接口,拦截未登录路由
			 * 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,
							agent:this.agent
							
						}
						uni.navigateTo({
							url: item.url
						})
					}
				}
			},
			confirmWarehouse() {},
			confirmPositon() {},
			init() {
				console.log(this.userInfo)
				this.$api.doRequest('get', '/warehouseBaseInfo/selectWarehouseSelf', {
					compId: '',
				}).then(res => {
					if (res.data.data) {
						console.log('res',res.data.data)
						if (res.data.data.length == 0) {
							this.isShowAlert = true
						} else {
							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
							console.log('this.warehouseCWList',this.warehouseCWList)
							// this.makeBinNumber()
						}
					}
				})
			},
			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.warehouseCWList = this.warehouseList[e[0]].positionInfos
				console.log(e)
			},
			warehouseCWchange(e){
				this.binNumber = this.warehouseCWList[e[0]].binNumber
			}

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

	.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;

		.left,
		.right {
			display: flex;
		}
	}
</style>
