<template>
  <div class="main-container" v-show="loaded">
    <template v-if="list.length">
      <alarm-item
        v-for="item in list"
        :data="item"
        :key="item.id"
        @click-edit="onUpdate"
        @update="refersh"
      ></alarm-item>
    </template>
    <van-empty description="暂无内容" v-else />
    <div class="add-button">
      <div class="inner" @click="popupVisible = true"></div>
    </div>
    <!-- 添加/修改 -->
    <van-popup v-model="popupVisible" position="bottom" @closed="onClosed">
      <alarm-update
        :isUpdate="isUpdate"
        :data="activeItem"
        @success="onSuccess"
        v-if="popupVisible"
      ></alarm-update>
    </van-popup>
    <van-popup v-model="followPopupVisible" style="border-radius:6px;">
      <div class="qrcode">
        <p>开启学习提醒功能，需先关注紫荆职教公众号。</p>
        <img src="../../../assets/images/wechat_mp_qrcode.png" />
        <h5>保存二维码图片，扫码关注</h5>
      </div>
    </van-popup>
  </div>
</template>

<script>
import * as api from './api'
import AlarmItem from './components/alarmItem.vue'
import AlarmUpdate from './components/update.vue'
export default {
  name: 'AlarmIndex',
  components: { AlarmItem, AlarmUpdate },
  metaInfo: {
    title: '学习提醒'
  },
  data() {
    return {
      loaded: false,
      list: [],
      popupVisible: false,
      isUpdate: false,
      activeItem: null,
      followPopupVisible: false
    }
  },
  methods: {
    // 检查是否关注公众号
    checkFollowWechat() {
      api.checkFollowWechat().then(response => {
        this.followPopupVisible = !response.flag
      })
    },
    // 获取提醒列表
    getAlarmList() {
      api
        .getAlarmList()
        .then(response => {
          this.list = response
        })
        .finally(() => {
          this.loaded = true
        })
    },
    refersh() {
      this.getAlarmList()
    },
    onUpdate(data) {
      this.isUpdate = true
      this.activeItem = data
      this.popupVisible = true
    },
    onSuccess() {
      this.popupVisible = false
      this.refersh()
    },
    onClosed() {
      this.isUpdate = false
      this.activeItem = null
    }
  },
  beforeMount() {
    this.checkFollowWechat()
    this.getAlarmList()
  }
}
</script>

<style lang="scss" scoped>
.main-container {
  padding: 20px 0;
}
.add-button {
  height: 64px;
  margin-bottom: 50px;
  .inner {
    position: fixed;
    left: 50%;
    bottom: 50px;
    transform: translateX(-50%);
    width: 64px;
    height: 64px;
    background: url('../../../assets/images/icon_remind_add.png') no-repeat;
    background-size: 100%;
  }
}
.qrcode {
  width: 5.9rem;
  padding: 0.4rem;
  box-sizing: border-box;
  img {
    margin: 10px 0;
    width: 100%;
  }
  p {
    font-size: 15px;
    color: #222;
  }
  h5 {
    font-size: 15px;
    color: #222;
    font-weight: bold;
    text-align: center;
  }
}
</style>
