<template>
  <section class="mmList">
    <ai-list>
      <ai-title slot="title" title="群众留言" isShowBottomBorder/>
      <template #content>
        <ai-search-bar>
          <template #left>
            <ai-select v-model="search.status" placeholder="留言状态" :selectList="dict.getDict('msgStatus')"
                       @change="page.current=1,getTableData()"/>
            <ai-select v-model="search.type" placeholder="留言类型" :selectList="dict.getDict('leaveMessageType')"
                       @change="page.current=1,getTableData()"/>
          </template>
          <template #right>
            <el-input size="small" placeholder="搜索标题/内容" v-model="search.title" clearable
                      @change="page.current=1,getTableData()"/>
          </template>
        </ai-search-bar>
        <ai-table :tableData="tableData" :total="page.total" :current.sync="page.current" :size.sync="page.size"
                  @getList="getTableData" :col-configs="colConfigs" :dict="dict">
          <el-table-column slot="options" label="操作" fixed="right" align="center">
            <template slot-scope="{row}">
              <el-button v-if="row.isOpen==0" type="text" @click="handleEnable(row)">公示</el-button>
              <el-button v-else-if="row.isOpen==1" type="text" @click="handleEnable(row)">取消公示</el-button>
              <el-button type="text" @click="showDetail(row.id)">详情</el-button>
            </template>
          </el-table-column>
        </ai-table>
      </template>
    </ai-list>
  </section>
</template>

<script>
import {mapState} from "vuex";

export default {
  name: "mmList",
  props: {
    instance: Function,
    dict: Object,
    permissions: Function
  },
  computed: {
    ...mapState(['user'])
  },
  data() {
    return {
      search: {name: ""},
      page: {current: 1, size: 10, total: 0},
      tableData: [],
      colConfigs: [
        {label: "标题", prop: "title"},
        {label: "类型", prop: "type", dict: "leaveMessageType"},
        {label: "内容", prop: "content"},
        {label: "留言人", prop: "leaveName"},
        {label: "留言提交时间", prop: "createTime"},
        {label: "最后回复时间", prop: "lastReplyTime"},
        {label: "状态", prop: "status", dict: "msgStatus"},
        {label: "是否公示", prop: "isOpen", dict: "yesOrNo", align: 'center'},
        {slot: "options"}
      ]
    }
  },
  methods: {
    getTableData() {
      this.instance.post("/appleavemessage/list", null, {
        params: {...this.page, ...this.search}
      }).then(res => {
        if (res?.data) {
          this.tableData = res.data?.records
          this.page.total = res.data.total
        }
      })
    },
    showDetail(id) {
      this.$router.push({query: {id}})
    },
    handleEnable(row) {
      let openLabel = row.isOpen == 1 ? "取消公示" : "公示", isOpen = (Number(row.isOpen) + 1) % 2
      this.$confirm(`是否要${openLabel}留言?`).then(() => {
        this.instance.post("/appleavemessage/setIsOpen", null, {
          params: {id: row.id, isOpen}
        }).then(res => {
          if (res?.code == 0) {
            this.$message.success(openLabel + "成功!")
            this.getTableData()
          }
        })
      }).catch(() => 0)
    },
  },
  created() {
    this.getTableData()
    this.search.areaId = this.user.info.areaId
  }
}
</script>

<style lang="scss" scoped>
.mmList {
  height: 100%;
}
</style>
