<template>
	<view class="untieAdd">
		<head-view :title="title" url="/pages/shipWork/untieCord"></head-view>
		<view class="container contentFixed">
			<view class="info">
				<view class="ul">
					<view class="li">
						<p>船名：</p>
						<text>{{shipInfo.vslCnname}}</text>
					</view>
					<view class="li">
						<p><text class="required" v-if="obj.state != 'look'">*</text>船舶净吨：</p>
						<uni-number-box v-model="zlShip" :max="1000000000" 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="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>{{importExportFlagName}}</text>
					</view>
					<view class="li">
						<p>贸易类型：</p>
						<text>{{shipInfo.spmTradeName}}</text>
					</view>
					<view class="li">
						<p><text class="required" v-if="obj.state != 'look'">*</text>泊位：</p>
						<uni-data-select v-model="bthId" :localdata="bwList" @change="bwChange"
							v-if="obj.state != 'look'"></uni-data-select>
						<text v-else>{{bthIdName}}</text>
					</view>
					<view class="li">
						<p><text class="required" v-if="obj.state != 'look'">*</text>系缆时间：</p>
						<uni-datetime-picker v-model="attachTime" @change="changeLog1" v-if="obj.state != 'look'"
							:border="false" />
						<text v-else>{{attachTime}}</text>
					</view>
					<view class="li">
						<p>解缆时间：</p>
						<uni-datetime-picker v-model="unmoorTime" @change="changeLog2" v-if="obj.state != 'look'"
							:border="false" />
						<text v-else>{{unmoorTime}}</text>
					</view>
					<view class="li">
						<p>移泊时间：</p>
						<uni-datetime-picker v-model="shiftingBerthTime" @change="changeLog3" v-if="obj.state != 'look'"
							:border="false" />
						<text v-else>{{shiftingBerthTime}}</text>
					</view>
					<view class="li">
						<p>停泊时间：</p>
						<uni-datetime-picker v-model="noProductBerthTime" type="datetimerange" rangeSeparator="-"
							:hide-second="true" :border="false" v-if="obj.state != 'look'" @change="changeLog" />
						<text v-else>{{noProductBerthStTime}} - {{noProductBerthEdTime}}</text>
					</view>
				</view>
				<uni-popup ref="popup" type="dialog">
					<uni-popup-dialog type="error" confirmText="确定" title="通知" content="是否删除此条数据"
						@confirm="delConfirm"></uni-popup-dialog>
				</uni-popup>
				<view class="btnList">
					<van-button type="default" @click="cancel">取消</van-button>
					<template v-if="status == 0 || status == 4 || status == 5 || status == 'null'">
						<van-button type="danger" v-if="obj.state == 'look'" @click="del">删除</van-button>
						<van-button type="info" v-if="obj.state == 'look'" @click="toGo('edit')">编辑</van-button>
						<van-button type="info" v-if="obj.state == 'add' || obj.state == 'edit'"
							@click="save">保存</van-button>
					</template>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import sqlite from "../../common/sqlite.js"
	import api from "../../common/api.js"
	import {
		v4 as uuidv4
	} from 'uuid';
	export default {
		data() {
			return {
				title: "",
				shipInfo: {},
				obj: {},
				vtpId: "",
				untieRow: {},
				untieRowIndex: 0,
				// 航次下拉框
				vvyId: "",
				vvyName: "",
				hcList: [],
				// 船舶净吨
				zlShip: 0,
				// 泊位下拉框
				bthId: "",
				bthIdName: "",
				bwList: [],
				// 系缆时间
				attachTime: [''],
				// 解缆时间
				unmoorTime: [''],
				// 移泊时间
				shiftingBerthTime: [''],
				// 停泊时间
				noProductBerthTime: ['', ''],
				noProductBerthStTime: "",
				noProductBerthEdTime: "",

				// 审核状态
				status: 0,

				// 删除系解缆id
				delSauIds: [],

				vvyInfo: [],
				importExportFlagName: "",

				// 下拉数据
				optionData: [],
			}
		},
		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()
			}
			if (this.obj.state == "edit") {
				this.title = "编辑系解缆"
			} else if (this.obj.state == "look") {
				this.title = "查看系解缆"
			} else {
				this.title = "新增系解缆"
			}
			this.vtpId = uni.getStorageSync('vtpId')
			this.delSauIds = uni.getStorageSync("delSauIds")
			this.executeSql1('shipOption')
			this.executeSql1("shipInfoTable")
			this.executeSql1("voyageScheduleDataDetailRespDTOList")
		},
		methods: {
			// 查
			executeSql1(tableName) {
				let sql = `select * from ${tableName}  WHERE vtpId = '${this.vtpId}';`
				sqlite.executeSqlCeshi(sql).then((value) => {
					// 在resolve时执行的回调函数
					if (tableName == 'shipOption') {
						this.optionData = value
						this.getShip()
					} else if (tableName == 'shipInfoTable') {
						this.shipInfo = value[0]
					} else if (tableName == 'voyageScheduleDataDetailRespDTOList') {
						this.vvyInfo = value
						value.forEach((v, index) => {
							this.hcList.push({
								text: v.vvyName,
								value: v.vvyId
							})
						})
					}
				}).catch((error) => {
					// 在reject时执行的回调函数
					console.error(error);
				});
			},
			// 获取船只信息
			getShip() {
				// 泊位下拉数据
				let bwOption = []
				this.optionData.forEach((v, index) => {
					if (v.dictionariesName == 'bthList') {
						bwOption.push(v)
					}
				})
				bwOption.forEach((v, index) => {
					this.bwList.push({
						text: v.ptrDesc,
						value: v.ptrCode
					})
				})
			},
			// 获取当前行信息
			getRow() {
				this.untieRow = uni.getStorageSync('untieRow');
				console.log(this.untieRow)
				this.untieRowIndex = uni.getStorageSync('untieRowIndex');
				this.vvyId = this.untieRow.vvyId
				this.vvyName = this.untieRow.vvyName
				this.importExportFlagName = this.untieRow.importExportFlagName
				this.zlShip = this.untieRow.shipDeadWeight
				this.bthId = this.untieRow.bthId
				this.bthIdName = this.untieRow.bthIdName
				this.attachTime = (this.untieRow.attachTime[0] ? this.untieRow.attachTime : '')
				if (this.untieRow.unmoorTime == "null") {
					this.unmoorTime = ""
				} else {
					this.unmoorTime = this.untieRow.unmoorTime
				}
				if (this.untieRow.shiftingBerthTime == "null") {
					this.shiftingBerthTime = ""
				} else {
					this.shiftingBerthTime = this.untieRow.shiftingBerthTime
				}
				this.noProductBerthStTime = this.untieRow.noProductBerthStTime
				this.noProductBerthEdTime = this.untieRow.noProductBerthEdTime
				this.noProductBerthTime = [this.noProductBerthStTime, this.noProductBerthEdTime]
				this.status = this.untieRow.status
			},
			// 航次下拉
			hcChange(e) {
				this.vvyId = e;
				this.hcList.forEach(v => {
					if (v.value == e) {
						this.vvyName = v.text
					}
				})
				this.vvyInfo.forEach(v => {
					if (v.vvyId == e) {
						this.importExportFlagName = v.importExportFlagName
					}
				})
			},
			// 泊位下拉
			bwChange(e) {
				this.bthId = e;
				this.bwList.forEach(v => {
					if (v.value == e) {
						this.bthIdName = v.text
					}
				})
			},
			// 系缆时间
			changeLog1(e) {
				this.attachTime = e
			},
			// 解缆时间
			changeLog2(e) {
				this.unmoorTime = e
			},
			// 移泊时间
			changeLog3(e) {
				this.shiftingBerthTime = e
			},
			// 停泊时间
			changeLog(e) {
				this.noProductBerthTime = e
				this.noProductBerthStTime = e[0]
				this.noProductBerthEdTime = e[1]
			},
			// 取消
			cancel() {
				uni.navigateTo({
					url: '/pages/shipWork/untieCord'
				})
			},
			// 弹框删除
			delConfirm() {
				let sql = `DELETE FROM attachUnmoorRespList WHERE sauId = '${this.untieRow.sauId}';`
				this.executeSql(sql)
				this.delSauIds.push(this.untieRow.sauId)
				uni.setStorageSync('delSauIds', this.delSauIds);
				uni.navigateTo({
					url: '/pages/shipWork/untieCord'
				})
			},
			// 删除
			del() {
				this.$refs.popup.open()
			},
			// 保存
			save() {
				let date = new Date().getTime()
				let sauId = uuidv4()
				let webStatus = 0
				let webDate = api.getDate(date)
				if (this.obj.state == "edit") {
					let sql =
						`UPDATE attachUnmoorRespList SET shipDeadWeight = '${this.zlShip}',vvyId = '${this.vvyId}',
						vvyName = '${this.vvyName}',importExportFlagName = '${this.importExportFlagName}',bthId = '${this.bthId}',bthIdName = '${this.bthIdName}',
						attachTime = '${this.attachTime}',unmoorTime = '${this.unmoorTime}',shiftingBerthTime = '${this.shiftingBerthTime}',
						noProductBerthStTime = '${this.noProductBerthStTime}',noProductBerthEdTime = '${this.noProductBerthEdTime}',webStatus = '${webStatus}',
						webDate = '${webDate}' WHERE sauId = '${this.untieRow.sauId}';`
					console.log(sql)
					this.executeSql(sql)
				} else if (this.obj.state == "add") {
					let sql =
						`insert into attachUnmoorRespList values('${sauId}','${this.vtpId}','${this.zlShip}','${this.vvyId}','${this.vvyName}',
					'${this.importExportFlagName}','${this.bthId}','${this.bthIdName}','${this.attachTime}','${this.unmoorTime}',
					'${this.shiftingBerthTime}','${this.noProductBerthStTime}','${this.noProductBerthEdTime}','${0}','${webStatus}','${webDate}')`
					this.executeSql(sql)
				}
				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}`
				})
			},
			executeSql(sql) {
				sqlite.executeSqlCeshi(sql).then((value) => {
					// 在resolve时执行的回调函数
				}).catch((error) => {
					// 在reject时执行的回调函数
					console.error(error);
				});
			},
		}
	}
</script>

<style lang="less" scoped>
	.untieAdd {
		.container {
			padding: 16px;
			background-color: #F6F7F9;

			.info {
				background-color: #fff;
			}

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

				.li {
					width: 49%;
					display: flex;
					justify-content: space-between;
					border-bottom: 1px solid #EEEEEE;
					padding: 0 16px;
					height: 56px;
					line-height: 56px;

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

					/deep/.uni-numbox {
						border: 1px solid #ccc;
						box-sizing: border-box;
						height: 38px;
						margin-top: 8px;
					}

					/deep/.uni-numbox-btns {
						padding: 0 14px;
						box-sizing: border-box;
					}

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

					/deep/.uni-date {
						flex: none;
						width: 52%;
						text-align: right;
						display: flex;
						flex-direction: column;
						justify-content: center;
					}

					/deep/.uni-date-x .icon-calendar {
						display: none;
					}

					/deep/.uni-date-x {
						height: 38px;
					}

					/deep/.uni-stat__select {
						flex: none;
						width: 30%;
					}

					/deep/.uni-select {
						border: none;
					}

					/deep/.uni-select__input-text {
						text-align: right;
						padding-right: 10px;
					}
				}
			}

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

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