<template>
  <div class="story-comment-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="4">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          {{ __('添加') }}
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="20">
        <el-button icon="el-icon-refresh" type="primary" @click="queryData">
          {{ __('刷新') }}
        </el-button>
      </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="comment_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('会员编号')"
        prop="user_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('原帖编号')"
        prop="story_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('评论内容')"
        prop="comment_content"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatState"
        :label="__('状态')"
        prop="comment_state"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('回复用户编号')"
        prop="to_user_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('点赞数量')"
        prop="comment_like_count"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatTime"
        :label="__('添加时间')"
        prop="comment_time"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        align="center"
        :formatter="formatShow"
        :label="__('是否显示')"
        prop="comment_is_show"
        show-overflow-tooltip
      />
    </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 { getList, doDelete } from '@/api/story/comment'
  import Edit from './components/StoryCommentEdit'
  import { renderTime } from '@/utils/date'

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

      formatTime(row) {
        const cardTime = row.comment_time
        return cardTime ? renderTime(cardTime) : ''
      },
      formatShow(row) {
        return row.comment_is_show === 1 ? this.__('显示') : this.__('隐藏')
      },
      formatState(row) {
        return row.comment_state === 0 ? this.__('正常') : this.__('屏蔽')
      },
      handleEdit(row) {
        if (row.comment_id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },

      handleDelete(row) {
        this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
          const { msg, status } = await doDelete({
            comment_id: row.comment_id,
          })
          this.$baseMessage(msg, 'success', false, 'vab-hey-message-success')
          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>
