<template>
  <section class="mmList">
    <ai-list>
      <ai-title slot="title" title="群众留言" isShowBottomBorder/>
      <template #content>
        <ai-search-bar>
          <template #left>
            <ai-select v-model="search.type" placeholder="留言状态"/>
            <ai-select v-model="search.type" placeholder="留言类型"/>
          </template>
          <template #right>
            <el-input size="small" placeholder="搜索企业名称、法人姓名、登录账号" v-model="search.enterpriseName" 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.isPublic==0" type="text" @click="handleEnable(row)">公示</el-button>
              <el-button v-else-if="row.isPublic==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: "isPublic",dict:"yesOrNo"},
        {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 status = (Number(row.status) + 1) % 2
      this.$confirm(`是否要${this.dict.getLabel('portalUserStatus', status)}留言?`).then(() => {
        this.instance.post("/appportaluser/addOrUpdate", {...row, status}).then(res => {
          if (res?.code == 0) {
            this.$message.success(this.dict.getLabel('portalUserStatus', status) + "成功!")
            this.getTableData()
          }
        })
      }).catch(() => 0)
    },
  },
  created() {
    this.getTableData()
    this.search.areaId = this.user.info.areaId
  }
}
</script>

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