<template>
	<view class="mixWork">
		<head-view :title="title" special="1" url="/pages/shipWork/documentList"></head-view>
		<view class="container contentFixed">
			<custom-tabs type="c1" :value="tabsValue" @change="changeIndex">
				<custom-tab-pane label="人员信息" name="c1_1">
					<view></view>
					<view class="signBox">
						<view class="addBtn" @click="add('add')">新增人员</view>
						<p class="sign" @click="sign">点击签名</p>
					</view>
					<view class="ul">
						<template v-if="peopleInfo.length > 0">
							<view class="li" v-for="(item,index) in peopleInfo" :key="index"
								@click="add('look',item,index)">
								<view class="title">
									<image src="../../static/images/shipWork/hc.png" mode="widthFix"></image>
									<text>航次：{{item.vvyName}}</text>
								</view>
								<view class="liInfo">
									<p>贸易类型：<text>{{shipInfo.spmTradeName}}</text></p>
									<p>进出口：<text>{{item.importExportFlagName}}</text></p>
									<p>作业组数：<text>{{item.workSuite}}</text></p>
									<p>单船人数：<text>{{item.shipPerson}}</text></p>
								</view>
								<view class="status didNot" v-if="item.status == 0 || item.status == 'null'">
									<p>待提交</p>
								</view>
								<view class="status didNot" v-if="item.status == 1">
									<p>待审核</p>
								</view>
								<view class="status didNot" v-if="item.status == 2">
									<p>审批中</p>
								</view>
								<view class="status success" v-if="item.status == 3">
									<p>审核通过</p>
								</view>
								<view class="status fail" v-if="item.status == 4">
									<p>审核驳回</p>
								</view>
								<view class="status success" v-if="item.status == 5">
									<p>审核通过</p>
								</view>
							</view>
						</template>
						<template v-else>
							<o-empty height="70vh" bg="#f5f6fa" width="100vw" />
						</template>
					</view>
				</custom-tab-pane>
				<custom-tab-pane label="工班信息" name="c1_2">
					<view></view>
					<view class="signBox">
						<view class="addBtn" @click="add('add')">新增工班</view>
						<p class="sign" @click="sign">点击签名</p>
					</view>
					<view class="ul shiftUl">
						<template v-if="shiftInfo.length > 0">
							<view class="li" v-for="(item,index) in shiftInfo" :key="index"
								@click="lookShift(item,index)">
								<view class="title">
									<image src="../../static/images/shipWork/hc.png" mode="widthFix"></image>
									<text>航次：{{item[0].vvyName}}</text>
								</view>
								<view class="shift" v-for="(item2,index2) in item[0].shiftArr" :key="index2">
									<text>{{item2.pwcTypeName}}</text>
									<text class="text">开始时间：</text><text class="time">{{item2.workStartTime}}</text>
									<text class="text">结束时间：</text><text class="time">{{item2.workEndTime}}</text>
								</view>
								<view class="status didNot" v-if="item[0].status == 0 || item[0].status == 'null'">
									<p>待提交</p>
								</view>
								<view class="status didNot" v-if="item[0].status == 1">
									<p>待审核</p>
								</view>
								<view class="status didNot" v-if="item[0].status == 2">
									<p>审批中</p>
								</view>
								<view class="status success" v-if="item[0].status == 3">
									<p>审核通过</p>
								</view>
								<view class="status fail" v-if="item[0].status == 4">
									<p>审核驳回</p>
								</view>
								<view class="status success" v-if="item[0].status == 5">
									<p>审核通过</p>
								</view>
							</view>
						</template>
						<o-empty v-else height="70vh" bg="#f5f6fa" />
					</view>
				</custom-tab-pane>
			</custom-tabs>
		</view>
	</view>
</template>

<script>
	import sqlite from "../../common/sqlite.js"
	export default {
		data() {
			return {
				title: "船只 - 杂项作业单",
				shipInfo: {},
				vtpId: "",
				tabsValue: 0,
				// 人员信息列表
				peopleInfo: [],
				// 工班信息列表
				shiftInfo: [],

			}
		},
		onShow() {
			uni.reLaunch({
				url: '/pages/shipWork/mixWork'
			})
		},
		mounted() {
			this.vtpId = uni.getStorageSync('vtpId')
			let title = uni.getStorageSync('shipWorkTitle')
			this.title = `${title} / 杂项作业单 `
			this.executeSql1('shipInfoTable')
			if (this.tabsValue == 0) {
				this.executeSql1('messageRespList')
			} else {
				this.executeSql1('workMessageRespList')
				this.executeSql1('workMessageRespInfoList')
				this.executeSql1('retallyMessageRespList')
				this.executeSql1('infoRespList')
			}
		},
		methods: {
			// 查
			executeSql1(tableName) {
				let sql = `select * from ${tableName}  WHERE vtpId = '${this.vtpId}'`
				sqlite.executeSqlCeshi(sql).then((value) => {
					if (tableName == 'shipInfoTable') {
						this.shipInfo = value[0]
					} else if (tableName == 'messageRespList') {
						this.peopleInfo = value
					} else if (tableName == 'workMessageRespList' && value.length > 0) {
						this.shiftInfo = Object.values(value.reduce((res, item) => {
							res[item.aId] ? res[item.aId].push(item) : res[item
								.aId] = [item];
							return res;
						}, {}));
						console.log(this.shiftInfo)
						this.shiftInfo.forEach(v => {
							v.forEach(i => {
								this.$set(i, "shiftArr", [])
							})
						})
					}
				}).catch((error) => {
					// 在reject时执行的回调函数
					console.error(error);
				});
			},
			// 查工班信息
			executeSql2(vvyId) {
				let sql = `SELECT 
					strftime('%Y-%m-%d %H:00:00',  datetime(workEndTime, '0 day', 'start of day', '+22 hours')) || ',' || strftime('%Y-%m-%d %H:00:00',  datetime(workEndTime, '1 day', 'start of day', '+22 hours')) AS work_shift 
					FROM workMessageRespList WHERE workMessageRespList.vtpId='${this.vtpId}' and vvyId = '${vvyId}'
					GROUP BY work_shift
					UNION SELECT strftime('%Y-%m-%d %H:00:00',  datetime(workEndTime, '-1 day', 'start of day', '+22 hours')) || ',' || strftime('%Y-%m-%d %H:00:00',  datetime(workEndTime, '0 day', 'start of day', '+22 hours')) AS work_shift 
					FROM workMessageRespList WHERE workMessageRespList.vtpId='${this.vtpId}' and vvyId = '${vvyId}'
					GROUP BY work_shift;`
				sqlite.executeSqlCeshi(sql).then((value) => {
					value.forEach((v) => {
						let startTime = v.work_shift.split(",")[0]
						let endTime = v.work_shift.split(",")[1]
						let sql2 =
							`Select COUNT(*),pwcTypeName,vvyId,vvyName,MIN(workStartTime) AS workStartTime, MAX(workEndTime) 
							AS workEndTime FROM (SELECT * FROM workMessageRespList WHERE workStartTime>='${startTime}' and 
							workEndTime<'${endTime}' and workMessageRespList.vtpId='${this.vtpId}' and vvyId = '${vvyId}') 
							GROUP BY pwcTypeName  ORDER BY workStartTime asc;`
						this.executeSql22(sql2, vvyId)
					})
				}).catch((error) => {
					// 在reject时执行的回调函数
					console.error(error);
				});
			},
			executeSql22(sql, vvyId) {
				sqlite.executeSqlCeshi(sql).then((value) => {
					this.shiftInfo.forEach(v => {
						v.forEach(i => {
							if (i.vvyId == vvyId) {
								i.shiftArr.push(...value)
							}
						})
					})
				}).catch((error) => {
					// 在reject时执行的回调函数
					console.error(error);
				});
			},
			// 点击签名
			sign() {
				uni.navigateTo({
					url: `/pages/shipWork/mixSign`
				})
			},
			// 点击tabs
			changeIndex(e) {
				this.tabsValue = e.value
				if (this.tabsValue == 0) {
					this.executeSql1('messageRespList')
				} else {
					this.executeSql1('workMessageRespList')
					this.executeSql1('workMessageRespInfoList')
					this.executeSql1('retallyMessageRespList')
					this.executeSql1('infoRespList')
					let that = this
					setTimeout(function() {
						that.shiftInfo.forEach((v, index) => {
							setTimeout(function() {
								that.executeSql2(v[0].vvyId)
							}, 200)
						})
					}, 100)
				}
			},
			// 点击新增
			add(state, item, index) {
				if (state != 'add') {
					uni.setStorageSync('peopleRow', item);
					uni.setStorageSync('peopleRowIndex', index);
				}
				let obj = {
					state: state
				}
				const params = encodeURIComponent(JSON.stringify(obj));
				if (this.tabsValue == 0) {
					uni.navigateTo({
						url: `/pages/shipWork/peopleAdd?params=${params}`
					})
				} else {
					uni.navigateTo({
						url: `/pages/shipWork/shiftAdd?params=${params}`
					})
				}
			},
			lookShift(item, index) {
				uni.setStorageSync('shiftRow', item);
				uni.navigateTo({
					url: `/pages/shipWork/shiftDetails`
				})
			},
		},
	}
</script>

<style lang="less" scoped>
	.mixWork {
		.headInfo {
			/deep/.uniui-bottom {
				display: none;
			}
		}

		.container {
			min-height: calc(100vh - 68px);

			/deep/.tab .tab-bar {
				height: 66px;
			}

			/deep/.tab .tab-bar-item {
				height: 66px;
				line-height: 66px;
				font-size: 20px;
				font-weight: bold;
			}

			/deep/.tab .tab-bar-item .active-line {
				width: 55px;
				height: 4px;
			}

			.signBox {
				display: flex;
				justify-content: flex-end;
				padding-right: 20px;

				.addBtn {
					width: 86px;
					height: 36px;
					line-height: 36px;
					text-align: center;
					background-color: #2979ff;
					color: #fff;
					margin-right: 10px;
					border-radius: 4px;
				}

				.sign {
					width: 86px;
					height: 36px;
					line-height: 36px;
					text-align: center;
					color: #2979ff;
					border: 1px solid #2979ff;
					border-radius: 4px;
					color: #2979ff;
				}
			}

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

				.tabsBox {
					width: 100px;
					height: 50px;
					border-radius: 4px;
					margin: 0px 10px;
					border: 1px solid #ccc;
					text-align: center;
					line-height: 50px;
				}

				.active {
					background: #2979ff;
					color: #fff;
				}
			}

			.ul {
				width: 100%;
				padding: 16px;
				display: flex;
				flex-wrap: wrap;

				.li {
					width: 33%;
					padding: 16px;
					border: 1px solid #ccc;
					background-color: #fff;
					border-radius: 8px;
					position: relative;
					margin-bottom: 10px;

					.title {
						display: flex;

						text {
							font-size: 16px;
							font-weight: bold;
							display: flex;
							flex-direction: column;
							justify-content: center;
							margin-left: 8px;
							margin-top: 2px;
						}

						image {
							width: 32px;
							height: 32px;
						}
					}

					.liInfo {
						display: flex;
						flex-wrap: wrap;
						justify-content: space-between;
						margin-top: 24px;

						p {
							width: 49%;
							margin-bottom: 12px;
							color: #999;

							text {
								font-weight: bold;
								color: #23262E;
							}
						}
					}

					.status {
						padding: 5px;

						position: absolute;
						top: 16px;
						right: 16px;
					}

					.didNot {
						background-color: #FFF7EE;
						color: #FC8300;
					}

					.success {
						background: #F1F8FF;
						color: #0067CF;
					}

					.fail {
						background: #FFF0F0;
						color: #E50101;
					}

					.shift {
						display: flex;
						padding: 16px 4px;
						background-color: #f7f7f7;
						margin: 5px 0;
						font-size: 14px;

						text:first-child {
							color: #0067CF;
							margin: 0 8px;
						}

						.text {
							color: #999;
						}

						.time {
							color: #23262E;
							margin-top: 3px;
							margin-right: 15px;
						}

					}

					.state {
						display: flex;
						flex-direction: column;
						justify-content: center;
					}
				}

				.li:nth-of-type(3n - 1) {
					margin: 0 0.5% 10px;
				}

				/deep/.o-empty {
					width: 100%;
				}
			}

			.shiftUl {
				.li {
					width: 49%;
					margin-right: 0.5%;
				}
			}
		}
	}
</style>