<template>
	<view style="padding-left: 24rpx;padding-right: 24rpx;padding-bottom: 16rpx;">
		<u-form :model="form" label-position="top" :label-style="labelStyle" ref="uForm" :rules="rules"
			:errorType="errorType">
			<u-form-item label="检查事项" v-if="form.item_">
				<view>
					<view>
						{{form.item_.cate_name}}
					</view>
					<view>{{form.item_.sortnum}}.{{form.item_.name}}</view>


				</view>
			</u-form-item>
			<u-form-item label="取证要求" v-if="form.item_">
				<span>{{form.item_.require}}</span>
			</u-form-item>
			<u-form-item label="检查结果" prop="result">
				<u-radio-group v-model="form.result">
					<u-radio v-for="(item, index) in resultList" :key="index" :name="item">
						{{ item }}
					</u-radio>
				</u-radio-group>
			</u-form-item>
			<u-form-item label="检查说明">
				<u-input v-model="form.note" type="textarea" :border="true" :maxlength="-1"/>
			</u-form-item>
			<u-form-item label="拍照取证">
				<u-upload :action="vuex_apifile" :header="header" ref="uUpload" :file-list="fileList"></u-upload>
			</u-form-item>
			<u-button @click="submit" type="primary">提交</u-button>
		</u-form>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {

				},
				header: '',
				fileList: [],
				resultList: ['未发现', '发现', '不适用'],
				labelStyle: {
					"color": "gray",
				},
				errorType: ['message'],
				rules: {
					result: [{
						required: true,
						message: '请选择结果',
						// 可以单个或者同时写两个触发验证方式 
						trigger: ['change', 'blur'],
					}],
				}
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		onLoad(options) {
			this.getRecord(options.id)
			this.getHeader()
		},
		methods: {
			getHeader() {
				this.header = {
					Authorization: "Bearer " + this.vuex_token
				}
			},
			getRecord(id) {
				this.$u.api.getInspectRecord(id).then(res => {
					this.form = res.data
					let fileList = []
					for (var i = 0; i < res.data.imgs_.length; i++) {
						fileList.push({
							id: res.data.imgs_[i].id,
							url: res.data.imgs_[i].file + '?token='+ this.vuex_token
						})
					}
					this.fileList = fileList
				})
			},
			submit() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						let sData = {
							id: this.form.id,
							result: this.form.result,
							note: this.form.note,
							imgs: []
						}
						let files = [];
						// 通过filter，筛选出上传进度为100的文件(因为某些上传失败的文件，进度值不为100，这个是可选的操作)
						files = this.$refs.uUpload.lists.filter(val => {
							return val.progress == 100;
						})

						// 如果您不需要进行太多的处理，直接如下即可
						// files = this.$refs.uUpload.lists;
						for (var i = 0; i < files.length; i++) {
							if (files[i].response) {
								sData.imgs.push(files[i].response.data.id)
							} else {
								for (var m = 0; m < this.fileList.length; m++) {
									if (this.fileList[m].url == files[i].url) {
										sData.imgs.push(this.fileList[m].id)
									}
								}
							}

						}
						// console.log(sData.imgs)

						this.$u.api.checkInspectRecord(sData.id, sData).then(res => {
							uni.setStorageSync('inspectrecord', res.data)
							this.$u.toast('提交成功');
							
							
							
							
							uni.navigateBack({

							})
						}).catch(e => {})
					} else {}
				});

			}

		}
	}
</script>

<style>

</style>
