<template>
  <div class="app-container">
    <search-form
      :list-loading="listLoading"
      @change="searchHandle"
    />
    <table-list
      :list-loading="listLoading"
      :data="tableData2"
      :columns="listConfig"
      :current-page="currentPage"
      :page-size="pageSize"
      :total-records="totalRecords"
      @currentChange="pageHandle"
      :isAdd="true"
      @add="openQPopup"
      :operationsDefaultLength="6"
    />
    <popup-edit
      :isShow="isDtlShow"
      :curObj="curObj"
      @close="closePopup"
    />
    <q-edit
      :isShow="isQShow"
      :curObj="curObj"
      @close="closeQPopup"
    />
    <a-edit
      :isShow="isAShow"
      :curObj="curObj"
      @close="closeAPopup"
    />
  </div>
</template>
<script>
import { arrToObj } from '@/utils'
import SearchForm from './components/searchForm/Index'
import PopupEdit from './components/popup/answer'
import QEdit from './components/popup/QEdit'
import AEdit from './components/popup/AEdit'
import baseTable from '_m/baseTable.js'
export default {
  name: 'index',
  components: {
    SearchForm,
    PopupEdit,
    QEdit,
    AEdit,
  },
  provide() {
    return {
      parentData: this
    }
  },
  mixins: [baseTable],
  data() {
    return {
      apiStr: 'user.admquestionlist',
      searchForm: null,
      isDtlShow: false,
      curObj: {},
      isQShow: false,
      isAShow: false,
    }
  },
  computed: {
    tableData2() {
      const arr = [...this.tableData]
      arr.map(item => {
        const question_tag = item.question_tag ? item.question_tag.split(',') : []
        const qtName = question_tag.map(v => {
          return arrToObj(this.$dictData.question_tag)[v]
        })
        item.qtName = qtName.join(',')
      })
      return arr
    }
  },
  created() {},
  mounted() {
    this.listConfig = {
      rows: [
        { label: '问题内容', prop: 'question_cont', fullShow: true, minWidth: 200, align: 'left' },
        { label: '标签', prop: 'qtName' },
        // { label: '昵称', prop: 'nickname' },
        // { label: '头像', prop: 'avatar', type: 'img' },
        { label: '提问时间', prop: 'create_at' },
        { label: '操作', width: 240, type: 'handle2', operations:
          [
            { label: '我来回答', func: this.openAPopup, btnType: 'primary' },
            { label: '查看回答', func: this.openPopup, btnType: 'success' },
            { label: '删除', func: this.delHandle, btnType: 'danger' },
          ]
        }
      ]
    }
  },
  methods: {
    openQPopup () {
      this.isQShow = true
    },
    closeQPopup (obj) {
      this.isQShow = false
      if (obj) {
        this.fetchData()
      }
    },
    openAPopup (row) {
      if (row && row.id) {
        this.curObj = row
      } else {
        this.curObj = {}
      }
      this.isAShow = true
    },
    closeAPopup (obj) {
      this.isAShow = false
      if (obj) {
        this.fetchData()
      }
    },
    delHandle(row) {
      this.$msg(`您确定要删除该问题吗？`, 'confirm', () => {
        this.$api.user.admquestiondel({
          question_id: row.id
        }).then(data => {
          this.$msgs(`已删除！`)
          this.fetchData()
        })
      }, null, true)
    },
    openPopup(row) {
      if (row && row.id) {
        this.curObj = row
      } else {
        this.curObj = {}
      }
      this.isDtlShow = true
    },
    closePopup(obj) {
      this.isDtlShow = false
    }
  }
}
</script>
