<template>
	<view>
		<view class="form-area">
			<uni-forms :modelValue="crashForm" label-position="top">
				<uni-forms-item
					label="报废数量"
					v-show="ifNo == 0"
				>
					<uni-easyinput type="number" placeholder="请输入" v-model="crashForm.scrapNum"></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item
					label="报废原因"
				>
					<uni-easyinput placeholder="请输入报废原因" v-model="crashForm.scrapReason"></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item
					label="损坏原因"
				>
					<uni-data-checkbox v-model="crashForm.scrapType" :localdata="brokeRange"></uni-data-checkbox>
				</uni-forms-item>
				<uni-forms-item
					label="备注"
				>
					<uni-easyinput placeholder="请输入" v-model="crashForm.remark"></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item
					label="报废图片"
				>
					<uni-file-picker 
						v-model="crashForm.ids" 
						@select="uploadPhoto" 
						limit="3" 
						title="最多选择3张图片"
						:del-icon="false"
					></uni-file-picker>
				</uni-forms-item>
				<button style="background-color: #3788FF; color: #fff;" @click="subForm">提交</button>
			</uni-forms>
		</view>
		<u-loading-page :loading="showLoading" color="#000" loading-text="表单提交中,请稍后..."></u-loading-page>
	</view>
</template>

<script>
import { basePath, systemPath } from '../../public';
	export default {
		data() {
			return {
				ifNo: '',
				showLoading: false,
				crashForm: {
					scrapNum: 1,
					scrapReason: '',
					scrapType: '',
					remark: '',
					fileIds: []
				},
				brokeRange: [
					{ text: '人为', value: '1' },
					{ text: '自然', value: '0' }
				],
				totalSub: {},
				id: '',
				taskId: '',
				maId: '',
				typeId: ''
			}
		},
		methods: {
			uploadPhoto (e) {
				let that = this
				console.log(e);
				// this.crashForm.crashPic = e.tempFilePaths
				// 上传图片
				for (let i = 0; i < e.tempFilePaths.length; i++) {
					that.$api.fix.uploadPic(e.tempFilePaths[i]).then(res => {
						let dealedData = JSON.parse(res.data)
						console.log(dealedData);
						if (dealedData.code == 200) {
							that.crashForm.fileIds.push(dealedData.data.id)
						}
					}).catch(err => {
						console.log(err);
					})
				}
			},
			subForm () {
				let that = this
				this.crashForm.fileIds = this.crashForm.fileIds.join(',')
				this.crashForm.id = this.id
				this.crashForm.taskId = this.taskId
				this.crashForm.maId = this.maId
				this.crashForm.typeId = this.typeId
				this.crashForm.companyId = uni.getStorageSync('userInfo').sysUser.companyId
				this.crashForm.userId = uni.getStorageSync('userInfo').userid
				this.crashForm.repairType = 3
				console.log(this.crashForm);
				this.showLoading = true
				that.$api.fix.submitFix(that.crashForm).then(res => {
					console.log(res);
					if (res.data.code == 200) {
						uni.showToast({
							icon: 'none',
							title: res.data.msg,
							success: () => {
								that.showLoading = false
								/* uni.switchTab({
									url: '/pages/workSpace/workSpace'
								}) */
								uni.navigateBack()
							}
						})
					} else {
						that.showLoading = false
						uni.showToast({
							icon: 'none',
							title: res.data.msg
						})
					}
				}).catch(err => {
					console.log(err);
				})
			}
		},
		onLoad(params) {
			this.id = params.id
			this.taskId = params.taskId
			this.maId = params.maId
			this.typeId = params.typeId
			this.ifNo = params.ifNo
		}
	}
</script>

<style lang="scss">
	.form-area{
		width: 90%;
		margin: 2vh auto;
	}
</style>
