<template>
	<view>
		<uni-collapse>
			<uni-forms ref="form" :modelValue="formData" :rules="rules">
				<uni-forms-item label="作业编码" :labelWidth='90' name="code">
					<utils-input prefixIcon="scan" @iconClick="scanBar" @confirm="scanBarCode" v-model="formData.code"  @input="handleInput"
						type="text" :key="input.boolean" :focus='true'  @focus="focus" @click="focus"></utils-input>
				<!-- 	<uni-easyinput suffixIcon="scan" @iconClick="scanBar" @confirm="scanBarCode" v-model="formData.code"  @input="handleInput"
						type="text" :key="input.boolean"/> -->
				</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";
	import utilsInput from "@/utils/uniapp/utilsInput.vue"
	export default {
		components:{
			utilsInput
		},
		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 {
				input:{
					boolean: 0 ,
					inputValue : '',
				},
				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)
				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++) {
					try {
						const result = await this.uploadFilePromise(lists[i].url)
						let item = this[`fileList${event.name}`][fileListLen]
						this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
							status: 'success',
							message: '',
							url: result
						}))
						fileListLen++
					} catch (e) {
						this[`fileList${event.name}`].splice(fileListLen, 1);
					}
					
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uploadImage({
						filePath: url
					}).then(response => {
						uni.showToast({
							title: "上传成功",
							icon: 'success'
						});
						resolve(response.fileName)
					}).catch(e => {
						reject(e);
					});
				})
			},
			// selectQcFlag(e) {
			// 	console.log(e, this.formData.qcFlag)
			// },
			scanBarCode(code) {
				if (code) {
					listPwoJob({
						code: code
					}).then(async 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(', ');
								}
								_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) {
							}
						}
					});
				});
			},
			
			/**
			 * 隐藏软键盘
			 */
			focus(){
				uni.hideKeyboard() 
			},
			/**
			 * 不让手动输入
			 * @param {Object} value
			 */
			handleInput(value) {
				const {inputValue} = this.input
				if(value.length - inputValue.length == 1 || inputValue.length - value.length == 1){
					this.$modal.msg("请扫描")
					this.formData.code = null
					this.input.inputValue = value
					this.input.boolean = this.input.boolean == 0 ? 10 :0
					return
				}
				this.input.inputValue = value
				
			},
		}
	}
</script>

<style>
</style>