<template>
  <div class="product-ask-base-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.user_nickname"
              clearable
              :placeholder="__('请输入用户昵称')"
            />
          </el-form-item>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.ask_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="__('咨询编号')"
        prop="ask_id"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        :label="__('商品编号')"
        prop="product_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('商品名称')"
        prop="product_name"
        show-overflow-tooltip
        width="240"
      />
      <el-table-column
        align="center"
        :label="__('用户昵称')"
        prop="user_nickname"
        show-overflow-tooltip
        width="140"
      />
      <el-table-column
        align="center"
        :label="__('咨询内容')"
        prop="ask_question"
        show-overflow-tooltip
        width="240"
      />
      <el-table-column
        align="center"
        :formatter="formatTime"
        :label="__('提问时间')"
        prop="ask_time"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        align="center"
        :label="__('答案')"
        prop="ask_answer"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatAnsweTime"
        :label="__('回答时间')"
        prop="ask_answer_time"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        align="center"
        :label="__('回复人')"
        prop="ask_answer_user_nickname"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('是否展示')"
        prop="ask_enable"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.ask_enable"


            @change="sChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('点赞数量')"
        prop="ask_helpful"
        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"
    />
  </div>
</template>

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

  export default {
    name: 'ProductAskBase',
    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,
          title: '',
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      formatTime(row) {
        return renderTime(row.ask_time)
      },
      formatAnsweTime(row) {
        return renderTime(row.ask_answer_time)
      },
      async sChange(row) {
        const { msg } = await doEdit({
          ask_id: row.ask_id,
          product_id: row.product_id,
          ask_enable: row.ask_enable,
        })
        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>
