<template>
  <div class="page">
    <van-list
      v-model="isLoadingMore"
      :finished="finished"
      :immediate-check="false"
      :finished-text="total ? '没有更多了' : ''"
      @load="onLoadMore"
    >
      <div class="page-top flex flex-col justify-center items-center">
        <img class="logo" :src="supplierInfo.logo" />
        <div class="mt-2 name">
          <span v-text="supplierInfo.shopName"></span>
          <span>官方授权设备</span>
        </div>
      </div>
      <div class="page-content">
        <!-- 搜索区域 -->
        <div class="search">
          <SimpleSearch
            v-model="listQuery.name"
            @search="onSearch"
            placeholder="搜索设备"
          />
        </div>
        <!-- 标题 -->
        <div class="title px-4 pt-12 pb-6">
          共<span v-text="total"></span>种设备
        </div>
        <!-- 列表 -->
        <div class="list">
          <div
            class="section flex items-center mb-4"
            v-for="item in list"
            :key="item.productTypeId"
            @click="toDetail(item)"
          >
            <img class="cover" :src="item.image" />
            <div class="name" v-text="item.name"></div>
          </div>
          <div class="empty" v-for="i in emptyList" :key="'empty-' + i"></div>
        </div>

        <!-- 列表为空 -->
        <SimpleEmpty
          v-if="!total && !isRequest"
          name="icon-empty-device.png"
          description="敬请期待~"
        ></SimpleEmpty>
      </div>
    </van-list>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { debounce } from '@/utils'
export default {
  layout: 'app',
  data() {
    return {
      isLoadingMore: true,
      finished: false,
      isRequest: true,
      listQuery: {
        authUserId: '',
        name: '',
        pageNum: 1,
        pageSize: 9,
      },
      list: [],
      total: 0,
    }
  },
  computed: {
    ...mapGetters(['supplierInfo', 'authUserId', 'routePrefix']),
    emptyList() {
      return 3 - (this.list.length % 3)
    },
  },
  mounted() {
    this.fetchList()
  },
  methods: {
    // 获取设备分类
    fetchList: debounce(async function () {
      try {
        this.isLoadingMore = true
        this.listQuery.authUserId = this.authUserId
        const res = await this.$http.api.getAuthProductCateList(this.listQuery)
        this.list = [...this.list, ...res.data.list]
        this.finished = !res.data.hasNextPage
        this.total = res.data.total
        this.isLoadingMore = false
        this.listQuery.pageNum += 1
      } catch (error) {
        console.log(error)
      } finally {
        this.isRequest = false
      }
    }, 400),

    // 设备详情
    toDetail(item) {
      const url = `${this.routePrefix}/approve/device/list?id=${item.productTypeId}`
      this.$router.push(url)
    },
    // 搜索
    onSearch() {
      this.listQuery.pageNum = 1
      this.list = []
      this.fetchList()
    },
    // 加载更多
    onLoadMore() {
      this.fetchList()
    },
  },
}
</script>

<style scoped lang="scss">
// pc 端
@media screen and (min-width: 768px) {
  .page {
    position: relative;
    min-height: calc(100vh - 80px - 80px);
    background-color: #fff;
  }
  .page-top {
    height: 420px;
    @include themify($themes) {
      background: themed('banner-device-pc');
      background-size: auto 420px;
    }

    .logo {
      display: block;
      width: 120px;
      height: 120px;
      border-radius: 50%;
      background: #fff;
    }
    .name {
      font-size: 30px;
      color: #fff;
    }

    .logo,
    .name {
      transform: translateY(-30px);
    }
  }
  .page-content {
    width: 1200px;
    margin: 0 auto;

    .search {
      position: absolute;
      left: 50%;
      top: 300px;
      transform: translateX(-50%);
    }

    .title {
      font-size: 16px;
      color: #404040;

      span {
        @include themify($themes) {
          color: themed('color');
        }
      }
    }

    .list {
      display: flex;
      align-items: center;
      flex-wrap: wrap;
      justify-content: space-between;

      .empty {
        width: 390px;
      }

      .section {
        width: 390px;
        height: 108px;
        background-color: #f3f5f6;
        border-radius: 4px;
        box-sizing: border-box;
        padding: 16px;
        cursor: pointer;
        transition: all 0.4s;

        &:hover {
          box-shadow: 0 0 24px rgba(0, 0, 0, 0.2);
        }

        .cover {
          display: block;
          width: 84px;
          height: 84px;
        }
        .name {
          width: 260px;
          font-size: 16px;
          color: #101010;
          text-overflow: ellipsis;
          white-space: nowrap;
          overflow: hidden;
          margin-left: 16px;
        }
      }
    }
  }
}

// 移动 端
@media screen and (max-width: 768px) {
  .page-top {
    height: 46vw;
    @include themify($themes) {
      background: themed('banner-device-h5');
      background-size: auto 46vw;
    }
    .logo {
      display: block;
      width: 14.8vw;
      height: 14.8vw;
      border-radius: 50%;
      background: #fff;
    }
    .name {
      font-size: 4vw;
      color: #fff;
    }
  }
  .page-content {
    position: relative;
    .search {
      position: absolute;
      left: 50%;
      top: 0;
      transform: translate(-50%, -50%);
    }

    .title {
      font-size: 3.4vw;
      color: #404040;

      span {
        @include themify($themes) {
          color: themed('color');
        }
      }
    }

    .list {
      display: flex;
      align-items: center;
      flex-direction: column;
      .section {
        width: 93.6vw;
        height: 26vw;
        background-color: #f3f5f6;
        border-radius: 4px;
        box-sizing: border-box;
        padding: 3.2vw;

        .cover {
          display: block;
          width: 19.6vw;
          height: 19.6vw;
        }
        .name {
          width: 66vw;
          font-size: 4vw;
          color: #101010;
          text-overflow: ellipsis;
          white-space: nowrap;
          overflow: hidden;
          margin-left: 3.2vw;
        }
      }
    }
  }
}
</style>
