<template>
  <div class="device">
    <div class="device-title">
      设备：{{ deviceList.length }}
    </div>
    <div class="device-content">
      <template
        v-for="(item, index) in deviceList"
        :key="index"
      >
        <div
          class="device-content-item"
          :class="{active:item.check}"
          @click="item.check= !item.check && showBar"
        >
          <n-image
            width="50"
            height="50"
            :preview-disabled="true"
            :src="item.url"
          />
          <div>
            <p>智能灯智能灯智能灯智能灯智能灯智能灯智能灯智能灯智能灯</p>
            <p>未连接</p>
          </div>
        </div>
      </template>
    </div>
    <div
      v-if="showBar"
      class="device-bar"
    >
      <span>添加设备</span>
      <n-button
        type="warning"
        @click="change"
      >
        完成
      </n-button>
    </div>
  </div>
</template>
<script setup lang="ts">
import useStore from '@/store'
import { computed, onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const store = useStore()
const deviceList = ref([
  { check: false, url: new URL('./img/1.png', import.meta.url).href },
  { check: false, url: new URL('./img/1.png', import.meta.url).href },
  { check: false, url: new URL('./img/1.png', import.meta.url).href },
  { check: false, url: new URL('./img/1.png', import.meta.url).href },
  { check: false, url: new URL('./img/1.png', import.meta.url).href },
  { check: false, url: new URL('./img/1.png', import.meta.url).href },
  { check: false, url: new URL('./img/1.png', import.meta.url).href },
  { check: false, url: new URL('./img/1.png', import.meta.url).href },
  { check: false, url: new URL('./img/1.png', import.meta.url).href },
  { check: false, url: new URL('./img/1.png', import.meta.url).href }
])
const backRoute:{[key:string]:string} = { '/list': '/list', '/create': '/room' }
const showBar = computed(() => !!(backRoute[router.options.history.state.back as string]))

function change() {
  router.push(backRoute[router.options.history.state.back as string])
  store.setRoomData(null)
}

onMounted(() => {
  console.log(123, store.newRoomData, router.options.history.state)
})
</script>
<style lang="scss" scoped>
.device {
  height: 100%;
  padding: 40px 4% 0 4%;
  min-width: 327px;

  &-title {
    font-size: 18px;
  }

  &-content {
    margin-top: 20px;
    padding-bottom: 20px;
    display: flex;
    flex-wrap: wrap;
    align-content: flex-start;
    height: calc(100% - 96px);
    overflow-y: auto;
    gap: 15px;

    &-item {
      /* 增长1 不缩小 基础宽度200px */
      flex: 1 0 160px;
      padding: 10px 15px;
      background: #383d43;
      box-shadow: 0px 4px 20px -2px rgba(50, 50, 71, 0.02), 0px 0px 5px 0px rgba(12, 26, 75, 0.04);
      border-radius: 12px;
      cursor: pointer;
      overflow: hidden;
      display: flex;

      &:hover {
        background-color: rgba(99, 226, 183, 0.15);
      }

      p {
        margin: 0;
      }

      &>div:last-child {
        margin-left: 10px;
        font-size: 15px;
        width: calc(100% - 60px);

        &>p:first-child {
          max-width: 100%;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }

        &>p:last-child {
          font-size: 13px;
        }
      }

    }

    .active {
      background: rgba(99, 226, 183, 0.15);
      color: #63e2b7;
    }
  }

  &-bar {
    width: 100%;
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: space-between;

    :deep(.n-button) {
      --n-width: 30%;
      max-width: 200px;
      --n-border-radius: 10px;
    }
  }
}
</style>
