<template>
	<view class="patrolAdd">
		<head-view :title="title" url="/pages/shipWork/patrol"></head-view>
		<view class="container">
			<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="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.importExportFlagName}}</text>
				</view>
				<view class="li">
					<p>贸易类型：</p>
					<text>{{shipInfo.tradeTypeName}}</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="type" placeholder="请输入"></uni-easyinput>
					<text v-else>{{type}}</text>
				</view>
				<view class="li tpLi">
					<p><text class="required" v-if="obj.state != 'look'">*</text>图片上传：</p>
					<template v-if="obj.state != 'look'">
						<view class="picture">
							<uni-file-picker limit="9" v-model="url" fileMediatype="image" @select="select"
								title="最多选择9张图片"></uni-file-picker>
						</view>
					</template>
					<view class="pictureLook" v-else>
						<view v-for="(item,index) in url" :key="index">
							<image :src="item">
						</view>
					</view>
				</view>
				<view class="li wLi">
					<p><text class="required" v-if="obj.state != 'look'">*</text>描述：</p>
					<uni-easyinput type="textarea" autoHeight v-model="remark" placeholder="请输入任务描述(200字以内)"
						maxlength="200" v-if="obj.state != 'look'"></uni-easyinput>
					<text v-else>{{remark}}</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 utils from '@/common/util.js';
	import {
		v4 as uuidv4
	} from 'uuid';
	export default {
		data() {
			return {
				title: "",
				shipInfo: {},
				obj: {},
				patrolRow: {},
				patrolRowIndex: {},
				// 航次下拉框
				vvyId: "",
				vvyName: "",
				hcList: [],
				// 类型
				type: "",
				// 图片
				url: [], // 待增加
				ysUrl: [],
				// 描述
				remark: "",

				// 下拉数据
				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.executeSql1('shipOption')
			this.executeSql1("shipInfoTable")
			this.executeSql2('safetyInspectionRespUrlList')
		},
		methods: {
			// 查
			executeSql1(tableName) {
				let sql = `select * from ${tableName}`
				sqlite.executeSqlCeshi(sql).then((value) => {
					// 在resolve时执行的回调函数
					if (tableName == 'shipOption') {
						this.optionData = value
						this.getShip()
					} else if (tableName == 'shipInfoTable') {
						this.shipInfo = value[0]
					}
				}).catch((error) => {
					// 在reject时执行的回调函数
					console.error(error);
				});
			},
			executeSql2(tableName) {
				let sql = `select * from ${tableName} WHERE contactId = '${this.patrolRow.webId}'`
				sqlite.executeSqlCeshi(sql).then((value) => {
					// 在resolve时执行的回调函数
					// console.log(value)
				}).catch((error) => {
					// 在reject时执行的回调函数
					console.error(error);
				});
			},
			// 获取船只信息
			getShip() {
				// this.shipInfo = uni.getStorageSync('shipInfo')
				// 航次下拉数据
				let hcOption = []
				this.optionData.forEach((v, index) => {
					if (v.dictionariesName == 'vvyList') {
						hcOption.push(v)
					}
				})
				hcOption.forEach((v, index) => {
					this.hcList.push({
						text: v.ptrDesc,
						value: v.ptrCode
					})
				})
			},
			// 获取当前行信息
			getRow() {
				this.patrolRow = uni.getStorageSync('patrolRow');
				this.patrolRowIndex = uni.getStorageSync('patrolRowIndex');
				this.vvyId = this.patrolRow.vvyId
				this.vvyName = this.patrolRow.vvyName
				this.type = this.patrolRow.type
				this.remark = this.patrolRow.remark
				// this.url = this.patrolRow.url // 待增加
			},
			// 航次下拉
			hcChange(e) {
				this.vvyId = e;
				this.hcList.forEach(v => {
					if (v.value == e) {
						this.vvyName = v.text
					}
				})
			},
			// 图片上传
			select(e) {
				console.log(e)
				let file = e.tempFiles[0].file
				this.fileToBase64(file).then(res => {
					console.log(res)
					utils.compress(res, 500, 0.3).then(res2 => {
						console.log(res2)
						this.ysUrl.push(res2)
					});
					console.log(this.ysUrl)
				}).catch((error) => {
					// 在reject时执行的回调函数
					console.error(error);
				});
			},
			fileToBase64(file) {
				return new Promise((resolve, reject) => { ///专门用来读文件的工具类
					const reader = new FileReader();
					reader.readAsDataURL(file);
					reader.onload = () => resolve(reader.result);
					reader.onerror = (error) => reject(error);
				});
			},
			// 取消
			cancel() {
				uni.navigateTo({
					url: '/pages/shipWork/patrol'
				})
			},
			// 弹框删除
			delConfirm() {
				let sql = `DELETE FROM safetyInspectionRespList WHERE webId = '${this.patrolRow.webId}';`
				this.executeSql(sql)
				uni.navigateTo({
					url: '/pages/shipWork/patrol'
				})
			},
			// 删除
			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 safetyInspectionRespList SET vvyId = '${this.vvyId}', vvyName = '${this.vvyName}', 
						type = '${this.type}',remark = '${this.remark}', webStatus = '${webStatus}',
						webDate = '${webDate}' WHERE webId = '${this.patrolRow.webId}';`
					this.executeSql(sql)
				} else if (this.obj.state == "add") {
					let sql = `insert into safetyInspectionRespList values('${webId}','${this.vvyId}','${this.vvyName}',
						'${this.type}','${this.remark}','${this.shipInfo.tradeTypeName}','${this.shipInfo.importExportFlagName}',
						'${this.shipInfo.spmName}','${webStatus}','${webDate}')`
					this.executeSql(sql)
					console.log(this.ysUrl)
					for (var i = 0; i < this.ysUrl.length; i++) {
						let webId2 = uuidv4()
						let sql2 = `insert into safetyInspectionRespUrlList values('${webId2}','${webId}','${this.ysUrl[i]}',
							'${webStatus}','${webDate}')`
						console.log(sql2)
						this.executeSql(sql2)
					}
				}
				uni.navigateTo({
					url: '/pages/shipWork/patrol'
				})
			},
			// 编辑
			toGo(state) {
				this.obj.state = state;
				const params = encodeURIComponent(JSON.stringify(this.obj));
				uni.navigateTo({
					url: `/pages/shipWork/patrolAdd?params=${params}`
				})
			},
			executeSql(sql) {
				sqlite.executeSqlCeshi(sql).then((value) => {
					// 在resolve时执行的回调函数
					// console.log(value);
				}).catch((error) => {
					// 在reject时执行的回调函数
					console.error(error);
				});
			},
		}
	}
</script>

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

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

				.wLi {
					width: 100% !important;
				}

				.tpLi {
					width: 100% !important;
					display: block !important;

					p {
						text-align: left !important;
					}
				}

				.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;
					}

					.picture {
						/deep/.file-picker__box {
							width: 120px !important;
							height: 120px !important;
							padding-top: 0;
						}
					}

					.pictureLook {
						display: flex;
						flex-wrap: wrap;

						image {
							width: 120px;
							height: 120px;
							margin: 10px;
						}
					}
				}
			}

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

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