<!-- 结单 -->
<template>
	<view class="finish">
		<!-- <navbar title="结单" :leftClick="leftClick"></navbar> -->

		<uv-form labelPosition="top" :model="form" errorType="toast" :rules="rules" ref="form" labelWidth="140">
			<uv-form-item @click="processingPickerOpen" label="处理结果" prop="successTitle">
				<uv-input readonly placeholder="请选择处理结果" v-model="form.successTitle"
					:fontSize="30"></uv-input>
			</uv-form-item>

			<uv-form-item label="收费金额" prop="successPrice">
				<uv-input placeholder="请输入收费金额" type="number" v-model="form.successPrice" :fontSize="30"></uv-input>
			</uv-form-item>

			<uv-form-item label="处理说明" prop="sucessText">
				<uv-textarea v-model="form.sucessText" :maxlength="200" :height="120" count
					placeholder="请输入处理说明"></uv-textarea>
			</uv-form-item>

			<uv-form-item label="照片" prop="successImage">
				<view class="image-list">
					<view @click="openImageMenu(index)" v-for="(item,index) in form.successImage" :key="index"
						class="image-item">
						<image :src="item" mode="widthFix"></image>
					</view>
					<view v-if="form.successImage.length < maxUpload" @click="uploadImage" class="upload">
						<uv-icon name="camera-fill" color="#3c9cff" size="50"></uv-icon>
					</view>
				</view>
			</uv-form-item>

			<uv-button type="primary" text="结单" shape="circle" customStyle="margin-top: 10px"
				@click="submit"></uv-button>

			<!-- 选择完成状态 -->
			<uv-picker ref="processing" :columns="processingList" :itemHeight="100" :round="20" keyName="label"
				title="选择处理结果" @confirm="floorConfirm"></uv-picker>

			<!-- 图片操作菜单 -->
			<uv-action-sheet ref="actionSheet" :actions="list" :round="20" safeAreaInsetBottom
				@select="selectImageSheet"> </uv-action-sheet>
		</uv-form>
	</view>
</template>

<script>
	import navbar from '../../components/base/navbar.vue'

	export default {
		name: 'Finish',
		components: {
			navbar
		},
		data() {
			return {
				form: {
					successTitle: '',
					successPrice: '',
					sucessText: '',
					successImage: []
				},
				rules: {
					successTitle: {
						type: 'string',
						required: true,
						message: '请选择处理结果',
						trigger: ['blur', 'change']
					},
					successPrice: {
						type: 'string',
						required: true,
						message: '请填写收费金额',
						trigger: ['blur', 'change']
					},
					sucessText: {
						type: 'string',
						required: true,
						message: '请输入处理说明',
						trigger: ['blur', 'change']
					},
					successImage: {
						type: 'array',
						required: true,
						message: '请上传图片',
						trigger: ['blur', 'change']
					},
				},
				processingList: [
					[{
							id: 0,
							label: '处理完成'
						},
						// {
						// 	id: 1,
						// 	label: '未处理'
						// }
					]
				],
				maxUpload: 4,
				currentIndex: 0,
				list: [ //图片操作菜单操作项
					{
						name: '查看图片',
						id: 0
					},
					{
						name: '删除图片',
						id: 1
					}
				],
			}
		},
		onLoad: function(options) {
			this.form.id = options.orderId || ''
		},
		methods: {
			//返回
			leftClick() {
				uni.reLaunch({
					url: '/pages/order/order'
				})
			},

			//打开处理结果
			processingPickerOpen() {
				uni.hideKeyboard()
				this.$refs.processing.open();
			},

			//用户选择了处理结果
			floorConfirm(floor) {
				this.form.successTitle = floor.value[0].label
			},

			//上传图片
			uploadImage() {
				let self = this
				this.$Oss.ossUploadImage({
					success(res) {
						self.form.successImage.push(res)
					}
				})
			},

			//打开图片操作菜单
			openImageMenu(index) {
				this.currentIndex = index
				this.$refs.actionSheet.open();
			},

			//用户选择了图片操作选项
			selectImageSheet(imageSheet) {
				let {
					id
				} = imageSheet
				if (id) {
					this.deleteImageAsList()
				} else {
					this.viewImageAsList()
				}
			},

			//查看图片
			viewImageAsList() {
				this.$utils.previewImage({
					current: this.currentIndex,
					urls: this.form.successImage
				})
			},

			//删除图片
			deleteImageAsList() {
				this.form.successImage = this.form.successImage.filter((_, index) => {
					return index != this.currentIndex
				})
			},

			//结单
			submit() {
				this.$refs.form.validate().then(res => {
					this.$api('editSchoolOrderSuccess', { ...this.form , successImage : this.form.successImage.join(',') }, res => {
						if (res.code == 200) {
							uni.showToast({
								icon: 'none',
								title: '结单完成'
							})
							this.leftClick()
						}
					})
				}).catch(errors => {})
			}
		}
	}
</script>

<style>
	.finish {
		width: 96%;
		margin: 0rpx auto;
	}

	.image-list {
		width: 100%;
		display: flex;
		flex-wrap: wrap;
		padding-top: 20rpx;
	}

	.image-item,
	.upload {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 24%;
		height: 174rpx;
		background: #f8f8f8;
		margin-left: 1%;
		border-radius: 15rpx;
		overflow: hidden;
		margin-bottom: 20rpx;
		;
	}

	.image-item image {
		width: 100%;
	}
	
	/deep/ .uv-safe-area-inset-bottom {
	    padding-bottom: 0 !important
	}
</style>