<template>
  <div class="AppServicePublic">
    <AiTopFixed v-if="tabs.length">
      <div class="tab-select">
        <div class="item" :class="tabIndex == index ? 'active' : ''" v-for="(item, index) in tabs" :key="index"
             @click="tabClick(index, item)">{{ item.categoryName }}<span></span></div>
      </div>
    </AiTopFixed>
    <div class="header-top">
      <div>区域选择</div>
      <AiAreaPicker v-model="areaId" :areaId="user.areaId" @select="areaSelect" :name.sync="areaName">
        <span class="label" v-if="areaName">{{ areaName }}</span>
        <span v-else>请选择</span>
        <u-icon name="arrow-right" color="#666" size="24" style="margin-left:4px;" />
      </AiAreaPicker>
    </div>
    <u-search class="serach_content" placeholder="请输入标题" :show-action="false" v-model="keyword" @clear="clearSearch"
              @search="search"></u-search>
    <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>
          <p class="item-content">
            <u-parse :html="item.content"></u-parse>
          </p>
          <div class="img-list" v-if="item.files.length && item.contentType != 1">
            <img :src="items.accessUrl" alt="" v-for="(items, index) in item.files" :key="index" v-if="index < 3"
                 @click.stop="previewImage(item.files, items.accessUrl)"/>
          </div>
          <div class="img-list" v-if="item.files.length && item.contentType == 1">
            <video class="video" :src="item.files[0].url"></video>
          </div>
          <div class="flex">
            <span class="left">
              <span class="type" v-if="item.categoryName">{{ item.categoryName }}</span>
              <span class="times">{{ item.createTime }}</span>
            </span>

            <span class="right">
              <img src="./img/view-icon.png" alt="">
              <span>{{ item.viewCount }}</span>
            </span>
          </div>
        </template>

        <template #menu>
          <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: 'AppServicePublic',
  appName: '内容发布',
  components: {},
  props: {},
  data() {
    return {
      datas: [],
      keyword: '',
      areaId: '',
      areaName: '',
      current: 1,
      size: 10,
      pages: 0,
      deletShow: false,
      deletId: '',
      moduleId: '',
      listName: '',
      tabs: [],
      tabIndex: 0,
      categoryId: ''
    }
  },
  computed: {
    ...mapState(['user']),

    loadmore() {
      return this.pages <= this.current ? 'loading ' : 'nomore'
    },
  },
  watch: {},
  onLoad(o) {
    this.areaId = this.user.areaId
    this.areaName = this.user.areaName
    this.moduleId = o.moduleId
    this.listName = o.listName
    this.getType()
    this.getList()
    uni.$on('update', () => {
      this.getList()
    })

  },
  onShow() {
    document.title = this.listName
  },
  methods: {
    previewImage(images, img) {
      uni.previewImage({
        urls: images.map(v => v.url),
        current: img
      })
    },
    getType() {
      this.$http.post(`/app/appcontentmoduleinfo/listByName?names=${this.listName}`).then((res) => {
        if (res.code == 0) {
          if (res.data && res.data[0].categoryList.length) {
            var all = [{
              categoryName: '全部',
              id: '',
              showIndex: 0,
            }]
            this.tabs = [...all, ...res.data[0].categoryList]
          }
        }
      })
    },
    tabClick(index, row) {
      this.tabIndex = index
      this.categoryId = row.id
      this.current = 1
      this.getList()
    },
    search() {
      this.current = 1
      this.getList()
    },
    getList() {
      this.$http.post('/app/appcontentinfo/list', null, {
        params: {
          size: 20,
          current: this.current,
          areaId: this.areaId,
          title: this.keyword,
          moduleId: this.moduleId,
          categoryId: this.categoryId
        },
      })
      .then((res) => {
        if (res.code == 0) {
          this.datas = this.current > 1 ? [...this.datas, ...res.data.records] : res.data.records
          this.pages = res.data.pages
        }
      })
    },

    areaSelect(e) {
      this.areaId = e
      this.getList()
    },

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

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

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

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

::v-deep .content {
  padding: 0 !important;
}

.tab-select {
  width: 100%;
  height: 96px;
  line-height: 96px;
  background: #3975C6;
  display: flex;

  .item {
    flex: 1;
    text-align: center;
    font-size: 28px;
    font-family: PingFangSC-Regular, PingFang SC;
    color: #CDDCF0;
  }

  .active {
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    position: relative;
    color: #fff;

    span {
      width: 48px;
      height: 4px;
      background: #FFF;
      position: absolute;
      bottom: 14px;
      left: 50%;
      margin-left: -24px;
    }
  }
}

.iconfont-iconMore {
  // margin-top: 8px;
}

.AppServicePublic {
  height: 100%;

  .header-top {
    display: flex;
    background: #fff;
    justify-content: space-between;
    align-items: center;
    padding: 10px 30px;
    border-bottom: 1px solid #d8dde6;
  }

  .u-search {
    background: #fff;
    padding: 20px 30px;
  }

  ::v-deep .AiCard {
    background: #f3f6f9;
    position: relative;

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

      .titles {
        width: 600px;
        font-size: 32px;
        font-weight: 500;
        color: #333333;
        margin-bottom: 16px;
        line-height: 50px;
        word-break: break-all;
      }

      .item-content {
        width: 100%;
        word-break: break-all;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 3;
        -webkit-box-orient: vertical;
        font-size: 32px;
        font-family: PingFangSC-Regular, PingFang SC;
        color: #999;
        line-height: 44px;
        margin-bottom: 24px;
      }

      .img-list {
        margin-bottom: 24px;

        img {
          width: calc(33% - 16px);
          height: 204px;
          margin: 0 16px 8px 0;
        }
      }

      .flex {
        display: flex;
        justify-content: space-between;

        .left {
          .garydiv {
            font-size: 28px;
            color: #999999;
            background: #eeeeee;
            border-radius: 24px;
            padding: 4px 16px;
          }

          .times {
            font-size: 28px;
            color: #999999;
          }

          .type {
            display: inline-block;
            padding: 0 16px;
            line-height: 48px;
            background: #EEE;
            border-radius: 24px;
            font-size: 24px;
            font-family: PingFangSC-Regular, PingFang SC;
            font-weight: 400;
            color: #999;
            margin-right: 16px;
          }
        }

        .right {
          font-size: 28px;
          color: #999;

          img {
            width: 32px;
            height: 32px;
            vertical-align: middle;
            margin-right: 8px;
          }

          .font {
            color: #4181ff;
          }
        }
      }

      .iconfont-iconMore {
        position: absolute;
        top: 42px;
        right: 32px;
      }
    }

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

  .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%;
      }
    }
  }

  ::v-deep uni-video {
    width: 100%;
  }
}
</style>
