<template>
  <el-dialog
    v-model="visible"
    :title="type === 'edit' ? '修改' : '新增'"
    :before-close="fnClose"
  >
    <el-form
      ref="formRef"
      :model="form"
      :rules="data.rules"
      label-width="200px"
      style="margin-top: 20px"
    >
      <el-row>
        <el-col :span="24">
          <el-form-item label="水库名称" prop="reservoirName">
            <el-input
              v-model="form.reservoirName"
              placeholder="请输入水库名称"
            />
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="水库类型" prop="reservoirType">
            <el-select
              v-model="form.reservoirType"
              placeholder="请选择水库类型"
            >
              <el-option
                v-for="item in reservoirTypeList"
                :key="item.bianma"
                :label="item.name"
                :value="item.bianma"
              />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="水库等级" prop="reservoirLevel">
            <el-select
              v-model="form.reservoirLevel"
              placeholder="请选择水库等级"
            >
              <el-option
                v-for="item in reservoirLevelList"
                :key="item.bianma"
                :label="item.name"
                :value="item.bianma"
              />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="极限库容（万立方米）" prop="maxCapacity">
            <el-input v-model="form.maxCapacity" placeholder="请输入极限库容" />
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="当前库容（万立方米）" prop="currentCapacity">
            <el-input
              v-model="form.currentCapacity"
              placeholder="请输入当前库容"
            />
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <div style="display: flex">
            <el-form-item label="经度" prop="longitude" style="flex: 1">
              <el-input v-model="form.longitude" placeholder="请选择经度" />
            </el-form-item>
            <el-form-item label="纬度" prop="latitude" style="flex: 1">
              <el-input v-model="form.latitude" placeholder="请选择纬度" />
            </el-form-item>
            <el-form-item label-width="10px">
              <el-button
                class="ml-10"
                type="primary"
                @click="fnSelectedPosition"
                >点击定位</el-button
              >
            </el-form-item>
          </div>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <el-button @click="fnClose">取 消</el-button>
      <el-button type="primary" @click="fnSubmit">确 定</el-button>
    </template>

    <app-map
      v-model:visible="mapDialog.visible"
      v-model:longitude="mapDialog.longitude"
      v-model:latitude="mapDialog.latitude"
      @submit="fnMapSubmit"
    />
  </el-dialog>
</template>

<script setup>
import { debounce } from "throttle-debounce";
import useFormValidate from "@/hooks/useFormValidate.js";
import { ElMessage } from "element-plus";
import { reactive, ref } from "vue";
import { useVModels } from "@vueuse/core";
import AppMap from "@/components/map/map.vue";
import {
  setReservoirBasicAdd,
  setReservoirBasicUpdate,
} from "@/request/kangzai.js";
import { getDataDictionary } from "@/request/data_dictionary.js";

const props = defineProps({
  visible: {
    type: Boolean,
    required: true,
    default: false,
  },
  type: {
    type: String,
    required: true,
    default: "",
  },
  form: {
    type: Object,
    required: true,
    default: () => ({}),
  },
});
const { validate, formRef } = useFormValidate();

const emits = defineEmits(["update:visible", "update:form", "getData"]);
const { visible, form } = useVModels(props, emits);

// 字典数据
const reservoirTypeList = ref([]);
const reservoirLevelList = ref([]);

const fnGetDictData = async () => {
  const [typeRes, levelRes] = await Promise.all([
    getDataDictionary({ parentId: "848f91ce29ee1f2d078bc6ec69d440df" }),
    getDataDictionary({ parentId: "a8b7c6d5e4f3g2h1i0j9k8l7m6n5o4p" }),
  ]);
  reservoirTypeList.value = typeRes.dictionariesList;
  reservoirLevelList.value = levelRes.dictionariesList;
};
fnGetDictData();

const data = reactive({
  rules: {
    reservoirName: [
      { required: true, message: "请输入水库名称", trigger: "blur" },
    ],
    reservoirType: [
      { required: true, message: "请选择水库类型", trigger: "change" },
    ],
    reservoirLevel: [
      { required: true, message: "请选择水库等级", trigger: "change" },
    ],
    longitude: [{ required: true, message: "请输入经度", trigger: "blur" }],
    latitude: [{ required: true, message: "请输入纬度", trigger: "blur" }],
  },
});

const mapDialog = ref({
  visible: false,
  longitude: "",
  latitude: "",
});
const fnSelectedPosition = () => {
  mapDialog.value.visible = true;
  mapDialog.value.longitude = form.value.longitude;
  mapDialog.value.latitude = form.value.latitude;
};
const fnMapSubmit = ({ longitude, latitude }) => {
  form.value.longitude = longitude;
  form.value.latitude = latitude;
};

const fnClose = () => {
  visible.value = false;
};

const fnSubmit = debounce(
  1000,
  async () => {
    await validate();
    const param = {
      ...form.value,
    };

    if (props.type === "add") {
      await setReservoirBasicAdd(param);
    } else if (props.type === "edit") {
      await setReservoirBasicUpdate(param);
    }

    ElMessage.success("操作成功");
    fnClose();
    emits("getData");
  },
  { atBegin: true }
);
</script>

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