<template>
  <div>
    <Modal v-model="showModal" :mask-closable="false">
      <p slot="header">
        {{ modalTitle }}
      </p>
      <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: 408px"
                :maxlength="20"
                show-word-limit
              />
          </Form-item>
          <Form-item label="上级角色" prop="parentName">
            <el-input
              v-model="parentName"
              placeholder="请选择上级角色"
              readonly
              @click.native="_selectTree"
            />
            <input v-model.number="formObj.pid" name="pid" hidden>
          </Form-item>
          <!--<Form-item label="数据范围" prop="dataScope">-->
          <!--<Select @on-change="_selectChange" v-model="formObj.dataScope" style="width:100%">-->
          <!--<Option v-for="item in scopeList" :value="item.value" :key="item.value">{{ item.label }}</Option>-->
          <!--</Select>-->
          <!--</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:410px"
                :maxlength="100"
                show-word-limit
              />
          </Form-item>
        </Form>
      </div>
      <div slot="footer">
        <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult" />
      </div>
    </Modal>
    <!-- 父id  ztree  -->
    <selRoleTree ref="treeModal" @on-result-change="_treeRestul" />
  </div>
</template>
<script>
/**
 * 添加编辑
 */
import { sysRole } from '../../../api'
import selRoleTree from './tree/selRoleTree'
export default {
  components: { selRoleTree },
  data() {
    return {
      id: '',
      modalTitle: '',
      parentName: '',
      formObj: {
        name: '',
        pid: '0',
        parentName: '',
        remark: '',
        sort: 1,
        status: 1,
        dataScope: 0
      },
      ruleValidate: {
        companyId: [
          { required: true, message: '公司 ID不能为空', trigger: 'blur' }
        ],
        name: [{ required: true, message: '名称不能为空', trigger: 'blur' }]
      },
      showModal: false,
      scopeList: [
        { value: 0, label: '仅看本人数据' },
        { value: 1, label: '所有数据' },
        { value: 2, label: '所在公司及以下数据' },
        { value: 3, label: '所在公司数据' },
        { value: 4, label: '所在部门及以下数据' },
        { value: 5, label: '所在部门数据' },
        { value: 6, label: '按明细设置' }
      ],
      statusList: [{ value: 0, label: '禁用' }, { value: 1, label: '正常' }],
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '保存', type: 'primary' }
      ]
    }
  },
  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,
            dataScope: this.formObj.dataScope
          })
          this._save(data)
        } else {
          this._hideLoading()
          this.$message.error('表单验证失败!')
        }
      })
    },
    _save: async function(data) {
      if (this.$string(this.id).isEmpty()) {
        const result = await sysRole.add(data)
        this._resultChange(result, '添加成功')
      } else {
        const flag = this.$judgeClassify({
          id: this.id,
          classifyId: this.formObj.pid
        })
        if (flag) {
          const result = await sysRole.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._hideLoading()
      this.$refs.formObj.resetFields() // 重置
      if (this.$string(formObj).isEmpty()) {
        this.id = ''
        this.parentName = ''
        this.formObj.pid = '0'
        this.formObj.sort = 1
        this.modalTitle = '添加角色'
      } else {
        this.id = formObj.id
        this.formObj = formObj
        this.parentName =
          formObj.parentName !== undefined ? formObj.parentName : ''
        this.modalTitle = '编辑角色'
      }
    },
    _selectTree() {
      this.$refs.treeModal._open() // 打开上ztreeModel
    },
    _treeRestul(result) {
      if (result === undefined) {
        // 未选中数据的时候
        this.formObj.pid = '0'
        this.parentName = ''
      } else {
        // 选中数据以后
        this.formObj.pid = result.id
        this.parentName = result.name
      }
    },
    _selectChange(data) {
      this.formObj.dataScope = data
    }
  }
}
</script>
