<template>
  <div class="merchants">
    <div class="top">
      <image :src="user.merchantInfo.logoFiles && user.merchantInfo.logoFiles[0].url" />
      <div class="right">
        <h2>{{ user.merchantInfo.merchantName }}</h2>
        <p>{{ user.merchantInfo.address }}</p>
      </div>
    </div>
    <div class="merchants-wrapper">
      <h2>我发布的优惠</h2>
      <div class="merchants-list">
        <div class="item" v-for="(item, index) in list" :key="index">
          <div class="item-top">
            <h2>{{ item.title }}</h2>
            <p>{{ item.content }}</p>
            <div class="item-tags">
              <span :class="'status-' + 1" v-if="item.status === '1'">审核通过</span>
              <span :class="'status-' + 0" v-if="item.status === '0'">待审核</span>
              <span :class="'status-' + 2" v-if="item.status === '2'">审核拒绝</span>
            </div>
          </div>
          <div class="item-bottom">
            <div hover-class="bg-hover" @click="$linkTo('./AddDiscounts?id=' + item.id)" v-if="item.status === '0'">修改</div>
            <div hover-class="bg-hover" @click="remove(item.id)">删除</div>
          </div>
        </div>
        <AiEmpty v-if="!list.length && isMore"></AiEmpty>
      </div>
    </div>
    <div class="btn-wrapper">
      <div class="btn" hover-class="text-hover" @click="$linkTo('./AddDiscounts')">发布优惠信息</div>
    </div>
  </div>
</template>

<script>
  import { mapState } from 'vuex'
  export default {
    appName: '商户中心',
    name: 'Merchants',

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

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

    onLoad () {
      this.getList()

      uni.$on('updateList', () => {
        this.current = 1
        this.isMore = false

        this.getList()
      })
    },

    methods: {
      getList () {
        if (this.isMore) return

        this.$instance.post(`/api/appmerchantinfo/discountList`, null, {
          params: {
            current: this.current,
            size: 10,
            openId: this.user.openId
          }
        }).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
            }

            this.pageShow = true

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

              return false
            }

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

      remove (id) {
        this.$dialog.confirm({
          title: '温馨提示',
          content: '确定删除该优惠信息吗？'
        }).then(() => {
          this.$loading()
          this.$instance.post(`/api/appmerchantinfo/deleteDiscount?id=${id}`).then(res => {
            if (res.code === 0) {
              this.current = 1
              this.isMore = false

              this.getList()
              this.$toast('删除成功')
            }
          })
        }).catch(() => {

        })
      }
    },

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

<style lang="scss" scoped>
  .merchants {
    margin: 0 32px;
    padding-top: 32px;
    padding-bottom: 140px;

    .merchants-wrapper {
      margin-top: 58px;

      & > h2 {
        margin-bottom: 46px;
        font-weight: 600;
        font-size: 34px;
        color: #1D2229;
      }

      .item {
        margin-bottom: 24px;
        background: #FCFCFC;
        box-shadow: 0 0 8px 0 rgba(0,0,0,0.02);
        border-radius: 16px;

        .item-top {
          padding: 24px;

          h2 {
            margin-bottom: 12px;
            font-size: 34px;
            color: #333333;
          }

          p {
            line-height: 1.3;
            font-size: 30px;
            color: #999999;
            display: -webkit-box;
            -webkit-line-clamp: 3;
            -webkit-box-orient: vertical;
            overflow: hidden;
          }

          .item-tags {
            display: flex;
            align-items: center;
            margin-top: 24px;

            span {
              height: 48px;
              line-height: 48px;
              padding: 0 16px;
              color: #3BBC37;
              font-size: 26px;
              background: #F5FCF5;
              border: 1px solid #E2F6E1;
              border-radius: 8px;

              &.status-2 {
                color: #E23C3C;
                background: #E23C3C;
                border: 1px solid #FAE2E2;
              }

              &.status-0 {
                color: #FF883C;
                background: #FFF9F5;
                border: 1px solid #FFEDE2;
              }
            }
          }
        }

        .item-bottom {
          display: flex;
          align-items: center;
          height: 88px;
          border-top: 1px solid #EEEEEE;

          div {
            flex: 1;
            height: 100%;
            line-height: 88px;
            text-align: center;
            font-size: 28px;
            color: #687DA6;
          }
        }
      }
    }

    .top {
      display: flex;
      align-items: center;
      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;
      }

      .right {
        flex: 1;
      }

      h2 {
        margin-bottom: 14px;
        font-weight: 600;
        font-size: 34px;
        color: #333333;
      }

      p {
        line-height: 1.3;
        font-size: 26px;
        color: #999999;
      }
    }
  }
</style>
