<script setup>
import { ref, onMounted, watch } from "vue";
import { setLighting, setSingleSwitch } from "@/api/lighting";
const lightControl = ref([]);
// 照明控制
const controlBtn = ref("");
const toggleControl = (event) => {
  if (event.srcElement.className === "off") {
    controlBtn.value = "off";
    let dataMap = {
      name: props.name,
      value: "全关",
    };

    setSingleSwitch(dataMap)
      .then((res) => {
        if (res.code == 200) {
          lightControl.value.forEach((ele) => {
            ele.State = false;
          });
        } else {
          return false;
        }
      })
      .catch((err) => {
        console.log(err);
      });
  } else if (event.srcElement.className === "on") {
    controlBtn.value = "on";
    let dataMap = {
      name: props.name,
      value: "全开",
    };

    setSingleSwitch(dataMap)
      .then((res) => {
        if (res.code == 200) {
          lightControl.value.forEach((ele) => {
            ele.State = true;
          });
        } else {
          return false;
        }
      })
      .catch((err) => {
        console.log(err);
      });
  }
};
const props = defineProps({
  show: {
    type: Boolean,
    default: false,
  },
  name: {
    type: String,
    default: "",
  },
  tableData: {
    type: Array,
  },
});
console.log(props.name,props.show);
const emit = defineEmits(["update"]);
const closeDialog = () => {
  // 向父组件传值
  emit("update", false);
};
//照明控制
function control(params) {
  let dataMap = {
    name: params.Id,
    value: params.State == true ? "255" : "0",
  };
  setLighting(dataMap)
    .then((res) => {
      if (res.code == 200) {
        controlBtn.value = "";
        emit("closeAll", false);
      } else {
        return false;
      }
    })
    .catch((err) => {
      console.log(err);
    });
}
// 使用 watch 监听数组的变化
watch(
  () => props.tableData,
  (newVal, oldVal) => {
    // 当数组变化时，可以在这里执行一些操作
    newVal.forEach((item) => {
      item.State = item.State == "255" ? true : false;
    });
    lightControl.value = newVal;
    controlBtn.value = "";
  }
);
</script>

<template>
  <div class="dialog" v-if="props.show">
    <div class="close-btn" @click="closeDialog"></div>
    <div class="dialog-title">
      <span>{{ props.name }}</span>
    </div>
    <div class="control-btn">
      <!-- <span
        class="on"
        :class="controlBtn === 'on' ? 'select' : ''"
        @click="toggleControl"
        >全开</span
      > -->
      <span
        style="margin-left: 0.7rem"
        class="off"
        :class="controlBtn === 'off' ? 'select' : ''"
        @click="toggleControl"
        >全关</span
      >
    </div>
    <div class="dialog-content">
      <div class="runningState">
        <div class="tableTitle">
          <span>回路名称</span>
          <span>操作</span>
        </div>
        <div class="tableContent">
          <div
            class="tableBoby"
            v-for="(item, index) in lightControl"
            :key="index"
          >
            <span class="name">{{ item.name }}</span>

            <span
              ><el-switch
                v-model="item.State"
                style="left: 8px"
                @change="control(item)"
            /></span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.dialog-content {
  position: relative;
  width: 100%;
  height: 74%;
  box-sizing: border-box;
  //padding: 1rem .5rem 0 1rem;
}
.control-btn {
  padding-left: 1rem;
  padding-top: 0.8rem;
  box-sizing: border-box;
  display: flex;
  .select {
    background-image: url("@/assets/images/air-tab-select.png") !important;
  }
  span {
    background-image: url("@/assets/images/air-tab.png");
    cursor: pointer;
    font-family: normal;
    color: rgba(221, 255, 253, 1);
    background-size: 100% 100%;
    font-size: 0.8rem;
    padding: 0.2rem 1.1rem;
  }
}

:deep(.el-switch__core) {
  border-radius: 0;
  background: transparent;
  border: 1px solid rgba(223, 94, 94, 1);
}

:deep(.el-switch.is-checked .el-switch__core) {
  background: transparent;
  border: 1px solid rgba(80, 233, 83, 1);
}
:deep(.el-switch__core .el-switch__action) {
  border-radius: 0;
  height: 13px;
  width: 13px;
  background: rgba(223, 94, 94, 1);
}
:deep(.el-switch.is-checked .el-switch__action) {
  border-radius: 0;
  height: 13px;
  width: 13px;
  background: rgba(80, 233, 83, 1);
}
</style>
