<!--
 * @Author: zwq
 * @Date: 2021-11-18 14:16:25
 * @LastEditors: DY
 * @LastEditTime: 2024-02-22 10:08:13
 * @Description:
-->
<template>
	<el-form
		:model="dataForm"
		:rules="dataRule"
		ref="dataForm"
		@keyup.enter.native="dataFormSubmit()"
		label-width="100px">
		<el-form-item label="设备" prop="equipmentId">
			<el-cascader
				placeholder="请选择设备"
				v-model="dataForm.equipmentId"
				:options="plLineList"
				:props="{value: 'id', label: 'name', children: 'children'}"
				style="width: 100%"
				filterable />
		</el-form-item>
		<el-form-item label="关联表名" prop="plcId">
			<el-select
				v-model="dataForm.plcId"
				filterable
				placeholder="请选择关联表"
				style="width: 100%">
				<el-option
					v-for="dict in plcList"
					:key="dict.id"
					:label="dict.plcTableName"
					:value="dict.id" />
			</el-select>
		</el-form-item>
	</el-form>
</template>

<script>
import basicAdd from '../../../../core/mixins/basic-add';
import { createCorePL, updateCorePL, getCorePL, getCode, getCorePLList } from "@/api/base/coreProductionLine";
import { createEquipmentPlcConnect, updateEquipmentPlcConnect } from '@/api/base/equipmentPlcConnect';
import { getplcAllList, listByParentId } from "@/api/equipment/base/config/config";

export default {
	mixins: [basicAdd],
	data() {
		return {
			urlOptions: {
			  isGetCode: true,
        codeURL: getCode,
				createURL: createCorePL,
				updateURL: updateCorePL,
				infoURL: getCorePL,
			},
			dataForm: {
        id: undefined,
        equipmentId: undefined,
        plcId: undefined
			},
			plcList: [],
			plLineList: [],
			dataRule: {
        equipmentId: [{ required: true, message: "设备不能为空", trigger: "blur" }],
				plcId: [{ required: true, message: "关联表名不能为空", trigger: "blur" }]
      },
			options: [{
          value: 'zhinan',
          label: '指南',
          children: [{
            value: 'shejiyuanze',
            label: '设计原则',
            children: [{
              value: 'yizhi',
              label: '一致'
            }, {
              value: 'fankui',
              label: '反馈'
            }, {
              value: 'xiaolv',
              label: '效率'
            }, {
              value: 'kekong',
              label: '可控'
            }]
          }, {
            value: 'daohang',
            label: '导航'
          }]
        }]
		};
	},
	created() {
		this.getDict()
	},
	methods: {
		async getDict() {
			// 关联表名列表
			const res = await getplcAllList();
			this.plcList = res.data;
			// 产线列表
			const res1 = await getCorePLList();
			this.plLineList = res1.data;
			this.plLineList.forEach(item => {
				listByParentId({ id: item.id }).then(resp => {
					if (resp.data.length > 0) {
						// item.children = resp.data
						this.$set(item, 'children', resp.data)
						// this.$forceUpdate()
					}
				})
			})
		},
		// 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (!valid) {
          return false;
        }
        // 修改的提交
        if (this.dataForm.id) {
          updateEquipmentPlcConnect({
						id: this.dataForm.id,
						equipmentId: this.dataForm.equipmentId[this.dataForm.equipmentId.length],
						plcId: this.dataForm.plcId
					}).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.visible = false;
            this.$emit("refreshDataList");
          });
          return;
        }
        // 添加的提交
        createEquipmentPlcConnect({
						id: this.dataForm.id,
						equipmentId: this.dataForm.equipmentId[this.dataForm.equipmentId.length - 1],
						plcId: this.dataForm.plcId
					}).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.visible = false;
          this.$emit("refreshDataList");
        });
      });
    }
	}
};
</script>
