<template>
  <div class="plantform-feedback-container">
    <vab-query-form>
      <vab-query-form-right-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-select v-if="false" v-model="queryForm.feedback_question_status" style="width: 100%">
              <el-option :label="__('全部')" :value="0" />
              <el-option :label="__('待回复')" :value="1" />
              <el-option :label="__('已处理')" :value="2" />
            </el-select>
            <el-radio-group v-model="queryForm.feedback_question_status" @change="queryData">
              <el-radio-button :label="0" >{{__('全部')}}</el-radio-button>
              <el-radio-button :label="1" >{{__('待回复')}}</el-radio-button>
              <el-radio-button :label="2" >{{__('已处理')}}</el-radio-button>
            </el-radio-group>
          </el-form-item>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.feedback_question"
              clearable
              :placeholder="__('请输入反馈内容')"
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              {{ __('查询') }}
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
    >
      <el-table-column align="center" :label="__('操作')" width="120">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">
            {{ __('回复') }}
          </el-button>
          <el-button type="text" @click="handleDelete(row)">
            {{ __('删除') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('编号')"
        prop="feedback_id"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :label="__('分类编号')"
        prop="feedback_category_id"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :label="__('用户编号')"
        prop="user_id"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :label="__('分类名称')"
        prop="feedback_category_name"
        show-overflow-tooltip
        width="100"
      />

      <el-table-column
        align="center"
        :label="__('反馈问题')"
        prop="feedback_question"
        show-overflow-tooltip
      />
      <!-- <el-table-column
        align="center"
        :label="__('页面链接')"
        prop="feedback_question_url"
        show-overflow-tooltip
      /> -->
      <el-table-column
        align="center"
        :label="__('回复')"
        prop="feedback_question_answer"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        :formatter="formatEnable"
        :label="__('是否回复')"
        prop="feedback_question_status"
        show-overflow-tooltip
        width="100"
      />
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'

  import { doDelete, getList } from '@/api/plantform/feedback'
  import Edit from './components/PlantformFeedbackEdit'

  export default {
    name: 'PlantformFeedback',
    components: { Edit },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          feedback_question_status: 0,
          feedback_question: "",
          pageNum: 1,
          pageSize: 20,
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,

      formatEnable(row) {
        const bankEnable = row.feedback_question_status
        return bankEnable==2 ? this.__('已回复') : this.__('待回复')
      },
      handleEdit(row) {
        if (row.feedback_id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        if (row.feedback_id) {
          this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
            const { msg, status } = await doDelete({
              feedback_id: row.feedback_id,
            })
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            await this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            const feedback_id = this.selectRows
              .map((item) => item.feedback_id)
              .join()
            this.$baseConfirm(
              this.__('你确定要删除选中项吗'),
              null,
              async () => {
                const { msg, status } = await doDelete({ feedback_id })
                if (200 == status) {
                  this.$baseMessage(msg, 'success')
                } else {
                  this.$baseMessage(msg, 'error')
                }
                await this.fetchData()
              }
            )
          }
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
    },
  }
</script>
