<template>
  <div>
    <div class="app-container home">
      <div id="cesiumContainer">
        <div class="center-title-box">
          <div class="control-state">
            <!-- 简化class绑定语法 -->
            <p :class="{ active: intelligentControl.value == 0 }">
              <span class="icon-img"></span>
              <span>{{ intelligentControl.name }}</span>
            </p>
          </div>
          <!-- 优化为数据驱动循环 -->
          <div
            class="title-box"
            v-for="(item, index) in sumpData"
            :key="index"
            :class="['top', 'center', 'bottom'][index]"
          >
            <p class="title">
              <span v-if="item.sumpName !== '蓄水池'"
                >液体高度: {{ item | sumpFilter("heghtThreshold") }}m</span
              >
            </p>
            <p class="name">{{ item | sumpFilter("sumpName") }}</p>
          </div>
        </div>
        <div
          class="water-pump"
          v-for="(item, i) in pumpList"
          :key="item.pumpId || `pump_${i}`"
          :class="`pst${Number(item.locationNum) - 3}`"
        >
          <div class="H36">
            <div
              class="button on"
              :class="item.status == 1 ? 'active' : ''"
              @click="openWaterPump(item, 1)"
            >
              开启
            </div>
            <div
              class="button off"
              :class="item.status == 0 ? 'active' : ''"
              @click="closeWaterPump(item, 0)"
            >
              关闭
            </div>
          </div>
          <div class="H164">
            <div
              class="phd-left"
              v-show="[5, 6, 1, 2].includes(Number(item.locationNum) - 3)"
            ></div>
            <div class="phd-right">
              <div class="phd-top"></div>
              <div
                class=""
                :class="
                  [5, 6, 1, 2].includes(Number(item.locationNum) - 3)
                    ? 'water-pump-img-left'
                    : 'water-pump-img-right'
                "
              >
                <div
                  class="tip-box"
                  :class="
                    [5, 6, 1, 2].includes(Number(item.locationNum) - 3)
                      ? 'dw'
                      : 'dwr'
                  "
                >
                  <p>
                    <span>水泵前温</span>：{{
                      item.pumpFrontTemp | decimalFilter
                    }}℃
                  </p>
                  <p>
                    <span>水泵后温</span>：{{
                      item.pumpRearTemp | decimalFilter
                    }}℃
                  </p>
                  <p>
                    <span>电机前温</span>：{{
                      item.motorFrontTemp | decimalFilter
                    }}℃
                  </p>
                  <p>
                    <span>电机后温</span>：{{
                      item.motorRearTemp | decimalFilter
                    }}℃
                  </p>
                  <p>
                    <span>出水压力</span>：{{
                      item.outletPressure | decimalFilter
                    }}Mpa
                  </p>
                  <p>
                    <span>吸水负压</span>：{{
                      item.inletVacuum | decimalFilter
                    }}pa
                  </p>
                  <p>
                    <span>前振动</span>：{{
                      item.frontVibration | decimalFilter
                    }}mm/s
                  </p>
                  <p>
                    <span>后振动</span>：{{
                      item.rearVibration | decimalFilter
                    }}mm/s
                  </p>
                </div>
              </div>
              <p>
                <span>{{ item.pumpName }}</span>
              </p>
            </div>
            <div
              class="phd-left"
              v-show="[7, 3, 4].includes(Number(item.locationNum) - 3)"
            ></div>
          </div>
        </div>
      </div>
    </div>
    <div class="up-window" v-show="upWindowShow">
      <div class="up-window-box">
        <p><span></span>开启/关闭水泵</p>
        <i class="el-icon-close close-button" @click="upWindowShow = false"></i>
        <div class="W400">
          <span>请输入密码</span>
          <el-input placeholder="" v-model="password" show-password></el-input>
        </div>
        <div>
          <div class="button close" @click="upWindowShow = false">取消</div>
          <div class="button" @click="submitPumpPassword()">确认</div>
        </div>
      </div>
    </div>
    <div class="example"></div>
    <pool style="position: absolute; top: 195px; left: 793px"></pool>
    <pool style="position: absolute; top: 580px; left: 793px"></pool>
    <pool style="position: absolute; top: 962px; left: 795px"></pool>
  </div>
</template>
<script>
import { getDraData, updatePumpSwitch } from "@/api/tyler/psxt.js";
import pool from "@/components/Pool/pool.vue";
export default {
  name: "psxt",
  components: {
    pool,
  },
  data() {
    return {
      password: "",
      upWindowShow: false,
      pumpId: "",
      intelligentControl: {},
      pumpList: [
        {
          pumpId: 1,
          pumpName: "5#水泵",
          pumpStatus: 1,
        },
      ],
      sumpData: [],
      pumpList: [],
      pumpStatus: 0, //水泵状态
      timer: null, // 新增定时器引用
    };
  },
  created() {
    this.initPumpList();
  },
  activated() {
    // 激活时启动定时器
    this.timer = setInterval(() => {
      this.initPumpList();
    }, 10000);
  },
  deactivated() {
    // 停用时清除定时器（使用可选链操作符）
    if (this.timer) {
      clearInterval(this.timer);
      this.timer = null; // 必须重置为null
    }
  },
  beforeDestroy() {
    // 双重保障清除
    if (this.timer) {
      clearInterval(this.timer);
      this.timer = null;
    }
  },
  mounted() {},
  methods: {
    initPumpList() {
      getDraData().then((res) => {
        //水池数据
        if (res.code == 200) {
          this.sumpData = res.data[0].value;
          this.pumpList = res.data[1].value;
          this.intelligentControl = res.data[2];
        } else {
          this.$modal.msgError(res.message);
        }
      });
    },
    // 水泵开启按钮
    openWaterPump(item, status) {
      console.log("我点击了");

      this.upWindowShow = true;
      // this.submitPumpPassword(pumpId, this.password, 0);
      this.pumpId = item.pumpId;
      this.pumpStatus = status;
    },
    // 水泵关闭按钮
    closeWaterPump(item, status) {
      this.upWindowShow = true;
      this.pumpId = item.pumpId;
      this.pumpStatus = status;

      this.password = "";
    },
    //提交水泵密码
    submitPumpPassword() {
      let pumpId = this.pumpId;
      let password = this.password;
      if (!password) {
        return this.$modal.msgError("请输入密码");
      }
      updatePumpSwitch({ pumpId, password, status: this.pumpStatus }).then(
        (res) => {
          if (res.code === 200) {
            this.upWindowShow = false;
            this.password = "";
            this.$modal.msgSuccess(res.msg);
            this.initPumpList();
          } else {
            this.$modal.msgError(res.msg);
          }
        }
      );
    },
  },
  filters: {
    decimalFilter(value) {
      return value === null || typeof value === "undefined"
        ? "--"
        : Number(value).toFixed(2);
    },
    // 水池显示数据过滤
    sumpFilter(item, key) {
      // 优化过滤逻辑
      return item?.[key] || "--";
    },
  },
  beforeDestroy() {
    // 组件销毁前清除定时器
    if (this.timer) {
      clearInterval(this.timer);
      this.timer = null;
    }
  },
};
</script>

<style scoped lang="scss">
.home {
  pointer-events: auto; // 新增这行
  width: 100%;
  height: 100%;
  background: url("~@/assets/images/screen/psxt/bg.png") no-repeat center;
  display: flex;
  justify-content: center;
  padding-top: 185px;
  font-family: "fangsong";
  #cesiumContainer {
    width: 1800px;
    height: 837px;
    background: url("~@/assets/images/screen/psxt/gd.png") no-repeat center;
    background-size: 100% 100%;
    position: relative;
    .center-title-box {
      z-index: 888;
      width: 200px;
      height: 100%;
      margin: auto;
      position: relative;
      .control-state {
        width: 260px;
        height: 44px;
        background: url("~@/assets/images/screen/psxt/bg1.png") no-repeat center;
        background-size: 100% 100%;
        position: absolute;
        top: 33px;
        left: 342px;
        p {
          font-weight: 400;
          font-size: 20px;
          color: #ffd929;
          line-height: 44px;
          margin: 0px;
          text-align: center;
          .icon-img {
            background: url("~@/assets/images/screen/psxt/icon1.png") no-repeat
              center;
            width: 20px;
            height: 20px;
            display: inline-block;
            margin-bottom: -4px;
            margin-right: 10px;
          }
          &.active {
            color: #e1e1e1;
            .icon-img {
              background: url("~@/assets/images/screen/psxt/icon3.png")
                no-repeat center;
            }
          }
        }
      }
      .title-box {
        text-align: center;

        .title {
          font-weight: 400;
          font-size: 18px;
          color: #11e0ff;
        }
        .name {
          font-weight: 400;
          font-size: 20px;
          color: #ffffff;
        }
      }
      .top {
        padding-top: 360px;
      }
      .center {
        padding-top: 280px;
      }
    }
    .water-pump {
      position: absolute;
      width: 172px;
      height: 200px;
      // background: #ffffff;
      .H36 {
        pointer-events: auto; // 新增这行
        position: relative; // 新增层级控制
        z-index: 999;
        .button {
          width: 86px;
          height: 36px;
          float: left;
          line-height: 36px;
          text-align: center;
          font-weight: bold;
          font-size: 18px;
          color: #96aeba;
          line-height: 32px;
          text-shadow: 0px 1px 4px rgba(5, 38, 68, 0.73);
          border: 2px solid #d8efff;
          border-radius: 2px;
          background: RGBA(0, 26, 49, 1);
          cursor: pointer;
        }
        .on {
          &.active {
            background: url("~@/assets/images/screen/psxt/button.png") no-repeat
              center;
            color: #ffffff;
          }
        }
        .off {
          border-left: none;
          &.active {
            background: url("~@/assets/images/screen/psxt/button2.png")
              no-repeat center;
            color: #ffffff;
          }
        }
      }
      .H164 {
        height: calc(100% - 36px);
        .phd-left {
          width: 50px;
          height: calc(100% - 36px);
          float: left;
        }
        .phd-right {
          width: 122px;
          height: calc(100% - 36px);
          float: left;
          .phd-top {
            height: 40px;
          }
          .water-pump-img-left {
            width: 122px;
            height: 70px;
            background: url("~@/assets/images/screen/psxt/item.png") no-repeat
              center;
            position: relative;
            .tip-box {
              width: 192px;
              height: 206px;
              background: #0a1e36;
              border: 2px solid #00deff;
              padding: 7px 13px 15px 10px;
              opacity: 0.8;
              p {
                font-size: 17px;
                color: #11e0ff;
                line-height: 22px;
                margin-bottom: -2px;
                text-align: left;
                font-weight: 400;
              }
              &.dw {
                position: absolute;
                top: -80px;
                left: 131px;
                span {
                  display: inline-block;
                  width: 70px;
                  text-align: justify;
                  text-align-last: justify;
                  color: #11e0ff;
                  background: #11e0ff;
                  -webkit-background-clip: text;
                  -webkit-text-fill-color: transparent;
                  background-clip: text;
                }
              }
            }
          }
          .water-pump-img-right {
            width: 122px;
            height: 70px;
            background: url("~@/assets/images/screen/psxt/item1.png") no-repeat
              center;
          }
          p {
            font-weight: 400;
            font-size: 20px;
            line-height: 30px;
            text-align: center;
            margin-top: 3px;
            background: url("~@/assets/images/screen/psxt/sbmcbg.png") no-repeat
              center;
            span {
              font-family: "fangzhengyaoti";
              background: linear-gradient(180deg, #ffffff 0%, #ffd200 100%);
              -webkit-background-clip: text;
              -webkit-text-fill-color: transparent;
              background-clip: text;
              text-fill-color: transparent;
            }
          }
          .tip-box {
            width: 192px;
            height: 206px;
            background: #0a1e36;
            border: 2px solid #00deff;
            padding: 7px 13px 15px 10px;
            opacity: 0.8;
            p {
              font-size: 17px;
              color: #11e0ff;
              line-height: 22px;
              margin-bottom: -2px;
              text-align: left;
              font-weight: 400;
              background: none;
            }
            &.dwr {
              position: absolute;
              top: 0px;
              left: -199px;
              span {
                display: inline-block;
                width: 70px;
                text-align: justify;
                text-align-last: justify;
                color: #11e0ff;
                background: #11e0ff;
                -webkit-background-clip: text;
                -webkit-text-fill-color: transparent;
                background-clip: text;
              }
            }
          }
        }
      }
      &.pst5 {
        top: 169px;
        left: 143px;
      }
      &.pst6 {
        top: 169px;
        left: 582px;
      }
      &.pst7 {
        top: 169px;
        left: 1408px;
      }
      &.pst1 {
        top: 555px;
        left: 0px;
      }
      &.pst2 {
        top: 555px;
        left: 440px;
      }
      &.pst3 {
        top: 555px;
        left: 1188px;
      }
      &.pst4 {
        top: 555px;
        left: 1630px;
      }
    }
  }
}
.up-window {
  width: 100%;
  height: 100%;
  background: RGBA(4, 21, 42, 0.6);
  position: absolute;
  z-index: 9999;
  top: 0;
  left: 0;
  .up-window-box {
    width: 588px;
    height: 350px;
    background: linear-gradient(0deg, #062451 0%, #09162d 100%);
    box-shadow: 0px 15px 11px 2px rgba(0, 20, 39, 0.31);
    border: 2px solid #11b9ff;
    opacity: 1;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    padding: 20px 37px 37px 27px;
    color: #ffffff;
    p {
      width: 500px;
      height: 37px;
      margin: 0px;
      background: url("~@/assets/images/screen/psxt/title.png") no-repeat center;
      background-size: 100%;
      font-weight: 500;
      font-size: 22px;
      color: #ffffff;
      height: 37px;
      background-position-y: 9px;
      span {
        width: 27px;
        display: inline-block;
      }
    }
    .close-button {
      font-size: 30px;
      position: absolute;
      top: 16px;
      right: 16px;
      cursor: pointer;
      &:hover {
        -webkit-transform: rotate(360deg);
        transform: rotate(360deg);
        -webkit-transition: -webkit-transform 1s linear;
        transition: transform 1s linear;
      }
    }
    .W400 {
      width: 400px;
      margin: auto;
      ::v-deep .el-input__inner {
        border-radius: 0px !important;
        height: 52px;
      }
      span {
        font-weight: 400;
        font-size: 21px;
        color: #ffffff;
        margin-top: 60px;
        display: inline-block;
        margin-bottom: 15px;
      }
    }
    .button {
      width: 110px;
      height: 42px;
      background: #11b9ff;
      border-radius: 4px;
      font-weight: 400;
      font-size: 20px;
      color: #ffffff;
      text-align: center;
      line-height: 42px;
      float: left;
      margin-left: 100px;
      margin-top: 55px;
      cursor: pointer;
      &:hover {
        background: #66d2ff;
      }
      &.close {
        background: none;
        color: #fff;
        &:hover {
          color: #66d2ff;
        }
      }
    }
  }
}
.example {
  width: 1070px;
  height: 921px;
  position: absolute;
  left: 50%;
  margin-left: -50px;
  background: #00deff;
}
@keyframes wave {
  0% {
    background-position-x: 0;
    border-radius: 50% 50% 0 0 / v-bind('waveHeight + "px"');
  }
  50% {
    border-radius: 50% 50% 0 0 / v-bind('(waveHeight + 2) + "px"');
  }
  100% {
    background-position-x: 200px;
    border-radius: 50% 50% 0 0 / v-bind('waveHeight + "px"');
  }
}
</style>
