<template>
  <el-dialog
    custom-class="center-dialog"
    :title="title"
    top="0vh"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item :label="__('父编号')" prop="district_parent_id">
        <el-input
          v-model="form.district_parent_id"
          disabled
          :placeholder="__('父编号')"
        />
      </el-form-item>
      <el-form-item :label="__('地区名称')" prop="district_name">
        <el-input v-model="form.district_name" :placeholder="__('地区名称')" />
      </el-form-item>

      <el-form-item :label="__('排序')" prop="district_sort">
        <el-input v-model="form.district_sort" :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/base/district'

  export default {
    name: 'BaseDistrictEdit',
    data() {
      return {
        form: { unit_name: '' },
        rules: {
          district_id: [
            { required: true, trigger: 'blur', message: this.__('地区编号') },
          ],
          district_name: [
            { required: true, trigger: 'blur', message: this.__('地区名称') },
          ],
          district_sort: [
            { required: true, trigger: 'blur', message: this.__('排序') },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      __,
      showEdit(row, district_parent_id) {
        if (district_parent_id === 0 || district_parent_id) {
          this.title = this.__('添加')
          this.form.district_parent_id = district_parent_id
        } else {
          this.title = this.__('编辑')
          this.form = {
            district_id: row.district_id,
            district_name: row.district_name,
            district_sort: row.district_sort,
            district_parent_id: row.district_parent_id,
          }
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = { unit_name: '' }
        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')
            }

            let district_parent_id = this.form.district_parent_id
            if (district_parent_id) {
              this.$emit('refreshNodeBy', district_parent_id)
            } else {
              this.$emit('fetch-data')
            }
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
