<template>
  <BaseDialog
    :dialogVisible="props.visible"
    @close="onclone"
    :titleName="titleName + '隐患点'"
    width="50%"
    @onSubmit="handleSubmit"
    :footerclosed="true"
    :diafooter="true"
  >
    <div class="detailForm-content">
      <el-form
        ref="formRef"
        label-width="120"
        :rules="ruleForm"
        :model="formData"
        class="center-form"
      >
        <el-form-item label="点位名称" prop="name">
          <el-input placeholder="请输入" type="input" v-model="formData.name" />
        </el-form-item>
        <el-form-item label="点位类型" prop="type">
          <el-select
            v-model="type"
            @change="getList"
            clearable
            placeholder="请选择"
          >
            <el-option
              v-for="item in types"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="详细地址" prop="address" style="width: 94%">
          <el-input
            placeholder="请输入"
            type="input"
            v-model="formData.address"
          />
        </el-form-item>
        <el-form-item label="简介/备注" prop="remarks" style="width: 94%">
          <el-input
            placeholder="请输入"
            :rows="4"
            type="textarea"
            v-model="formData.remarks"
        /></el-form-item>
      </el-form>
    </div>
  </BaseDialog>
</template>
<script setup lang='ts'>
import { reactive, ref, onMounted, watch } from "vue";
import { ElMessage, FormInstance, FormRules } from "element-plus";
import { getUUID } from "@/utils/common";
const uuid = ref(getUUID());

import { inListAdd } from "@/api/industry";

const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
  form: Object,
});

const emits = defineEmits(["close"]);
const titleName = ref("新增");
// 表单
const formRef = ref<FormInstance>();
const ruleForm = reactive<FormRules>({
  name: [{ required: true, message: "请输入名字", trigger: "blur" }],
  type: [{ required: true, message: "请选择类型", trigger: "blur" }],
  address: [{ required: true, message: "请输入地址", trigger: "blur" }],
  remarks: [{ required: true, message: "请输入简介/备注", trigger: "blur" }],
});
const formData = ref<any>({});

const types = ref([
  { label: "防汛", value: 1 },
  { label: "火灾", value: 2 },
  { label: "治安", value: 3 },
  { label: "安全生产", value: 4 },
]);

//保存并退出
const handleSubmit = () => {
  formRef.value?.validate((valid) => {
    if (valid) {
      formData.value.sortId = formData.value.sortId || 1;
      formData.value.industryId = props.id;
      formData.value.delState = formData.value.delState || 1;
      inListAdd(formData.value).then((res: any) => {
        if (res.code == 1) {
          ElMessage.success({
            message: "操作成功",
            type: "success",
          });
          onclone();
        } else {
          ElMessage.error({
            message: res.message,
            type: "error",
          });
        }
      });
    }
    return false;
  });
};

//关闭弹窗
const onclone = () => {
  formRef.value?.clearValidate();
  formData.value = {};
  emits("close");
};
onMounted(() => {});

watch(
  () => props.form,
  (val: any) => {
    titleName.value = val.id ? "编辑" : "新增";
    formData.value = JSON.parse(JSON.stringify(val));
  }
);
</script>
<style scoped></style>