<template>
	<view class="content">
		<u-navbar back-text="返回" title="隐患整改清单" back-icon-color="#ffffff" :back-text-style="{color: '#ffffff'}" title-color="#ffffff"
		 :background="{backgroundColor: mainColor}" :border-bottom="false"></u-navbar>
		<view class="handle-box">
			<text class="handle-button" @click="goToAutographForm" v-if="form.signState == 1">签字</text>
			<text class="handle-button" @click="downloadPDF">下载</text>
		</view>
		<u-image width="100%" mode="widthFix" :src="'data:image/png;base64,' + item.pic" v-for="(item, index) in contractInfo"
		 :key="index"></u-image>
		<view class="handle-fix-box">
			<u-button type="primary" :ripple="true" :custom-style="customStyle" @click="submitRectify()" :disabled="form.signState != 1">已签阅</u-button>
		</view>
		<u-top-tips ref="uTips" :navbar-height="statusBarHeight + navbarHeight"></u-top-tips>
	</view>
</template>

<script>
	var context = null
	import {
		mapGetters
	} from 'vuex'
	const NET = require('@/utils/request')
	const API = require('@/config/api')
	export default {
		computed: {
			...mapGetters([
				'mainColor',
				'customStyle',
			])
		},
		data() {
			return {
				statusBarHeight: uni.getSystemInfoSync().statusBarHeight,
				navbarHeight: 44,
				form: {
					tenantId: '',
					assetId: '',
					taskId: '',
					signState: '',
				},
				contractInfo: [],
			}
		},
		onLoad(options) {
			this.form.tenantId = options.tenantId
			this.form.assetId = options.assetId
			this.form.taskId = options.taskId
			this.form.signState = options.signState
			this.getContractInfo()
		},
		onShow() {},
		methods: {
			//  获取数据
			getContractInfo(parentName, sex) {
				NET.request(API.getDangerInfo + this.form.taskId, {}, 'GET').then(res => {
					this.contractInfo = res.data
				}).catch(error => {
					this.$refs.uTips.show({
						title: error.message,
						type: 'warning',
					})
				})
			},
			//  跳转签字表单
			goToAutographForm() {
				uni.navigateTo({
					url: '/pagesHandle/autographForm?taskId=' + this.form.taskId
				});
			},
			//  下载PDF
			downloadPDF() {
				NET.request(API.downloadPDF + this.form.taskId, {}, 'GET').then(res => {
					uni.downloadFile({
						url: res.data,
						header: {
							Authorization: 'Bearer ' + uni.getStorageSync('token')
						},
						success: (res1) => {
							uni.saveFile({
								tempFilePath: res1.tempFilePath,
								success: (res2) => {
									uni.getSavedFileList({
									  success: (res3) => {
											// this.$refs.uTips.show({
											// 	title: res3.fileList.length,
											// 	type: 'success',
											// })
									  }
									});
									uni.openDocument({
										filePath: res2.savedFilePath,
										success: (res3) => {
											this.$refs.uTips.show({
												title: '下载成功',
												type: 'success',
											})
										}
									});D
								},
							});
						}
					});
				}).catch(error => {
					this.$refs.uTips.show({
						title: error.message,
						type: 'warning',
					})
				})
			},
			//  已签阅
			submitRectify() {
				uni.navigateTo({
					url: '/pagesHandle/autographForm?taskId=' + this.form.taskId
				});
				return false
				NET.request(API.submitRectifyForm, {}, 'POST').then(res => {
					this.$refs.uTips.show({
						title: '签阅成功',
						type: 'success',
					})
					setTimeout(() => {
						uni.reLaunch({
							url: '/pages/index/index'
						})
					}, 1000)
				}).catch(error => {
					this.$refs.uTips.show({
						title: error.message,
						type: 'warning',
					})
				})
			}
		},
	}
</script>

<style>
	page {
		width: 100%;
		height: 100%;
	}
</style>
<style lang="scss" scoped>
	@import "@/static/css/themes.scss";

	.content {
		width: 100%;
		height: 100vh;
		float: left;
		padding: 0px 15px 60px 15px;
		box-sizing: border-box;

		.handle-box {
			width: calc(100% + 30px);
			margin-left: -15px;
			padding: 0px 15px;
			height: 30px;
			background-color: $mainColor;
			text-align: right;

			.handle-button {
				color: #FFFFFF;
				font-size: 14px;
				line-height: 30px;
				margin-left: 16px;
			}
		}
	}
</style>
