<!-- 
    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 :span="12">
				<el-form-item
					label="产线"
					prop="productionLineId"
					:rules="[{ required: true, message: '不能为空', trigger: 'blur' }]">
					<el-select
						v-model="dataForm.productionLineId"
						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 :span="12">
				<el-form-item
					label="工段"
					prop="workshopSectionId"
					:rules="[{ required: true, message: '不能为空', trigger: 'blur' }]">
					<el-select
						v-model="dataForm.workshopSectionId"
						filterable
						placeholder="请选择工段"
						@change="$emit('update', dataForm)">
						<el-option
							v-for="opt in worksectionList"
							: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="equipmentId"
					:rules="[{ required: true, message: '不能为空', trigger: 'blur' }]">
					<el-select
						v-model="dataForm.equipmentId"
						filterable
						placeholder="请选择设备"
						@change="$emit('update', dataForm)">
						<el-option
							v-for="opt in equipmentList"
							: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="sort">
					<el-input-number
						v-model="dataForm.sort"
						filterable
						min="0"
						max="100"
						@change="$emit('update', dataForm)"
						placeholder="请输入工段排序" />
				</el-form-item>
			</el-col>
		</el-row>
		<el-row :gutter="20">
			<el-col :span="12">
				<el-form-item label="产线数据类型" prop="lineDataType">
					<el-select
						v-model="dataForm.lineDataType"
						placeholder="请选择产线数据类型"
						@change="$emit('update', dataForm)">
						<el-option
							v-for="opt in [
								{ label: '无类型', value: 0 },
								{ label: '进口统计', value: 1 },
								{ label: '出口统计', value: 2 },
							]"
							: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="sectionDataType">
					<el-select
						v-model="dataForm.sectionDataType"
						placeholder="请选择工段数据类型"
						@change="$emit('update', dataForm)">
						<el-option
							v-for="opt in [
								{ label: '无类型', value: 0 },
								{ 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-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: [],
			equipmentList: [],
			worksectionList: [],
			dataFormCache: null,
		};
	},
	mounted() {
		Promise.all([this.getProductLineList(), this.getEquipmentList()]).then(
			() => {
				this.formLoading = false;
			}
		);
	},
	watch: {
		'dataForm.productionLineId': {
			handler: async function (plId) {
				if (plId) await this.getWorksectionList(plId);
			},
			immediate: true,
		},
	},
	methods: {
		/** 模拟透传 ref  */
		validate(cb) {
			return this.$refs.form.validate(cb);
		},
		resetFields(args) {
			return this.$refs.form.resetFields(args);
		},
		async handleProductlineChange(id) {
			await this.getWorksectionList(id);
			this.dataForm.workshopSectionId = null;
			this.$emit('update', this.dataForm);
		},
		// getCode
		async getCode(url) {
			const response = await this.$axios(url);
			return response.data;
		},
		// 获取产线列表
		async getProductLineList() {
			const response = await this.$axios('/base/production-line/listAll');
			this.productionLineList = response.data.map((item) => ({
				label: item.name,
				value: item.id,
			}));
		},
		// 获取设备列表
		async getEquipmentList() {
			const response = await this.$axios(
				'/base/equipment/page?pageNo=1&pageSize=100'
			);
			this.equipmentList = response.data.list.map((item) => ({
				label: item.name,
				value: item.id,
			}));
		},
		// 获取工段列表
		async getWorksectionList(plId) {
			const response = await this.$axios(
				'/base/workshop-section/listByParentId',
				{
					params: {
						id: plId,
					},
				}
			);
			this.worksectionList = response.data.map((item) => ({
				label: item.name,
				value: item.id,
			}));
		},
	},
};
</script>

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