<template>
	<view class="">
		<view class="content1">
			<view>{{dataList.warehouseName}}</view>
			<view class="row2">
				<view class='locationwrap'>
					<image class='location' src="../../../static/img/liangmai/icon_ditu@2x.png" mode=""></image>
					{{warehouseNameAddress}}
				</view>
			</view>
			<view class="row3">
				<view>{{startDate}}创建（有效期{{dataList.validityDate}}）</view>
			</view>
		</view>
		<view class="content2">
			<view class="title">送货信息</view>
			<view class="wrap wrap1" v-for="(item,index) in dataList.shippingInformationList" :key='index'>
				<view class="row1">
					<view class="left">
						<view>货主{{index+1}}-{{item.customerName}}</view>
						<view @click="reduceBtn(index)">--</view>
					</view>
					<view class="right" @click='showCustomerName(item)'>
						<view>{{item.customerName}}</view>
						<u-icon name="arrow-right" color=""></u-icon>
						<u-picker @confirm="nameConfirm" range-key='customerName' mode="selector"
							v-model="item.showCustomerName" :range="nameList"></u-picker>
					</view>
				</view>
				<view class="row2">
					<view class="left">
						供应商（送粮人）
					</view>
					<view class="right">{{item.supplier}}</view>
				</view>
				<view class="row2" v-for="(item1,index1) in item.carNumberList" :key='index1'>
					<view class="left">
						车牌号-{{index1+1}}
					</view>
					<u-input v-model="item1.carNo" placeholder="输入7为车牌号" />
					<view>
						<view @click="addCarNumber(item.carNumberList)">+</view>
						<view @click="delCarNumber(item.carNumberList,index1)">-</view>
					</view>
				</view>


			</view>
			<u-button @click='addGoodPeople'>增加货主</u-button>
		</view>
		<u-button @click='submit'>提交</u-button>
		<u-modal v-model="isShowAlert" :title-style="{fontSize: '18px',fontWeight:'500'}"
			:content-style="{fontSize: '14px',fontWeight:'400'}" confirm-color='#22C572' confirm-text='确定' title='登录提示'
			showCancelButton='false' :content="content" @confirm="alertBtn" @cancel="cancelClick"></u-modal>
		<u-modal v-model="isShowAlert1" :title-style="{fontSize: '18px',fontWeight:'500'}"
			:content-style="{fontSize: '14px',fontWeight:'400'}" confirm-color='#22C572' confirm-text='确定' title='登录提示'
			showCancelButton='false' :content="content1" @confirm="alertBtn" @cancel="cancelClick"></u-modal>

	</view>
</template>
<script>
	import {
		mapState
	} from 'vuex';

	export default {
		components: {

		},
		data() {
			return {
				isShowAlert: false,
				isShowAlert1: false,
				content: '当前登入信息验证失败，是否重新登录？',
				content1: '确定提交送粮信息？',
				// show: false,
				show1: false,
				cargoOwner: [{
					name: '123'
				}],
				nameList: [],
				warehouseNameAddress: '',
				startDate: "",
				dataList: {
					warehouseName: "",
					validityDate: '',
					commonId: uni.getStorageSync("userInfo").id,
					shippingInformationList: [{
						shipperName: '',
						supplier: '',
						carNo: '',
						carNumberList: [{
							carNo: ''
						}]
					}]

				}
			}
		},

		onLoad(options) {
			debugger
			// let _pageToData = JSON.parse(options.itemValue)
			// this.dataList.warehouseName = _pageToData.warehouseName
			// this.startDate = this.getTime()[0]
			// this.dataList.validityDate = this.getTime()[1]
			// this.warehouseNameAddress = _pageToData.warehousePrivate + _pageToData.warehouseCity + _pageToData
			// 	.warehouseArea + _pageToData.detailedAddress
		},
		// #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") {
					this.isShowAlert = true;
				}
				this.getCustomer()
			})
			console.log("hasLogin", this.hasLogin)
		},
		methods: {
			showCustomerName(value) {debugger
			for(let i = 0;i<this.dataList.shippingInformationList.length;i++){
				this.dataList.shippingInformationList[i].showCustomerName = false
			}
				value.showCustomerName = true
			},
			getCustomer() {
				let _data = {
					commonId: uni.getStorageSync("userInfo").id,
				}
				this.$api.doRequest('get', '/identityAuthenticationInfo/identityAuthenticationInfoUser', _data).then(
					res => {
						if (res.data.code == 200) {
							// debugger
							this.nameList = res.data.data
							// for (let i = 0; i < this.nameList.length; i++) {
							// 	this.nameList[i].showCustomerName = false
							// }
							this.customerName = res.data.data[0].customerName
						}
					})
			},
			getTime() {
				let _startTime = new Date().format("yyyy-MM-dd");
				var _endTime = new Date();
				_endTime.setDate(_endTime.getDate() + 2);
				_endTime = _endTime.format("yyyy-MM-dd");
				return [_startTime, _endTime]
			},
			reduceBtn(index) {
				if (this.dataList.shippingInformationList.length > 1) {
					this.dataList.shippingInformationList.splice(index, 1)
					this.$forceUpdate()
				}
			},
			submit() {
				this.isShowAlert1 = true
			},
			nameConfirm() {},
			/**
			 * 统一跳转接口,拦截未登录路由
			 * navigator标签现在默认没有转场动画，所以用view
			 */
			navTo(url) {
				if (!this.hasLogin) {
					url = '/pages/public/login';
				}
				uni.navigateTo({
					url
				})
			},
			alertBtn() {
				for (let i = 0; i < this.dataList.shippingInformationList.length; i++) {
					delete this.dataList.shippingInformationList[i].carNumberList
				}
				this.$api.doRequest('post', '/grainDeliveryRegistration/api/insertInfo', this.dataList).then(res => {
					if (res.data.code == 200) {

					}
				})
			},
			cancelClick() {
				this.isShowAlert = false
				this.isShowAlert1 = false
			},
			addGoodPeople() {
				this.dataList.shippingInformationList.push({
					shipperName: '',
					supplier: '',
					carNo: '',
					carNumberList: [{
						carNo: ''
					}]
				})
			},
			addCarNumber(val) {
				val.push({
					carNo: ''
				})
			},
			delCarNumber(val, index) {
				if (val.length > 1) {
					val.splice(index, 1)
					this.$forceUpdate()
				}
			}
		}
	}
</script>
<style lang='scss' scoped>
	page {
		background: #F5F6FA;
	}

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

	.location {
		width: 32rpx;
		height: 32rpx;
		margin-right: 10rpx;
	}

	.locationwrap {
		font-size: 16px;
		display: flex;
		align-items: center;
	}
</style>
