<template>
	<view class="opinionAdd">
		<head-view :title="title" url="/pages/shipWork/opinion"></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-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="operationDate" type="date" :clear-icon="false" @change="changeLog"
							v-if="obj.state != 'look'" :border="false" />
						<text v-else>{{operationDate}}</text>
					</view>
				</view>	
				<view class="ul" v-for="(item,index) in vvyInfo">
					<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> -->
						<text>{{ item.vvyName }}</text>
					</view>
					<view class="li">
						<p>进出口：</p>
						<!-- <text>{{importExportFlagName}}</text> -->
						<text>{{ item.importExportFlagName }}</text>
					</view>
					<view class="li">
						<p>贸易类型：</p>
						<!-- <text>{{spmTradeName}}</text> -->
						<text>{{ item.tradeTypeName }}</text>
					</view>
					<view class="li">
						<p><text class="required" v-if="obj.state != 'look'">*</text>装卸类型：</p>
						<!-- <uni-data-select v-model="handlingType" :localdata="zxList" @change="zxChange"
							v-if="obj.state != 'look'"></uni-data-select> -->
						<!-- <text>{{handlingName}}</text> -->
						<text>{{ item.importExportFlagName == "进口" ? '卸货' : '装货' }}</text>
					</view>
					<view class="li">
						<p><text class="required" v-if="obj.state != 'look'">*</text>车辆数量：</p>
						<uni-number-box v-model="item['vehiclesNumber' + index]" :max="1000000000" v-if="obj.state != 'look'" />
						<text v-else>{{item['vehiclesNumber' + index]}}</text>
					</view>
					<view class="li">
						<p><text class="required" v-if="obj.state != 'look'">*</text>备件数量：</p>
						<uni-number-box v-model="item['spareNumber' + index]" :max="1000000000" v-if="obj.state != 'look'" />
						<text v-else>{{item['spareNumber' + index]}}</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">
					<view class="btn qx" @click="cancel">取消</view>
					<view class="btn sc" v-if="obj.state == 'look'" @click="del">删除</view>
					<view class="btn bc" v-if="obj.state == 'add' || obj.state == 'edit'" @click="save">保存</view>
					<view class="btn bc" v-if="obj.state == 'look'" @click="toGo('edit')">编辑</view>
				</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: "",
				opinionRow: {},
				opinionRowIndex: 0,
				// 航次下拉框
				vvyId: "",
				vvyName: "",
				hcList: [],
				// 泊位下拉框
				bthId: "",
				bthIdName: "",
				bwList: [],
				// 装卸类型下拉框
				handlingType: "",
				handlingName: "",
				zxList: [],
				// 车辆数量
				vehiclesNumber: 0,
				// 备件数量
				spareNumber: 0,
				// 作业时间
				operationDate: [''],

				// 删除质量意见征询单id
				delCsIds: [],

				vvyInfo: [],
				importExportFlagName: "",
				spmTradeName: "",

				// 下拉数据
				optionData: [],
			}
		},
		onLoad(options) {
			if ('params' in options) {
				// 获取传递的对象参数，使用decodeURIComponent解码，并转为对象
				this.obj = JSON.parse(decodeURIComponent(options.params))
			}
		},
		mounted() {
			this.vtpId = uni.getStorageSync('vtpId')
			this.delCsIds = uni.getStorageSync("delCsIds")
			this.executeSql1('shipOption')
			this.executeSql1("shipInfoTable")
			this.executeSql1("voyageScheduleDataDetailRespDTOList")
			if (this.obj.state == "edit") {
				this.title = "编辑质量意见征询"
			} else if (this.obj.state == "look") {
				this.title = "查看质量意见征询"
			} else {
				this.title = "新增质量意见征询"
			}
			let that = this
			setTimeout(() => {
				if (that.obj.state == 'edit' || that.obj.state == 'look') {
					that.getRow();
				}
			}, 100)
		},
		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]
						// console.log(this.shipInfo);
						if(this.obj.state == "edit" || this.obj.state == "add") {
							if(this.shipInfo.actualBerthingTime) this.operationDate = this.shipInfo.actualBerthingTime.split(' ')[0]
						}
					} else if (tableName == 'voyageScheduleDataDetailRespDTOList') {
						this.vvyInfo = value
						this.vvyInfo.map((item,index) => {
							let vehiclesNumber = 'vehiclesNumber' + index
							let spareNumber = 'spareNumber' + index
							item[vehiclesNumber] = 0
							item[spareNumber] = 0
							return item
						})
						// console.log(this.vvyInfo);
						value.forEach((v, index) => {
							this.hcList.push({
								text: v.vvyName,
								value: v.vvyId
							})
						})
					}
				}).catch((error) => {
					// 在reject时执行的回调函数
					console.error(error);
				});
			},
			// 获取船只信息
			getShip() {
				// 装卸类型数据
				let zxOption = []
				this.optionData.forEach((v, index) => {
					if (v.dictionariesName == 'handTypeList') {
						zxOption.push(v)
					}
				})
				zxOption.forEach((v, index) => {
					this.zxList.push({
						text: v.ptrDesc,
						value: v.ptrCode
					})
				})
				// 船舶下拉数据
				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.opinionRow = uni.getStorageSync('opinionRow');
				this.opinionRowIndex = uni.getStorageSync('opinionRowIndex');
				this.vvyId = this.opinionRow.vvyId
				this.vvyName = this.opinionRow.vvyName
				this.importExportFlagName = this.opinionRow.importExportFlagName
				this.bthId = this.opinionRow.bthId
				this.bthIdName = this.opinionRow.bthIdName
				this.handlingType = this.opinionRow.handlingType
				this.handlingName = this.opinionRow.handlingName
				this.vvyInfo[0].vehiclesNumber0 = this.opinionRow.vehiclesNumber
				this.vvyInfo[0].spareNumber0 = this.opinionRow.spareNumber
				if(this.vvyInfo.length > 1) {
					this.vvyInfo[1].vehiclesNumber1 = this.opinionRow.vehiclesNumberDe
					this.vvyInfo[1].spareNumber1 = this.opinionRow.spareNumberDe
				}
				this.operationDate = this.opinionRow.operationDate
				this.vvyInfo.forEach(v => {
					if (this.vvyId == v.vvyId) {
						this.spmTradeName = v.tradeTypeName
					}
				})
			},
			// 航次下拉
			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
						this.spmTradeName = v.tradeTypeName
						if (this.importExportFlagName = "进口") {
							this.handlingName = "卸货"
							this.handlingType = "2"
						} else {
							this.handlingName = "装货"
							this.handlingType = "1"
						}
					}
				})
			},
			// 泊位下拉
			bwChange(e) {
				this.bthId = e;
				this.bwList.forEach(v => {
					if (v.value == e) {
						this.bthIdName = v.text
					}
				})
			},
			// 装卸类型下拉
			zxChange(e) {
				console.log(e)
				this.handlingType = e;
				this.zxList.forEach(v => {
					if (v.value == e) {
						this.handlingName = v.text
					}
				})
			},
			// 作业时间
			changeLog(e) {
				this.operationDate = e
			},
			// 取消
			cancel() {
				uni.navigateTo({
					url: '/pages/shipWork/opinion'
				})
			},
			// 弹框删除
			delConfirm() {
				let sql = `DELETE FROM shipmentQualityConsultationRespList WHERE vqcId = '${this.opinionRow.vqcId}';`
				this.executeSql(sql)
				let reg = RegExp(/-/)
				if (reg.test(this.opinionRow.vqcId) == false) {
					this.delCsIds.push(this.opinionRow.vqcId)
					uni.setStorageSync('delCsIds', this.delCsIds);
				}
				uni.navigateTo({
					url: '/pages/shipWork/opinion'
				})
			},
			// 删除
			del() {
				this.$refs.popup.open()
			},
			// 保存
			save() {
				let date = new Date().getTime()
				let vqcId = uuidv4()
				let webStatus = 0
				let webDate = api.getDate(date)
				if (this.obj.state == "edit") {
					if(this.vvyInfo.length == 1) {
					let sql =
						`UPDATE shipmentQualityConsultationRespList SET vvyId = '${this.vvyInfo[0].vvyId}',vvyName = '${this.vvyInfo[0].vvyName}',importExportFlagName = '${this.vvyInfo[0].importExportFlagName}',
						bthId = '${this.bthId}',bthIdName = '${this.bthIdName}',handlingType = '${this.vvyInfo[0].importExportFlagName == '进口' ? '2' : '1'}',handlingName = '${this.vvyInfo[0].importExportFlagName == '进口' ? '卸货' : '装货'}',
						vehiclesNumber = '${this.vvyInfo[0].vehiclesNumber0}',spareNumber = '${this.vvyInfo[0].spareNumber0}',operationDate = '${this.operationDate}',webStatus = '${webStatus}',webDate = '${webDate}',vehiclesNumberDe='null',spareNumberDe='null' WHERE vqcId = '${this.opinionRow.vqcId}';`
					this.executeSql(sql)
					}
					if(this.vvyInfo.length > 1) {
						let vvyId = this.vvyInfo[0].vvyId+'/'+this.vvyInfo[1].vvyId
						let vvyName = this.vvyInfo[0].vvyName+'/'+this.vvyInfo[1].vvyName
						let importExportFlagName = this.vvyInfo[0].importExportFlagName+'/'+this.vvyInfo[1].importExportFlagName
						let handlingType0 = this.vvyInfo[0].importExportFlagName == '进口' ? '2' : '1'
						let handlingType1 = this.vvyInfo[1].importExportFlagName == '进口' ? '2' : '1'
						let handlingType = handlingType0+'/'+handlingType1
						let handlingName0 = this.vvyInfo[0].importExportFlagName == '进口' ? '卸货' : '装货'
						let handlingName1 = this.vvyInfo[1].importExportFlagName == '进口' ? '卸货' : '装货'
						let handlingName = handlingName0+'/'+handlingName1
						let sql =
						`UPDATE shipmentQualityConsultationRespList SET vvyId = '${vvyId}',vvyName = '${vvyName}',importExportFlagName = '${importExportFlagName}', 
						bthId = '${this.bthId}',bthIdName = '${this.bthIdName}',handlingType = '${handlingType}',handlingName = '${handlingName}',
						vehiclesNumber = '${this.vvyInfo[0].vehiclesNumber0}',spareNumber = '${this.vvyInfo[0].spareNumber0}',operationDate = '${this.operationDate}',webStatus = '${webStatus}',webDate = '${webDate}',vehiclesNumberDe='${this.vvyInfo[1].vehiclesNumber1}',spareNumberDe='${this.vvyInfo[1].spareNumber1}' WHERE vqcId = '${this.opinionRow.vqcId}';`
					 this.executeSql(sql)
					}
				} else if (this.obj.state == "add") {
					if(this.vvyInfo.length == 1) {
					let sql = `insert into shipmentQualityConsultationRespList values('${vqcId}','${this.vvyInfo[0].vtpId}','${this.vvyInfo[0].vvyId}','${this.vvyInfo[0].vvyName}','${this.vvyInfo[0].importExportFlagName}',
					'${this.bthId}','${this.bthIdName}','${this.vvyInfo[0].importExportFlagName == '进口' ? '2' : '1'}','${this.vvyInfo[0].importExportFlagName == '进口' ? '卸货' : '装货'}','${this.vvyInfo[0].vehiclesNumber0}',
					'${this.vvyInfo[0].spareNumber0}','${this.operationDate}','${webStatus}','${webDate}','null','null')`
					this.executeSql(sql)
					}
					if(this.vvyInfo.length > 1) {
						let vtpId = this.vvyInfo[0].vtpId
						let vvyId = this.vvyInfo[0].vvyId+'/'+this.vvyInfo[1].vvyId
						let vvyName = this.vvyInfo[0].vvyName+'/'+this.vvyInfo[1].vvyName
						let importExportFlagName = this.vvyInfo[0].importExportFlagName+'/'+this.vvyInfo[1].importExportFlagName
						let handlingType0 = this.vvyInfo[0].importExportFlagName == '进口' ? '2' : '1'
						let handlingType1 = this.vvyInfo[1].importExportFlagName == '进口' ? '2' : '1'
						let handlingType = handlingType0+'/'+handlingType1
						let handlingName0 = this.vvyInfo[0].importExportFlagName == '进口' ? '卸货' : '装货'
						let handlingName1 = this.vvyInfo[1].importExportFlagName == '进口' ? '卸货' : '装货'
						let handlingName = handlingName0+'/'+handlingName1
						let sql = `insert into shipmentQualityConsultationRespList values('${vqcId}','${vtpId}','${vvyId}','${vvyName}','${importExportFlagName}',
						'${this.bthId}','${this.bthIdName}','${handlingType}','${handlingName}','${this.vvyInfo[0].vehiclesNumber0}',
						'${this.vvyInfo[0].spareNumber0}','${this.operationDate}','${webStatus}','${webDate}','${this.vvyInfo[1].vehiclesNumber1}','${this.vvyInfo[1].spareNumber1}')`
					this.executeSql(sql)
					}
				}
				uni.navigateTo({
					url: '/pages/shipWork/opinion'
				})
			},
			// 编辑
			toGo(state) {
				this.obj.state = state;
				const params = encodeURIComponent(JSON.stringify(this.obj));
				uni.navigateTo({
					url: `/pages/shipWork/opinionAdd?params=${params}`
				})
			},
			executeSql(sql) {
				sqlite.executeSqlCeshi(sql).then((value) => {
					// 在resolve时执行的回调函数
				}).catch((error) => {
					// 在reject时执行的回调函数
					console.error(error);
				});
			},
		}
	}
</script>

<style lang="less" scoped>
	.opinionAdd {
		.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: 25%;
						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 {
				position: fixed;
				bottom: 0;
				left: 0;
				right: 0;
				height: 80px;
				background-color: #fff;
				display: flex;
				justify-content: center;
				box-shadow: 0 -3px 7px 0 rgba(0, 0, 0, 0.10);

				.btn {
					margin: 15.5px 17px;
					width: 200px;
					height: 49px;
					border-radius: 2px;
					font-size: 18px;
					text-align: center;
					line-height: 49px;
				}

				.qx {
					background-color: #eee;
				}

				.sc {
					background-color: #E50101;
					color: #fff;
				}

				.bc {
					color: #fff;
					background: #0067CF;
				}
			}
		}
	}
</style>