<template>
	<view class="mafiAdd">
		<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-data-select v-model="vvyId" :localdata="hcList" @change="hcChange"
						v-if="obj.state != 'look'"></uni-data-select>
					<text v-else>{{vvyName}}</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-datetime-picker v-model="workTime" type="date" :clear-icon="false" @change="changeLog"
						v-if="obj.state != 'look'" />
					<text v-else>{{workTime}}</text>
				</view>
				<view class="li">
					<p><text class="required" v-if="obj.state != 'look'">*</text>马菲板号：</p>
					<uni-easyinput v-if="obj.state != 'look'" v-model="mafiBarcode" placeholder="请输入"></uni-easyinput>
					<text v-else>{{mafiBarcode}}</text>
				</view>
				<view class="li">
					<p><text class="required" v-if="obj.state != 'look'">*</text>尺码：</p>
					<uni-number-box v-model="size" v-if="obj.state != 'look'" />
					<text v-else>{{size}}</text>
				</view>
				<view class="li">
					<p><text class="required" v-if="obj.state != 'look'">*</text>类型：</p>
					<uni-data-select v-model="lxValue" :localdata="lxList" @change="lxChange"
						v-if="obj.state != 'look'"></uni-data-select>
					<text v-else>{{lxTextValue}}</text>
				</view>
				<view class="li">
					<p><text class="required" v-if="obj.state != 'look'">*</text>装卸类型：</p>
					<uni-data-select v-model="loaderTypeId" :localdata="zxList" @change="zxChange"
						v-if="obj.state != 'look'"></uni-data-select>
					<text v-else>{{loaderType}}</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: {},
				mafiRow: {},
				mafiRowIndex: 0,
				// 航次下拉框
				vvyId: "",
				vvyName: "",
				hcList: [],
				// 作业时间
				workTime: [""],
				// 马菲板号
				mafiBarcode: "",
				// 尺码
				size: 0,
				// 类型下拉框
				lxValue: "",
				lxTextValue: "",
				lxList: [{
						value: 0,
						text: "空板"
					},
					{
						value: 1,
						text: "重板"
					},
				],
				// 装卸类型下拉框
				loaderTypeId: "",
				loaderType: "",
				zxList: [],
			}
		},
		onLoad(options) {
			if ('params' in options) {
				// 获取传递的对象参数，使用decodeURIComponent解码，并转为对象
				this.obj = JSON.parse(decodeURIComponent(options.params))
			}
		},
		mounted() {
			if (this.obj.state == 'edit' || this.obj.state == 'look') {
				this.getRow();
			}
			this.getShip()
		},
		methods: {
			// 获取船只信息
			getShip() {
				this.shipInfo = uni.getStorageSync('shipInfo')
				// 航次数据
				this.shipInfo.voyageScheduleDataList.forEach((v, index) => {
					this.hcList.push({
						text: v.vvyName,
						value: v.vvyId
					})
				})
				// 装卸类型数据
				this.shipInfo.handTypeList.forEach((v, index) => {
					this.zxList.push({
						text: v.ptrDesc,
						value: v.ptrCode
					})
				})
			},
			// 获取当前行信息
			getRow() {
				this.mafiRow = uni.getStorageSync('mafiRow');
				this.mafiRowIndex = uni.getStorageSync('mafiRowIndex');
				this.vvyId = this.mafiRow.vvyId
				this.vvyName = this.mafiRow.vvyName
				this.loaderTypeId = this.mafiRow.loaderTypeId
				this.loaderType = this.mafiRow.loaderType
				this.workTime = this.mafiRow.workTime
				this.lxValue = this.mafiRow.lxValue
				this.lxTextValue = this.mafiRow.lxTextValue
				this.size = this.mafiRow.size
				this.mafiBarcode = this.mafiRow.mafiBarcode
			},
			// 航次下拉
			hcChange(e) {
				this.vvyId = e;
				this.hcList.forEach(v => {
					if (v.value == e) {
						this.vvyName = v.text
					}
				})
			},
			// 类型下拉
			lxChange(e) {
				this.lxValue = e;
				this.lxList.forEach(v => {
					if (v.value == e) {
						this.lxTextValue = v.text
					}
				})
			},
			// 装卸类型下拉
			zxChange(e) {
				this.loaderTypeId = e;
				this.zxList.forEach(v => {
					if (v.value == e) {
						this.loaderType = v.text
					}
				})
			},
			// 作业时间
			changeLog(e) {
				this.workTime = e
			},
			// 取消
			cancel() {
				uni.navigateTo({
					url: '/pages/shipWork/mafi'
				})
			},
			// 删除
			del() {
				let addMafiArr = uni.getStorageSync('addMafiArr')
				addMafiArr.splice(this.mafiRowIndex, 1)
				uni.setStorageSync('addMafiArr', addMafiArr);
				uni.navigateTo({
					url: '/pages/shipWork/mafi'
				})
			},
			// 保存
			save() {
				let addMafiArr = uni.getStorageSync('addMafiArr')
				let addMafiObj = {
					vvyId: this.vvyId,
					vvyName: this.vvyName,
					loaderTypeId: this.loaderTypeId,
					loaderType: this.loaderType,
					lxValue: this.lxValue,
					lxTextValue: this.lxTextValue,
					size: this.size,
					mafiBarcode: this.mafiBarcode,
					workTime: this.workTime,
					tradeTypeName: this.shipInfo.voyageScheduleDataList[0].tradeTypeName,
					importExportFlagName: this.shipInfo.voyageScheduleDataList[0].importExportFlagName,
					spmName: this.shipInfo.voyageScheduleDataList[0].spmName,
				}
				if (this.obj.state == "edit") {
					addMafiArr[this.mafiRowIndex] = addMafiObj;
					uni.setStorageSync('addMafiArr', addMafiArr);
				} else if (this.obj.state == "add") {
					if (addMafiArr != "") {
						addMafiArr.push(addMafiObj)
						uni.setStorageSync('addMafiArr', addMafiArr);
					} else {
						addMafiArr = []
						addMafiArr.push(addMafiObj)
						uni.setStorageSync('addMafiArr', addMafiArr);
					}
				}
				uni.navigateTo({
					url: '/pages/shipWork/mafi'
				})
			},
			// 编辑
			toGo(state) {
				this.obj.state = state;
				const params = encodeURIComponent(JSON.stringify(this.obj));
				uni.navigateTo({
					url: `/pages/shipWork/mafiAdd?params=${params}`
				})
			}
		}
	}
</script>

<style lang="less" scoped>
	.mafiAdd {
		.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>