<template>
  <el-dialog
    custom-class="center-dialog"
    :title="title"
    top="0vh"
    :visible.sync="dialogFormVisible"
    width="1200px"
    @close="close"
  >
    <vab-query-form>
      <vab-query-form-left-panel :span="6">
        <el-button
          icon="el-icon-plus"
          type="primary"
          @click="handleEdit(queryForm.comment_id)"
        >
          {{ __('回复评论') }}
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="18">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.user_name"
              clearable
              :placeholder="__('评论者姓名')"
            />
          </el-form-item>

          <el-form-item>
            <el-input
              v-model.trim="queryForm.user_name_to"
              clearable
              :placeholder="__('输入回复用户名称')"
            />
          </el-form-item>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.comment_reply_content"
              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="500">
      <el-table-column
        align="center"
        :label="__('买家评论者姓名')"
        prop="user_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('回复用户名称')"
        prop="user_name_to"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        align="center"
        :label="__('评论回复内容')"
        prop="comment_reply_content"
        show-overflow-tooltip
        width="150px"
      />
      <el-table-column
        align="center"
        :formatter="formatTime"
        :label="__('评论回复时间')"
        prop="comment_reply_time"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        align="center"
        :label="__('是否展示')"
        prop="comment_reply_enable"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.comment_reply_enable"

            :active-value="1"

            :inactive-value="0"
            @change="sChange(scope.row)"
          />
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        :formatter="formatIsadmin"
        :label="__('管理员回复')"
        prop="comment_reply_isadmin"
        show-overflow-tooltip
        width="180"
      />
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">{{ __('取消') }}</el-button>
    </div>
    <edit ref="edit" @fetch-data="refresh" />
  </el-dialog>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { listCommentReply } from '@/api/product/comment/reply'

  import Edit from '@/views/product/sellerComment/components/addProductCommentReplyEdit'
  import { renderTime } from '@/utils/date'
  import { enable } from '@/api/product/comment'

  export default {
    name: 'ProductCommentTable',
    components: { Edit },
    data() {
      return {
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        title: '',
        total: 0,
        dialogFormVisible: false,
        queryForm: {
          pageNum: 1,
          pageSize: 20,
          user_name: '',
          user_name_to: '',
          comment_reply_content: '',
        },
      }
    },
    created() {},

    methods: {
      __,

      formatTime(row) {
        const cardTime = row.comment_reply_time
        return cardTime ? renderTime(cardTime) : ''
      },

      formatIsadmin(row) {
        return row.comment_reply_isadmin === 0 ? this.__('否') : this.__('是')
      },

      handleEdit(comment_id) {
        this.$refs['edit'].showEdit(comment_id)
      },

      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async showTable(row) {
        this.queryForm.comment_id = row.comment_id
        this.dialogFormVisible = true
        this.fetchData()
      },
      close() {
        this.dialogFormVisible = false
      },

      async sChange(row) {
        const { msg, status } = await enable({
          comment_reply_id: row.comment_reply_id,
          comment_reply_enable: row.comment_reply_enable,
        })
        if (status === 250) {
          this.$baseMessage(msg, 'error', false)
          await this.fetchData()
        } else {
          this.$baseMessage(msg, 'success', false, 'vab-hey-message-success')
          await this.fetchData()
        }
      },
      refresh() {
        this.fetchData()
        this.$emit('fetch-data')
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await listCommentReply(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
    },
  }
</script>
