<template>
	<view class="supplyAdd">
		<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-data-select v-model="stopBerthage" :localdata="bwList" @change="bwChange"
						v-if="obj.state != 'look'"></uni-data-select>
					<text v-else>{{stopBerthageName}}</text>
				</view>
				<view class="li">
					<p><text class="required" v-if="obj.state != 'look'">*</text>供给类型：</p>
					<uni-data-select v-model="supplyTypeId" :localdata="gjList" @change="gjChange"
						v-if="obj.state != 'look'"></uni-data-select>
					<text v-else>{{supplyType}}</text>
				</view>
				<view class="li">
					<p><text class="required" v-if="obj.state != 'look'">*</text>供给量：</p>
					<uni-number-box v-model="supplyAmount" v-if="obj.state != 'look'" />
					<text v-else>{{supplyAmount}}</text>
				</view>
				<view class="li">
					<p><text class="required" v-if="obj.state != 'look'">*</text>日期：</p>
					<uni-datetime-picker v-model="supplyDate" type="date" :clear-icon="false" @change="changeLog"
						v-if="obj.state != 'look'" />
					<text v-else>{{supplyDate}}</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>
				<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>
	import sqlite from "../../common/sqlite.js"
	import api from "../../common/api.js"
	import {
		v4 as uuidv4
	} from 'uuid';
	export default {
		data() {
			return {
				shipInfo: {
					voyageScheduleDataList: [{
						spmName: "",
						importExportFlagName: "",
						tradeTypeName: ""
					}]
				},
				obj: {},
				supplyRow: {},
				supplyRowIndex: 0,
				// 航次下拉框
				vvyId: "",
				vvyName: "",
				hcList: [],
				// 泊位下拉框
				stopBerthage: "",
				stopBerthageName: "",
				bwList: [],
				// 供给类型
				supplyTypeId: "",
				supplyType: "",
				gjList: [],
				supplyAmount: 0,
				// 日期
				supplyDate: [""],
			}
		},
		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.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
					})
				})
				this.shipInfo.supplyTypeList.forEach((v, index) => {
					this.gjList.push({
						text: v.ptrDesc,
						value: v.ptrCode
					})
				})
			},
			// 获取当前行信息
			getRow() {
				this.supplyRow = uni.getStorageSync('supplyRow');
				this.supplyRowIndex = uni.getStorageSync('supplyRowIndex');
				this.vvyId = this.supplyRow.vvyId
				this.vvyName = this.supplyRow.vvyName
				this.stopBerthage = this.supplyRow.stopBerthage
				this.stopBerthageName = this.supplyRow.stopBerthageName
				this.supplyTypeId = this.supplyRow.sssId
				this.supplyType = this.supplyRow.supplyType
				this.supplyAmount = this.supplyRow.supplyAmount
				this.supplyDate = (this.supplyRow.supplyDate[0] ? this.supplyRow.supplyDate : '')
			},
			// 航次下拉
			hcChange(e) {
				this.vvyId = e;
				this.hcList.forEach(v => {
					if (v.value == e) {
						this.vvyName = v.text
					}
				})
			},
			// 泊位下拉
			bwChange(e) {
				this.stopBerthage = e;
				this.bwList.forEach(v => {
					if (v.value == e) {
						this.stopBerthageName = v.text
					}
				})
			},
			// 供给类型下拉
			gjChange(e) {
				this.supplyTypeId = e;
				this.gjList.forEach(v => {
					if (v.value == e) {
						this.supplyType = v.text
					}
				})
			},
			// 日期选择
			changeLog(e) {
				this.attachTime = e.slice(0, e.length - 3)
			},
			// 取消
			cancel() {
				uni.navigateTo({
					url: '/pages/shipWork/supply'
				})
			},
			// 弹框删除
			delConfirm() {
				// let addSupplyArr = uni.getStorageSync('addSupplyArr')
				// addSupplyArr.splice(this.supplyRowIndex, 1)
				// uni.setStorageSync('addSupplyArr', addSupplyArr);
				let sql = `DELETE FROM shipmentShipSupplyRespList WHERE webId = '${this.supplyRow.webId}';`
				this.executeSql(sql)
				uni.navigateTo({
					url: '/pages/shipWork/supply'
				})
			},
			// 删除
			del() {
				this.$refs.popup.open()
			},
			// 保存
			save() {
				let date = new Date().getTime()
				let webId = uuidv4()
				let webStatus = 0
				let webDate = api.getDate(date)
				if (this.obj.state == "edit") {
					let sql =
						`UPDATE shipmentShipSupplyRespList SET vvyId = '${this.vvyId}',vvyName = '${this.vvyName}',
						stopBerthage = '${this.stopBerthage}',stopBerthageName = '${this.stopBerthageName}',sssId = '${this.supplyTypeId}',
						supplyType = '${this.supplyType}',supplyAmount = '${this.supplyAmount}',supplyDate = '${this.supplyDate}',webStatus = '${webStatus}',
						webDate = '${webDate}' WHERE webId = '${this.supplyRow.webId}';`
					this.executeSql(sql)
				} else if (this.obj.state == "add") {
					let sql =
						`insert into shipmentShipSupplyRespList values('${webId}','${this.vvyId}','${this.vvyName}',
						'${this.stopBerthage}','${this.stopBerthageName}','${this.supplyTypeId}',
						'${this.supplyType}','${this.supplyAmount}','${this.supplyDate}',
						'${this.shipInfo.voyageScheduleDataList[0].tradeTypeName}',
						'${this.shipInfo.voyageScheduleDataList[0].importExportFlagName}','${this.shipInfo.voyageScheduleDataList[0].spmName}',
						'${webStatus}','${webDate}')`
					this.executeSql(sql)
				}
				// let addSupplyArr = uni.getStorageSync('addSupplyArr')
				// let addSupplyObj = {
				// 	vvyId: this.vvyId,
				// 	vvyName: this.vvyName,
				// 	stopBerthage: this.stopBerthage,
				// 	stopBerthageName: this.stopBerthageName,
				// 	sssId: this.supplyTypeId,
				// 	supplyType: this.supplyType,
				// 	supplyAmount: this.supplyAmount,
				// 	supplyDate: this.supplyDate,
				// 	tradeTypeName: this.shipInfo.voyageScheduleDataList[0].tradeTypeName,
				// 	importExportFlagName: this.shipInfo.voyageScheduleDataList[0].importExportFlagName,
				// 	spmName: this.shipInfo.voyageScheduleDataList[0].spmName,
				// }
				// if (this.obj.state == "edit") {
				// 	addSupplyArr[this.supplyRowIndex] = addSupplyObj;
				// 	uni.setStorageSync('addSupplyArr', addSupplyArr);
				// } else if (this.obj.state == "add") {
				// 	if (addSupplyArr != "") {
				// 		addSupplyArr.push(addSupplyObj)
				// 		uni.setStorageSync('addSupplyArr', addSupplyArr);
				// 	} else {
				// 		addSupplyArr = []
				// 		addSupplyArr.push(addSupplyObj)
				// 		uni.setStorageSync('addSupplyArr', addSupplyArr);
				// 	}
				// }
				uni.navigateTo({
					url: '/pages/shipWork/supply'
				})
			},
			// 编辑
			toGo(state) {
				this.obj.state = state;
				const params = encodeURIComponent(JSON.stringify(this.obj));
				uni.navigateTo({
					url: `/pages/shipWork/supplyAdd?params=${params}`
				})
			},
			executeSql(sql) {
				sqlite.executeSqlCeshi(sql).then((value) => {
					// 在resolve时执行的回调函数
					console.log(value);
				}).catch((error) => {
					// 在reject时执行的回调函数
					console.error(error);
				});
			},
			onBackPress(event) {
				uni.navigateTo({
					url: '/pages/shipWork/supply'
				})
				return true;
			},
		}
	}
</script>

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