<template>
  <div class="Store">
    <div class="store-list">
      <div class="store-item" v-for="(item, index) in list" :key="index" hover-class="text-hover" @click="$linkTo('./StoreDetail?id=' + item.id)">
        <image :src="item.photoUrl" mode="aspectFill" />
        <div class="middle">
          <h2>{{ item.merchantName }}</h2>
          <p>{{ item.title }}</p>
        </div>
        <image class="right" src="https://cdn.cunwuyun.cn/wxmp/tianfuxing/ContentRightArrow@2x.png" />
      </div>
    </div>
    <AiEmpty v-if="!list.length"></AiEmpty>
  </div>
</template>

<script>
  import { mapActions, mapState } from 'vuex'
  export default {
    appName: '商家优惠',
    name: 'Store',

    data () {
      return {
        current: 1,
        list: [],
        isMore: false,
        isFixed: false,
        statusBarHeight: 20,
      }
    },

    computed: {
      ...mapState(['user', 'token'])
    },

    onLoad () {
      this.getList()

      if (!this.token) {
        this.autoLogin()
      } else {
        this.getUserInfo()
      }
    },

    methods: {
      ...mapActions(['autoLogin', 'getUserInfo']),

      getList () {
        if (this.isMore) return

        this.$loading()
        this.$instance.post(`/api/appmerchantinfo/discountList`, null, {
          withoutToken: true,
          params: {
            current: this.current,
            size: 10,
            status: 1
          }
        }).then(res => {
          if (res.code === 0) {
            this.$hideLoading()
            if (this.current > 1) {
              this.list = [...this.list, ...res.data.records]
            } else {
              this.list = res.data.records
            }

            if (res.data.records.length < 10) {
              this.isMore = true

              return false
            }

            this.current = this.current + 1
          } else {
            this.isMore = true
          }
        }).catch(() => {
          this.$hideLoading()
        })
      }
    },

    onReachBottom () {
      this.getList()
    }
  }
</script>

<style lang="scss" scoped>
  .Store {
    margin: 0 32px;
    padding: 32px 0 20px;
  }

  .store-item {
    display: flex;
    align-items: center;
    margin-bottom: 24px;
    padding: 24px;
    background: #FFFFFF;
    box-shadow: 0 0 8px 0 rgba(0,0,0,0.02);
    border-radius: 16px;

    image {
      width: 112px;
      height: 112px;
      margin-right: 24px;
    }

    .middle {
      flex: 1;
    }

    .right {
      width: 32px;
      height: 32px;
      margin-right: 0;
    }

    h2 {
      line-height: 48px;
      margin-bottom: 8px;
      font-weight: 500;
      font-size: 34px;
      color: #333333;
    }

    p {
      font-size: 26px;
      color: #FFB94C;
    }
  }
</style>
