<template>
	<view>
		<fileCard :gwId="gwId"></fileCard>
		<view class="office_main">
			<view class="office_title">
				<uni-section v-if="istransmit" titleColor="#36a7f3" class="mb-10" title="承办转发"
					type="line"></uni-section>
				<uni-section v-if="isoffice" titleColor="#36a7f3" class="mb-10" title="办公室拟办" type="line"></uni-section>
				<uni-section v-if="isleader" titleColor="#36a7f3" class="mb-10" title="领导批示" type="line"></uni-section>
				<uni-section v-if="isundertake" titleColor="#36a7f3" class="mb-10" title="承办科室"
					type="line"></uni-section>
			</view>
			<view class="office_info">
				<u--form v-if="istransmit" :model="auditForm" labelWidth="130px" class="u-form default_title"
					labelPosition="left" ref="auditForm">
					<u-form-item label="转发领导" borderBottom prop="nextLeadUser">
						<jp-picker v-model="auditForm.nextLeadUser" placeholder="请选择转发领导" :range="leaderList"
							rangeKey="name" rangeValue="loginName"></jp-picker>
					</u-form-item>
					<u-form-item v-if="!one" label="科室承办" borderBottom prop="nextDepUser">
						<office-user-select v-model="auditForm.nextDepUser" placeholder="请选择科室承办"
							title="科室承办"></office-user-select>
					</u-form-item>
					<u-form-item label="备注" borderBottom prop="remark">
						<u--input v-model="auditForm.remark" placeholder="请输入备注" border="none"></u--input>
					</u-form-item>
					<view class="submit_btn flex ">
						<u-button v-if="isAll" @click="formSubmit" type="success" text="归档"></u-button>
						<u-button v-if="!loading" type="primary" :disabled="auditForm.nextLeadUser==''" text="确认签字"
							@click="doSubmit"></u-button>
						<u-button v-if="loading" :loading="loading" type="primary" text="加载中"></u-button>
					</view>
				</u--form>
			</view>
			<view class="office_info">
				<u--form v-if="isoffice" :model="auditForm" labelWidth="130px" class="u-form default_title"
					labelPosition="left" ref="auditForm">
					<u-form-item label="办公室拟办" borderBottom prop="content">
						<u--textarea v-model="auditForm.content" placeholder=""></u--textarea>
					</u-form-item>
					<view class="submit_btn flex ">
						<u-button v-if="!loading" type="primary" text="确认签字" @click="doSubmit"></u-button>
						<u-button v-if="loading" :loading="loading" type="primary" text="加载中"></u-button>
					</view>
				</u--form>
				<u--form v-if="isleader" :model="auditForm" labelWidth="130px" class="u-form default_title"
					labelPosition="left" ref="auditForm">
					<u-form-item label="领导批示" borderBottom prop="content">
						<u--textarea v-model="auditForm.content" placeholder=""></u--textarea>
					</u-form-item>
					<!-- <u-form-item v-if="show" label="签字" borderBottom prop="signfj">
						<signInput ref="sign" canvasId="twoDrowCanvas" canvasIds="twoRotateCanvas" :header="header"
							:action="action" @signToUrl="signToUrl">
						</signInput>
					</u-form-item> -->
					<view class="submit_btn flex ">
						<!-- <u-button v-if="!show" type="primary" text="确认签字" @click="show = true"></u-button> -->
						<u-button v-if="!loading" type="primary" text="确认签字" @click="doSubmit"></u-button>
						<u-button v-if="loading" :loading="loading" type="primary" text="加载中"></u-button>
					</view>
				</u--form>
				<u--form v-if="isundertake" :model="auditForm" labelWidth="130px" class="u-form default_title"
					labelPosition="left" ref="auditForm">
					<u-form-item label="承办科室" borderBottom prop="content">
						<u--textarea v-model="auditForm.content" placeholder=""></u--textarea>
					</u-form-item>
					<view class="submit_btn flex ">
						<u-button v-if="loading" :loading="loading" type="primary" text="加载中"></u-button>
						<u-button v-if="!loading" type="primary" text="确认签字" @click="doSubmit"></u-button>
					</view>
				</u--form>
			</view>
		</view>
		<u-toast ref="uToast"></u-toast>
		<u-overlay :show="loading">
			<view class="warp">
				<view class="rect"><u-button plain loading loadingText="加载中"></u-button></view>
			</view>
		</u-overlay>
	</view>

</template>

<script>
	import fileCard from "./fileCard.vue"
	import BASE_URL from '@/config.js'
	import moment from "moment"
	import signInput from "@/components/am-sign-input/am-sign-input.vue"
	import officeUserSelect from "@/components/office-user-select/office-user-select.vue"
	import userService from "@/api/sys/userService"
	import gwFlowService from '@/api/circulation/gwFlowService.js'
	import gwCirculationCard2Service from '@/api/circulation/gwCirculationCard2.js'
	import * as $auth from "@/common/auth.js"
	export default {
		onLoad(option) {
			this.gwId = option.id
			gwFlowService.queryByGwId(this.gwId).then(data => {
				let down = data.filter(item => item.state == 0)
				if (down.length != 1) this.one = false
				if (this.one) {
					this.roleId = "1770353074553331713"
				} else {
					this.roleId = "1773547256256622594"
				}
				userService.leaderList(this.roleId).then(data => {
					this.leaderList = data
					if (this.one) this.auditForm.nextLeadUser = data[0].loginName
					if (!this.auditForm.content) this.auditForm.content = '已阅'
				})
				let user = $auth.getUserInfo()
				let role = $auth.getUserInfo().roleNames
				let gw = data.filter(item => {
					return item.nextUser == user.id && item.state == 1
				})
				
				if (gw.length > 0) {
					this.auditForm = gw[0]
					if (gw[0].which == '4') this.isAll = true
					if (!this.auditForm.content) this.auditForm.content = '已阅'
				}
				if (role == '办公室管理员') {
					this.istransmit = true
					if (gw.length == 0) {
						let gw1 = data.filter(item => {
							return item.which == 3
						})
						gw1.sort((a, b) => b.createTime.localeCompare(a.createTime) || b.createTime.localeCompare(a
							.createTime));
						this.auditForm = gw1[0]
						// if (!this.auditForm.content) this.auditForm.content = '已阅'
					}
				}
				if (role == '局领导' && gw.length > 0) this.isleader = true
				if (role == '办公室主任' && gw.length > 0) this.isoffice = true
				if (role == '分管领导' && gw.length > 0) this.isleader = true
				if (role == '科室负责人' && gw.length > 0) this.isundertake = true

			})
		},
		components: {
			fileCard,
			signInput,
			officeUserSelect
		},
		data() {
			return {
				loading: false,
				isAll: false,
				show: false,
				one: true,
				action: this.BASE_URL + '/gwfile/upload?uploadPath=sign', //上传服务器的地址
				header: {
					"token": $auth.getUserToken()
				}, //图片上传携带头部信息
				auditForm: {
					id: '',
					gwId: '',
					content: '已阅',
					signfj: '',
					createTruename: '',
					nextTruename: '',
					nextUser: '',
					which: '0',
					state: '0',

					next: '',
					nextLeadUser: '',
					nextDepUser: '',
					remark: '',
				},
				gwForm: {},
				gwId: "",
				isoffice: false,
				isleader: false,
				isundertake: false,
				istransmit: false,
				leaderList: [],
				tablePage: {
					total: 0,
					currentPage: 1,
					pageSize: 1000,
					orders: [{
						column: "a.create_time",
						asc: false
					}],
				},
				searchForm: {

					// loginName: "",
					// name: "",
					// companyDTO: {
					// 	id: "",
					// },
					// officeDTO: {
					// 	id: "",
					// },
				},
				roleId: ""
			}
		},
		methods: {
			showToast(params) {
				this.loading = false
				this.$refs.uToast.show({
					...params,
					complete() {
						params.url && uni.redirectTo({
							url: params.url
						})
					}
				})
			},
			/**
			 * @param {Object} e
			 * 签名完成回调
			 */
			signToUrl(e) {

				this.auditForm.signfj = e.data
				if (e.error_code && e.error_code === '201') {
					uni.showToast({
						title: e.msg,
						icon: 'none'
					})
					return
				}
			},
			// 提交表单
			doSubmit() {
				let inputForm = this.auditForm;
				this.loading = true
				if (this.istransmit) {
					inputForm.next = '1'
					if (!this.one) inputForm.which = '3'
				}
				inputForm.state = "0"
				inputForm.gwId = this.gwId
				const date = moment().format('YYYY-MM-DD HH:mm:ss');
				inputForm.updateTime = date

				gwFlowService.save(inputForm).then(data => {
					let param = {
						type: 'success',
						message: data,
						iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/success.png',
					}
					this.showToast(param);
					setTimeout(() => {
						// uni.$emit('refresh', {
						// 	type: 1
						// })
						// 先获取页面栈
						let pages = getCurrentPages();
						// 当前页面的前一个页面
						let prevPage = pages[pages.length - 2];
						// 如果 prevPage 存在,则重定向到上一页
						if (prevPage) {
							// 重定向到上一页
							uni.redirectTo({
								url: `/pages/fileTransmit/fileInfo?id=${this.gwId}` // 注意此处可能需要加上参数保留
							});
						}
					}, 500)
				}).catch(() => {
					let param = {
						type: 'error',
						message: data,
						iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/error.png',
					}
					this.showToast(param);
				})
			},
			// 归档
			formSubmit() {
				this.loading = true
				let inputForm = Object.assign({}, this.auditForm);;
				inputForm.state = '0'
				inputForm.next = "0"
				gwFlowService.save(inputForm).then(data1 => {
					let param = {
						type: 'success',
						message: "已归档",
						iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/success.png',
					}
					this.showToast(param);
					setTimeout(() => {
						// uni.$emit('refresh', {
						// 	type: 3
						// })
						// 先获取页面栈
						let pages = getCurrentPages();
						// 当前页面的前一个页面
						let prevPage = pages[pages.length - 2];
						// 如果 prevPage 存在,则重定向到上一页
						if (prevPage) {
							// 重定向到上一页
							uni.redirectTo({
								url: `/pages/fileTransmit/fileInfo?id=${this.gwId}` // 注意此处可能需要加上参数保留
							});
						}
					}, 500)
				})

			}
		}
	}
</script>

<style>
	.warp {
		display: flex;
		align-items: center;
		justify-content: center;
		height: 100%;
	}

	.office_main {
		width: 100%;
		margin: 5px 0;
		padding: 0 20px;
		background-color: #fefefe;
	}

	.office_main .office_title {
		width: 100%;
		border-bottom: 1px solid #eee;
	}

	.submit_btn {
		background-color: #fefefe;
		bottom: 10px;
		box-shadow: none;
		padding: 20px 0;
	}

	.submit_btn button {
		height: 40px;
		width: 40%;
		border-radius: 30px;
	}
</style>