<template>
  <div class="product-comment-container">
    <vab-query-form>
      <vab-query-form-right-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.product_id"
              clearable
              :placeholder="__('请输入产品SPU编码')"
            />
          </el-form-item>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.item_name"
              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"
        fixed="left"
        :label="__('产品SPU')"
        prop="product_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('商品名称')"
        prop="item_name"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        align="center"
        :label="__('卖家店铺编号')"
        prop="store_id"
        show-overflow-tooltip
        width="140"
      />
      <el-table-column
        align="center"
        :label="__('店铺名称')"
        prop="store_name"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :label="__('买家编号')"
        prop="user_id"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :label="__('买家姓名')"
        prop="user_name"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :label="__('评价内容')"
        prop="comment_content"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        align="center"
        :label="__('评论图片')"
        prop="comment_image"
        show-overflow-tooltip
        width="100"
      >
        <template slot-scope="scope">
          <el-popover placement="top-start" title="" trigger="hover">
            <img
              v-if="scope.row.comment_image"
              alt=""
              :src="scope.row.comment_image"
              style="width: 150px; height: 150px"
            />
            <img
              v-if="scope.row.comment_image"
              slot="reference"
              :src="scope.row.comment_image"
              style="width: 30px; height: 30px"
            />
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :formatter="formattime"
        :label="__('评价时间')"
        prop="comment_time"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        align="center"
        :label="__('是否展示')"
        prop="comment_enable"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.comment_enable"

            :active-value="1"

            :inactive-value="0"
            @change="sChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :formatter="formatReply"
        :label="__('是否管理员回复')"
        prop="admin_is_reply"
        show-overflow-tooltip
        width="140"
      />
      <el-table-column
        align="center"
        :formatter="formatNum"
        :label="__('评价回复数量')"
        prop="comment_reply_num"
        show-overflow-tooltip
        width="120"
      />
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { enable, getList } from '@/api/product/comment'
  import { renderTime } from '@/utils/date'

  export default {
    name: 'ProductComment',
    components: {},
    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,
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      formattime(row) {
        return renderTime(row.comment_time)
      },
      formatReply(row) {
        if (row.admin_is_reply === 1) return this.__('是')
        else return this.__('否')
      },
      formatNum(row) {
        if (!row.comment_reply_num) return 0
        else return row.comment_reply_num
      },
      async sChange(row) {
        const { msg } = await enable({
          comment_id: row.comment_id,
          comment_enable: row.comment_enable,
          enable_name: 'comment_enable',
          enable_id: '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>
