<template>
  <el-dialog
    v-model="visible"
    :title="type === 'add' ? '新增' : '修改'"
    :before-close="fnClose"
  >
    <el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="上级电子围栏">
        <el-tag>{{ parentName }}</el-tag>
      </el-form-item>
      <el-form-item label="所属电子围栏" prop="ELECTRONIC_FENCE_NAME">
        <div style="flex: 1; display: flex">
          <el-input v-model="form.ELECTRONIC_FENCE_NAME" disabled />
          <el-button type="primary" class="ml-10" @click="data.drawer = true">
            添加
          </el-button>
          <el-button type="danger" class="ml-10" @click="fnRemoveRegName">
            移除
          </el-button>
        </div>
      </el-form-item>
      <el-form-item label="电子围栏区域" prop="ELECTRONIC_FENCE_AREA_NAME">
        <el-input v-model="form.ELECTRONIC_FENCE_AREA_NAME" />
      </el-form-item>
      <el-form-item label="负责人" prop="HEADMAN">
        <el-input v-model="form.HEADMAN" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="电话" prop="TEL">
        <el-input v-model="form.TEL" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="备注" prop="BZ">
        <el-input
          v-model="form.BZ"
          :autosize="{
            minRows: 3,
          }"
          type="textarea"
          placeholder="请输入"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" @click="fnSubmit">确认</el-button>
      <el-button @click="fnClose">关闭</el-button>
    </template>
    <electronic-fence v-model:visible="data.drawer" v-model:form="form" />
  </el-dialog>
</template>

<script setup>
import { useVModels } from "@vueuse/core";
import { ref, reactive } from "vue";
import { debounce } from "throttle-debounce";
import useFormValidate from "@/assets/js/useFormValidate.js";
import {
  setElectronicFenceEdit,
  setElectronicFenceData,
} from "@/request/enterprise_management.js";
import ElectronicFence from "@/components/electronic_fence/index.vue";
import { ElMessage } from "element-plus";

const props = defineProps({
  visible: {
    type: Boolean,
    required: true,
    default: false,
  },
  form: {
    type: Object,
    required: true,
    default: () => ({}),
  },
  type: {
    type: String,
    required: true,
    default: "",
  },
  parentName: {
    type: String,
    required: true,
    default: "",
  },
  parentId: {
    type: String,
    required: true,
    default: "",
  },
});
const data = reactive({
  drawer: false,
});
const emits = defineEmits(["update:visible", "update:form", "get-data"]);
const { visible, form } = useVModels(props, emits);
const rules = {
  ELECTRONIC_FENCE_AREA_NAME: [
    { required: true, message: "电子围栏区域名称", trigger: "blur" },
  ],
  ELECTRONIC_FENCE_NAME: [
    { required: true, message: "电子围栏必选", trigger: "blur" },
  ],
};
const formRef = ref(null);
const fnClose = () => {
  formRef.value.resetFields();
  visible.value = false;
};
const fnRemoveRegName = debounce(
  1000,
  async () => {
    form.value.ELECTRONIC_FENCE_NAME = "";
    form.value.ELECTRONIC_FENCE_ID = "";
  },
  { atBegin: true }
);
const fnSubmit = debounce(
  1000,
  async () => {
    await useFormValidate(formRef);
    props.type === "add"
      ? await setElectronicFenceData({
          ...form.value,
          PARENT_ID: props.parentId,
        })
      : await setElectronicFenceEdit({
          ...form.value,
        });
    ElMessage.success("操作成功");
    fnClose();
    emits("get-data");
  },
  { atBegin: true }
);
</script>

<style scoped lang="scss"></style>
