<template>
  <div class="volumn-box" :class="lampData.isOpen ? 'active' : ''">
    <div>
      <div class="top">
        <img :src="parseImgUrl('taiguv1/envmonitor', lampData.isOpen ? 'active/lamp.svg' : 'lamp.svg')" alt="" />
        <div class="top-right">
          <SwitchButton
            :loading="allLampStatus.all?.loading"
            @click.stop
            v-model="lampData.isOpen"
            @change="setLampStatus('isOpen')" />
        </div>
      </div>
      <div class="filter-box">
        <div class="filter-item" @click="searchMore">
          <img :src="FilterIcon" alt="" />
        </div>
      </div>
    </div>

    <div class="bottom">
      <div class="bottom-box">
        <div class="bottom-left">
          <div class="text">{{ $t(`lamp.光照`) }}</div>
          <div class="status">{{ $t(`lamp.${lampData.lampStatusText}`) }}</div>
        </div>

        <div class="bottom-right" v-if="lampData.isOpen">{{ lampData.brightValue || 0 }} <sup>%</sup></div>
      </div>

      <div class="lamp-slider" v-if="lampData.isOpen">
        <Slider
          v-model="lampData.brightValue"
          :min="min"
          :max="max"
          @onStart="sendEvent(true)"
          @onEnd="setLampStatus('brightValue')"></Slider>
      </div>
    </div>
  </div>
</template>

<script setup>
import FilterIcon from "@/assets/taiguv1/svg/filter_icon.svg"
import Slider from "@/components/slider/Slider.vue"
import SwitchButton from "@/components/switch-button/SwitchButton.vue"
import useDeviceControl from "@/hooks/useDeviceControl"
import { useStore } from "@/store"
import { parseImgUrl } from "@/utils"
import { computed, ref, watch } from "vue"
import eventBus from "@/utils/eventBus"
const min = 0
const max = 100
let closeUpdate = false
const emit = defineEmits(["getStatus", "showMore"])

// 接收父组件传递的开关状态
const props = defineProps({
  lampStatus: {
    type: Object,
    default: () => {
      return {
        isOpen: false,
        brightValue: 0
      }
    }
  },
  equipList: {
    type: Array,
    default: () => {
      return []
    }
  }
})

const store = useStore()
const deviceControl = useDeviceControl()
const allLampStatus = computed(() => store.state.taiguv1.lampSwitchStatus)

const searchMore = () => {
  emit("showMore", "lamp", true)
}

const lampData = ref({
  isOpen: false,
  brightValue: 0
})

watch(
  () => props.lampStatus,
  (newVal, oldVal) => {
    if (!newVal || closeUpdate) {
      return
    }
    compareStatus(newVal)
  },
  { deep: true } // 添加深度监听
)

const compareStatus = data => {
  const updateLampData = () => {
    lampData.value = {
      ...data,
      isOpen: data.isOpen
    }
  }
  if (allLampStatus.value.all) {
    if (allLampStatus.value.all.lastSwitchStatus == data.isOpen) {
      store.dispatch("taiguv1/setLampStatus", {
        id: "all",
        status: {
          loading: false,
          lastSwitchStatus: null
        }
      })
      updateLampData()
    } else if (allLampStatus.value.all.lastSwitchStatus === null) {
      updateLampData()
    } else {
      lampData.value = {
        ...data,
        isOpen: allLampStatus.value.all.lastSwitchStatus
      }
    }
  } else {
    lampData.value = {
      ...data,
      isOpen: data.isOpen
    }
  }
  emit("getStatus", lampData.value.isOpen)
}
const sendEvent = close => {
  closeUpdate = close
  eventBus.emit("close_deviece_timer", { close })
}
const debouncedSendEvent = deviceControl.debounce(sendEvent, 1500)

const setLampStatus = type => {
  if (type == "isOpen") {
    store.dispatch("taiguv1/setLampStatus", {
      id: "all",
      status: {
        loading: true,
        lastSwitchStatus: lampData.value.isOpen
      }
    })
  }

  sendEvent(true)
  const params = deviceControl.assemblyLampCommand(lampData.value[type], type, props.equipList)

  deviceControl.sendCommands(params, () => {
    debouncedSendEvent(false)
  })

  emit("getStatus", lampData.value.isOpen)
}
</script>
<style lang="scss" scoped>
.volumn-box {
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 16px;
  width: 100%;
  height: 100%;
  border-radius: 24px 24px 44px 24px;
  background-color: rgba(255, 255, 255, 0.2);
  -webkit-backdrop-filter: blur(40px);
  backdrop-filter: blur(40px);
  box-shadow: 0px 10px 20px 0px rgba(0, 20, 40, 0.1);
  .top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    img {
      width: 30px;
      height: 30px;
    }
  }
  .filter-box {
    margin-top: 10px;
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    .filter-item {
      display: flex;
      width: 36px;
      height: 36px;
      justify-content: center;
      align-items: center;
      gap: 10px;
      flex-shrink: 0;
      border-radius: 60px;
      background: var(--White-White-40, rgba(255, 255, 255, 0.4));
    }
  }
  .bottom {
    .bottom-box {
      flex: 1;
      display: flex;
      justify-content: space-between;
    }
    .text {
      width: 100%;
      //styleName: Chi/Body Large;
      font-family: PingFang SC;
      font-size: 16px;
      font-weight: 300;
      line-height: 22px;
      letter-spacing: 0.02em;
      text-align: left;
      text-underline-position: from-font;
      text-decoration-skip-ink: none;
      color: rgba(255, 255, 255, 1);
      padding-bottom: 2px;
    }
    .status {
      width: 100%;
      //styleName: Chi/Body XS;
      font-family: PingFang SC;
      font-size: 11px;
      font-weight: 400;
      line-height: 15px;
      letter-spacing: 0.02em;
      color: rgba(255, 255, 255, 0.4);
    }
    .bottom-right {
      text-align: center;
      color: var(--Blue, #001428);
      /* En/H2 */
      font-family: Jost;
      font-size: 20px;
      font-style: normal;
      font-weight: 300;
      line-height: normal;
      width: 50px;
      height: 30px;
      // color: var(--Blue, #001428);
      align-self: end;
      line-height: 100%;
      display: flex;
      align-items: center;
      justify-content: end;
    }
    .lamp-slider {
      margin-top: 8px;
      background: rgba(255, 255, 255, 0.6);
      width: 100%;
      height: 34px;
      border-radius: 12px;
    }
  }
  // .switch-btn {
  //     // margin-top: 0;
  //     // width: 50px !important;
  //     // height: 28px !important;
  //     // border: none;
  // }
}
.active {
  background: rgba(255, 255, 255, 0.8);
  //   -webkit-backdrop-filter: blur(40px);
  //   backdrop-filter: blur(40px);
  box-shadow: 0px 10px 20px 0px rgba(0, 20, 40, 0.1);

  .bottom {
    .text {
      color: rgba(0, 20, 40, 1);
    }
    .status {
      color: rgba(116, 128, 141, 1);
    }
    .bottom-right {
      font-family: Jost;
      color: rgba(0, 20, 40, 1);
    }
  }
}
</style>
