<script setup lang="ts" name="FormBox">
import { onMounted, ref, toRefs, reactive, watch } from "vue";
import type { FormInstance } from "element-plus";
import { ElMessage } from "element-plus";
import { update } from '@/api/systemConfig/ipTheChange.js'

const props = defineProps<{
  itemData: Object;
}>();

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

const data = reactive({
  form: <any>{},
  rules: {
    networkInterface: [
      {
        required: true,
        message: "请输入网卡",
        trigger: ["blur"],
      },
    ],
    inetAddress: [
      {
        required: true,
        message: "请输入IP地址",
        trigger: ["blur"],
      },
    ],
    subnetMask: [
      {
        required: true,
        message: "请输入子网掩码",
        trigger: ["blur"],
      },
    ],
    gateway: [
      {
        required: true,
        message: "请输入默认网关",
        trigger: ["blur"],
      },
    ],
    dns: [
      {
        required: true,
        message: "请输入DNS",
        trigger: ["blur"],
      },
    ],
  },
});

const { form, rules } = toRefs(data);

const formRef = ref<FormInstance>();
const readOnly = ref(true);
const title = ref('');

const onSubmit = () => {
  formRef.value?.validate((valid) => {
    if (valid) {
      updateFunc()
    } else {
    }
  });
}

const updateFunc = () => {
  update(form.value).then((res) => {
    const { flag } = res;
    if (flag) {
      ElMessage.success("更新成功");
      readOnly.value = true;
      emit("confirm");
    }
  });
}

const onCancel = () => {
  readOnly.value = true;
  const data = JSON.parse(JSON.stringify(props.itemData));
  form.value = { ...form.value, ...data };
}

watch(
  () => props.itemData,
  (newVal) => {
    if (props.itemData) {
      const data = JSON.parse(JSON.stringify(props.itemData));
      form.value = { ...form.value, ...data };
      title.value = data.networkInterface;
    }
  },
  { deep: true, immediate: true }
);

</script>

<template>
  <div class="formBox">
    <el-divider content-position="left">{{ title }}</el-divider>
    <div class="formBox-content">
      <div class="formBox-left">
        <!-- <div class="edit-icon" @click="readOnly = false">
          <el-icon :size="16" color="#fff">
            <edit />
          </el-icon>
        </div> -->
      </div>
      <el-form ref="formRef" class="form" :model="form" :rules="rules" label-width="100px" :disabled="readOnly">
        <el-form-item label="网卡" prop="networkInterface" required>
          <el-input v-model="form.networkInterface"></el-input>
        </el-form-item>
        <el-form-item label="IP地址" prop="inetAddress" required>
          <el-input v-model="form.inetAddress"></el-input>
        </el-form-item>
        <el-form-item label="子网掩码" prop="subnetMask" required>
          <el-input v-model="form.subnetMask"></el-input>
        </el-form-item>
        <el-form-item label="默认网关" prop="gateway" required>
          <el-input v-model="form.gateway"></el-input>
        </el-form-item>
        <el-form-item label="DNS" prop="dns" required>
          <el-input v-model="form.dns"></el-input>
        </el-form-item>
        <!-- <div v-if="!readOnly" style="display: flex;justify-content: flex-end;margin-top: 2px;">
          <el-button @click="onCancel">取消</el-button>
          <el-button type="primary" @click="onSubmit">确定</el-button>
        </div> -->
      </el-form>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.formBox {
  width: 100%;
  &-content {
    display: flex;
    width: 100%;
  }
  &-left {
    width: 98px;
    padding-left: 30px;
    .edit-icon {
      width: 30px;
      height: 30px;
      text-align: center;
      line-height: 30px;
      border-radius: 50%;
      background: #2c9ef7;
      color: #fff;
      cursor: pointer;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
  .form{
    width: 45%;
    :deep(.el-form-item__label) {
    background: #f0f0f0;
    }
    :deep(.el-form-item) {
      margin-bottom: 5px;
    }
    :deep(.el-form-item__error) {
      left: auto;
      right: 83px;
      top: 24%;
    }
  }
}
</style>