<!--
    filename: dialogForm.vue
    author: liubin
    date: 2023-09-11 15:55:13
    description: DialogForm for qualityInspectionRecord only
-->

<template>
	<el-form
		ref="form"
		:model="innerDataForm"
		label-width="100px"
		v-loading="formLoading">
		<el-row :gutter="20">
			<el-col :span="12">
				<el-form-item
					label="检测内容"
					prop="inspectionDetId"
					:rules="[{ required: true, message: '不能为空', trigger: 'blur' }]">
					<el-select
						v-model="innerDataForm.inspectionDetId"
						placeholder="请选择检测内容"
						filterable
						clearable
						@change="handleInspectionDetChange">
						<el-option
							v-for="opt in inspectionDetList"
							:key="opt.value"
							:label="opt.label"
							:value="opt.value" />
					</el-select>
				</el-form-item>
			</el-col>

			<el-col :span="12">
				<el-form-item
					label="来源"
					prop="source"
					:rules="[{ required: true, message: '不能为空', trigger: 'blur' }]">
					<el-select
						v-model="innerDataForm.source"
						placeholder="请选择来源"
						filterable
						clearable
						@change="$emit('update', innerDataForm)">
						<el-option
							v-for="opt in [
								{ label: '手动', value: 1 },
								{ label: '自动', value: 2 },
							]"
							:key="opt.value"
							:label="opt.label"
							:value="opt.value" />
					</el-select>
				</el-form-item>
			</el-col>
		</el-row>
		<el-row :gutter="20">
			<el-col :span="12">
				<el-form-item
					label="产线"
					prop="productionLineId"
					:rules="[{ required: true, message: '不能为空', trigger: 'blur' }]">
					<el-select
						v-model="innerDataForm.productionLineId"
						placeholder="请选择产线"
						filterable
						clearable
						@change="handleProductlineChange">
						<el-option
							v-for="opt in productionLineList"
							:key="opt.value"
							:label="opt.label"
							:value="opt.value" />
					</el-select>
				</el-form-item>
			</el-col>
			<el-col :span="12">
				<el-form-item
					label="工段"
					prop="sectionId"
					:rules="[{ required: true, message: '不能为空', trigger: 'blur' }]">
					<el-select
						v-model="innerDataForm.sectionId"
						placeholder="请选择工段"
						clearable
						filterable
						@change="$emit('update', innerDataForm)">
						<el-option
							v-for="opt in sectionList"
							:key="opt.value"
							:label="opt.label"
							:value="opt.value" />
					</el-select>
				</el-form-item>
			</el-col>
		</el-row>
		<el-row :gutter="20">
			<el-col :span="12">
				<el-form-item label="检测人员" prop="checkPerson">
					<el-input
						v-model="innerDataForm.checkPerson"
						clearable
						@change="$emit('update', innerDataForm)"
						placeholder="请输入检测人员" />
				</el-form-item>
			</el-col>

			<el-col :span="12">
				<el-form-item
					label="检测时间"
					prop="checkTime"
					:rules="[{ required: true, message: '不能为空', trigger: 'blur' }]">
					<el-date-picker
						v-model="innerDataForm.checkTime"
						type="datetime"
						placeholder="请选择检测时间"
						value-format="timestamp"
						@change="$emit('update', innerDataForm)"></el-date-picker>
				</el-form-item>
			</el-col>
		</el-row>

		<el-row :gutter="20">
			<el-col>
				<el-form-item label="描述" prop="explainText">
					<el-input
						v-model="innerDataForm.explainText"
						placeholder="请输入描述信息"
						@change="$emit('update', innerDataForm)"
						type="textarea"></el-input>
				</el-form-item>
			</el-col>
			<el-col>
				<el-form-item label="备注" prop="remark">
					<el-input
						v-model="innerDataForm.remark"
						@change="$emit('update', innerDataForm)"
						placeholder="请输入备注"></el-input>
				</el-form-item>
			</el-col>
		</el-row>
	</el-form>
</template>

<script>
export default {
	name: 'DialogForm',
	model: {
		prop: 'dataForm',
		event: 'update',
	},
	emits: ['update'],
	components: {},
	props: {
		dataForm: {
			type: Object,
			default: () => ({}),
		},
	},
	data() {
		return {
			formLoading: true,
			inspectionDetList: [],
			productionLineList: [],
			sectionList: [],
			innerDataForm: {},
			cacheInspectionDetList: null,
		};
	},
	mounted() {
		Promise.all([this.getProductLineList(), this.getInspectionDetList()]).then(
			() => {
				this.formLoading = false;
			}
		);
	},
	watch: {
		// 'innerDataForm.productionLineId': {
		// 	handler: async function (plId) {
		// 		if (plId) await this.getWorksectionList(plId);
		// 	},
		// 	immediate: true,
		// },
		dataForm: {
			handler: function (dataForm) {
				this.innerDataForm = Object.assign({}, dataForm);

				if (dataForm.productionLineId)
					this.getWorksectionList(dataForm.productionLineId);
			},
			immediate: true,
		},
	},
	methods: {
		/** 模拟透传 ref  */
		validate(cb) {
			return this.$refs.form.validate(cb);
		},
		resetFields(args) {
			return this.$refs.form.resetFields(args);
		},

		handleInspectionDetChange(value) {
			const { id, content } = this.cacheInspectionDetList.find(
				(item) => item.id == value
			);
			this.innerDataForm.inspectionDetId = id;
			this.innerDataForm.inspectionDetContent = content;
			this.$emit('update', this.innerDataForm);
		},

		async handleProductlineChange(id) {
			// await this.getWorksectionList(id);
			this.innerDataForm.sectionId = null;
			this.$emit('update', this.innerDataForm);
		},

		// getCode
		async getCode(url) {
			const response = await this.$axios(url);
			return response.data;
		},

		// 获取产线列表
		async getProductLineList() {
      const response = await this.$axios('/base/core-production-line/listAll');
			this.productionLineList = response.data.map((item) => ({
				label: item.name,
				value: item.id,
			}));
		},

		// 获取检测内容列表
		async getInspectionDetList() {
			const response = await this.$axios(
				'/base/quality-inspection-det/listAll'
			);
			this.cacheInspectionDetList = response.data;
			this.inspectionDetList = response.data.map((item) => ({
				label: item.content,
				value: item.id,
			}));
		},

		// 获取工段列表
		async getWorksectionList(plId) {
			const response = await this.$axios(
				'/base/workshop-section/listByParentId',
				{
					params: {
						id: plId,
					},
				}
			);
			this.sectionList = response.data.map((item) => ({
				label: item.name,
				value: item.id,
			}));
		},
	},
};
</script>

<style scoped lang="scss">
.el-date-editor,
.el-select {
	width: 100%;
}
</style>
