<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="name mt-2"
          v-text="supplierInfo.shopName + '认证记录'"
        ></div>
      </div>
      <div class="page-content">
        <template v-if="list.length > 0">
          <div class="page-title">设备认证</div>
          <div
            class="device-list"
            v-for="item in list"
            :key="item.productId"
            @click="toEdit(item)"
          >
            <div class="device">
              <div class="name">
                <span class="label">设备名称：</span>
                <span class="content">{{
                  item.productName ? item.productName : ''
                }}</span>
              </div>
              <div class="status" :class="auditStatusColor(item.auditStatus)">
                <span class="label">状态：</span>
                <span class="content">{{
                  item.auditStatus | auditStatusFilter
                }}</span>
              </div>
            </div>
          </div>
        </template>
        <template v-else>
          <SimpleEmpty name="icon-empty-device.png" description="暂无设备~" />
        </template>
      </div>
    </van-list>
  </div>
</template>

<script>
import SimpleEmpty from '@/components/SimpleEmpty'
import { mapGetters } from 'vuex'
export default {
  layout: 'app-ph',
  components: {
    SimpleEmpty,
  },
  data() {
    return {
      isLoadingMore: true,
      finished: false,
      isRequest: true,
      list: [],
      listQuery: {
        authId: 0,
        listType: 2,
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
    }
  },
  filters: {
    auditStatusFilter(value) {
      // 认证状态：0审核未通过，1审核通过，2待审核
      const map = {
        0: '审核未通过',
        1: '审核通过',
        2: '待审核',
      }
      return map[value]
    },
  },
  created() {
    this.initData()
  },
  computed: {
    ...mapGetters(['supplierInfo', 'authUserId', 'routePrefix', 'authId']),
  },
  methods: {
    toEdit(item) {
      this.$router.push(
        `${this.routePrefix}/record/device/detail?id=${item.productId}&relationId=${item.relationId}`
      )
    },
    initData() {
      this.listQuery.authId = this.$route.query.authId
      if(!this.listQuery.authId) return
      this.authProductList()
    },
    // 获取机构列表
    async authProductList() {
      try {
        this.isLoadingMore = true
        const res = await this.$http.api.getClubAuthProductList(this.listQuery)
        this.total = res.data.total
        this.list = [...this.list, ...res.data.list]
        this.finished = !res.data.hasNextPage
        this.isLoadingMore = false
        this.listQuery.pageNum += 1
      } catch (error) {
        console.log(error)
      } finally {
        this.$toast.clear()
        this.isRequest = false
      }
    },
    auditStatusColor(value) {
      // 认证状态：0 danger，1 success，2 warning
      const map = {
        0: 'danger',
        1: 'success',
        2: 'warning',
      }
      return map[value]
    },
    // 加载更多
    onLoadMore() {
      this.authProductList()
    },
  },
}
</script>

<style lang="scss" scoped>
@media screen and (min-width: 768px) {
  .page {
    background: #fff;
  }

  .page-top {
    height: 360px;
    @include themify($themes) {
      background: themed('pc-banner-record-device');
      background-size: auto 360px;
    }
    .logo {
      display: block;
      width: 120px;
      height: 120px;
      border-radius: 50%;
      background: #fff;
    }
    .name {
      font-size: 30px;
      color: #fff;
    }
  }
  .page-content {
    width: 700px;
    margin: 0 auto;
    overflow: hidden;
    min-height: calc(100vh - 80px - 80px - 360px);
    box-sizing: border-box;
    padding-bottom: 40px;

    .page-title {
      font-size: 24px;
      font-weight: bold;
      text-align: center;
      padding: 40px 0;
    }

    .device-list {
      .device {
        position: relative;
        padding: 36px 0 12px;
        border-bottom: 1px solid #c2c2c2;
        cursor: pointer;

        .name {
          margin-bottom: 8px;
        }

        .label {
          font-size: 18px;
          color: #666;
        }

        .content {
          font-size: 18px;
          color: #282828;
        }
        .status {
          &.success {
            .content {
              color: #f3920d;
            }
          }
          &.warning {
            .content {
              color: #1890ff;
            }
          }
          &.danger {
            .content {
              color: #f94b4b;
            }
          }
        }

        &::after {
          content: '';
          position: absolute;
          right: 0;
          top: 50%;
          transform: translateY(-50%);
          display: block;
          width: 20px;
          height: 20px;
          background: url(~assets/theme-images/common/pc-icon-detail-more.png)
            no-repeat center;
          background-size: 18px;
        }
      }
    }
  }
}

@media screen and (max-width: 768px) {
  .page {
    background: #fff;
  }

  .page-top {
    height: 46vw;
    @include themify($themes) {
      background: themed('h5-banner-record-device');
      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 {
    box-sizing: border-box;
    padding: 8vw 7vw;

    .page-title {
      font-size: 4.2vw;
      font-weight: bold;
      text-align: center;
      color: #282828;
      margin-bottom: 4.6vw;
    }

    .device-list {
      .device {
        position: relative;
        padding: 2.6vw 0;
        border-bottom: 0.1vw solid #c2c2c2;
        cursor: pointer;

        .name {
          margin-bottom: 2.2vw;
        }

        .label {
          font-size: 3.4vw;
          color: #666;
        }

        .content {
          font-size: 3.4vw;
          color: #282828;
        }
        .status {
          &.success {
            .content {
              color: #f3920d;
            }
          }
          &.warning {
            .content {
              color: #1890ff;
            }
          }
          &.danger {
            .content {
              color: #f94b4b;
            }
          }
        }

        &::after {
          content: '';
          position: absolute;
          right: 0;
          top: 50%;
          transform: translateY(-50%);
          display: block;
          width: 20px;
          height: 20px;
          background: url(~assets/theme-images/common/h5-icon-detail-more.png)
            no-repeat center;
          background-size: 18px;
        }
      }
    }
  }
}
</style>
