<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="600px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item :label="__('分类名称')" prop="category_name">
        <el-input v-model="form.category_name" :placeholder="__('分类名称')" />
      </el-form-item>
      <el-form-item v-if="false" :label="__('关键词')" prop="category_keywords">
        <el-input
          v-model="form.category_keywords"
          :placeholder="__('分类关键词')"
        />
      </el-form-item>
      <el-form-item :label="__('分类描述')" prop="category_desc">
        <el-input v-model="form.category_desc" :placeholder="__('分类描述')" />
      </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>
  </el-dialog>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'

  import { doEdit } from '@/api/article/category'

  export default {
    name: 'ArticleCategoryEdit',
    data() {
      return {
        form: {},
        rules: {
          category_name: [
            { required: true, trigger: 'blur', message: this.__('分类名称') },
          ],
          category_keywords: [
            { required: true, trigger: 'blur', message: this.__('分类关键词') },
          ],
          category_desc: [
            { required: true, trigger: 'blur', message: this.__('分类描述') },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    methods: {
      __,
      showEdit(row, category_parent_id) {
        if (category_parent_id || category_parent_id === 0) {
          this.form.category_parent_id = category_parent_id
          this.title = this.__('添加')
        } else {
          this.title = this.__('编辑')
          this.form = {
            category_id: row.category_id,
            category_name: row.category_name,
            category_keywords: row.category_keywords,
            category_desc: row.category_desc,
          }
        }
        this.dialogFormVisible = true
      },
      close() {
        this.form = {
          unit_name: '',
        }
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      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>
