<template>
  <div class="StoreDetail">
    <u-navbar back-icon-color="#000" :back-icon-name="isShare ? 'home': 'nav-back'" title="门店情况" title-color="#000" title-width="300" title-size="32" 
      :title-bold="true" :background="backgroundNavbar" :is-fixed="true" height="44" :custom-back="goBack"></u-navbar>
    <div class="header-info">
      <div class="flex-top">
        <div class="left">
          <div class="name">{{ detailInfo.shopName }}</div>
          <div class="star-info">
            <u-icon name="star-fill" color="#FF8C19" size="24" v-for="(items, indexs) in detailInfo.xzStar" :key="indexs"></u-icon>
            <u-icon name="star-fill" color="#F0F0F0" size="24" v-for="(items, indexs) in detailInfo.wxzStar" :key="indexs"></u-icon>
            <span class="star-num">{{ detailInfo.storeScore }}</span>

            <div class="right-btn" @click="toEvaluateForm">
              <img src="https://cdn.cunwuyun.cn/wechat/fd-store/store-detail-edit-icon.png" alt="">评价
            </div>
          </div>
        </div>
        <div class="right">
          <img :src="detailInfo.qrCodeUrl" alt="" @click="previewShopImg">
        </div>
      </div>
      <div class="address"><u-icon name="map" color="#666" size="24"></u-icon>{{ detailInfo.address }}</div>
      <div class="flex-info">
        <div class="flex-item">
          <p>经营者</p>
          <div>{{ detailInfo.name }}</div>
          
        </div>
        <div class="flex-item">
          <p>经营类型</p>
          <div>{{ $dict.getLabel('operatorType', detailInfo.operatorType) }}</div>
          <span></span>
        </div>
        <div class="flex-item item-phone" v-if="detailInfo.phone" @click="callPhone(detailInfo.phone)">
          <span></span>
          <img src="https://cdn.cunwuyun.cn/wechat/fd-store/phone-icon.png" alt="">
        </div>
      </div>
    </div>
    <div class="line-bg"></div>
    <div class="list-content">
      <div class="type-list">
        <div :class="typeIndex == index ? 'type-item active' : 'type-item' " v-for="(item, index) in typeNumList" :key="index" @click="typeClick(index)">{{ item.dictName }} {{item.num}}</div>
      </div>
      <div class="item" v-for="(item, index) in assessList" :key="index">
        <div class="user-info">
          <img :src="item.evaluatorUrl ? item.evaluatorUrl : 'https://cdn.cunwuyun.cn/wxmp/fengdu/avatar.png'" alt="" class="user-img">
          <div class="user-name">
            <p>{{ item.evaluator }}</p>
            <div v-if="item.evaluationTime">{{ item.evaluationTime.substring(0, 10) }}</div>
          </div>
        </div>
        <div class="star-info">
          <u-icon name="star-fill" color="#FF8C19" size="24" v-for="(items, indexs) in item.xzStar" :key="indexs"></u-icon>
          <u-icon name="star-fill" color="#F0F0F0" size="24" v-for="(items, indexs) in item.wxzStar" :key="indexs"></u-icon>
          <span class="star-num">{{ item.score || 0}}</span>
        </div>
        <div class="text">{{ item.remark }}</div>
        <div v-if="item.imgList && item.imgList.length">
          <img :src="img" alt="" v-for="(img, indexs) in item.imgList" :key="indexs" class="img" @click="preview(item.imgList, indexs)">
        </div>
      </div>
      <AiEmpty v-if="!assessList.length" />
    </div>
  </div>
</template>

<script>
import { mapActions, mapState } from 'vuex'
export default {
  name: 'StoreDetail',
  appName: '门店情况',
  navigationBarBackgroundColor: '#ffffff',
  navigationBarTextStyle: 'black',
  customNavigation: true,
  data() {
    return {
      shopId: '',
      detailInfo: {},
      assessList: [],
      typeIndex: 0,
      current: 1,
      pages: 2,
      typeNumList: [],
      shopEvaluateType: '',
      backgroundNavbar: {
        background: '#fff',
      },
      isShare: false, //是否分享进页面
    }
  },
  computed: {
    ...mapState(['token']),
  },
  onLoad(query) {
    if (decodeURIComponent(query.scene)) {
      this.shopId = decodeURIComponent(query.scene)
      this.isShare = true
    }

    if (query.shopId) {
      this.shopId = query.shopId
      this.isShare = false
    }

    if (this.token) {
      this.$dict.load(['operatorType', 'shopEvaluateType']).then(() => {
        this.getDetail()
        this.getAssessList()
        this.getTypeNum()
      })
    } else {
      this.autoLogin().then(() => {
        this.$dict.load(['operatorType', 'shopEvaluateType']).then(() => {
          this.getDetail()
          this.getAssessList()
          this.getTypeNum()
        })
      })
    }

    uni.$on('updateStore', () => {
      this.getDetail()
      this.getAssessList()
      this.getTypeNum()
    })
  },
  onShow() {
    uni.setNavigationBarColor({
      frontColor: 'black',
      backgroundColor: '#ffffff'
    })
  },
  methods: {
    ...mapActions(['autoLogin']),
    getDetail() {
      this.$instance.post(`/app/appshoparchives/queryDetailById?id=${this.shopId}`).then(res => {
        if (res?.data) {
          this.detailInfo = res.data
          this.detailInfo.xzStar = Math.floor(this.detailInfo.storeScore)
          var wxz = 5 - this.detailInfo.xzStar
          this.detailInfo.wxzStar = wxz <= 0 ? 0 : wxz
        }
      })
    },
    getAssessList() {
      if(this.current > this.pages) return
      this.$loading()
      this.$instance.post(`/app/appshopassess/list?shopId=${this.shopId}&current=${this.current}&shopEvaluateType=${this.shopEvaluateType}`).then(res => {
        if (res?.data) {
          res.data.records.map((item) => {
            if(item.pictureUrl) {
              item.imgList = item.pictureUrl.split(',')
            }
            item.xzStar = Math.floor(item.score)
            var wxz = 5 - item.xzStar
            item.wxzStar = wxz <= 0 ? 0 : wxz
          })
          this.assessList = this.current == 1 ? res.data.records : [this.list, res.data.records].flat()
          this.pages = res.data.pages
        }
      }).finally(() => uni.hideLoading())
    },
    getTypeNum() {
      this.$instance.post(`/app/appshopassess/queryAppScoreCount?id=${this.shopId}`).then(res => {
        if (res.data) {
          var list = this.$dict.getDict('shopEvaluateType')
          list.map((item) => {
            if(item.dictName == '全部') {
              item.num = res.data.queryAllCount
            }
            if(item.dictName == '最新') {
              item.num = res.data.newCount
            }
            if(item.dictName == '好评') {
              item.num = res.data.goodCount
            }
            if(item.dictName == '差评') {
              item.num = res.data.badCount
            }
            if(item.dictName == '有图') {
              item.num = res.data.picCount
            }
            
          })
          this.typeNumList = list
        }
      }).finally(() => uni.hideLoading())
    },
    typeClick(e) {
      this.typeIndex = e
      this.shopEvaluateType = this.typeNumList[e].dictValue
      this.current = 1
      this.pages = 2
      this.getAssessList()
    },
    toEvaluateForm() {
      uni.navigateTo({url: `./EvaluateForm?shopId=${this.shopId}`})
    },
    goBack() {
      if (getCurrentPages().length === 1) {
        uni.switchTab({
          url: "/pages/AppHome/AppHome",
        })
      }else {
        uni.navigateBack()
      }
    },
    preview(imgList, index) {
      var list = []
      imgList.map((item) => {
        var e = {url: item}
        list.push(e)
      })
      console.log(list, index)
      this.$previewImage(list, index, 'url');
    },
    previewShopImg() {
      var imgList = [{url: this.detailInfo.fileUrl}]
      this.$previewImage(imgList, 0, 'url');
    },
    callPhone(phone) {
      uni.makePhoneCall({ phoneNumber: phone })
    },
  },
  onReachBottom() {
    this.current++
    this.getAssessList()
  }

}
</script>

<style lang="scss" scoped>
.StoreDetail {
  background-color: #fff;
  .header-info {
    width: 100%;
    // height: 390px;
    padding: 40px 32px 32px 32px;
    box-sizing: border-box;
    background-image: url("https://cdn.cunwuyun.cn/wechat/fd-store/store-detail-headerBg.png");
    background-size: cover;
    background-repeat: no-repeat;
    .flex-top {
      display: flex;
      .left {
        width: calc(100% - 152px);
        .name {
          padding-top: 8px;
          width: 100%;
          word-break: break-all;
          line-height: 48px;
          font-family: PingFangSC-SNaNpxibold;
          font-weight: 600;
          font-size: 40px;
          color: #333;
        }
        .star-info {
          font-family: PingFangSC-Regular;
          font-size: 24px;
          color: #FF8C19;
          line-height: 24px;
          margin-bottom: 44px;
          .star-num {
            display: inline-block;
            margin-left: 8px;
          }
          .right-btn {
            display: inline-block;
            width: 112px;
            line-height: 48px;
            background: #2D7DFF;
            border-radius: 24px;
            padding-left: 20px;
            box-sizing: border-box;
            font-family: PingFangSC-Regular;
            font-weight: 400;
            font-size: 24px;
            color: #FFF;
            margin-left: 32px;
            vertical-align: bottom;
            img {
              width: 28px;
              height: 28px;
              margin-right: 4px;
              vertical-align: text-bottom;
            }
          }
        }
      }
      .right {
        img {
          width: 112px;
          height: 112px;
          padding: 4px;
          background: #FFF;
          border-radius: 6px;
        }
      }
    }
    .address {
      line-height: 36px;
      font-family: PingFangSC-Regular;
      font-size: 24px;
      color: #666;
      margin-bottom: 20px;
      u-icon {
        margin-right: 10px;
      }
    }
    .flex-info {
      width: 100%;
      padding: 16px 0;
      display: flex;
      background-color: #F0F5FF;
      box-sizing: border-box;
      border-radius: 16px;
      .flex-item {
        flex: 1;
        position: relative;
        font-family: PingFangSC;
        font-size: 24px;
        line-height: 40px;
        text-align: center;
        p {
          color: #999;
          margin-bottom: 4px;
        }
        div {
          color: #222;
        }
        span {
          position: absolute;
          top: 16px;
          left: 0;
          height: 48px;
          border-right: 1px solid #D9D9D9;
        }
      }
      .item-phone {
        img {
          width: 40px;
          height: 40px;
          margin-top: 16px;
        }
      }
    }
  }
  .line-bg {
    width: 100%;
    height: 32px;
    background-color: #F4F5FA;
  }
  .list-content {
    padding: 32px;
    box-sizing: border-box;
    .type-list {
      margin-bottom: 16px;
      .type-item {
        display: inline-block;
        padding: 0 24px;
        line-height: 56px;
        background: #F5F5F5;
        font-family: AppleSystNaNpxUIFont;
        font-size: 28px;
        color: #333;
        margin-right: 12px;
        margin-bottom: 16px;
        border-radius: 8px;
      }
      .active {
        color: #fff;
        background-color: #2D7DFF;
      }
    }
    .item {
      margin-bottom: 32px;
      .user-info {
        display: flex;
        margin-bottom: 24px;
        .user-img {
          width: 72px;
          height: 72px;
          border-radius: 50%;
          margin-right: 16px;
        }
        .user-name {
          p {
            font-family: PingFangSC-Medium;
            font-weight: 500;
            font-size: 28px;
            color: #323233;
            line-height: 40px;
          }
          div {
            font-family: PingFangSC-Regular;
            font-weight: 400;
            font-size: 24px;
            color: #969799;
            line-height: 36px;
          }
        }
      }
      .star-info {
        font-family: PingFangSC-Regular;
        font-size: 24px;
        color: #FF8C19;
        line-height: 24px;
        margin-bottom: 32px;
        .star-num {
          display: inline-block;
          margin-left: 8px;
        }
      }
      .text {
        width: 100%;
        font-family: PingFangSC-Regular;
        font-size: 28px;
        color: #323233;
        line-height: 40px;
        word-break: break-all;
        margin-bottom: 16px;
      }
      .img {
        width: 120px;
        height: 120px;
        margin: 0 8px 8px 0;
      }
    }
  }
}
</style>