<template>
  <div class="add">
    <el-dialog
      v-model="dialogVisible"
      :title="tabName + (logType === 'add' ? '新增' : '修改')"
      width="500"
    >
      <el-form
        :label-position="labelPosition"
        label-width="auto"
        :model="formLabelAlign"
        style="max-width: 600px"
        ref="ruleFormRef"
      >
        <el-form-item
          label="配置地址"
          :label-position="itemLabelPosition"
          prop="configIp"
        >
          <el-input v-model="formLabelAlign.configIp" />
        </el-form-item>

        <el-form-item
          label="配置名称"
          :label-position="itemLabelPosition"
          prop="configName"
        >
          <el-input v-model="formLabelAlign.configName" />
        </el-form-item>

        <el-form-item
          label="仪器编码	"
          :label-position="itemLabelPosition"
          prop="instrumentCode"
        >
          <el-input v-model="formLabelAlign.instrumentCode" />
        </el-form-item>

        <el-form-item
          label="仪器类型		"
          :label-position="itemLabelPosition"
          prop="instrumentType"
        >
          <el-select
            v-model="formLabelAlign.instrumentType"
            placeholder="请选择仪器类型"
            size="large"
          >
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.dictLabel"
              :value="item.dictValue"
            />
          </el-select>
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="saveFun"> 保存 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, watch } from "vue";
import { selectInstrumentType } from "@/api/config";

const dialogVisible = ref(false);

import type { FormItemProps, FormProps } from "element-plus";

const labelPosition = ref<FormProps["labelPosition"]>("right");
const itemLabelPosition = ref<FormItemProps["labelPosition"]>("");

interface VersionItem {
  instrumentType: string;
  configName: string;
  configIp: string;
  instrumentProperty: number;
  instrumentCode: string;
}
//configIp	配置地址		true
//configName	配置名称		true
//engineeringId	测试工程id		false
//instrumentCode	仪器编码		true
//instrumentProperty	仪器属性(1工程仪器 2执行仪器)		true
//instrumentType	仪器类型

const formLabelAlign = ref<VersionItem>({
  instrumentType: "",
  configName: "",
  configIp: "",
  instrumentProperty: 1,
  instrumentCode: "",
});

const emit = defineEmits(["save"]);

const saveFun = () => {
  emit("save", formLabelAlign.value, flag.value);
};

const props = defineProps(["tabName"]);

const ruleFormRef = ref();
const flag = ref("");
//show弹框
const logType = ref("");
const showDialog = (type: string, data: any) => {
  logType.value = type;
  dialogVisible.value = true;
  ruleFormRef.value && ruleFormRef.value.resetFields();
  flag.value = type;
  formLabelAlign.value = JSON.parse(JSON.stringify(data));
};
const hiddenDialog = () => {
  dialogVisible.value = false;
};
defineExpose({
  showDialog,
  hiddenDialog,
});
const options = ref([{}]);

onMounted?.(() => {
  //查询仪器类型,可以初始化查询，也可以在 showDialog 弹框显示的时候查询
  selectInstrumentType({}).then((res) => {
    options.value = res.data;
  });
});
</script>
<style scoped lang="scss">
// .add {
//   // position: fixed;
//   // width: 100%;
//   // height: 100%;
//   // z-index: 10;
//   // background-color: #000;
// }
</style>
