<template>
	<view class="app">
		<head-info :navIndex="5"></head-info>
		<view class="container">
			<view class="content">
				<view class="form">
					<view class="end">
						<superwei-combox class="input" :candidates="tradeList" :isJSON="true" keyName="name"
							placeholder="请选择贸易类型" v-model="tradeName" @select="tradeSelect"></superwei-combox>
						<superwei-combox class="input" :candidates="importExportList" :isJSON="true" keyName="name"
							placeholder="请选择进出口" v-model="importExport" @select="importExportSelect"></superwei-combox>
						<superwei-combox class="input" :candidates="shipList" :isJSON="true" keyName="vvyShip"
							placeholder="请选择船名/航次" v-model="vvyShip" @select="shipSelect"></superwei-combox>
						<button class="btn" @click="onSearch">搜索</button>
					</view>
				</view>
				<view class="itemList">
					<template v-if="itemList.length > 0">
						<view v-for="(item, index) in itemList" :key="index" class="item">
							<view @click="toDetails(item)">
								<view class="title">
									<view class="titleft">
										<image src="../../static/images/ship.png" mode="widthFix"></image>
										<view class="name">
											{{item.spmName}}
										</view>
									</view>
									<view class="status">
										<p v-if="item.handoverStatus==0" class="dfs">未完成</p>
										<p v-if="item.handoverStatus==1" class="zyz">进行中</p>
										<p v-if="item.handoverStatus==2" class="ywc">已完成</p>
									</view>
								</view>
								<view class="row">
									<view class="nitem">
										航次: <text>{{item.vvyName}}</text>
									</view>
									<view class="nitem">
										贸易类型: <text>{{item.tradeType}}</text>
									</view>
								</view>
								<view class="row">
									<view class="nitem">
										进出口: <text>{{item.importExportFlagName}}</text>
									</view>
									<view class="nitem">
										完工时间: <text>{{item.actualFinishTimeFmt}}</text>
									</view>
								</view>
							</view>
						</view>
						<template v-if="isMore">
							<uni-load-more iconType="circle" status="loading" style="width: 100%;" />
						</template>
					</template>
					<o-empty v-else height="70vh" bg="#f5f6fa" />
				</view>
				<!-- <view class="pageBox" v-if="itemList.length > 0">
					<uni-pagination :show-icon="true" :total="total" :pageSize="pageSize" :current="current"
						@change="changePage" />
				</view> -->
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 登录信息
				loginObj: {},
				// 港区信息
				portObj: {},
				// 进出口
				importExport: "出口",
				importExportList: [{
						value: "E",
						name: "出口"
					},
					{
						value: "I",
						name: "进口"
					}
				],
				// 内外贸
				tradeName: "外贸",
				tradeList: [{
						value: "1",
						name: "外贸"
					},
					{
						value: "2",
						name: "内贸"
					}
				],
				// 船名航次
				vvyShip: "",
				vvyId: "",
				shipId: "",
				shipName: '',
				shipList: [],

				itemList: [],

				// 分页
				total: 0,
				pageSize: 12,
				current: 1,

				isMore: false,
			}
		},
		onBackPress(options) {
			// 触发返回就会调用此方法，这里实现的是禁用物理返回，顶部导航栏的自定义返回 uni.navigateBack 仍可使用
			if (options.from == 'backbutton') {
				return true;
			} else if (options.from == 'navigateBack') {
				return false;
			}
		},
		onReachBottom() {
			this.current++
			this.initData()
			this.isMore = true
		},
		mounted() {
			this.loginObj = uni.getStorageSync('loginObj')
			this.portObj = uni.getStorageSync('portObj')
			this.getShip()
			this.initData()
		},
		methods: {
			// 切换贸易类型
			tradeSelect(e) {
				this.tradeName = e.name
				this.shipId = ""
				this.shipName = ""
				this.vvyId = ""
				this.vvyShip = ""
				this.getShip()
			},
			// 切换进出口
			importExportSelect(e) {
				this.importExport = e.name
				this.shipId = ""
				this.shipName = ""
				this.vvyId = ""
				this.vvyShip = ""
				this.getShip()
			},
			// 选择船
			shipSelect(e) {
				this.shipId = e.spmId
				this.shipName = e.vslCnname
				this.vvyId = e.vvyId
				this.vvyShip = e.vvyShip
			},
			// 获取船舶
			getShip() {
				if (this.tradeName == '外贸') {
					this.tradeType = "W"
				} else {
					this.tradeType = "N"
				}
				let ieType = ""
				if (this.importExport == '出口') {
					ieType = "E"
				} else {
					ieType = "I"
				}
				let key = ""
				let spmId = ""
				uni.request({
					url: `${this.$local}/api/shipInstructions/queryByKey?ieType=${ieType}&key=${ieType}&pamId=${this.portObj.portId}&spmId=${spmId}&tradeType=${this.tradeType}`,
					header: {
						'Content-Type': 'application/json', //自定义请求头信息
						'Authorization': `Bearer ${this.loginObj.access_token}`
					},
					method: 'GET', //请求方式，必须为大写
					success: (res) => {
						if (res.data.status == "200") {
							this.shipList = res.data.data
							this.shipList.forEach(v => {
								let vvyShip = `${v.vslCnname}/${v.vvyName}`
								this.$set(v, "vvyShip", vvyShip)
							})
						}
					}
				})
			},
			// 点击搜索
			onSearch() {
				this.current = 1
				this.itemList = []
				this.initData()
			},
			// 获取船只
			initData() {
				let importExportFlag = ""
				if (this.importExport == '出口') {
					importExportFlag = "E"
				} else {
					importExportFlag = "I"
				}
				let tradType = ""
				if (this.tradeName == '外贸') {
					tradType = "W"
				} else {
					tradType = "N"
				}
				uni.request({
					url: `${this.$local}/shp/iEDeliverySlipController/page?pamId=${this.portObj.portId}&vvyId=${this.vvyId}&size=${this.pageSize}&current=${this.current}&importExportFlag=${importExportFlag}&tradType=${tradType}`,
					header: {
						'Content-Type': 'application/json', //自定义请求头信息
						'Authorization': `Bearer ${this.loginObj.access_token}`
					},
					method: 'GET', //请求方式，必须为大写
					success: (res) => {
						this.isMore = false
						this.total = res.data.data.total
						this.itemList.push(...res.data.data.records)
						console.log(this.itemList)
					}
				})
			},
			// 点击分页
			changePage(e) {
				this.current = e.current;
				this.initData()
			},
			// 点击进入详情
			toDetails(item) {
				let obj = {
					vvyId: item.vvyId,
				}
				const params = encodeURIComponent(JSON.stringify(obj));
				uni.navigateTo({
					url: '/pages/receipt/details?params=' + params
				})
			},
		}
	}
</script>

<style lang="less" scoped>
	.content {
		padding: 20px;
		min-height: calc(100vh - 68px - 40px);
		margin-top: 68px;

		.form {
			display: flex;

			.end {
				display: flex;
				justify-content: space-between;

				.input {
					width: 200px;
					height: 35px;
					line-height: 35px;
					padding-left: 10px;
					margin-right: 15px;
				}

				.btn {
					height: 35px;
					line-height: 35px;
					margin-left: 0;
					font-size: 16px;
					color: #fff;
					background-color: #0067CF;
					margin-right: 10px;
				}
			}

		}

		.itemList {
			width: 100%;
			display: flex;
			justify-content: flex-start;
			flex-wrap: wrap;
			position: relative;
			margin-top: 15px;
			padding-bottom: 50px;

			/deep/.o-empty {
				width: 100%;
				margin-top: 15px;
			}

			.item {
				width: 32.2%;
				background-color: #fff;
				border-radius: 8px;
				padding: 16px;
				position: relative;
				margin-bottom: 16px;
				margin-right: 16px;

				.title {
					display: flex;
					justify-content: space-between;
					margin-bottom: 24px;

					.titleft {
						display: flex;
					}

					image {
						width: 32px;
						height: 32px;
						margin-right: 12px;
					}

					.name {
						margin-top: 5px;
						font-size: 16px;
						color: #23262E;
						font-weight: bold;
					}
				}

				.row {
					display: flex;
					justify-content: space-between;
					font-size: 16px;
					margin-bottom: 12px;

					.nitem {
						width: 49%;
						overflow: hidden;
						text-overflow: ellipsis;
						white-space: nowrap;
						color: #999;

						text {
							color: #23262e;
						}
					}
				}

				.status {
					font-size: 16px;
					position: absolute;
					right: 0;
					top: 23px;

					p {
						padding: 5px;
						border-radius: 13px 0 0 13px;
					}

					.dfs {
						color: #0067CF;
						background: #F1F8FF;
					}

					.zyz {
						color: #04B578;
						background: #E8FFF7;
					}

					.ywc {
						color: #666666;
						background: #F7F7F7;
					}
				}
			}

			.item:nth-child(3n) {
				margin-right: 0;
			}
		}

		.pageBox {
			margin-top: 20px;
		}
	}
</style>