<template>
  <div class="page md:flex md:justify-between">
    <div class="page-title">机构认证</div>
    <div class="page-top">
      <div class="swiper">
        <SimpleSwiper :imageList="clubInfo.bannerList"></SimpleSwiper>
      </div>
    </div>
    <div class="page-content">
      <div class="club-info">
        <div class="section flex justify-between items-center">
          <div class="info">
            <div class="name" v-text="clubInfo.authParty"></div>
            <div class="mobile">{{ clubInfo.mobile | formatEmpty }}</div>
            <div class="address" v-text="address"></div>
          </div>
          <div class="logo"><img :src="clubInfo.logo" /></div>
        </div>
        <div class="section flex justify-between items-center mt-6">
          <div class="navigation" @click="onMapNav">导航</div>
          <div
            class="distance"
            v-if="clubInfo.distance && clubInfo.distance < 99999"
            v-text="'距你' + clubInfo.distance + 'km'"
          ></div>
        </div>
      </div>
      <div class="divider"></div>
      <div class="device-list">
        <div class="title">已认证设备</div>
        <div class="list">
          <div
            class="device flex justify-between items-center"
            v-for="item in clubInfo.productList"
            :key="item.productId"
          >
            <div class="info">
              <div class="name" v-text="item.productName"></div>
              <div class="code">SN码：{{ item.snCode | formatSnCode }}</div>
            </div>
            <div class="detail" @click="toDetail(item)">查看认证</div>
          </div>
        </div>
      </div>
      <SimpleEmpty
        v-if="isEmpty"
        name="icon-empty-device.png"
        description="暂无已认证设备"
      ></SimpleEmpty>
    </div>

    <SimpleMapNav ref="mapNav" @click="navigation"></SimpleMapNav>
  </div>
</template>

<script>
import { drawLogo } from '@/utils'
import { mapNavigate } from '@/utils/map-utils'
import { mapGetters } from 'vuex'
export default {
  layout: 'app',
  filters: {
    formatEmpty(val) {
      return val || '未知'
    },
    formatSnCode(code) {
      if (!code) return ''
      return code.replace(/^(\w{2})\w+(\w{4})$/, '$1******$2')
    },
  },
  data() {
    return {
      authId: '',
      clubInfo: {},
    }
  },
  computed: {
    ...mapGetters(['routePrefix']),
    address() {
      return (
        (this.clubInfo.area &&
          this.clubInfo.area + this.clubInfo.address &&
          this.clubInfo.address) ||
        '未知'
      )
    },
    isEmpty() {
      return this.clubInfo.productList
        ? this.clubInfo.productList.length === 0
        : true
    },
  },
  mounted() {
    this.initData()
  },
  beforeDestroy() {
    this.$removeStorage(this.routePrefix, 'clubInfo')
  },
  methods: {
    // 设备详情
    toDetail(item) {
      window.location.href = `${process.env.CIMEI_LOCAL}/product/auth/product-${item.productId}.html`
    },
    // 初始化
    initData() {
      this.authId = parseInt(this.$route.query.id)
      const clubInfo = this.$getStorage(this.routePrefix, 'clubInfo')
      if (clubInfo) {
        this.clubInfo = clubInfo
      }
      this.fetchDetail()
    },
    // 获取机构详细信息
    async fetchDetail() {
      try {
        const res = await this.$http.api.getAuthClubDetail({
          authId: this.authId,
        })
        this.clubInfo = { ...this.clubInfo, ...res.data } // 合并
      } catch (error) {
        console.log(error)
      }
      // 默认轮播图
      if (this.clubInfo.bannerList.length <= 0) {
        this.clubInfo.bannerList.push('/placeholder.png')
      }
      // 默认logo
      if (!this.clubInfo.logo) {
        this.clubInfo.logo = drawLogo(this.clubInfo.authParty)
      }
    },
    // 地图导航
    onMapNav() {
      this.$refs.mapNav.open()
    },
    // 导航
    navigation(type) {
      const point = this.clubInfo.lngAndLat.split(',')
      const lng = point[0]
      const lat = point[1]
      mapNavigate(
        { lat, lng, title: this.clubInfo.authParty, address: this.address },
        type
      )
      this.$refs.mapNav.close()
    },
  },
}
</script>

<style scoped lang="scss">
// pc 端
@media screen and (min-width: 768px) {
  .page {
    position: relative;
    width: 1200px;
    height: 612px;
    margin-left: auto;
    margin-right: auto;
    margin-top: 80px;
    background-color: #fff;
    box-sizing: border-box;
    padding: 16px;
    padding-right: 0;
  }

  .page-title {
    position: absolute;
    font-size: 24px;
    color: #333;
    top: -50px;
    left: 0;
  }

  .page-top {
    .swiper {
      width: 580px;
      height: 580px;
      background: #f7f7f7;
      ::v-deep {
        img {
          width: 580px;
          height: 580px;
        }
      }
    }
  }

  .page-content {
    width: 580px;
    overflow-y: auto;
    .club-info {
      padding: 32px 24px;
      background: linear-gradient(
        180deg,
        #ffe6e8 0%,
        rgba(255, 255, 255, 0) 100%
      );

      .info {
        width: 320px;
        .name {
          font-size: 24px;
          color: #101010;
          font-weight: bold;
          margin-bottom: 34px;
        }
        .mobile,
        .address {
          position: relative;
          padding-left: 24px;
          margin-top: 16px;
          line-height: 24px;
          font-size: 16px;
          color: #404040;

          &::after {
            content: '';
            display: block;
            width: 16px;
            height: 16px;
            position: absolute;
            left: 0;
            top: 4px;
            background-size: 16px;
            background-repeat: no-repeat;
          }
        }
        .mobile {
          &::after {
            background-image: url(https://static.caimei365.com/www/authentic/pc/icon-phone-active.png);
          }
        }
        .address {
          &::after {
            background-image: url(https://static.caimei365.com/www/authentic/pc/icon-address-active.png);
          }
        }
      }

      .logo {
        position: relative;
        width: 114px;
        height: 114px;
        border-radius: 50% 50% 0 50%;
        overflow: hidden;

        &::after {
          position: absolute;
          bottom: 0;
          right: 0;
          content: '';
          display: block;
          width: 23px;
          height: 23px;
          background: url(https://static.caimei365.com/www/authentic/pc/icon-avatar-v.png)
            no-repeat center;
          background-size: 23px;
        }
      }
      .navigation {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 72px;
        height: 32px;
        border: 1px solid #bc1724;
        border-radius: 4px;
        background-color: #ffe6e8;
        font-size: 16px;
        color: #bc1724;
        cursor: pointer;

        &::after {
          content: '';
          display: block;
          width: 16px;
          height: 16px;
          background: url(https://static.caimei365.com/www/authentic/pc/icon-arround-right.png)
            no-repeat center;
          background-size: 16px;
        }
      }
      .distance {
        font-size: 14px;
        color: #404040;
      }
    }

    .device-list {
      .title {
        padding: 16px;
        font-size: 20px;
        font-weight: bold;
        color: #404040;
        background-color: #f3f5f6;
      }

      .list {
        padding-right: 16px;
      }

      .device {
        padding: 16px 0;
        border-bottom: 1px solid #d8d8d8;
        .info {
          .name {
            font-size: 18px;
            color: #101010;
          }
          .code {
            margin-top: 16px;
            font-size: 14px;
            color: #666;
          }
        }
        .detail {
          display: flex;
          justify-content: center;
          align-items: center;
          width: 80px;
          height: 32px;
          background: #bc1724;
          border-radius: 4px;
          font-size: 14px;
          color: #ffffff;
        }
      }
    }
  }
}

// 移动 端
@media screen and (max-width: 768px) {
  .page-title {
    display: none;
  }

  .page-top {
    .swiper {
      height: 100vw;

      background: #f7f7f7;
      ::v-deep {
        img {
          height: 100vw;
        }
      }
    }
  }

  .page-content {
    .divider {
      height: 3.2vw;
      background-color: #f7f7f7;
    }
    .club-info {
      padding: 4vw;
      background: linear-gradient(
        180deg,
        #ffe6e8 0%,
        rgba(255, 255, 255, 0) 100%
      );

      .info {
        width: 67vw;
        .name {
          font-size: 4.8vw;
          color: #101010;
          font-weight: bold;
          margin-bottom: 4vw;
        }
        .mobile,
        .address {
          position: relative;
          padding-left: 5vw;
          margin-top: 1.6vw;
          line-height: 5vw;
          font-size: 3.2vw;
          color: #404040;

          &::after {
            content: '';
            display: block;
            width: 4vw;
            height: 4vw;
            position: absolute;
            left: 0;
            top: 0.5vw;
            background-size: 4vw 4vw;
            background-repeat: no-repeat;
          }
        }
        .mobile {
          &::after {
            background-image: url(https://static.caimei365.com/www/authentic/h5/icon-phone-active.png);
          }
        }
        .address {
          &::after {
            background-image: url(https://static.caimei365.com/www/authentic/h5/icon-address-active.png);
          }
        }
      }

      .logo {
        position: relative;
        width: 18vw;
        height: 18vw;
        border-radius: 9vw 9vw 0 9vw;
        overflow: hidden;

        &::after {
          position: absolute;
          bottom: 0;
          right: 0;
          content: '';
          display: block;
          width: 3.6vw;
          height: 3.6vw;
          background: url(https://static.caimei365.com/www/authentic/h5/icon-avatar-v.png)
            no-repeat center;
          background-size: 3.6vw;
        }
      }
      .navigation {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 14.4vw;
        height: 6.4vw;
        border: 0.1vw solid #bc1724;
        border-radius: 0.4vw;
        background-color: #ffe6e8;
        font-size: 3.2vw;
        color: #bc1724;

        &::after {
          content: '';
          display: block;
          width: 3.6vw;
          height: 3.6vw;
          background: url(https://static.caimei365.com/www/authentic/h5/icon-arround-right.png)
            no-repeat center;
          background-size: 3.6vw;
        }
      }
      .distance {
        font-size: 3vw;
        color: #404040;
      }
    }

    .device-list {
      .title {
        padding: 4vw;
        padding-bottom: 0;
        font-size: 4vw;
        font-weight: bold;
        color: #101010;
      }
      .device {
        padding: 4vw 0;
        margin: 0 4vw;
        border-bottom: 0.4vw solid #d8d8d8;
        .info {
          .name {
            font-size: 3.6vw;
            color: #101010;
          }
          .code {
            margin-top: 3.2vw;
            font-size: 3vw;
            color: #666;
          }
        }
        .detail {
          display: flex;
          justify-content: center;
          align-items: center;
          width: 15.8vw;
          height: 6.4vw;
          background: #bc1724;
          border-radius: 0.4vw;
          font-size: 3vw;
          color: #ffffff;
        }
      }
    }
  }
}
</style>
