<template>
	<view class="office_page">
		<fileCard :gwId="gwId"></fileCard>
		<view class="office_main">
			<view class="office_title">
				<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="isoffice" :model="auditForm" labelWidth="130px" class="u-form default_title"
					labelPosition="left" ref="auditForm">
					<u-form-item v-if="one" label="办公室拟办" borderBottom prop="content">
						<u--textarea v-model="auditForm.content" placeholder=""></u--textarea>
					</u-form-item>
					<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 label="科室承办" borderBottom prop="nextDepUser">
						<office-user-select v-model="auditForm.nextDepUser" placeholder="请选择科室承办"
							title="科室承办"></office-user-select>
					</u-form-item>
					<u-form-item v-if="one" label="备注" borderBottom prop="remark">
						<u--input v-model="auditForm.remark" placeholder="请输入备注" border="none"></u--input>
					</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="isAll" @click="formSubmit" type="success" text="归档"></u-button>
						<u-button v-if="!show && one" :disabled="!auditForm.nextLeadUser!=''" type="primary" text="确认签字"
							@click="show = true"></u-button>
						<u-button v-if="(show || !one) && !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="show && !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>
					<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" :loading="loading" type="primary" text="加载中"></u-button>
						<u-button v-if="show && !loading" type="primary" text="提交" @click="doSubmit"></u-button>
					</view>
				</u--form>
			</view>
		</view>
		<u-toast ref="uToast"></u-toast>

	</view>

</template>

<script>
	import fileCard from "./fileCard.vue"
	import BASE_URL from '@/config.js'
	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 => {
				if (data.length > 1) this.one = false
				let user = $auth.getUserInfo()
				let role = $auth.getUserInfo().roleNames
				let gw = data.filter(item => {
					return item.nextUser == user.id && item.state == 1
				})
				let down = data.filter(item => item.state == 0)
				if (down.length == data.length) this.isAll = true
				if (gw.length > 0) {
					this.auditForm = gw[0]
				}
				if (role == '办公室主任') this.isoffice = true
				if (role == '局领导' && gw.length > 0) this.isleader = true
				if (role == '科室负责人' && gw.length > 0) this.isundertake = true
			})
		},
		components: {
			fileCard,
			signInput,
			officeUserSelect
		},
		mounted() {

			this.searchForm.officeDTO.id = "1770354910488272897"
			userService.list({
				current: this.tablePage.currentPage,
				size: this.tablePage.pageSize,
				orders: this.tablePage.orders,
				...this.searchForm
			}).then(data => {
				this.leaderList = data.records
			})
		},
		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: '',
					state: '0',

					next: '',
					nextLeadUser: '',
					nextDepUser: '',
					remark: '',
				},
				gwId: "",
				isoffice: false,
				isleader: false,
				isundertake: false,
				leaderList: [],
				tablePage: {
					total: 0,
					currentPage: 1,
					pageSize: 1000,
					orders: [{
						column: "a.create_time",
						asc: false
					}],
				},
				searchForm: {
					loginName: "",
					name: "",
					companyDTO: {
						id: "",
					},
					officeDTO: {
						id: "",
					},
				},
			}
		},
		methods: {
			showToast(params) {
				this.$refs.uToast.show({
					...params,
					complete() {
						params.url && uni.redirectTo({
							url: params.url
						})
					}
				})
			},
			/**
			 * @param {Object} e
			 * 签名完成回调
			 */
			signToUrl(e) {
				console.log("签字=====",e);
				this.auditForm.signfj = e.data
				if (e.error_code && e.error_code === '201') {
					uni.showToast({
						title: e.msg,
						icon: 'none'
					})
					return
				}
			},
			// 提交表单
			doSubmit() {
				this.loading = true
				if (this.auditForm.nextLeadUser != '' || this.auditForm.nextDepUser != '') this.auditForm.next = '1'
				this.auditForm.state = "0"
				gwFlowService.save(this.auditForm).then(data => {
					let param = {
						type: 'success',
						message: data,
						iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/success.png',
						url:"/pages/fileTransmit/fileTransmitList?type=1"
					}
					this.showToast(param);
					// setTimeout(() => {
					// 	uni.navigateTo({
					// 		url: ''
					// 	});
					// }, 500)
				}).catch(() => {
					let param = {
						type: 'error',
						message: data,
						iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/error.png',
					}
					this.showToast(param);
				})
			},
			// 归档
			formSubmit() {
				gwCirculationCard2Service.queryById(this.gwId).then(data => {
					data.state = '3'
					gwCirculationCard2Service.save(data).then(res => {
						let param = {
							type: 'success',
							message: data,
							iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/success.png',
						}
						this.showToast(param);
						setTimeout(() => {
							uni.navigateTo({
								url: 'pages/fileTransmit/fileTransmitList?type=3'
							});
						}, 500)
					}).catch(() => {
						let param = {
							type: 'error',
							message: data,
							iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/error.png',
						}
						this.showToast(param);
					})
				})
			}
		}
	}
</script>

<style>
	.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>