<template>
	<view class="container">
		<view class="item-lists">
			<view class="title">工单号</view>
			<view class="info">{{info.ticketNo || ''}}</view>
		</view>
		<view class="item-lists">
			<view class="title">工单标题</view>
			<view class="info">{{info.title || ''}}</view>
		</view>
		<view class="item-lists">
			<view class="title">提交用户</view>
			<view class="info">{{info.userName || '-'}}</view>
		</view>
		<view class="item-lists">
			<view class="title">问题描述</view>
			<view class="info">{{info.content || ''}}</view>
		</view>
		<view class="item-lists">
			<view class="title">工单状态</view>
			<view class="info">{{ticketStatusOptions[info.status] || ''}}</view>
		</view>
		<view class="item-lists">
			<view class="title">创建时间</view>
			<view class="info">{{info.createTime || '-'}}</view>
		</view>
		<view class="item-lists">
			<view class="title">预期完成时间</view>
			<view class="info">{{info.expectedCompleteTime || '-'}}</view>
		</view>
		<view class="item-lists">
			<view class="title">处理人</view>
			<view class="info">{{info.workName || '-'}}</view>
		</view>
		<view class="item-lists">
			<view class="title">上传图片</view>
			<view class="info">
				<uni-file-picker v-model="imgUrl" :auto-upload="false" :limit="1" file-mediatype="image"
					file-extname="png,jpg" :image-styles="imageStyles" @select="select">
				</uni-file-picker>
			</view>
		</view>
		<button class="submit-button" type="primary" style="margin-top:30px;" :loading="loading"
			@click="submit">提交</button>
	</view>
</template>

<script>
	import request from '@/utils/request'
	import {
		getToken
	} from '@/utils/auth'
	import config from '@/config'
	import {
		mapState
	} from 'vuex'
	import {
		getTicket,
		updateTicket
	} from '@/api/ems/ticket'
	export default {
		computed: {
			...mapState({
				ticketStatusOptions: (state) => state.ems.ticketStatusOptions
			})
		},
		data() {
			return {
				imageStyles: {
					width: 150,
					height: 100,
				},
				loading: false,
				info: {},
				imgUrl: [],
			}
		},
		methods: {
			submit() {
				if (this.imgUrl.length === 0 || this.imgUrl[0].url === '') {
					uni.showToast({
						icon: 'none',
						title: '请上传图片',
						duration: 2000
					});
					return
				}
				this.loading = true
				const images = this.imgUrl.map(item => item.url)
				updateTicket({
					id: this.info.id,
					images: JSON.stringify(images)
				}).then(response => {
					if (response.code === 200) {
						uni.showToast({
							title: '提交成功',
							duration: 2000
						});
						uni.switchTab({
							url: '/pages/ticket/list'
						});
					}
				}).finally(() => this.loading = false)

			},
			// 获取上传状态
			select(e) {
				uni.uploadFile({
					url: config.baseUrl + "/common/upload",
					header: {
						Authorization: "Bearer " + getToken(),
					},
					filePath: e.tempFilePaths[0],
					name: 'file', //对应接口的名称
					complete: (res) => {
						console.log('返回数据', res);
						if (res?.statusCode === 200 && res?.data) {
							//图片上传成功
							if (res?.data) {
								const data = JSON.parse(res.data)
								this.imgUrl = [{
									name: data.newFileName || '',
									extname: data.originalFilename || '',
									url: data.url || ''
								}]
							} else {
								uni.showToast({
									icon: 'none',
									title: '图片上传失败',
									duration: 2000
								});
								this.imgUrl = []
							}

						} else {
							uni.showToast({
								icon: 'none',
								title: '图片上传失败',
								duration: 2000
							});
						}
					}
				})
			},
		},
		onLoad(options) {
			uni.showLoading()
			getTicket(options.id).then(response => {
				this.info = JSON.parse(JSON.stringify(response?.data || {}))
				const {
					images = ''
				} = this.info
				this.info.images = JSON.parse(images)
				const imgList = this.info.images || []
				if (imgList.length > 0) {
					this.imgUrl = imgList.map(item => {
						return {
							name: item || '',
							extname: item || '',
							url: item
						}
					})
				}
			}).finally(() => {
				uni.hideLoading()
			})

		}
	}
</script>

<style scoped lang="scss">
	.container {
		background-color: #ffffff;
		padding: 40rpx;
		color: #19242d;
	}

	.item-lists {
		box-shadow: 0 0 20rpx rgba(0, 0, 0, .1), 0 0 0 rgba(0, 0, 0, .5);
		border-radius: 10rpx;
		font-size: 26rpx;
		line-height: 30rpx;
		margin-bottom: 40rpx;

		.title {
			border-radius: 14rpx 14rpx 0 0;
			border-bottom: 1px solid #eee;
			padding: 20rpx 30rpx;
			font-weight: 700;
			position: relative;

			&::after {
				content: '';
				display: block;
				background-color: #4c7af3;
				height: 30rpx;
				width: 6rpx;
				position: absolute;
				left: 10rpx;
				top: 20rpx;
			}
		}

		.info {
			padding: 20rpx 30rpx;
			font-size: 24rpx;
		}
	}

	.submit-button {
		font-size: 28rpx;
	}
</style>
