<template>
	<view class="mixSign">
		<head-view :title="title" url="/pages/shipWork/mixWork"></head-view>
		<custom-tabs type="c1" :value="tabsValue" @change="tabsChange">
			<custom-tab-pane :label="item.name" v-for="(item,index) in tabsList" :name="'c1_'+index" :key="index">
				<view></view>
				<view class="container">
					<table>
						<tbody>
							<tr>
								<td class="h-l1r1">
									<image id="HT-logo" src="../../static/images/theme/logo.png" mode="widthFix"></image>
								</td>
								<td class="h-l1r2">&nbsp;</td>
								<td class="h-l1r3">
									<p>上海海通国际汽车码头有限公司</p>
									<p>Shanghai HaiTong International Automotive Terminal Co.,Ltd.</p>
								</td>
							</tr>
							<tr>
								<td colspan="3">
									<p class="bh-dz">QR-7.5.1-01-11-M</p>
								</td>
							</tr>
							<tr>
								<td colspan="3" class="h-l2r1">
									<p class="name-dz">海通码头装卸和杂项作业单</p>
									<p class="ename-dz">SHANGHAI HARBOUR BUREAU STEVEDORE WORKING RECORD</p>
								</td>
							</tr>
						</tbody>
					</table>
					<table class="m-b-30">
						<tbody>
							<tr>
								<td>Name of ship:</td>
								<td class="td-b-b1">{{shipInfo.vslCnname}}</td>
								<td>Voy No:</td>
								<td class="td-b-b1">{{shipInfo.vvyName}}</td>
								<td>Weather:</td>
								<td class="td-b-b1"></td>
							</tr>
							<tr>
								<td>Date:</td>
								<td class="td-b-b1"></td>
								<td>Gangs:</td>
								<td class="td-b-b1"></td>
								<td>Foreman:</td>
								<td class="td-b-b1"></td>
							</tr>
							<tr>
								<td>Foreman Working time:</td>
								<td class="td-b-b0"></td>
								<td>Berthed at Haitong:</td>
								<td class="td-b-b1"></td>
							</tr>
						</tbody>
					</table>
					<table class="t-zx">
						<tbody>
							<tr>
								<td rowspan="2">&nbsp;</td>
								<td colspan="6">Working time</td>
								<td colspan="4">Kind Of Works</td>
								<td rowspan="2">B/L</td>
								<td colspan="3">Handling method</td>
							</tr>
							<tr>
								<td colspan="3">From</td>
								<td colspan="3">To</td>
								<td colspan="4">Loading□/UnLoading□</td>
								<td>Ro/Ro</td>
								<td>PortDevices</td>
								<td>Others</td>
							</tr>
							<tr v-for="(item,index) in shiftList" :key="index + 'shift'">
								<td>{{item.pwcTypeName}}</td>
								<td colspan="3">{{item.workStartTime}}</td>
								<td colspan="3">{{item.workEndTime}}</td>
								<td colspan="4">车型:{{item.carTypeName}},数量:{{item.goodsNumber}}</td>
								<td>{{item.remark}}</td>
								<td><text v-if="item.loadingTypeName == 'Ro/Ro'">√</text></td>
								<td><text v-if="item.loadingTypeName == 'PortDevice'">√</text></td>
								<td><text v-if="item.loadingTypeName == 'Others'">√</text></td>
							</tr>
							<tr>
								<td :rowspan="fcList.length + 2">翻仓信息</td>
								<td colspan="2" rowspan="2">Kind of Rehangdling</td>
								<td colspan="2">DK/H</td>
								<td colspan="6">Working time</td>
								<td colspan="4">Weight/Measure</td>
							</tr>
							<tr>
								<td>From</td>
								<td>To</td>
								<td colspan="3">From</td>
								<td colspan="3">To</td>
								<td colspan="2">Vehicle type/Quantity</td>
								<td colspan="2">Total W/M</td>
							</tr>
							<tr v-for="(item,index) in fcList" :key="index + 'fc'">
								<td colspan="2">{{item.retallyTypeName}}</td>
								<td>{{item.retallyOrigin}}</td>
								<td>{{item.retallyTerminus}}</td>
								<td colspan="3">{{item.retallyStartTime}}</td>
								<td colspan="3">{{item.retallyEndTime}}</td>
								<td colspan="2">车型:{{item.carTypeName}},数量:{{item.goodsNumber}}</td>
								<td colspan="2">重量:{{item.goodsWeight}},体积:{{item.goodsVolume}}</td>
							</tr>
							<tr>
								<td>&nbsp;</td>
								<td colspan="2">Reason</td>
								<td colspan="3">技术工人</td>
								<td>From</td>
								<td>To</td>
								<td>Weight/Measure</td>
								<td colspan="3">普通工人</td>
								<td>From</td>
								<td>To</td>
								<td>Weight/Measure</td>
							</tr>
							<tr>
								<td>Extra Labour（辅助作业）</td>
								<td colspan="2">{{qtList.auxRemark}}</td>
								<td colspan="3">{{qtList.tecNum}}</td>
								<td>{{qtList.tecStartTm}}</td>
								<td>{{qtList.tecEndTm}}</td>
								<td>重量:{{qtList.tecWeight}},体积:{{qtList.tecVolume}}</td>
								<td colspan="3">{{qtList.genNum}}</td>
								<td>{{qtList.genStartTm}}</td>
								<td>{{qtList.genEndTm}}</td>
								<td>重量:{{qtList.genWeight}},体积:{{qtList.genVolume}}</td>
							</tr>
							<tr>
								<td>Stand by（待工）</td>
								<td colspan="15">{{qtList.waitRemark}}</td>
							</tr>
							<tr>
								<td>Remark（备注）</td>
								<td colspan="15">{{qtList.workRemark}}</td>
							</tr>
						</tbody>
					</table>
					<view class="sginBox">
						<view class="box">
							<p>Foreman(指导员):</p>
							<p class="sign" @click="togoSign('0')" v-if="zdySign.url == ''">点击签名</p>
							<template v-else>
								<image :src="zdySign.url"  mode="widthFix"></image>
								<text class="del" @click="delSign(zdySign.id)">删除</text>
							</template>
						</view>
						<view class="box">
							<p>Capt/Chief Mate(船长/大副):</p>
							<p class="sign" @click="togoSign('1')" v-if="czSign.url == ''">点击签名</p>
							<template v-else>
								<image :src="czSign.url"  mode="widthFix"></image>
								<text class="del" @click="delSign(czSign.id)">删除</text>
							</template>
						</view>
					</view>
				</view>
			</custom-tab-pane>
		</custom-tabs>
		<uni-popup ref="delPopup" type="dialog">
			<uni-popup-dialog type="error" confirmText="确定" title="通知" content="是否删除此条数据"
				@confirm="delConfirm"></uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	import sqlite from "../../common/sqlite.js"
	export default {
		data() {
			return {
				title: "杂项单证签名",
				tabsValue: 0,
				tabsList: [],
				aIdList: [],
				aId: "",
				// 船舶信息
				shipInfo: {},
				// 工班信息
				shiftList: [],
				// 翻仓信息
				fcList: [],
				// 其他信息
				qtList: {},
				// 签名信息
				zdySign: {
					url: "",
					id: ""
				},
				czSign: {
					url: "",
					id: ""
				},
				delId: "",
			}
		},
		onLoad(option) {
			if ('params' in option) {
				this.tabsValue = JSON.parse(decodeURIComponent(option.params)).tabsValue
			}
		},
		mounted() {
			this.executeSql('shipInfoTable')
		},
		methods: {
			// 查船舶信息
			executeSql(tableName) {
				let sql = `select * from ${tableName}`
				sqlite.executeSqlCeshi(sql).then((value) => {
					// 在resolve时执行的回调函数
					if (tableName == 'shipInfoTable') {
						this.shipInfo = value[0]
						this.executeSql1()
					} else {
						console.log(value)
					}
				}).catch((error) => {
					// 在reject时执行的回调函数
					console.error(error);
				});
			},
			// 查标签
			executeSql1() {
				let sql = `select distinct aId from workMessageRespList`
				sqlite.executeSqlCeshi(sql).then((value) => {
					// 在resolve时执行的回调函数
					this.aIdList = value
					this.aIdList.forEach((item, index) => {
						let obj = {
							name: `杂项单证${index + 1}`,
						}
						this.tabsList.push(obj)
					})
					this.aId = this.aIdList[this.tabsValue].aId
					this.executeSql2()
					this.executeSql3()
					this.executeSql4()
					this.executeSql5()
				}).catch((error) => {
					// 在reject时执行的回调函数
					console.error(error);
				});
			},
			// 查工班信息
			executeSql2() {
				let sql =
					`select w.aId, w.vvyId, w.vvyName,w.pwcType,w.pwcTypeName,w.workStartTime,w.workEndTime,wi.carType,wi.carTypeName,wi.goodsNumber,wi.remark,wi.loadingType,wi.loadingTypeName 
						from workMessageRespInfoList wi Left join workMessageRespList w on wi.contactId=w.webId WHERE aId = '${this.aId}' and w.vvyId='${this.shipInfo.vvyId}' order by w.pwcType asc;`
				sqlite.executeSqlCeshi(sql).then((value) => {
					// 在resolve时执行的回调函数
					// let arr = []
					// value.forEach(item => {
					// 	if (!arr[item.pwcType]) {
					// 		arr[item.pwcType] = {
					// 			pwcType: item.pwcType,
					// 			pwcTypeName: item.pwcTypeName,
					// 			workStartTime: item.workStartTime,
					// 			workEndTime: item.workEndTime,
					// 			shiftInfo: []
					// 		};
					// 	}
					// 	arr[item.pwcType].shiftInfo.push({
					// 		vvyId: item.vvyId,
					// 		vvyName: item.vvyName,
					// 		carType: item.carType,
					// 		carTypeName: item.carTypeName,
					// 		goodsNumber: item.goodsNumber,
					// 		remark: item.remark,
					// 		loadingType: item.loadingType,
					// 		loadingTypeName: item.loadingTypeName
					// 	});
					// });
					// this.shiftList = Object.values(arr)
					this.shiftList = value
				}).catch((error) => {
					// 在reject时执行的回调函数
					console.error(error);
				});
			},
			// 查翻仓信息
			executeSql3() {
				let sql =
					`select vvyId,vvyName,retallyType,retallyTypeName,retallyOrigin,retallyTerminus,retallyStartTime,retallyEndTime,
					   carType,carTypeName,goodsNumber,goodsVolume,goodsWeight,tradeTypeName,importExportFlagName,spmName
					   from retallyMessageRespList WHERE aId = '${this.aId}' and vvyId='${this.shipInfo.vvyId}' order by retallyType asc;`
				sqlite.executeSqlCeshi(sql).then((value) => {
					// 在resolve时执行的回调函数
					// let arr = []
					// value.forEach(item => {
					// 	if (!arr[item.retallyType]) {
					// 		arr[item.retallyType] = {
					// 			retallyType: item.retallyType,
					// 			retallyTypeName: item.retallyTypeName,
					// 			fcInfo: []
					// 		};
					// 	}
					// 	arr[item.retallyType].fcInfo.push({
					// 		vvyId: item.vvyId,
					// 		vvyName: item.vvyName,
					// 		retallyOrigin: item.retallyOrigin,
					// 		retallyTerminus: item.retallyTerminus,
					// 		carType: item.carType,
					// 		carTypeName: item.carTypeName,
					// 		datetime: [item.retallyStartTime, item.retallyEndTime],
					// 		retallyStartTime: item.retallyStartTime,
					// 		retallyEndTime: item.retallyEndTime,
					// 		goodsNumber: item.goodsNumber,
					// 		goodsVolume: item.goodsVolume,
					// 		goodsWeight: item.goodsWeight
					// 	});
					// });
					// this.fcList = Object.values(arr)
					this.fcList = value
				}).catch((error) => {
					// 在reject时执行的回调函数
					console.error(error);
				});
			},
			// 查其他信息
			executeSql4() {
				let sql =
					`select vvyId,vvyName,tecNum,tecStartTm,tecEndTm,tecWeight,tecVolume,genNum,genStartTm,genEndTm,genWeight,genVolume,auxRemark,
					   waitRemark,workRemark,tradeTypeName,importExportFlagName from infoRespList
					   WHERE aId = '${this.aId}' and vvyId='${this.shipInfo.vvyId}';`
				sqlite.executeSqlCeshi(sql).then((value) => {
					// 在resolve时执行的回调函数
					this.qtList = value[0]
				}).catch((error) => {
					// 在reject时执行的回调函数
					console.error(error);
				});
			},
			// 查单证签字表
			executeSql5() {
				let sql = `select * from workSignTable WHERE signId = '${this.aId}';`
				sqlite.executeSqlCeshi(sql).then((value) => {
					// 在resolve时执行的回调函数
					let zdyList = []
					let czList = []
					if (value.length > 0) {
						value.forEach(v => {
							if (v.signType == '0') {
								this.zdySign = {
									url: v.url,
									id: v.webId
								}
								zdyList.push(v)
							} else {
								this.czSign = {
									url: v.url,
									id: v.webId
								}
								czList.push(v)
							}
						})
					}
					if (zdyList.length == 0) {
						this.zdySign = {
							url: "",
							id: ""
						}
					}
					if (czList.length == 0) {
						this.czSign = {
							url: "",
							id: ""
						}
					}
				}).catch((error) => {
					// 在reject时执行的回调函数
					console.error(error);
				});
			},
			// 删除当前签名
			delSign(id) {
				this.$refs.delPopup.open()
				this.delId = id
			},
			// 弹框删除
			delConfirm() {
				let sql = `DELETE FROM workSignTable WHERE webId = '${this.delId}';`
				sqlite.executeSqlCeshi(sql).then((value) => {
					// 在resolve时执行的回调函数
					this.executeSql5()
				}).catch((error) => {
					// 在reject时执行的回调函数
					console.error(error);
				});
			},
			// 点击标签
			tabsChange(e) {
				this.aId = this.aIdList[e.value].aId
				this.tabsValue = e.value
				this.zdySign = {
					url: "",
					id: ""
				}
				this.czSign = {
					url: "",
					id: ""
				}
				this.executeSql2()
				this.executeSql3()
				this.executeSql4()
				this.executeSql5()
			},
			// 点击签名
			togoSign(signType) {
				let obj = {
					id: this.aId,
					url: "mixSign",
					signType: signType, // 0 指导员  1 船长/大副
					signTable: "mixWork",
					tabsValue: this.tabsValue,
				}
				const params = encodeURIComponent(JSON.stringify(obj));
				uni.navigateTo({
					url: `/pages/shipWork/sign?params=${params}`
				})
			},
		}
	}
</script>

<style lang="less" scoped>
	@import "../../style/css/main-dz.css";

	.container {
		padding: 10px 20px;
	}

	.bowei {
		height: 60px;
		text-align: center;
	}

	.rd-td2 {
		width: 100px;
		height: 30px;
		text-align: center;
	}

	.m-b-30 {
		margin-bottom: 30px;
	}

	.m-b-30 td {
		padding: 0px 20px;
		height: 36px;
		width: 250px;
	}

	.td-b-b0 {
		width: 600px !important;
		border-bottom: #000000 solid 1px;
		text-align: center;
	}

	.td-b-b1 {
		border-bottom: #000000 solid 1px;
		text-align: center;
	}

	.t-zx td {
		height: 36px;
	}

	.sginBox {
		display: flex;

		.box {
			flex: 1;
			display: flex;

			image {
				width: 200px;
				height: 50px;
			}

			.del {
				color: red;
			}
		}
	}

	.sign {
		color: #2979ff;
		margin-left: 10px;
	}
</style>