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

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

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

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

			<uv-form-item label="照片" prop="form.images">
				<view class="image-list">
					<view @click="openImageMenu(index)" v-for="(item,index) in form.images" :key="index"
						class="image-item">
						<image :src="item" mode="widthFix"></image>
					</view>
					<view v-if="form.images.length < maxUpload" @click="chooseImage" 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" cancelText="取消" title="图片操作"
				@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: {
					processingResult: '',
					money: 0,
					instructions: '',
					images: []
				},
				rules: {

				},
				processingList: [
					[{
							id: 0,
							label: '已处理'
						},
						{
							id: 1,
							label: '未处理'
						}
					]
				],
				maxUpload: 4,
				currentIndex: 0,
				list: [ //图片操作菜单操作项
					{
						name: '查看图片',
						id: 0
					},
					{
						name: '删除图片',
						id: 1
					}
				],
			}
		},
		methods: {
			//返回
			leftClick() {
				uni.switchTab({
					url: '/pages/repairList/repairList'
				})
			},

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

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

			//选择文件
			chooseImage() {
				this.$utils.chooseImage(res => {
					res.tempFiles.forEach(file => {
						console.log(file);
						this.aliUpload(file)
					})
				})
			},

			//阿里云oss上传
			aliUpload(file) {
				this.$uploadFileToOSS(file).then(filePath => {
					this.form.images.push(filePath)
				})
			},

			//打开图片操作菜单
			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.images
				})
			},

			//删除图片
			deleteImageAsList() {
				this.form.images = this.form.images.filter((_, index) => {
					return index != this.currentIndex
				})
			},
		}
	}
</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%;
	}
</style>