<template>
  <div class="cpt-nest-station mr30 mb10">
    <div class="title-box">
      <div class="title mt10">
        <span class="text cf">
          云台控制
        
        </span>
      </div>
      <span
      class="ml14 cp cfc iconfont mr5"
      :class="flag ? 'icon-jiesuo2' : 'icon-jiesuosuozhu'"
      @click="flag = !flag"
    />
    </div>
    <div class="ctx-box mt10">
      <template v-if="mounts && mounts.length">
        <div class="mount-list" :class="flag ? '' : 'preventClick'">
          <div
            class="mount-item pr"
            v-for="(item, index) in mounts"
            :key="index"
          >
            <div class="pa left0 cp top40" @click="changeLeftBtn"><img class="wih100 dib ht100" src="mmc/assets/images/observe/fckernel/left.png" alt=""></div>
            <div class="pa right0 cp top40" @click="changeRightBtn"><img class="wih100 dib ht100" src="mmc/assets/images/observe/fckernel/right.png" alt=""></div>
            <div class="icon-box" @click="$emit('change', item)">
              <img class="cp" :src="item.icon" />
            </div>
          </div>
        </div>
      </template>
      <template v-else>
        <span  class="wih100 tc">{{ tips }}</span>
      </template>
    </div>
    
  </div>
</template>

<script>
export default {
  props: {
    mounts: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      flag: false,
      mode: 1,
      tips: "",
      mountIndex: 1,
      ws: null,
      selected_mount: null,
    };
  },
  methods: {
    changeLeftBtn() {
        if(this.mounts.length==1)return;
      if (this.mountIndex >= 1) {
        let arr = this.mounts[0];
        this.mounts[0] = this.mounts[this.mountIndex];
        this.mounts[this.mountIndex] = arr;
        this.$forceUpdate();
        this.mountIndex--;
      } else {
              this.mountIndex = this.mounts.length - 1;
      }
    },
    changeRightBtn() {
        if(this.mounts.length==1)return;
      if (this.mountIndex < this.mounts.length) {
        let arr = this.mounts[0];
        this.mounts[0] = this.mounts[this.mountIndex];
        this.mounts[this.mountIndex] = arr;
        this.$forceUpdate();
        this.mountIndex++;
      } else {
        this.mountIndex = 1;
      }
    },
    handle_change_mode(value) {
      if (value) {
        if (value === 2) {
          this.$el_message("请使用摇杆控制");
          this.ws = new WebSocket("ws://127.0.0.1:8802");
          this.ws.onmessage = (e) => {
            let reader = new FileReader();
            reader.onload = (event) => {
              let data = event.currentTarget.result;
              data = JSON.parse(data).data;
              this.$emit("fn", {
                type: 200,
                cmdControlType: 111,
                data,
              });
            };
            reader.readAsText(e.data);
          };
        } else {
          this.ws && this.ws.close();
          this.ws = null;
        }
      }
    },

  },

  created() {
    this.tips = "正在识别使用中的挂载....";
  },
  beforeDestroy() {
    if (this.ws) {
      this.ws.close();
      this.ws = null;
    }
  },
};
</script>

<style lang="scss" scoped>
.cpt-nest-station {
  width: 170px;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items:center;
  flex-direction: column;
  box-shadow: 0 2px 4px 0 rgba(1, 162, 255, 0.35),
    inset 0 0 40px 0 rgba(0, 184, 255, 0.5);
/* border: 1px solid #00ffff; */
  .title-box {
    flex-shrink: 0;
    display: flex;
    width:100%;
    height:50px;
    justify-content:space-between;
    align-items: center;
    .title {
      display: flex;
      align-items: center;
      .text {
        font-family: Microsoft YaHei;
        font-size: 14px;
        line-height: 14px;
        font-weight: 400;
        margin-left: 8px;
      }
    }

    .icon-box {
      display: flex;
      align-items: center;
      cursor: pointer;
      ::v-deep .el-select {
        margin-right: 12px;
        .el-input {
          .el-input__inner {
            width: 110px;
            height: 28px;
            background: #000000;
            border: 1px solid #00ffff;
            color: #fff;
          }
          .el-input__suffix {
            .el-input__suffix-inner {
              .el-input__icon {
                line-height: 28px;
              }
            }
          }
        }
      }
      ::v-deep .el-switch {
        .el-switch__core {
          background: #000000;
          border: 1px solid #00ffff;
          //   &::after {
          //     width: 18px;
          //     height: 18px;
          //   }
          &::before {
            content: "关";
            position: absolute;
            right: 4px;
            left: "auto";
            color: #fff;
            font-size: 12px;
            color: #00ffff;
            top: -1px;
          }
        }
        &.is-checked {
          .el-switch__core {
            &::before {
              content: "开";
              position: absolute;
              right: "auto";
              left: 4px;
              color: #fff;
              font-size: 12px;
              color: #00ffff;
              top: -1px;
            }
          }
        }
      }
    }
  }
  .ctx-box {
    display: flex;
    width: 100px;
    // overflow: hidden;
    justify-content: flex-start;
    align-items: center;
    height: 100px;
    color: #fff;
    .mount-list {
      display: flex;
      .mount-item {
        width: 100px;
        height: 100px;
        img {
          width: 100%;
          height: 100%;
        }
      }
    }
  }
}
</style>