<template>
  <ai-list class="notice">
    <template slot="title">
      <ai-title title="邻里互助" isShowBottomBorder isShowArea :hideLevel="hideLevel-1" v-model="search.areaId" @change="changeArea">
        <template #rightBtn>
          <el-button size="small" type="primary" @click="toGagList">禁言名单</el-button>
        </template>
      </ai-title>
    </template>
    <template slot="content">
      <ai-search-bar class="search-bar">
        <template #left>
          <el-button size="small" type="primary" icon="iconfont iconAdd" @click="toAdd('')">我要发帖</el-button>
          <el-date-picker
            v-model="dateList"
            @change="search.current = 1,getList()"
            type="daterange"
            size="small"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </template>
        <template #right>
          <el-input
            v-model="search.createUserName"
            class="search-input"
            size="small"
            v-throttle="() => {search.current = 1, getList()}"
            placeholder="请输入发帖人"
            clearable
            @clear="search.current = 1, search.createUserName = '', getList()"
            suffix-icon="iconfont iconSearch">
          </el-input>
        </template>
      </ai-search-bar>
      <ai-table
        :tableData="tableData"
        :col-configs="colConfigs"
        :total="total"
        style="margin-top: 6px;"
        :current.sync="search.current"
        :size.sync="search.size"
        @getList="getList">
        <el-table-column slot="options" width="180px" fixed="right" label="操作" align="center">
          <template slot-scope="{ row }">
            <div class="table-options">
              <el-button type="text" @click="toDetail(row.id)">详情</el-button>
              <el-button type="text" @click="remove(row.id)">删除</el-button>
              <el-button type="text" @click="gag(row.createUserId, row.blacklist)">{{ row.blacklist ? '解除禁言' : '禁言' }}</el-button>
            </div>
          </template>
        </el-table-column>
      </ai-table>
    </template>
  </ai-list>
</template>

<script>
  import { mapState } from 'vuex'
  export default {
    name: 'List',

    props: {
      instance: Function,
      dict: Object
    },

    data () {
      return {
        search: {
          current: 1,
          size: 10,
          createUserName: '',
          areaId: ''
        },
        total: 0,
        colConfigs: [
          { prop: 'content',  label: '内容', align: 'left' },
          { prop: 'createUserName',  label: '发帖人', align: 'center', width: '120' },
          { prop: 'createUserAreaName',  label: '所属地区', align: 'center' },
          { prop: 'createTime',  label: '创建时间', align: 'center' },
          { prop: 'commentCount',  label: '评论数', align: 'center', width: '120' },
          { prop: 'appreciateCount',  label: '点赞数', align: 'center', width: '120' },
          { prop: 'sharedCount',  label: '分享数', align: 'center', width: '120' },
          { prop: 'blacklist',  label: '状态', align: 'center', format: v => v ? '禁言' : '正常' },
          { slot: 'options'},
        ],
        tableData: [],
        dateList: [],
      }
    },
    computed: {
      ...mapState(['user']),
      hideLevel() {
        return this.user.info.areaList?.length || 0
      },
    },

    created() {
      this.search.areaId = this.user.info.areaId
      this.getList()
    },

    methods: {
      getList() {
        this.instance.post(`/app/appneighborhoodassistance/list`, null, {
          params: {
            ...this.search,
            createUserAreaId: this.search.areaId,
            beginDate: this.dateList[0] || '',
            endDate: this.dateList[1] || '',
          }
        }).then(res => {
          if (res.code == 0) {
            this.tableData = res.data.records
            this.total = res.data.total
          }
        })
      },

      changeArea () {
        this.search.current = 1

        this.$nextTick(() => {
          this.getList()
        })
      },

      toGagList () {
        this.$emit('change', {
          type: 'GagList'
        })
      },

      gag (id, status) {
        this.$confirm(`确定${status ? '解除禁言' : '禁言'}该用户？`).then(() => {
          this.instance.post(`/app/appneighborhoodassistance/addBlacklist?userId=${id}`).then(res => {
            if (res.code == 0) {
              this.$message.success(`${status ? '解除禁言' : '禁言'}成功！`)
              this.getList()
            }
          })
        })
      },

      remove (id) {
        this.$confirm('确定删除该帖子？').then(() => {
          this.instance.post(`/app/appneighborhoodassistance/delete?id=${id}`).then(res => {
            if (res.code == 0) {
              this.$message.success('删除成功！')
              this.getList()
            }
          })
        })
      },

      toDetail (id) {
        this.$emit('change', {
          type: 'Detail',
          params: {
            id: id || ''
          }
        })
      },

      toAdd(id) {
        this.$emit('change', {
          type: 'Add',
          params: {
            id: id || ''
          }
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
</style>
