<template>
  <div class="AppUniMsg">
    <u-tabs :list="tabList" :is-scroll="false" :current="currentTabs" height="96" bg-color="#3975C6" inactive-color="#fff" active-color="#fff" @change="change"></u-tabs>

    <template v-if="datas.length > 0">
      <AiCard v-for="(item, i) in datas" :ref="item.id" :key="i" @click.native="toAdd(item, 1)">
        <template #custom>
          <div class="titles">
            {{ item.title }}
          </div>

          <div class="publishUnitNames">
            <span>发布部门：</span>
            <span>{{ item.publishUnitName }}</span>
          </div>

          <div class="createTimes">
            <span>发布时间：</span>
            <span>{{ item.createTime }}</span>
          </div>

          <div class="imgs">
            <img :src="items.url" alt="" v-for="(items, i) in JSON.parse(item.images || '[]')" :key="i" v-if="i < 3" @click.stop="previewImage(JSON.parse(item.images || '[]'), items.url)" />
          </div>
        </template>

        <template #menu v-if="item.createUserId == user.id">
          <div class="menu" @tap.stop="toAdd(item, 2)">编辑</div>
          <div class="menu" @tap.stop="toDetele(item)">删除</div>
        </template>
      </AiCard>

      <u-loadmore :status="loadmore" color="#999" font-size="24" margin-top="32" margin-bottom="80" />
    </template>

    <AiEmpty description="暂无数据" v-else></AiEmpty>

    <AiFixedBtn>
      <div class="addBtn iconfont iconfont-iconfangda" @tap.stop="toAdd()" />
    </AiFixedBtn>

    <u-modal v-model="deletShow" content="您确认要删除该条信息吗？" :show-cancel-button="true" :mask-close-able="true" :show-title="false" @confirm="delet"></u-modal>
  </div>
</template>

<script>
import { mapState } from 'vuex'

export default {
  name: 'AppUniMsg',
  appName: '小程序公告',
  components: {},
  props: {},
  data() {
    return {
      datas: [],
      current: 1,
      size: 10,
      deletShow: false,
      deletId: '',
      tabList: [
        {
          name: '全部公告',
        },
        {
          name: '我的发布',
        },
      ],
      currentTabs: 0,
    }
  },
  computed: {
    ...mapState(['user']),

    loadmore() {
      return this.pages <= this.current ? 'loading ' : 'nomore'
    },
  },
  watch: {},
  onLoad() {
    this.getList()
    uni.$on('updateList', () => {
      this.current = 1
      this.getList()
    })
  },
  onShow() {
    document.title = '小程序公告'
  },
  methods: {
    getList() {
      this.$http
        .post('/app/appmininotice/list', null, {
          params: {
            size: this.size,
            current: this.current,
            createUserId: this.currentTabs == 1 ? this.user.id : '',
          },
        })
        .then((res) => {
          if (res.code == 0) {
            this.datas = this.current > 1 ? [...this.datas, ...res.data.records] : res.data.records
            this.pages = res.data.pages
          }
        })
    },

    toAdd(item, type) {
      if (item?.id) {
        this.$refs?.[item.id]?.[0]?.handleClose()
      }
      if (type == '1') {
        uni.navigateTo({ url: `./Detail?id=${item.id}` })
      }
      if (type == '2') {
        uni.navigateTo({ url: `./Add?id=${item.id}` })
      }
      if (type == null) {
        uni.navigateTo({ url: `./Add` })
      }
    },

    toDetele(item) {
      this.deletShow = true
      this.deletId = item.id
      this.$refs?.[item.id]?.[0]?.handleClose()
    },

    delet() {
      this.$http.post(`/app/appmininotice/delete?ids=${this.deletId}`).then((res) => {
        if (res.code == 0) {
          this.$u.toast('删除成功！')
          this.getList()
        }
      })
    },

    change(index) {
      this.datas = []
      this.current = 1
      this.currentTabs = index
      this.getList()
    },

    previewImage(images, img) {
      uni.previewImage({
        urls: images.map((v) => v.url),
        current: img,
      })
    },
  },
  onReachBottom() {
    this.current = this.current + 1
    this.getList()
  },
}
</script>

<style scoped lang="scss">
uni-page-body {
  height: 100%;
}

.AppUniMsg {
  height: 100%;

  ::v-deep .AiCard {
    background: #f5f5f5;

    .start {
      background: #fff;
      padding: 32px;
      border-radius: 16px;

      .fill {
        .titles {
          font-size: 30px;
          color: #333;
          font-family: 500;
        }

        .imgs {
          margin-top: 10px;

          img {
            width: 200px;
            height: 200px;
            margin-right: 8px;
          }

          img:nth-child(3n) {
            margin-right: 0;
          }
        }
      }
    }

    .mask {
      .moreMenu {
        transform: translate(-175px, 20px);

        .menu {
          text-align: center;
          line-height: 80px;
          width: 192px;
          height: 80px;
          font-size: 28px;
          font-weight: 400;
          color: #333333;
        }
      }
    }
  }

  .u-load-more-wrap {
    background: #f5f5f5 !important;
    margin: 0 !important;
    padding: 34px 0;
  }

  .AiFixedBtn {
    .movableArea {
      .addBtn {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 96px;
        height: 96px;
        flex-shrink: 0;
        background: $uni-color-primary;
        box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2);
        font-size: 48px;
        color: #fff;
        border-radius: 50%;
      }
    }
  }
}
</style>
