<template>
  <div>
    <el-dialog
      v-dialogDrag
      :visible.sync="showModal"
      :close-on-click-modal="false"
      width="700px"
      :title="modalTitle"
    >
      <div>
        <Form id="edit-form" ref="formObj" :model="formObj" :rules="ruleValidate" :label-width="80">
          <Form-item label="名称" prop="name">
            <el-input
              v-model="formObj.name"
              name="name"
              placeholder="请输入名称"
              style="width:100%"
              :maxlength="20"
              show-word-limit
            />
          </Form-item>
          <Form-item label="上级组织" prop="parentName">
            <el-input
              v-model="formObj.parentName"
              readonly
              @click.native="_selectZtree"
            />
            <input v-model.number="formObj.pid" name="pid" hidden>
          </Form-item>
          <Form-item label="状态" prop="status">
            <el-radio-group v-model="formObj.status" @change="_selectStatus">
              <el-radio v-for="item in statusList" :key="item.value" :label="item.value">
                {{ item.label }}
              </el-radio>
            </el-radio-group>
          </Form-item>
          <Form-item label="排序" prop="sort">
            <el-input-number
              v-model.number="formObj.sort"
              name="sort"
              placeholder="请输入排序"
              :min="1"
              style="width: 100%"
              size="small"
              controls-position="right"
              step-strictly
            />
          </Form-item>
          <Form-item label="备注" prop="remark">
            <el-input
              v-model="formObj.remark"
              name="remark"
              placeholder="请输入备注"
              style="width:100%"
              :maxlength="100"
              show-word-limit
            />
          </Form-item>
        </Form>
      </div>
      <div slot="footer">
        <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult" />
      </div>
    </el-dialog>
    <!-- 父id  ztree  -->
    <sysOrgZtree ref="ztreeModal" @on-result-change="_ztree" />
  </div>
</template>
<script>
/**
 * 添加编辑组织表
 */
import { sysOrg } from '../../../api'
import sysOrgZtree from './sysOrgZtree'

export default {
  components: { sysOrgZtree },
  data() {
    return {
      id: '',
      modalTitle: '添加组织表',
      formObj: {
        companyId: '',
        name: '',
        pid: 0,
        parentName: '',
        group: '',
        remark: '',
        sort: 0,
        status: 'NORMAL'
      },
      ruleValidate: {
        name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
        sort: [
          {
            required: true,
            message: '排序不能为空',
            trigger: 'blur',
            type: 'number'
          }
        ],
        parentName: [
          { required: true, message: '上级组织不能为空', trigger: 'blur' }
        ]
      },
      showModal: false,
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '保存', type: 'primary' }
      ],
      statusList: [{ value: '1', label: '正常' }, { value: '0', label: '禁用' }]
    }
  },
  methods: {
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '保存':
          this._ok()
          break
      }
    },
    _selectStatus(data) {
      this.formObj.status = data
    },
    _resultChange(result, msg) {
      if (result) {
        this.showModal = false
        this.$message.success(msg)
        this.$emit('on-result-change')
      }
      this._hideLoading()
    },
    _ok() {
      this.$refs.formObj.validate(valid => {
        if (valid) {
          const data = this.$serialize('edit-form')
          this.$extend(data, { status: this.formObj.status })
          this._save(data)
        } else {
          this._hideLoading()
          this.$message.error('表单验证失败!')
        }
      })
    },
    _save: async function(data) {
      if (this.$string(this.id).isEmpty()) {
        const result = await sysOrg.add(data)
        this._resultChange(result, '添加成功!')
      } else {
        const flag = this.$judgeClassify({
          id: this.id,
          classifyId: this.formObj.pid
        })
        if (flag) {
          const result = await sysOrg.edit({ id: this.formObj.id, obj: data })
          this._resultChange(result, '编辑成功!')
        } else {
          this.$message.warning('上级组织与组织名不能重复，请重新选择~')
          this._hideLoading()
        }
      }
    },
    _cancel() {
      this.showModal = false
    },
    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    },
    _open(formObj) {
      this.showModal = true
      this.$nextTick(() => {
        this.$refs.formObj.resetFields()
        this._hideLoading()
        if (this.$string(formObj).isEmpty()) {
          this.id = ''
          this.modalTitle = '添加组织'
          this.formObj.parentName = ''
        } else {
          this.id = formObj.id
          this.formObj = formObj
          this.modalTitle = '编辑组织'
          this.formObj.status = formObj.status.value
          this.formObj.sort = formObj.sort ? formObj.sort : 0
        }
      })
    },
    _selectZtree() {
      this.$refs.ztreeModal._open() // 打开上ztreeModel
    },
    _ztree(result) {
      this.$set(this.formObj, 'pid', result.id)
      this.$set(this.formObj, 'parentName', result.name)
    }
  }
}
</script>
