<template>
  <div id="edit-dialog">
    <div class="distance-back">
      <p>当前距离洞口：{{ pointDistance_str }}</p>
      <img
        src="/images/htmlEditDialog/back-icon.png"
        alt=""
        @click="handleCancel"
      />
    </div>
    <div class="equ-info">当前风压:{{ p }}Pa</div>
    <div class="setting">
      <div class="setting-item">
        <p>传感器类型</p>

        <select-input
          :options="options"
          @selectChange="handleTypeChange"
          placeholder="请选择传感器类型"
          ref="equipmentType"
        />
      </div>
      <div class="setting-item">
        <p>设备选择</p>
        <select-input
          :options="options2"
          @selectChange="handleEquipmentChange"
          placeholder="请选择设备"
          ref="equipment"
        />
      </div>
      <input-num
        name="阈值"
        placeholder="请输入阈值"
        @inputValue="handelInput"
        :disabled="isDisabledInputNum"
      />
    </div>
    <div class="btn">
      <button @click="removeEquipment">删除</button>
      <button @click="addEquipment">添加</button>
    </div>
  </div>
</template>

<script setup>
import InputNum from "./childComps/InputNum.vue";
import SelectInput from "./childComps/SelectInput.vue";
import { getEquipmentType, getEquipment } from "@/api/tunnelScene";
import { reactive, computed, defineEmits, defineProps, watch, ref } from "vue";
import { ElMessage } from "element-plus";
import { saveEquipment, initData } from "../request";
const emit = defineEmits(["cancel", "removeEquipment", "addEquipment"]);
const params = defineProps([
  "pointNum",
  "pointGap",
  "tunnelId",
  "position",
  "hasEquipment",
  "form",
]);

let p = ref(57);
const equipment = ref(null);
const equipmentType = ref(null);

let maxConstructionLength = ref(0);
//计算锚点之间距离
const pointDistance_str = computed(
  () =>
    `${params.pointGap}米*${params.pointNum}=${(
      params.pointGap * params.pointNum
    ).toFixed(2)}米`
);

watch(
  () => params.form,
  () => {
    initData(params.tunnelId, params.form);
  },
  {
    deep: true,
  }
);

// 请求数据模型
const equipmentSetting = reactive({
  equipmentId: null,
  equipmentName: "",
  equipmentType: "", //设备类型
  position: "",
  threshold: "", //阈值
  typeKey: null, //设备选择(设备名称)
});
function resetEquipmentSetting() {
  equipmentSetting.equipmentId = null;
  equipmentSetting.equipmentName = "";
  equipmentSetting.equipmentType = "";
  equipmentSetting.position = "";
  equipmentSetting.threshold = "";
  equipmentSetting.typeKey = null;
  equipment.value.reset();
  equipmentType.value.reset();
}

// 绑定选择的设备类型
function handleTypeChange(optionItem) {
  equipmentSetting.equipmentType = optionItem.value;
}
function handleEquipmentChange(equipmentItem) {
  equipmentSetting.equipmentName = equipmentItem.label;
  equipmentSetting.equipmentId = equipmentItem.equipmentId;
  equipmentSetting.typeKey = equipmentItem.value;
}
// 设备类型选项参数
let options = reactive([{ label: "暂无数据", value: "" }]);

// 设备编号参数
let options2 = reactive([{ equipmentId: 0, label: "暂无数据", value: "" }]);

// 模拟生成option2
watch(
  () => equipmentSetting.equipmentType,
  (newVal) => {
    console.log(params.tunnelId, newVal);
    equipment.value.reset();
    getEquipment(params.tunnelId, newVal, 1).then((res) => {
      options2.splice(0, options2.length);
      options2.push(
        ...res.data.map((item) => {
          return {
            equipmentId: item.equipmentId,
            label: item.equipmentName,
            value: item.equipmentType,
          };
        })
      );
    });
  }
);

// 设备类型请求
getEquipmentType()
  .then((res) => {
    options.splice(0, options.length);
    options.push(...res.data);
  })
  .catch((err) => console.log(err));

function handelInput(e) {
  equipmentSetting.threshold = e;
}

// 处理取消事件
function handleCancel() {
  resetEquipmentSetting();
  // 发射事件给tunnel父组件
  emit("cancel");
}

// 删除模型
function removeEquipment() {
  emit("removeEquipment");
}

// 添加设备
function addEquipment() {
  if (params.hasEquipment) {
    ElMessage({
      message: "该点位已存在设备！",
      type: "warning",
    });
    return;
  }
  if (!equipmentSetting.equipmentType || !equipmentSetting.equipmentId) {
    ElMessage({
      message: "选项不能为空！",
      type: "warning",
    });
    return;
  }
  equipmentSetting.position = params.position;
  // 合法请求
  saveEquipment(equipmentSetting, params.pointGap)
    .then((res) => {
      emit("addEquipment", equipmentSetting);
      ElMessage({
        message: "添加成功！",
        type: "success",
      });
      resetEquipmentSetting();
    })
    .catch((err) => {
      if (err === 3000) {
        ElMessage({
          message: "该设备已添加！",
          type: "warning",
        });
        resetEquipmentSetting();
      } else {
        ElMessage({
          message: "添加异常！",
          type: "warning",
        });
      }
    });
}
</script>

<style lang="scss" scoped>
#edit-dialog {
  width: 540px;
  // min-height: 683px;
  background: rgba(7, 35, 72, 0.79);
  border-radius: 20px;
  border: 2px solid #0f82af;
  position: absolute;
  z-index: 999;
  display: block;
  opacity: 0;

  .distance-back {
    height: 30px;
    display: flex;
    justify-content: space-between;
    margin: 20px 20px 0px 30px;

    p {
      width: 388px;
      height: 35px;
      font-size: 24px;
      font-family: MicrosoftYaHei;
      color: #ffffff;
      line-height: 35px;
    }

    img {
      cursor: pointer;
    }

    img:active {
      transform: scale(0.9);
    }
  }

  .equ-info {
    width: 190px;
    height: 35px;
    font-size: 26px;
    font-family: MicrosoftYaHei;
    color: #f7b500;
    line-height: 35px;
    margin: 20px 30px 0px 30px;
  }

  .setting {
    .setting-item {
      padding: 30px 30px 10px 30px;

      p {
        width: 130px;
        height: 35px;
        font-size: 26px;
        font-family: MicrosoftYaHei;
        color: #ffffff;
        line-height: 35px;
        margin-bottom: 20px;
      }
    }
  }

  .btn {
    margin: 100px 40px 40px 40px;
    display: flex;
    justify-content: space-between;

    button {
      cursor: pointer;
      transition: transform 0.1s linear 0s;
    }

    & :nth-child(1) {
      width: 190px;
      height: 60px;
      border-radius: 11px;
      border: 2px solid #0f82af;
      background: transparent;
      font-size: 28px;
      font-family: MicrosoftYaHei;
      color: #08b7b8;
      line-height: 60px;
    }

    & :nth-child(2) {
      width: 190px;
      height: 60px;
      background: #08b7b8;
      border-radius: 11px;
      font-size: 28px;
      font-family: MicrosoftYaHei, MicrosoftYaHei;
      font-weight: bold;
      color: #ffffff;
      line-height: 60px;
    }

    button:active {
      transform: scale(0.9);
    }
  }
}
</style>
