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

<template>
	<el-form
		ref="form"
		:model="dataForm"
		label-width="100px"
		v-loading="formLoading">
		<el-row :gutter="20">
			<el-col>
				<el-form-item
					label="产线"
					prop="productionId"
					:rules="[{ required: true, message: '不能为空', trigger: 'blur' }]">
					<el-select
						v-model="dataForm.productionId"
						placeholder="请选择产线"
						filterable
						@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>
				<el-form-item
					label="工段"
					prop="sectionId"
					:rules="[{ required: true, message: '不能为空', trigger: 'blur' }]">
					<el-select
						v-model="dataForm.sectionId"
						placeholder="请选择工段"
						filterable
						@change="$emit('update', dataForm)">
						<el-option
							v-for="opt in workshopSectionList"
							:key="opt.value"
							:label="opt.label"
							:value="opt.value" />
					</el-select>
				</el-form-item>
			</el-col>

			<el-col>
				<el-form-item
					label="按钮盒识别码"
					prop="buttonId"
					:rules="[
						{
							type: 'number',
							message: '请输入整数',
							trigger: 'blur',
							transform: (val) => Number.isInteger(Number(val)) && Number(val),
						},
					]">
					<el-input
						v-model="dataForm.buttonId"
						@change="$emit('update', dataForm)"
						placeholder="请输入整数" />
				</el-form-item>
				<!-- 				
				<el-form-item
					label="报警编码"
					prop="code"
					:rules="[{ required: true, message: '不能为空', trigger: 'blur' }]">
					<el-select
						v-model="dataForm.code"
						placeholder="请选择产线"
						@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>
				<el-form-item label="按钮盒模式" prop="model">
					<el-input
						v-model="dataForm.model"
						@change="$emit('update', dataForm)"
						placeholder="请输入按钮盒模式" />
				</el-form-item>
			</el-col>

			<el-col>
				<el-form-item
					label="按钮值"
					prop="keyValue"
					:rules="[
						{
							type: 'number',
							message: '请输入100以内的整数',
							trigger: 'blur',
							transform: (val) =>
								Number(val) <= 100 && Number.isInteger(+val) && Number(val),
						},
					]">
					<el-input
						v-model="dataForm.keyValue"
						type="number"
						min="0"
						max="100"
						@change="$emit('update', dataForm)"
						placeholder="请输入按钮盒模式" />
				</el-form-item>
			</el-col>

			<el-col>
				<el-form-item label="检测内容" prop="inspectionDetContent">
					<el-input
						type="textarea"
						v-model="dataForm.inspectionDetContent"
						placeholder="请输入检测内容"
						@change="$emit('update', dataForm)"></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,
			productionLineList: [],
			workshopSectionList: [],
		};
	},
	mounted() {
		this.getProductionLineList();
		// this.getWorksectionList();
		// this.getCode('/base/equipment-group-alarm/getCode').then((code) => {
		// 	this.formLoading = false;
		// 	this.$emit('update', {
		// 		...this.dataForm,
		// 		code,
		// 	});
		// });
	},
	watch: {
		'dataForm.productionId': {
			handler(id) {
				this.getWorksectionList(id);
			},
			immediate: true
		}
	},
	methods: {
		/** 模拟透传 ref  */
		validate(cb) {
			return this.$refs.form.validate(cb);
		},
		resetFields(args) {
			return this.$refs.form.resetFields(args);
		},
		async getProductionLineList() {
			this.formLoading = true;
			const res = await this.$axios({
				url: '/base/production-line/listAll',
				method: 'get',
			});
			if (res.code == 0) {
				this.productionLineList = res.data.map((item) => ({
					label: item.name,
					value: item.id,
				}));
			}
			this.formLoading = false;
		},

		async getWorksectionList(id) {
			this.formLoading = true;
			const res = await this.$axios({
				url: '/base/workshop-section/listByParentId',
				method: 'get',
				params: {
					id,
				},
			});
			if (res.code == 0) {
				this.workshopSectionList = res.data.map((item) => ({
					label: item.name,
					value: item.id,
				}));
			}
			this.formLoading = false;
		},

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

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

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