<template>
  <el-dialog v-model="visible" title="设备管理" width="60%" :before-close="closeDialog">
    <el-form :model="addForm" label-width="100px" :rules="rules" ref="ruleFormRef">
      <el-form-item label="设备名称:" prop="name">
        <el-input v-model="addForm.name" placeholder="请输入点位名称" clearable />
      </el-form-item>
      <el-form-item label="设备id:" prop="id">
        <el-input v-model="addForm.id" placeholder="请输入设备id" clearable type="number" />
      </el-form-item>
      <el-form-item label="模板名称:" prop="template_name">
        <el-select v-model="addForm.template_name" size="mini" placeholder="请选择模板名称">
          <el-option v-for="item in modelArr" :key="item.name" :label="item.name"
            :value="item.name" />
        </el-select>
      </el-form-item>

      <el-form-item label="设备描述:" prop="description">
        <el-input v-model="addForm.description" placeholder="请输入点位描述" clearable />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="saveSet"> 保存 </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script>
import { onMounted, reactive, ref, toRefs, computed, nextTick } from "vue";
import infoApi from "@/api/infoApi.js";
import { ElMessage, ElMessageBox } from "element-plus";
export default {
  props: ["formData", "dialogVisible"],
  emits: ["dialogClose", "dialogSuccess"],
  setup(props, ctx) {
    const ruleFormRef = ref(null);
    const state = reactive({
      addForm: {
        name: '',
        template_name: '',
        id: 1,
        description: ''
      },
      modelArr: [],
      rules: {
        name: [{ required: true, message: "请输入设备名称", trigger: "blur" }],
        template_name: [{ required: true, message: "请选择模板名称", trigger: "blur" }],
        id: [{ required: true, message: "请输入设备ID", trigger: "blur" }],
      },
    });
    const visible = computed(() => {
      return props.dialogVisible;
    });

    onMounted(() => {
      console.log(123, props.formData)
      // 获取模板数据
      getTxTemplate();
    });
    const getTxTemplate = async () => {
      const param = {
        driver_name: props.formData.name
      }
      const res = await infoApi.getTxTemplate(param);

      if (res.code == 0) {
        // 获取数据
        state.modelArr = [];
        const data = res.data;
        for (let i in data) {
        let obj = {
          key: Number(i),
          name: data[i]
        }
        state.modelArr.push(obj);
        console.log(33,state.modelArr);
      }
        console.log(state.modelArr);
        state.addForm.template_name = state.modelArr[0].name;
      } else {
        ElMessage.error(res.message || "请求失败");
      }
    };
    const closeDialog = () => {
      ctx.emit("dialogClose");
    };

    const scanData = () => { };

    const saveSet = async () => {
      console.log(ruleFormRef.value);
      await ruleFormRef.value.validate(async (valid) => {
        if (valid) {
          const param = {
            driver_name: props.formData.name,
            ...state.addForm,
            id: Number(state.addForm.id)
          }
          const res = await infoApi.addSet(param);
          if (res.code == 0) {
            ElMessage.success(res.message || "请求成功");
            ctx.emit("dialogClose");
          } else {
            ElMessage.error(res.message);
          }
        } else {
          console.log("error submit!");
        }
      });


    };
    return {
      ...toRefs(state),
      visible,
      closeDialog,
      saveSet,
      ruleFormRef
    };
  },
};
</script>
<style lang="scss" scoped>
.top-title {
  width: 100%;

  ::v-deep .el-card__body {
    width: 100%;
    display: flex;
    justify-content: space-between;
    font-weight: 800;
    font-size: 16px;
  }
}

.btns {
  padding: 15px 0;
  width: 100%;
  display: flex;
  justify-content: space-between;
}

.flex-center {
  display: flex;
  justify-content: center;
  padding: 10px 0;
  cursor: pointer;
}
</style>
