<template>
	<view>
		<uni-collapse>
			<uni-forms ref="form" :modelValue="formData" :rules="rules">
				<uni-forms-item label="作业编码" :labelWidth='90' name="code">
					<uni-easyinput suffixIcon="scan" @iconClick="scanBar" @confirm="scanBarCode" v-model="formData.code"
						type="text" />
				</uni-forms-item>
				<uni-forms-item label="质检结果" :labelWidth='90' name="qcFlag">
					<u-radio-group v-model="formData.qcFlag" placement="row">
						<u-radio v-for="(item, index) in qc_bill_result_Options" :key="index" :label="item.text"
							:name="item.name"></u-radio>
					</u-radio-group>
				</uni-forms-item>
				<uni-forms-item label="质检人" :labelWidth='90' name="qcBy">
					<uni-easyinput v-model="formData.qcBy" type="text" suffixIcon="scan" @iconClick="scanBarqcBy" />
				</uni-forms-item>
				<uni-forms-item label="图片" :labelWidth='90' name="qcImg">
					<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
						:maxCount="10"></u-upload>
				</uni-forms-item>
			</uni-forms>
		</uni-collapse>
		<u-button type="primary" @click="submit">提交</u-button>
	</view>
</template>

<script>
	import {
		getPwoJob,
		listPwoJob,
		updatePwoJob,
	} from "@/api/mes/jobReport.js";
	import {
		getDicts
	} from "@/api/system/dict/dictData.js";
	import {
		uploadImage,
		getStandardList,
		addQualityHistoryList,
		listQualityHistory,
		updateQualityHistory
	} from "@/api/qc/qc.js";
	export default {
		mounted() {
			getDicts("qc_bill_result").then(res => {
				this.qc_bill_result_Options = res.data.map(dict => {
					return {
						text: dict.dictLabel,
						name: dict.dictValue,
						diasble: false
					}
				});
			})
		},
		data() {
			return {
				qc_bill_result_Options: [],
				fileList1: [],
				formData: {
					code: null,
					qcFlag: null,
					qcBy: null,
					qcImg: null
				},
				rules: {
					code: {
						rules: [{
							required: true,
							errorMessage: '请输入作业!'
						}]
					},
					qcFlag: {
						rules: [{
							required: true,
							errorMessage: '请输入质检结果!'
						}]
					},
					qcBy: {
						rules: [{
							required: true,
							errorMessage: '请输入质检人!'
						}]
					},
				}
			}
		},
		methods: {
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				console.log(event)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					// console.log(item)
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uploadImage({
						filePath: url
					}).then(response => {
						uni.showToast({
							title: "上传成功",
							icon: 'success'
						});
						console.log(response)
						resolve(response.fileName)
					});
				})
			},
			// selectQcFlag(e) {
			// 	console.log(e, this.formData.qcFlag)
			// },
			scanBarCode(code) {
				if (code) {
					listPwoJob({
						code: code
					}).then(async res => {
						console.log(res);
						if (res.rows != null && res.rows.length > 0) {
							this.formData = res.rows[0]
						} else {
							this.$modal.msg("未查询到该作业！");
						}
					});
				}
			},
			//作业编码
			scanBar() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.formData.code = res.result;
						_this.scanBarCode(_this.formData.code);
					}
				});
			},
			//质检人
			scanBarqcBy() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.formData.qcBy = res.result;
						_this.scanBarCode(_this.formData.qcBy);
					}
				});
			},
			submit() {
				const _this = this;
				_this.$refs.form.validate().then(res => {
					uni.showModal({
						title: '提示',
						content: '您确定完成该质检吗？',
						success: function(res) {
							if (res.confirm) {
								if (_this.fileList1.length > 0) {
									_this.formData.qcImg = _this.fileList1.map(obj => obj.url)
										.join(', ');
								}
								console.log(_this.formData)
								_this.$modal.loading('提交中')
								updatePwoJob(_this.formData).then(response => {
									_this.$modal.closeLoading();
									_this.$modal.msgSuccess("质检成功!");
									setTimeout(() => {
										_this.$tab.switchTab(
											"/pages/work/index");
									}, 500);
								});
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				});
			},
		}
	}
</script>

<style>
</style>