<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="650px"
    @close="close"
  >
    <el-form
      ref="form"
      label-position="left"
      label-width="100px"
      :model="form"
      :rules="rules"
      size="medium"
    >
      <el-form-item :label="__('商品编号')" prop="product_id">
        <el-input
          v-model="form.product_id"
          clearable
          :placeholder="__('请选择商品编号')"
          readonly
          :style="{ width: '100%' }"
          @focus="showItemTable"
        />
      </el-form-item>
      <el-form-item :label="__('咨询内容')" prop="ask_question">
        <el-input
          v-model="form.ask_question"
          clearable
          :placeholder="__('请输入咨询内容')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('答案')" prop="ask_answer">
        <el-input
          v-model="form.ask_answer"
          clearable
          :placeholder="__('请输入答案')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">{{ __('取消') }}</el-button>
      <el-button type="primary" @click="save">{{ __('确定') }}</el-button>
    </div>
    <ProductItemTable ref="productItemTable" @checkItem="getCheckItem" />
  </el-dialog>
</template>

<script>
  // eslint-disable-next-line no-unused-vars
  import { translateTitle as __ } from '@/utils/i18n'
  import { doEdit } from '@/api/product/ask/base'
  import { getList } from '@/api/product/base'
  import ProductItemTable from '@/components/activity/ProductBaseDialogTable'

  export default {
    name: 'ProductAskSellerBaseEdit',
    components: { ProductItemTable },
    data() {
      return {
        isNew: false,
        total: 0,
        queryForm: {
          pageNum: 1,
          pageSize: 20,
        },
        layout: 'total, sizes, prev, pager, next, jumper',
        title: '',
        dialogFormVisible: false,
        form: {
          product_id: undefined,
        },
        rules: {
          product_id: [
            {
              required: true,
              message: '请输入产品SPU编码',
              trigger: 'change',
            },
          ],
          ask_question: [
            {
              required: true,
              message: '请输入咨询内容',
              trigger: 'blur',
            },
          ],
        },
      }
    },
    methods: {
      __,
      showEdit(row) {
        if (!row) {
          this.isNew = true
          this.title = this.__('添加')
        } else {
          this.title = this.__('编辑')
          this.form = {
            ask_id: row.ask_id,
            product_id: row.product_id,
            ask_question: row.ask_question,
            ask_answer: row.ask_answer,
          }
        }
        this.dialogFormVisible = true
      },
      close() {
        this.isNew = false
        this.form = { product_id: undefined }
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      showItemTable() {
        this.$refs['productItemTable'].showTable()
      },
      getCheckItem(row) {
        this.form.product_id = row.product_id
        this.form.product_name = row.product_name
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg, status } = await doEdit(this.form)
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
