<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="700px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item :label="__('分类名称')" prop="chain_category_name">
        <el-input
          v-model="form.chain_category_name"
          :placeholder="__('分类名称')"
        />
      </el-form-item>
      <el-form-item :label="__('父编号')" prop="chain_category_pid">
        <el-input
          v-model="form.chain_category_pid"
          :disabled="isEdit"
          :placeholder="__('父编号')"
        />
      </el-form-item>
      <el-form-item :label="__('排序')" prop="chain_category_sort">
        <el-input
          v-model="form.chain_category_sort"
          :placeholder="__('排序')"
        />
      </el-form-item>
      <el-form-item :label="__('是否启用')" prop="chain_category_enable">
        <el-radio-group v-model="form.chain_category_enable">
          <el-radio :label="1">{{ __('启用') }}</el-radio>
          <el-radio :label="0">{{ __('关闭') }}</el-radio>
        </el-radio-group>
      </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/chain/category/base'

  export default {
    name: 'ChainCategoryEdit',
    data() {
      return {
        form: {
          chain_category_enable: 1,
          chain_category_sort: 0,
        },
        isEdit: false,
        rules: {},
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    mounted() {
      this.rules = {
        chain_category_name: [
          { required: true, trigger: 'blur', message: this.__('分类名称') },
        ],
        chain_category_pid: [
          { required: true, trigger: 'blur', message: this.__('父编号') },
        ],
        chain_category_sort: [
          { required: true, trigger: 'blur', message: this.__('排序') },
        ],
        chain_category_enable: [
          { required: true, trigger: 'blur', message: this.__('是否启用') },
        ],
      }

      this.$forceUpdate()
    },
    methods: {
      __,
      showEdit(row, lecturer_category_id) {
        if (!row) {
          this.title = this.__('新增')
          if (lecturer_category_id != null) {
            this.form.chain_category_pid = lecturer_category_id
          } else {
            this.form.chain_category_pid = 0
          }
          this.isEdit = true
          this.dialogFormVisible = true
          return
        } else {
          this.title = this.__('编辑')
          this.isEdit = true
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },

      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.form.children) {
              delete this.form.children
            }
            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>
