<template>
  <ai-list class="notice">
    <template slot="title">
      <ai-title title="话题设置" isShowBottomBorder>
      </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>
        </template>
        <template #right>
          <el-input
            v-model="search.title"
            class="search-input"
            size="small"
            v-throttle="() => {search.current = 1, getList()}"
            placeholder="请输入话题名称/创建人"
            clearable
            @clear="search.current = 1, search.title = '', 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="status" width="120px" label="是否启用" align="center">
          <template slot-scope="{ row }">
            <el-switch v-model="row.status" @change="changeStatus(row)" active-value="1" inactive-value="0"></el-switch>
          </template>
        </el-table-column>
        <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="toAdd(row.id)">编辑</el-button>
              <el-button type="text" @click="remove(row.id)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </ai-table>
    </template>
  </ai-list>
</template>

<script>
  export default {
    name: 'List',

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

    data () {
      return {
        search: {
          current: 1,
          size: 10,
          title: '',
          status: ''
        },
        total: 10,
        colConfigs: [
          { prop: 'title',  label: '话题名称', align: 'left', width: '200' },
          { prop: 'description',  label: '话题描述', align: 'left', 'show-overflow-tooltip': true},
          { prop: 'partakeCount',  label: '参与话题数', align: 'center', width: '120' },
          { prop: 'showIndex',  label: '排序', align: 'center', width: '120' },
          { prop: 'createUserName',  label: '创建人', align: 'center', width: '120' },
          { prop: 'createTime',  label: '创建时间', align: 'center', width: '180' },
          { slot: 'status'},
          { slot: 'options'},
        ],
        tableData: [],
      }
    },

    created() {
      this.getList()
    },

    methods: {
      getList() {
        this.instance.post(`/app/appneighborhoodassistancetheme/list`, null, {
          params: {
            ...this.search
          }
        }).then(res => {
          if (res.code == 0) {
            this.tableData = res.data.records
            this.total = res.data.total
          }
        })
      },

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

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

      changeStatus(row) {
        this.$confirm(`确定${row.status == 1 ? '启用' : '关闭'}该话题？`).then(() => {
          this.instance.post(`/app/appneighborhoodassistancetheme/enable?id=${row.id}`).then(res => {
            if (res.code == 0) {
              this.$message.success('操作成功！')
              this.getList()
            }
          })
        }).catch(() => {
          this.getList()
        })
        // this.instance.post(`/app/appneighborhoodassistancetheme/enable?id=${row.id}`).then(res => {
        //   if (res.code == 0) {
        //     this.$message.success('操作成功！')
        //     this.getList()
        //   }
        // })
      }
    }
  }
</script>

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