<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-radio-group v-model="queryForm.comment_is_reply" @change="queryData">
              <el-radio-button v-for="item in stateOptions" :key="item.value" :label="item.value" >{{item.label}}</el-radio-button>
            </el-radio-group>
          </el-form-item>
          <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="__('操作')"
        width="150"
      >
        <template #default="{ row }">
          <el-button type="text" @click="handleTable(row)">
            {{ __('查看') }}
          </el-button>
          <el-button type="text" @click="hadleEdit(row.comment_id)">
            {{ __('回复') }}
          </el-button>
          <el-button type="text" @click="handleDelete(row)">
            {{ __('删除') }}
          </el-button>
        </template>
      </el-table-column>
      <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="user_name"
        show-overflow-tooltip
        width="100"
      />
      <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="140px"
      >
        <template slot-scope="scope">
          <el-image
            v-show="scope.row.comment_image"
            :preview-src-list="[scope.row.comment_image]"
            :src="scope.row.comment_image"
            style="width: 35px; height: 35px"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('有帮助')"
        prop="comment_helpful"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :formatter="formatTime"
        :label="__('评价时间')"
        prop="comment_time"
        show-overflow-tooltip
        width="180"
      />
      <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="100"
      />
      <el-table-column
        align="center"
        :formatter="formatNum"
        :label="__('评价回复')"
        prop="comment_reply_num"
        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"
    />
    <Table ref="table" @fetch-data="fetchData" />
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { doDelete, enable, getList } from '@/api/product/comment'
  import Table from './components/ProductCommentTable'
  import Edit from '@/views/product/sellerComment/components/addProductCommentReplyEdit'
  import { renderTime } from '@/utils/date'

  export default {
    name: 'ProductComment',
    components: { Table, Edit },

    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        queryForm: {
          pageNum: 1,
          pageSize: 20,
          comment_is_reply:null,
        },

        stateOptions: [
          {
            value: null,
            label: this.__('全部'),
          },
          {
            value: 0,
            label: this.__('未回复'),
          },
          {
            value: 1,
            label: this.__('已回复'),
          },
        ],
      }
    },
    created() {
      let query = this.$route.query
      if (typeof query.comment_is_reply != "undefined" ) {
        this.queryForm.comment_is_reply = Number(query.comment_is_reply)
      }

      this.fetchData()
    },
    methods: {
      __,
      handleDelete(row) {
        this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
          const { msg, status } = await doDelete({
            comment_ids: row.comment_id,
          })
          if (200 == status) {
            this.$baseMessage(msg, 'success')
          } else {
            this.$baseMessage(msg, 'error')
          }
          await this.fetchData()
        })
      },
      formatTime(row) {
        return renderTime(row.comment_time)
      },

      formatNum(row) {
        if (row.comment_reply_num == null) {
          return 0
        } else {
          return row.comment_reply_num
        }
      },
      handleTable(row) {
        this.$refs['table'].showTable(row)
      },
      hadleEdit(comment_id) {
        this.$refs['edit'].showEdit(comment_id)
      },
      formatReply(row) {
        return row.admin_is_reply ? this.__('是') : this.__('否')
      },
      async sChange(row) {
        const { msg, status } = await enable({
          comment_id: row.comment_id,
          comment_enable: row.comment_enable,
          enable_name: 'comment_enable',
          enable_id: 'comment_id',
        })
        if (status !== 200) {
          this.$baseMessage(msg, 'failed', false, 'vab-hey-message-failed')
          await this.fetchData()
        } else {
          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
        this.$emit('fetch-data')
      },
    },
  }
</script>
