<template>
  <div class="AppVillageDiscuss">
    <AiTopFixed>
      <u-tabs :list="tabslist" :is-scroll="false" :current="currentTabs" active-color="#fff" inactive-color="#CDDCF0" bg-color="#3975C6" @change="change"></u-tabs>
    </AiTopFixed>

    <div class="areatop">
      <u-form label-width="auto">
        <u-form-item label="区域选择" right-icon="arrow-right" :border-bottom="false" class="addresss">
          <AiAreaPicker v-model="areaId" :areaId="user.areaId" @select="seachObj" style="color: #333" :name.sync="areaName"></AiAreaPicker>
        </u-form-item>
      </u-form>
    </div>

    <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 flex class="w-100">
            <div class="avatar" v-text="item.avatar" />
            <div flex class="column start fill">
              <b class="color-333" v-text="item.createUserName" />
              <span class="color-999" v-text="item.createTime" />
            </div>
            <div class="statusTag" :class="{ over: item.status > 0 }" v-text="item.status == 0 ? (item.type == 0 ? '征集中' : '投票中') : $dict.getLabel('discussStatus', item.status)" />
          </div>
          <p class="item-content">{{ item.content }}</p>
          <div v-if="item.images && item.images.length">
            <div class="img-list" v-if="item.contentType != 1">
              <img :src="items.url" alt="" v-for="(items, index) in item.images" :key="index" v-if="index < 3" @click.stop="previewImage(item.images, items.url)" />
            </div>
            <div class="img-list" v-else>
              <video class="video" :src="item.images[0].url" />
            </div>
          </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()" />
      <div class="addBtn iconfont iconfont-iconDouble_Up" @tap.stop="backTop" v-if="showBackTop" />
    </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: 'AppVillageDiscuss',
  appName: '居民议事',
  components: {},
  props: {},
  data() {
    return {
      datas: [],
      current: 1,
      size: 10,
      pages: 0,
      deletShow: false,
      deletId: '',
      listName: '',
      tabIndex: 0,
      showBackTop: false,

      tabslist: [
        {
          name: '进行中',
        },
        {
          name: '公示中',
        },
        {
          name: '已完成',
        },
      ],
      currentTabs: 0,
      areaName: '',
      areaId: '',
    }
  },
  computed: {
    ...mapState(['user']),
    loadmore() {
      return this.pages <= this.current ? 'loading ' : 'nomore'
    },
    tabs() {
      return this.$dict.getDict('discussStatus')
    },
  },
  onLoad(o) {
    console.log(this.user)
    this.moduleId = o.moduleId
    this.listName = o.listName

    this.$areaId = this.user.areaName
    this.areaName = this.user.areaName
    this.areaId = this.user.areaId

    this.getList()
    uni.$on('update', () => {
      this.current = 1
      this.getList()
    })
    this.$dict.load('discussStatus')
  },
  onShow() {
    document.title = '居民议事'
  },
  onPageScroll(obj) {
    this.showBackTop = obj.scrollTop > 0
  },
  methods: {
    getList() {
      this.$http
        .post('/app/appvillagediscuss/listUp', null, {
          params: { size: 20, current: this.current, status: this.currentTabs, areaId: this.areaId },
        })
        .then((res) => {
          if (res?.data) {
            res.data.records.forEach((e) => {
              // e.avatar = e.createUserName?.substring(0, 2) || '游客'
              if (e.createUserName) {
                e.avatar = e.createUserName.substring(e.createUserName.length, e.createUserName.length - 2)
              } else {
                e.avatar = '游客'
              }

              if (e.images) {
                e.images = JSON.parse(e.images)
              }
            })
            this.datas = this.current > 1 ? [...this.datas, ...res.data.records] : res.data.records
            this.pages = res.data.pages
          }
        })
    },

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

    seachObj(e) {
      this.areaId = e
      this.current = 1
      this.getList()
    },

    previewImage(images, img) {
      uni.previewImage({
        urls: images.map((v) => v.url),
        current: img,
      })
    },
    // tabClick(type) {
    //   this.type = type
    //   this.current = 1
    //   this.getList()
    // },
    search() {
      this.current = 1
      this.getList()
    },

    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/appcontentinfo/delete?ids=${this.deletId}`).then((res) => {
        if (res.code == 0) {
          this.current = 1
          this.$u.toast('删除成功！')
          this.getList()
        }
      })
    },
    backTop() {
      uni.pageScrollTo({
        scrollTop: 0,
      })
    },
  },
  onReachBottom() {
    this.current++
    this.getList()
  },
}
</script>

<style scoped lang="scss">
.AppVillageDiscuss {
  padding-bottom: 10px;
  background: #f2f2f2;

  ::v-deep .AiCard {
    border-radius: 0 !important;
    box-shadow: none;

    .start {
      border-radius: 0 !important;
    }
  }

  .areatop {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;

    .u-form {
      width: 100%;

      .addresss {
        ::v-deep .u-form-item__body {
          .u-form-item--right {
            .u-form-item--right__content {
              .u-form-item--right__content__slot {
                .AiAreaPicker {
                  width: 100%;
                  display: flex;
                  justify-content: flex-end;
                  .areaSelector {
                    .location {
                      opacity: 0;
                    }
                  }
                }
              }
              .u-form-item--right__content__icon {
                .u-icon {
                  margin-bottom: 8px !important;
                }
              }
            }
          }
        }
      }
    }
  }

  .avatar {
    height: 64px;
    width: 64px;
    color: #fff;
    background: $uni-color-primary;
    border-radius: 50%;
    font-size: 24px;
    text-align: center;
    line-height: 64px;
    margin-right: 16px;
  }

  .color-999 {
    color: #999999;
  }

  .color-333 {
    color: #333;
  }

  .w-100 {
    width: 100%;
  }

  .statusTag {
    padding: 0 12px;
    line-height: 30px;
    border: 1px solid #2573ff;
    color: #2573ff;
    font-size: 22px;

    &.over {
      border-color: #666;
      color: #666;
    }
  }

  ::v-deep.AiTopFixed {
    .content {
      padding: 0;
    }
  }

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

  ::v-deep .AiCard {
    background: #f3f6f9;
    position: relative;
    padding: 0;
    margin-bottom: 16px;

    & > .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: 4;
        -webkit-box-orient: vertical;
        font-size: 26px;
        font-family: PingFangSC-Regular, PingFang SC;
        color: #333;
        line-height: 44px;
        margin-top: 32px;
        margin-bottom: 16px;
      }

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

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

        & + .addBtn {
          margin-top: 22px;
        }
      }
    }
  }

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