<template>
	<view>
		<commonCard :gwId="gwId"></commonCard>
		<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="istransmit" 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 :model="auditForm" labelWidth="130px" class="u-form default_title" labelPosition="left"
					ref="auditForm">
					<u-form-item :label="istransmit?'办公室意见':isundertake?'承办科(室)意见':isoffice?'分管领导意见':'主要领导意见'"
						borderBottom prop="content">
						<u--textarea v-model="auditForm.content" placeholder=""></u--textarea>
					</u-form-item>
					<u-form-item v-if="!isleader" label="下一位审批人" borderBottom prop="neUser">
						<office-user-select v-model="auditForm.neUser" placeholder="请选择下一位审批人"
							title="审批人" :limit="isoffice?1000:1" :multiple="isoffice"></office-user-select>
					</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>
		<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 commonCard from "./commonCard.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 yzFlowService from '@/api/commonseal/yzFlowService.js'
	import * as $auth from "@/common/auth.js"
	export default {
		onLoad(option) {
			this.gwId = option.id
			yzFlowService.queryByYzId(this.gwId).then(data => {
				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 (role == '局领导' && gw.length > 0) this.isleader = true
				if (role == '办公室主任' && gw.length > 0) this.istransmit = true
				if (role == '分管领导' && gw.length > 0) this.isoffice = true
				if (role == '科室负责人' && gw.length > 0) this.isundertake = true

			})
		},
		components: {
			commonCard,
			signInput,
			officeUserSelect
		},
		data() {
			return {
				loading: false,
				isAll: false,
				show: false,
				one: true,
				auditForm: {
					id: '',
					yzId: '',
					content: '同意',
					signfj: '',
					createTruename: '',
					nextTruename: '',
					nextUser: '',
					which: '0',
					state: '0',
					next: '',
					neUser: ''
				},
				gwId: "",
				isoffice: false,
				isleader: false,
				isundertake: false,
				istransmit: false,
			}
		},
		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 (inputForm.neUser && inputForm.neUser != '') {
					inputForm.next = '1'
				}else {
					inputForm.next = ''
				}
				inputForm.state = "0"
				inputForm.gwId = this.gwId
				const date = moment().format('YYYY-MM-DD HH:mm:ss');
				inputForm.updateTime = date

				yzFlowService.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.redirectTo({
							url: `/pages/commonseal/commonInfo?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"
				yzFlowService.save(inputForm).then(data1 => {
					let param = {
						type: 'success',
						message: "已归档",
						iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/success.png',
					}
					this.showToast(param);
					setTimeout(() => {
						// 重定向到上一页
						uni.redirectTo({
							url: `/pages/commonseal/commonInfo?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>