<template>
  <div class="page">
    <van-list
      v-model="loadingMore"
      :finished="finished"
      :immediate-check="false"
      :finished-text="total ? '没有更多了' : ''"
      @load="fetchClubList"
    >
      <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">
          <simple-search
            v-model="listQuery.authParty"
            @search="filterClubList"
            placeholder="搜索机构"
          />
        </div>
        <!-- 地区选择 -->
        <div class="city">
          <SimpleCity
            ref="citySelect"
            @change="onCityChange"
            :options="cityList"
            labelName="name"
            valueName="id"
          ></SimpleCity>
        </div>
        <template v-if="starList.length > 0">
          <!-- 明星机构 -->
          <div class="title flex justify-between px-4 pt-8 pb-8 md:px-0">
            <div>明星机构</div>
            <nuxt-link :to="routePrefix + '/approve/club/star-list'"
              >更多<i class="el-icon-arrow-right"></i
            ></nuxt-link>
          </div>
          <div class="list">
            <template v-for="item in starList">
              <div
                class="section flex justify-between mb-4"
                :key="item.authId"
                @click="toDetail(item)"
              >
                <img
                  class="cover"
                  :src="item.logo || drawLogo(item.authParty)"
                />
                <div class="info">
                  <div class="name" v-text="item.authParty"></div>
                  <div class="mobile">{{ item.mobile || '暂无' }}</div>
                  <div class="address">
                    {{ formatAddress(item.area, item.address) }}
                  </div>
                </div>
              </div>
            </template>
            <div
              class="empty"
              v-for="i in starEmptyList"
              :key="'empty-' + i"
            ></div>
          </div>
        </template>
        <!-- 标题 -->
        <div class="title flex justify-between px-4 pt-8 pb-8 md:px-0">
          <div>距您最近...</div>
          <div>共<span v-text="total"></span>家机构</div>
        </div>
        <!-- 列表 -->
        <div class="list">
          <template v-for="item in list">
            <div
              class="section flex justify-between mb-4"
              :key="item.authId"
              @click="toDetail(item)"
            >
              <img class="cover" :src="item.logo || drawLogo(item.authParty)" />
              <div class="info">
                <div class="name" v-text="item.authParty"></div>
                <div class="mobile">{{ item.mobile || '暂无' }}</div>
                <div class="address">
                  {{ formatAddress(item.area, item.address) }}
                </div>
                <div
                  class="distance"
                  v-text="formatDistance(item.distance)"
                  v-if="item.distance && item.distance !== 99999"
                ></div>
              </div>
            </div>
          </template>
          <div class="empty" v-for="i in emptyList" :key="'empty-' + i"></div>
        </div>
        <!-- 列表为空 -->
        <SimpleEmpty
          v-if="!total && !isRequest"
          name="icon-empty-club.png"
          description="敬请期待~"
        ></SimpleEmpty>
      </div>
    </van-list>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import clubListMixin from '@/mixins/clubList'
import { objectCover } from '@/utils'
export default {
  layout: 'app-ph',
  mixins: [clubListMixin],
  data() {
    return {
      cityList: [],
      selectValue: [],
    }
  },
  computed: {
    ...mapGetters(['supplierInfo']),
    emptyList() {
      return 3 - (this.list.length % 3)
    },
    starEmptyList() {
      return 3 - (this.starList.length % 3)
    },
  },
  created() {
    this.fetchCityList()
  },
  methods: {
    // 从缓存中获取数据
    initFromCache(cacheData) {
      const data = objectCover(this, cacheData)
      this.$nextTick(() => {
        this.$refs.citySelect.setSelectValue(data.selectValue)
      })
    },

    // 获取地址列表
    fetchCityList() {
      this.$http.api.fetchAllCityList().then((res) => {
        this.cityList = res.data
      })
    },

    // 城市变化
    onCityChange(selectValue) {
      this.listQuery.provinceId = ''
      this.listQuery.cityId = ''
      this.listQuery.townId = ''
      this.selectValue = selectValue

      selectValue.map((item, index) => {
        if (index === 0) {
          this.listQuery.provinceId = item.id
        } else if (index === 1) {
          this.listQuery.cityId = item.id
        } else {
          this.listQuery.townId = item.id
        }
      })
      this.filterClubList()
    },
  },
}
</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('pc-banner-club');
    }
    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(-60px);
    }
  }
  .page-content {
    width: 1200px;
    margin: 0 auto;
    .search {
      position: absolute;
      left: 50%;
      top: 260px;
      transform: translateX(-50%);
    }

    .city {
      position: absolute;
      left: 50%;
      top: 320px;
      transform: translateX(-50%);
      z-index: 9;
    }

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

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

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

      .empty {
        width: 390px;
      }

      .section {
        width: 390px;
        height: 108px;
        background-color: #f3f5f6;
        border-radius: 4px;
        box-sizing: border-box;
        padding: 12px;
        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;
        }
        .info {
          position: relative;
          margin-left: 12px;
          .name {
            width: 200px;
            font-size: 16px;
            color: #101010;
            font-weight: bold;
            margin-bottom: 16px;
            text-overflow: ellipsis;
            white-space: nowrap;
            overflow: hidden;
          }
          .mobile,
          .address {
            width: 268px;
            position: relative;
            font-size: 14px;
            color: #404040;
            padding-left: 24px;
            line-height: 24px;
            text-overflow: ellipsis;
            white-space: nowrap;
            overflow: hidden;
            &::after {
              content: '';
              display: block;
              width: 16px;
              height: 16px;
              position: absolute;
              left: 0;
              top: 50%;
              transform: translateY(-50%);
              background-size: 16px;
              background-repeat: no-repeat;
            }
          }
          .mobile {
            &::after {
              background-image: url(~assets/theme-images/common/pc-icon-mobile.png);
            }
          }
          .address {
            &::after {
              background-image: url(~assets/theme-images/common/pc-icon-address.png);
            }
          }

          .distance {
            position: absolute;
            font-size: 12px;
            color: #404040;
            top: 2px;
            right: 0;
          }
        }
      }
    }
  }
}

// 移动 端
@media screen and (max-width: 768px) {
  .page-top {
    height: 46vw;
    @include themify($themes) {
      background: themed('h5-banner-club');
    }
    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%);
    }

    .city {
      position: relative;
      z-index: 9;
      padding-top: 12vw;
    }

    .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;
        }
        .info {
          position: relative;
          margin-left: 3.2vw;
          .name {
            width: 48vw;
            font-size: 4vw;
            color: #101010;
            font-weight: bold;
            margin-bottom: 4vw;
            text-overflow: ellipsis;
            white-space: nowrap;
            overflow: hidden;
          }
          .mobile,
          .address {
            width: 66vw;
            position: relative;
            font-size: 3vw;
            color: #404040;
            padding-left: 5vw;
            line-height: 5vw;
            text-overflow: ellipsis;
            white-space: nowrap;
            overflow: hidden;
            &::after {
              content: '';
              display: block;
              width: 4vw;
              height: 4vw;
              position: absolute;
              left: 0;
              top: 50%;
              transform: translateY(-50%);
              background-size: 4vw 4vw;
              background-repeat: no-repeat;
            }
          }
          .mobile {
            &::after {
              background-image: url(~assets/theme-images/common/h5-icon-mobile.png);
            }
          }
          .address {
            &::after {
              background-image: url(~assets/theme-images/common/h5-icon-address.png);
            }
          }

          .distance {
            position: absolute;
            font-size: 3vw;
            color: #404040;
            top: 0.8vw;
            right: 0;
          }
        }
      }
    }
  }
}
</style>
