<template>
	<view class="detail">
		<view class="card">
			<view class="title">查看详细</view>
			<view class="row">
				<label>工作岗位：</label>
				<label>{{detail.postName}}</label>
			</view>
			<view class="row">
				<label>姓名：</label>
				<label>{{detail.stuName}}</label>
			</view>
			<view class="row">
				<label>总工作时长/{{ detail.postType == '固定岗位A' ? '天' : '小时' }}：</label>
				<label>{{detail.totalTime}}</label>
			</view>
			<view class="row">
				<label>部门意见：</label>
				<uni-data-checkbox v-model="detail.deptCmt" :localdata="deptCmt" />
			</view>
			<button @click="doAudit" type="primary" plain>审核</button>
		</view>
	</view>
</template>

<script>
	import {
		CheckImgExists,
		isEmpty
	} from "@/api/helpFunc";
	import {
		baseUrl
	} from "@/config.js";
	import {
		previewImg
	} from "@/utils/uploadImg.js"
	import {
		doAudit,
	} from "@/api/workStudy/money";

	export default {
		data() {
			return {
				detail: null,
				attachment: [],
				basePath: baseUrl,
				deptCmt: [{
					text: '同意',
					value: '同意'
				}, {
					text: '拒绝',
					value: '拒绝'
				}, {
					text: '驳回',
					value: '驳回'
				}],
			}
		},
		onLoad(option) {
			this.detail = JSON.parse(option.detailData);
		},
		methods: {
			async doAudit() {
				let sdata = {
					...this.detail
				};
				if (isEmpty(sdata.deptCmt)) {
					uni.showToast({
						title: "请选择部门意见",
						icon: "error"
					});
					return;
				}

				uni.showLoading({
					title: "正在提交",
					success: async () => {
						try {
							if (sdata.deptCmt === '驳回') {
								sdata.applyStatus = "11"; // 驳回
								uni.showModal({
									title: '驳回原因',
									content: '',
									editable: true,
									placeholderText: '请填写',
									success: async (res) => {
										if (res.confirm && res.content.trim()) {
											sdata.auditRemark = res.content.trim();
											uni.showLoading({
												title: '处理中...'
											});
											try {
												let response = await doAudit(sdata);
												uni.hideLoading();
												if (response.code === 200) {
													uni.showToast({
														title: "提交成功"
													});
													setTimeout(() => {
														uni.navigateBack({
															success: () => {
																const pages = getCurrentPages();
																if (pages.length > 0) {
																	const prevPage = pages[pages.length -
																		2]
																	console.log(prevPage);
																	if (prevPage && typeof prevPage
																		.getList ===
																		'function') {
																		prevPage.queryParams.pageNum = 1;
																		prevPage.getList();
																	}
																}
															}
														})
													}, 1000)
												}
											} catch (error) {
												uni.hideLoading();
											}
										} else if (res.confirm) {
											uni.showToast({
												title: '请填写驳回原因',
												icon: 'none'
											});
										}
									}
								});
							} else {
								sdata.applyStatus = sdata.deptCmt === '同意' ? '2' : '10';

								uni.showLoading({
									title: '处理中...'
								});
								doAudit(sdata).then(response => {
									uni.hideLoading();
									if (response.code === 200) {
										uni.showToast({
											title: "提交成功"
										});
										setTimeout(() => {
											uni.navigateBack({
												success: () => {
													const pages = getCurrentPages();
													if (pages.length > 0) {
														const prevPage = pages[pages.length -
															2]
														console.log(prevPage);
														if (prevPage && typeof prevPage
															.getList ===
															'function') {
															prevPage.queryParams.pageNum = 1;
															prevPage.getList();
														}
													}
												}
											})
										}, 1000)
									}
								}).catch(() => {
									uni.hideLoading();
								});
							}

						} catch (error) {} finally {
							this.isSubmitting = false;
						}
					}
				})
				// if (!CheckImgExists(this.baseurl + sdata.zdlsSign)) {
				// 	this.$message.info("签名失效，请重新上传");
				// 	return;
				// }

				// let res = await doAudit(sdata);
				// if (res.code == 200) {
				// 	uni.showToast({
				// 		title: res.msg,
				// 		icon: "success"
				// 	});
				// 	this.getList();
				// 	this.lookV = false;
				// }
			},
			previewImg(imgs, index) {
				let images = imgs.map(img => {
					return {
						path: baseUrl + img
					}
				})
				previewImg(images, index);
			}
		}
	}
</script>

<style scoped lang="scss">
	.detail {
		height: calc(100vh - 44px);
		background-color: #F5F5F7;
		padding: 20rpx 30rpx 0;

		.card {
			margin: 30rpx 0rpx 0;
			padding: 30rpx;
			background-color: white;
			border-radius: 20rpx;

			.title {
				font-weight: bold;
				color: #202020;
				display: inline-block;
				font-size: 30rpx;
				margin-bottom: 30rpx;
			}

			.row {
				margin-bottom: 30rpx;

				&>label:first-child {
					color: #9C9C9C;
					margin-right: 40rpx;
				}

				.photos {
					margin-top: 10rpx;
					display: flex;
					flex-wrap: wrap;

					image {
						margin-bottom: 10rpx;
						width: 23.5%;
						border-radius: 8rpx;
						height: 136rpx;

						&:not(:nth-child(4n)) {
							margin-right: 2%;
						}
					}
				}
			}
		}
	}
</style>