<template>
	<view class="app-content">
		<!-- <img src="../../static/beijing.png" alt="" class="tuku"> -->
		<Navs :navname.sync="navname" @fall="fall" :index='index'></Navs>
		<view class="content">
			<view class="content_top">
				<view class="craid" v-if="flightDetail">
					<view class="craid_data">
						<p>{{flightDetail.carrierFlights}}</p>
						<span>{{flightDetail.carrierFlightsDate}}</span>
					</view>
					<view class="craid_data">
						<p>{{flightDetail.outAirport}}</p>
						<span>{{flightDetail.scheduleTakeOffTime}}</span>
					</view>
					<view class="aircraft"></view>
					<view class="craid_data">
						<p>{{flightDetail.landAirport}}</p>
						<span>{{flightDetail.scheduleLandInTime}}</span>
					</view>
				</view>
				<view class="craids" v-if="!flightDetail">
					<p>暂无航班信息</p>
				</view>
				<view class="scanning">
					<view :class="getFooterClass">
						<input class="input" v-model.trim="stratvalue" type="text"
							value=""
							placeholder-style="font-size:.8125rem;color:rgb(189, 214, 231);display:flex;justify-content: start;"
							placeholder="请输入操作位置" />
						<view class="log_l"></view>
						<view class="log"></view>
					</view>
					<view :class="getFooterClasser" v-if="navname == '装车'">
						<input class="input" v-model.trim="container_ID" type="text"
							value=""
							placeholder-style="font-size:.8125rem;color:rgb(189, 214, 231);display:flex;justify-content: start;"
							placeholder="请输入容器编号" />
						<view class="log_l"></view>
						<view class="log"></view>
					</view>
					<view :class="getlooterClass">
						<input class="input" v-model.trim="currentLuggageNum" disabled="true" @tap="baggageEntry" type="text"
							value=""
							placeholder-style="font-size:.8125rem;color:rgb(189, 214, 231);display:flex;justify-content: start;"
							placeholder="请输入行李号" />
						<view class="log_l"></view>
						<view class="log"></view>
					</view>
				</view>
			</view>
			<view class="content_bootom" :style="{top:dataheight}">
				<view class="choice" @touchmove="movetran">
					<view :class="action === true ?'handle':'handles'" @tap="clicker(true)">已处理</view>
					<view :class="action !== true ?'handle':'handles'" @tap="clicker(false)">未处理</view>
				</view>
				<scroll-view scroll-y="true" class="scroll-Y">
					<view v-if="action === true" :class="item.types ? 'ups':'up'" v-for="(item, index) in listdate1" :key="index"
						@touchstart="start" @touchend="(e)=>end(e,item)">
						<view class="list"  @tap="toDetail(item.luggageNum)">行李编号：{{ item.luggageNum }}</view>
						<view :class="item.abnormalState!=null?'error':'normal'">{{item.abnormalState!=null?item.abnormalName:"正常"}}</view>
						<view class="down_up" v-if="pagename == 'LOAD'" @tap="up(item,index)"></view>
						<view class="delete" v-if="pagename == 'LOAD'">
							<view class="log" @click="()=>up(item,index)"></view>
						</view>
					</view>
					<view v-if="action !== true" :class="item.types ? 'ups':'up'" v-for="(item, index) in listdate2" :key="index">
						<view class="list"  @tap="toDetail(item.luggageNum)">行李编号：{{ item.luggageNum }}</view>
						<view :class="item.abnormalState!=null?'error':'normal'">{{item.abnormalState!=null?item.abnormalName:"正常"}}</view>
						<!-- <view class="down_up" @tap="up"></view> -->
					</view>
				</scroll-view>
				<view class="but" v-if="!action" @tap="clickOrder">一键处理</view>
			</view>
		</view>
		<!-- 提示信息和下拉弹窗 -->
		<mask-model :headername="headername" :typeShow="typeShow" :tabname="tabname" ref="showMask" btnType="1"
			@confirm="confirm" @next="next" @load="load" @cancel="cancel" titleColoe="#666666" cancelColor="#666666" confirmColor="#007AFF"
			:maskTitle="maskTitle"></mask-model>
		<!-- 一键弹窗 -->
		<mask-model :headername="headername" :typeShow="typeShow" ref="showMask2" btnType="1"
			@confirm="confirm2" @cancel="cancel" titleColoe="#666666" cancelColor="#666666" confirmColor="#007AFF"
			:maskTitle="maskTitle2"></mask-model>
		<!-- 航班开始操作弹窗 -->
		<mask-model :headername="headername" :typeShow="typeShow" ref="showMask3" btnType="1"
			@confirm="confirm3" @cancel="cancel3" titleColoe="#666666" cancelColor="#666666" confirmColor="#007AFF"
			:maskTitle="maskTitle3"></mask-model>
	</view>
</template>

<script>
	import Navs from '../../components/header/navs.vue'
	import maskModel from '../../components/mask-model/mask-model.vue'
	import {
		Format
	} from '@/components/utils/validate'
	export default {
		data() {
			return {
				isShow: true,
				dataheight: '20.75rem',
				scrollTop: 0,
				input: '',
				arr: [],
				navname: '',
				action: true,
				listdate:[],
				listdate1: [],
				loadNumber:0,
				listdate2: [],
				startData: {
					clientX: 0,
					clientY: 0
				},
				stratvalue: '', //开始位置
				container_ID:'',
				endvalue: '', //结束位置
				maskTitle: '该行李不属于当前航班，请选择处理方式。',
				maskTitle2:"",
				maskTitle3:"",
				tabname: '', //航班名称
				typeShow: true,
				headername: '提示信息',
				flightNO:null,
				flightDetail:{},
				index:0,
				currentLuggageNum:null,
				pagename:"",
				currentLuggage:null,
				currentIndex:null,
				abnormalList:[
					{
						"type":null,
						"name":"无"
					},
					{
						"type":"NAL",
						"name":"未被授权的装载"
					},
					{
						"type":"OFF",
						"name":"已卸载"
					},
					{
						"type":"UNS",
						"name":"未找到"
					},
					{
						"type":"OND",
						"name":"在场未装载，未被授权"
					},
					{
						"type":"ONA",
						"name":"在场未装载，已授权"
					}
				],
			}
		},
		created(option) {},
		onLoad(option) {
			this.pagename = option.item
			this.flightNO = option.flightNO
			this.getNode()
			switch (this.pagename){
				case "SORT":
					this.navname = "分拣"
					break;
				case "LOAD":
					this.navname = "装车"
					break;
				case "INFL":
					this.navname = "装机"
					break;
				case "UNLOAD":
					this.navname = "卸机"
					break;
				case "ARRIVED":
					this.navname = "到达"
					break;
				default:
					break;
			}
			if(this.flightNO){
				this.getFlightData()
				this.getluggageList()
			}
		},
		onUnload() {
			// this.$refs.showMask.show();
		},
		onShow() {
			uni.$on("scancodemsg", (data) => {
				if (data.msg.length > 9) {
					this.currentLuggageNum = data.msg
					this.getluggage(this.currentLuggageNum,true)
				}
			});
			if (this.$storage.getJson('airport')) {
				this.index = this.$storage.getJson('airport')
			}
		},
		onHide() {
			uni.$off("scancodemsg");
		},
		mounted() {
			// this.$refs.showMask.show();
			// console.log(this.stratvalue.length)
		},
		computed: {
			getFooterClass() {
				return this.stratvalue ? 'scanning_cds' : 'scanning_cd'
			},
			getlooterClass() {
				return this.currentLuggageNum ? 'scanning_cds' : 'scanning_cd'
			},
			getFooterClasser() {
				return this.container_ID ? 'scanning_cds' : 'scanning_cd'
			},
		},
		components: {
			Navs,
			maskModel: maskModel,
		},
		methods: {
			async getNode() {
				let data = {
					"serviceId": 3003,
					"dataContent": {},
					"event": "0"
				}
				await this.$http.httpPost('/openApi/query', data).then(res => {
					if (res.code == "0") {
						res.returnData.map(item => {
							if(item.nodeCode == this.pagename){
								this.stratvalue = item.nodeDescribe;
							}
						})
					}
				})
			},
			next(data){
				this.flightNO = this.currentLuggage.flightNo;
				this.getFlightData()
				this.getluggageList()
				this.$refs.showMask.hide();
			},
			load(){
				this.listdate1.unshift({
					"luggageNum" : this.currentLuggage.luggageNum
				})
				this.setLuggage()
			},
			async getFlightData() {
				let data = {
					"serviceId": 3000,
					"page": 1,
					"pageSize": 10,
					"dataContent": [{
						"carrierFlights":this.flightNO
					}],
					"event": "0"
				}
				await this.$http.httpPost('/openApi/query', data).then(res => {
					if (res.code == "0") {
						this.flightDetail = res.returnData[0]
					}
					this.flightDetail.scheduleTakeOffTime = this.flightDetail.scheduleTakeOffTime.split("T")[1]
					this.flightDetail.scheduleLandInTime = this.flightDetail.scheduleLandInTime.split("T")[1]
				})
			},
			async getluggageList(){
				let data = {
				    "serviceId": 3001,
				    "dataContent": {
				        "carrierFlights": this.flightNO,
				    },
				    "event": "0"
				}
				
				await this.$http.httpPost('/openApi/query', data).then(res => {
					console.log(res)
					this.listdate1 = []
					this.listdate2 = []
					if (res.code == "0") {
						this.listdate = res.returnData;
						res.returnData.map(item => {
							this.abnormalList.map(item2 => {
								if(item.abnormalState == item2.type){
									item.abnormalName = item2.name
								}
							})
							switch (this.pagename){
								case "SORT":
									if(item.sorting_time!=null){
										this.listdate1.push(item)
									}
									else{
										this.listdate2.push(item)
									}
									break;
								case "LOAD":
									if(item.loading_time!=null){
										this.listdate1.push(item)
									}
									else{
										this.listdate2.push(item)
									}
									break;
								case "INFL":				
									if(item.installation_time!=null){
										this.listdate1.push(item)
									}
									else{
										this.listdate2.push(item)
									}
									break;
								case "UNLOAD":
									if(item.unloadtime!=null){
										this.listdate1.push(item)
									}
									else{
										this.listdate2.push(item)
									}
									break;
								case "ARRIVED":
									if(item.arrivedtime!=null){
										this.listdate1.push(item)
									}
									else{
										this.listdate2.push(item)
									}
									break;
								default:
									break;
							}
						})
						this.loadNumber = this.listdate2.length;
						this.setLuggage()
					}
	
				})
			},
			async getluggage(num,isSend){
				let data = {
				    "serviceId": 3002,
				    "page": 1,
				    "pageSize": 10,
				    "dataContent": {
				        "luggageNum":num,//行李号
				    },
				    "event": "0"
				}
				await this.$http.httpPost('/openApi/query', data).then(res => {
					if(res.code == "0"&&res.returnData.length>0){
						this.currentLuggage = res.returnData[0];
						if(this.flightNO&&this.flightNO == res.returnData[0].flightNo&&isSend==true){
							this.setLuggage()
						}
						else if(this.flightNO&&this.flightNO != res.returnData[0].flightNo){
							this.tabname = res.returnData[0].flightNo
							this.$refs.showMask.show();
						}
						else{
							this.currentIndex = index
							this.typeShow = false
							this.maskTitle3 = '是否对航班：'+ res.returnData[0].flightNo + "行李进行"+this.navname+"操作？"
							this.headername = '提示'
							this.$refs.showMask.show();
							this.flightNO = res.returnData[0].flightNo;
						}
					}
					else{
						uni.showToast({
							icon:'none',
							title: "未查询到"+this.currentLuggageNum+"行李信息",
							duration: 5000
						});
					}
				})
			},
			setLuggage(){
				this.currentLuggage.nodeCode = this.pagename;
				this.currentLuggage.nodeName = this.navname;
				this.currentLuggage.location_code = this.stratvalue;
				this.currentLuggage.processing_time = this.datatime();
				switch (this.pagename){
					case "SORT":
						this.listdate2.map((item,index) => {
							if(item.luggageNum == this.currentLuggageNum && item.sorting_time == null){
									this.listdate2.splice(index,1)
									item.sorting_time = this.datatime();
									item.sortState = true;
									this.listdate1.unshift(item);
							}
						})
						break;
					case "LOAD":
						this.currentLuggage.container_ID = this.container_ID
						this.currentLuggage.loadNumber = this.loadNumber;
						this.listdate2.map((item,index) => {
							if(item.luggageNum == this.currentLuggageNum){
								if(item.loading_time == null){
									this.listdate2.splice(index,1)
									item.loading_time = this.datatime();
									item.loadState = true;
									this.listdate1.unshift(item);
								}
							}
						})
						break;
					case "INFL":
						this.listdate2.map((item,index) => {
							if(item.luggageNum == this.currentLuggageNum){
								if(item.installation_time == null){
									this.listdate2.splice(index,1)
									item.installation_time = this.datatime();
									this.listdate1.unshift(item);
								}
							}
						})
						break;
					case "UNLOAD":
						this.listdate2.map((item,index) => {
							if(item.luggageNum == this.currentLuggageNum){
								if(item.unloadtime == null){
									this.listdate2.splice(index,1)
									item.unloadtime = this.datatime();
									this.listdate1.unshift(item);
								}
							}
						})
						break;
					case "ARRIVED":
						this.listdate2.map((item,index) => {
							if(item.luggageNum == this.currentLuggageNum){
								if(item.arrivedtime == null){
									this.listdate2.splice(index,1)
									item.arrivedtime = this.datatime();
									this.listdate1.unshift(item);
								}
							}
						})
						break;
					default:
						break;
				}
				this.sendData()
			},
			async sendData(type){
				this.currentLuggage.sourceAirport = this.$storage.get('airportName');
				let data = {
					"serviceId": 3005,
					"dataContent": [
						this.currentLuggage
					],
					"event": "0"
				}
				await this.$http.httpPost('/openApi/query', data).then(res => {
					console.log(res)
					if (res.code == "0") {
						uni.showToast({
							icon:'none',
							title: "操作行李："+this.currentLuggageNum+"成功",
							duration: 5000
						});
					}
					else{
						uni.showToast({
							icon:'none',
							title: "操作失败",
							duration: 5000
						});
					}
				})
				
			},
			datatime() {
				let dataTime = Format("yyyy-MM-dd hh:mm:ss", new Date());
				return dataTime
			},
			//下拉
			up(item,index) {
				this.currentLuggageNum = item.luggageNum;
				this.getluggage(this.currentLuggageNum,false);
				this.currentIndex = index
				this.typeShow = false
				this.maskTitle = '是否确定要拉下该件行李？'
				this.headername = '下拉提示'
				this.$refs.showMask.show();
			},
			confirm: function(e) { //确定按钮
				this.currentLuggage.abnormalState="OFF"
				this.currentLuggage.abnormalName="拉下"
				let arr = JSON.parse(JSON.stringify(this.listdate1))
				arr[this.currentIndex]=JSON.parse(JSON.stringify(this.currentLuggage))
				this.listdate1 = arr;
				this.sendData()
			},
			confirm2: function(e) { //确定按钮
				this.listdate2.map((item,index) =>{
					this.currentLuggage = item;
					this.setLuggage();
					this.listdate1.unshift(item)
				})
				this.listdate2 = [];
			},
			confirm3: function(e) { //确定按钮
				this.getFlightData();
				this.getluggageList();
			},
			cancel3: function() { //取消按钮
				this.flightNO=null
			},
			cancel: function() { //取消按钮
				console.log('您点击了取消按钮');
			},
			toDetail(num){
				uni.navigateTo({
					// url: "/pages/detail/index?luggageNum=" + data.msg
					url: "/pages/detail/index?luggageNum="+num
				})
			},
			clickOrder() {
				this.typeShow = false
				this.maskTitle2 = '是否确定要一键'+this.navname+'全部行李？'
				this.headername = '操作提示'
				this.$refs.showMask2.show();
			},
			baggageEntry() {
				uni.navigateTo({
					url: "/pages/baggageEntry/index"
				})
			},
			locationEntry() {
				uni.navigateTo({
					url: "/pages/locationEntry/index"
				})
			},
			fall() {
				uni.navigateBack({
					delta:1
				})
			},
			clicker(e) {
				this.action = !this.action
				if(e != this.action){
					this.action = e
				}
			},
			movetran(e) {
				let arr = uni.getSystemInfoSync().windowHeight;
				this.dataheight = (e.changedTouches[0].clientY / 16) + 'rem'
				if (e.changedTouches[0].clientY / 16 > 20.75) {
					this.dataheight = 20.75 + 'rem'
				} else if (e.changedTouches[0].clientY / 16 < 7.3125) {
					this.dataheight = 6.3125 + 'rem'
				}
			},
			start(e) {
				this.startData.clientX = e.changedTouches[0].clientX;
			},
			end(e, item) {
				const subX = e.changedTouches[0].clientX - this.startData.clientX;
				if (subX > 50) {
					// console.log('右滑')
					item.types = false
				} else if (subX < -50) {
					item.types = true
					// console.log('左滑')
				}
			},
			checkMore(item) {
				console.log(item)
			}
		},
	}
</script>

<style lang="scss" scoped>
	.app-content {
		position: relative;
		background: rgb(49, 85, 99);

		>.content {
			width: 100%;
			height: calc(100vh - 5.375rem);
			position: relative;

			// overflow: hidden;
			// background: rgb(49, 85, 99);
			>.content_top {
				width: 100%;
				// background: rgb(49, 85, 99);
				padding-bottom: 1.3125rem;

				>.craid {
					// width: 100%;
					height: 5.625rem;
					padding: 0 2.5625rem 0 2.5625rem;
					display: flex;
					justify-content: space-between;
					flex-wrap: wrap;
					align-items: center;

					>.craid_data {
						display: flex;
						flex-direction: column;
						justify-content: center;
						align-items: start;

						>p {
							color: rgb(255, 255, 255);
							font-family: Noto Sans SC;
							font-size: 18px;
							font-weight: 700;
						}

						>span {
							color: rgb(255, 255, 255);
							font-family: Noto Sans SC;
							font-size: 13px;
							font-weight: 500;
						}
					}

					>.aircraft {
						width: 1.75rem;
						height: 1.75rem;
						background: url('../../static/hang.png') no-repeat;
						background-size: 100% 100%;
					}
				}
				>.craids{
					height: 5.625rem;
					padding: 0 2.5625rem 0 2.5625rem;
					display: flex;
					justify-content: center;
					align-items: center;
					>p{
						color: rgb(189, 214, 231);
						font-family: Noto Sans SC;
						font-size: 13px;
						font-weight: 500;
					}
				}
				>.scanning {
					height: 12rem;
					padding: 0 1.5rem 0 1.5rem;
					opacity: 1;
					transition: all 3s;

					.scanning_cd {
						width: 100%;
						height: 3rem;
						opacity: 1;
						transition: all 3s;
						background: rgb(59, 103, 123);
						margin-top: 1rem;
						margin-bottom: .5rem;
						border-radius: 4px;
						display: flex;
						justify-content: center;
						align-items: center;
						color: rgb(255, 255, 255);
						font-family: Noto Sans SC;
						font-size: 16px;

						>.log {
							width: .875rem;
							height: .875rem;
							background: url('../../static/saoma.png') no-repeat;
							background-size: 100% 100%;
							margin-left: 1rem;
						}

						>.log_l {
							width: .125rem;
							height: 1.3125rem;
							margin-left: 4rem;
							background: rgb(255, 255, 255);
						}
					}

					.scanning_cds {
						width: 100%;
						height: 3rem;
						opacity: 1;
						transition: all 3s;
						background: rgb(51, 161, 165);
						margin-top: 1rem;
						margin-bottom: .5rem;
						border-radius: 4px;
						display: flex;
						justify-content: center;
						align-items: center;
						color: rgb(255, 255, 255);
						font-family: Noto Sans SC;
						font-size: 16px;

						>.log {
							width: .875rem;
							height: .875rem;
							background: url('../../static/saoma.png') no-repeat;
							background-size: 100% 100%;
							margin-left: 1rem;
						}

						>.log_l {
							width: .125rem;
							height: 1.3125rem;
							margin-left: 4rem;
							background: rgb(255, 255, 255);
						}
					}
				}
			}

			>.content_bootom {
				width: 100%;
				// height: calc(100vh - 20.9375rem - 4.375rem);
				padding-bottom: 0;
				position: absolute;
				bottom: 0;
				top: 20.75rem;
				border-radius: 6px 6px 0px 0px;
				overflow: hidden;
				background: rgb(255, 255, 255);

				>.choice {
					width: 100%;
					height: 2.5rem;
					display: flex;
					background: rgb(240, 241, 243);

					>.handle {
						width: 50%;
						display: flex;
						align-items: center;
						justify-content: center;
						color: rgb(0, 0, 0);
						font-family: Noto Sans SC;
						font-size: 15px;
						font-weight: 700;
						border-radius: 6px 6px 0px 0px;
						background: rgb(255, 255, 255);
					}

					>.handles {
						width: 50%;
						display: flex;
						align-items: center;
						justify-content: center;
						color: rgb(0, 0, 0);
						font-family: Noto Sans SC;
						font-size: 15px;
						font-weight: 700;
					}
				}

				.up {
					width: 100%;
					display: flex;
					align-items: center;
					margin-left: 1rem;
					border-bottom: 1px solid rgb(238, 238, 238);

					// transform: all 3s;
					>.down_up {
						width: .875rem;
						height: .875rem;
						background: url('../../static/xia.png') no-repeat;
						background-size: 100% 100%;
					}
					
					>.error{
						width: 20%;
						color:red
					}
					>.normal{
						width: 20%;
						color:#4682b4 
					}
					>.list {
						width: 70%;
						position: relative;
						height: 3.75rem;
						display: flex;
						align-items: center;
						transform: all 3s;
						right: 0;
						color: rgb(0, 0, 0);
						font-family: Noto Sans SC;
						font-size: 13px;
						font-weight: 500;
					}

					>.delete {
						width: 3.75rem;
						height: 3.75rem;
						background: rgb(237, 101, 101);
						display: flex;
						align-items: center;
						justify-content: center;
						position: relative;
						right: -3.75rem;
						transform: all 3s;

						>.log {
							width: .875rem;
							height: .875rem;
							background: url('../../static/vector@1x.png') no-repeat;
							background-size: 100% 100%;
						}
					}
				}

				.ups {
					width: 100%;
					display: flex;
					align-items: center;
					margin-left: 1rem;
					border-bottom: 1px solid rgb(238, 238, 238);

					// transform: all 3s;
					>.down_up {
						width: .875rem;
						height: .875rem;
						position: relative;
						right: 1.875rem;
						background: url('../../static/xia.png') no-repeat;
						background-size: 100% 100%;
					}

					>.list {
						width: 97%;
						position: relative;
						height: 3.75rem;
						display: flex;
						align-items: center;
						right: 3.75rem;
						// right: 0;
						color: rgb(0, 0, 0);
						font-family: Noto Sans SC;
						font-size: 13px;
						font-weight: 500;
						transform: all 3s;
					}

					>.delete {
						width: 3.75rem;
						height: 3.75rem;
						background: rgb(237, 101, 101);
						display: flex;
						align-items: center;
						justify-content: center;
						position: relative;
						right: 1rem;
						transform: all 3s;

						// right: -3.75rem;
						>.log {
							width: .875rem;
							height: .875rem;
							background: url('../../static/vector@1x.png') no-repeat;
							background-size: 100% 100%;
						}
					}
				}

				.scroll-Y {
					// height: calc(100% - 2.5rem - 2.75rem - 2.25rem - 2.25rem);
					height: calc(100% - 2.5rem - 2.75rem);
				}

				.but {
					width: 20.5625rem;
					height: 2.5rem;
					background: linear-gradient(179.10deg, rgba(51, 161, 165, 1.00) 11.326%, rgba(59, 111, 142, 1.00) 119.032%, rgba(59, 111, 142, 1.00) 119.032%, rgba(59, 111, 142, 1.00) 119.032%);
					border-radius: .25rem;
					color: rgb(255, 255, 255);
					font-family: Noto Sans SC;
					font-size: 1rem;
					margin: 0 auto;
					display: flex;
					align-items: center;
					justify-content: center;
					margin-top: 0.25rem;
					margin-bottom: 2.25rem;
				}
			}
		}
	}
</style>