<template>
	<view class="untieAdd">
		<view class="container">
			<view class="ul">
				<view class="li">
					<p>船名：</p>
					<text>{{shipInfo.voyageScheduleDataList[0].spmName}}</text>
				</view>
				<view class="li">
					<p><text class="required" v-if="obj.state != 'look'">*</text>船舶净吨：</p>
					<uni-number-box v-model="zlShip" v-if="obj.state != 'look'" />
					<text v-else>{{zlShip}}</text>
				</view>
				<view class="li">
					<p><text class="required" v-if="obj.state != 'look'">*</text>航次：</p>
					<uni-data-select v-model="hcValue" :localdata="hcList" @change="hcChange"
						v-if="obj.state != 'look'"></uni-data-select>
					<text v-else>{{hcTextValue}}</text>
				</view>
				<view class="li">
					<p>进出口：</p>
					<text>{{shipInfo.voyageScheduleDataList[0].importExportFlagName}}</text>
				</view>
				<view class="li">
					<p>贸易类型：</p>
					<text>{{shipInfo.voyageScheduleDataList[0].tradeTypeName}}</text>
				</view>
				<view class="li">
					<p><text class="required" v-if="obj.state != 'look'">*</text>泊位：</p>
					<uni-data-select v-model="bwValue" :localdata="bwList" @change="bwChange"
						v-if="obj.state != 'look'"></uni-data-select>
					<text v-else>{{bwTextValue}}</text>
				</view>
				<view class="li">
					<p><text class="required" v-if="obj.state != 'look'">*</text>系缆时间：</p>
					<uni-datetime-picker v-model="xlTime" @change="changeLog1" v-if="obj.state != 'look'" />
					<text v-else>{{xlTime}}</text>
				</view>
				<view class="li">
					<p>解缆时间：</p>
					<uni-datetime-picker v-model="jlTime" @change="changeLog2" v-if="obj.state != 'look'" />
					<text v-else>{{jlTime}}</text>
				</view>
				<view class="li">
					<p>移泊时间：</p>
					<uni-datetime-picker v-model="ybTime" @change="changeLog3" v-if="obj.state != 'look'" />
					<text v-else>{{ybTime}}</text>
				</view>
				<view class="li">
					<p>停泊时间：</p>
					<uni-datetime-picker v-model="tbTime" @change="changeLog4" v-if="obj.state != 'look'" />
					<text v-else>{{tbTime}}</text>
				</view>
			</view>
			<view class="btnList">
				<van-button type="default" @click="cancel">取消</van-button>
				<van-button type="danger" v-if="obj.state == 'look'" @click="del">删除</van-button>
				<van-button type="info" v-if="obj.state == 'add' || obj.state == 'edit'" @click="save">保存</van-button>
				<van-button type="info" v-if="obj.state == 'look'" @click="toGo('edit')">编辑</van-button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				shipInfo: {
					voyageScheduleDataList: [{
						spmName: "",
						importExportFlagName: "",
						tradeTypeName: ""
					}]
				},
				obj: {},
				untieRow: {},
				untieRowIndex: 0,
				// 航次下拉框
				hcValue: "",
				hcTextValue: "",
				hcList: [],
				// 船舶净吨
				zlShip: 0,
				// 泊位下拉框
				bwValue: "",
				bwTextValue: "",
				bwList: [],
				// 系缆时间
				xlTime: [''],
				// 解缆时间
				jlTime: [''],
				// 移泊时间
				ybTime: [''],
				// 停泊时间
				tbTime: [''],
			}
		},
		onLoad(options) {
			if ('params' in options) {
				// 获取传递的对象参数，使用decodeURIComponent解码，并转为对象
				this.obj = JSON.parse(decodeURIComponent(options.params))
				console.log(this.obj)
			}
		},
		mounted() {
			if (this.obj.state == "edit" || this.obj.state == "look") {
				this.getRow()
			}
			this.getShip()
		},
		methods: {
			// 获取船只信息
			getShip() {
				this.shipInfo = uni.getStorageSync('shipInfo')
				this.shipInfo.pubBerthesManageRespList.forEach((v, index) => {
					this.bwList.push({
						text: v.bthBthnm,
						value: v.bthId
					})
				})
				this.shipInfo.voyageScheduleDataList.forEach((v, index) => {
					this.hcList.push({
						text: v.vvyName,
						value: v.vvyId
					})
				})
			},
			// 获取当前行信息
			getRow() {
				this.untieRow = uni.getStorageSync('untieRow');
				this.untieRowIndex = uni.getStorageSync('untieRowIndex');
				console.log(this.untieRow)
				console.log(this.untieRowIndex)
				this.hcValue = this.untieRow.vvyId
				this.hcTextValue = this.untieRow.vvyName
				this.zlShip = this.untieRow.zlShip
				this.bwValue = this.untieRow.bthId
				this.bwTextValue = this.untieRow.bthIdName
				this.xlTime = this.untieRow.xlTime
				this.jlTime = this.untieRow.jlTime
				this.ybTime = this.untieRow.ybTime
				this.tbTime = this.untieRow.tbTime
			},
			// 航次下拉
			hcChange(e) {
				this.hcValue = e;
				this.hcList.forEach(v => {
					if (v.value == e) {
						this.hcTextValue = v.text
					}
				})
			},
			// 泊位下拉
			bwChange(e) {
				this.bwValue = e;
				this.bwList.forEach(v => {
					if (v.value == e) {
						this.bwTextValue = v.text
					}
				})
			},
			// 系缆时间
			changeLog1(e) {
				this.xlTime = e.slice(0, e.length - 3)
			},
			// 解缆时间
			changeLog2(e) {
				this.jlTime = e.slice(0, e.length - 3)
			},
			// 移泊时间
			changeLog3(e) {
				this.ybTime = e.slice(0, e.length - 3)
			},
			// 停泊时间
			changeLog4(e) {
				this.tbTime = e.slice(0, e.length - 3)
			},
			// 取消
			cancel() {
				uni.navigateTo({
					url: '/pages/shipWork/untieCord'
				})
			},
			// 删除
			del() {
				let addUntieArr = uni.getStorageSync('addUntieArr')
				addUntieArr.splice(this.untieRowIndex, 1)
				uni.setStorageSync('addUntieArr', addUntieArr);
				uni.navigateTo({
					url: '/pages/shipWork/untieCord'
				})
			},
			// 保存
			save() {
				let addUntieArr = uni.getStorageSync('addUntieArr')
				let addUntieObj = {
					zlShip: this.zlShip,
					vvyId: this.hcValue,
					vvyName: this.hcTextValue,
					bthId: this.bwValue,
					bthIdName: this.bwTextValue,
					tradeTypeName: this.shipInfo.voyageScheduleDataList[0].tradeTypeName,
					importExportFlagName: this.shipInfo.voyageScheduleDataList[0].importExportFlagName,
					spmName: this.shipInfo.voyageScheduleDataList[0].spmName,
					xlTime: this.xlTime,
					jlTime: this.jlTime,
					ybTime: this.ybTime,
					tbTime: this.tbTime
				}

				if (this.obj.state == "edit") {
					addUntieArr[this.untieRowIndex] = addUntieObj;
					uni.setStorageSync('addUntieArr', addUntieArr);
				} else if (this.obj.state == "add") {
					if (addUntieArr != "") {
						addUntieArr.push(addUntieObj)
						uni.setStorageSync('addUntieArr', addUntieArr);
					} else {
						addUntieArr = []
						addUntieArr.push(addUntieObj)
						uni.setStorageSync('addUntieArr', addUntieArr);
					}
				}
				uni.navigateTo({
					url: '/pages/shipWork/untieCord'
				})
			},
			// 编辑
			toGo(state) {
				this.obj.state = state;
				const params = encodeURIComponent(JSON.stringify(this.obj));
				uni.navigateTo({
					url: `/pages/shipWork/untieAdd?params=${params}`
				})
			}
		}
	}
</script>

<style lang="less" scoped>
	.untieAdd {
		.container {
			padding: 30px 20px;

			.ul {
				display: flex;
				flex-wrap: wrap;
				justify-content: space-between;

				.li {
					width: 44%;
					border-top: 1px solid #ccc;
					border-bottom: 1px solid #ccc;
					display: flex;
					padding: 10px 20px;
					line-height: 35px;
					margin-bottom: 20px;

					.required {
						color: red;
						margin-right: 5px;
					}

					p {
						min-width: 85px;
						text-align: right;
					}

					/deep/.uni-numbox {
						border: 1px solid #ccc;
						box-sizing: border-box;
					}

					/deep/.uni-numbox-btns {
						border-right: 1px solid #ccc;
						border-left: 1px solid #ccc;
						padding: 0 14px;
						box-sizing: border-box;
					}

					/deep/.uni-numbox__value {
						width: 100px;
						height: 35px;
						line-height: 35px;
						background-color: #fff !important;
					}
				}
			}

			.btnList {
				display: flex;
				justify-content: center;

				/deep/ .van-button {
					margin: 30px 20px;
					width: 120px;
					height: 50px;
				}
			}
		}
	}
</style>