<template>
  <Modal v-model="showModalRole" :mask-closable="false" :closable="false" :title="modalTitle" :value="value" width="820"
         v-draggable="options">

    <i-form label-position="right" :label-width="100" ref="roleForm" :model="bean" :rules="rules" inline>
      <Row>
        <i-col :span="12">
          <Form-item label="所属应用" prop="forSystemId" class="ivu-form-item-search">
            <Select v-model="bean.forSystemId" style="width: 243px" :disabled="disabled" @on-select="selectedSystem">
              <Option :value="'0'" :key="'0'">通用</Option>
              <Option v-for="item in systems" :value="item.id" :key="item.id" :tag="item.code" >{{ item.name }}</Option>
            </Select>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="角色编码" prop="roleCode" class="ivu-form-item-search">
            <Input v-model="bean.roleCode" style="width: 243px" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="角色名称" prop="roleName" class="ivu-form-item-search">
            <Input v-model="bean.roleName" style="width: 243px" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="角色类型" prop="type" class="ivu-form-item-search">
            <Select v-model="bean.type" style="width: 243px" :disabled="disabled">
              <Option :value="1" :key="1">普通角色</Option>
              <Option :value="2" :key="2">超级管理员</Option>
            </Select>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="默认角色" prop="isDefault" class="ivu-form-item-search">
            <Select v-model="bean.isDefault" style="width: 243px" :disabled="disabled">
              <Option :value="1" :key="1">是</Option>
              <Option :value="0" :key="0">否</Option>
            </Select>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="状态" prop="state" class="ivu-form-item-search">
            <Select v-model="bean.state" style="width: 243px" :disabled="disabled">
              <Option :value="1" :key="1">正常</Option>
              <Option :value="0" :key="0">禁用</Option>
            </Select>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="备注" class="ivu-form-item-search">
            <Input v-model="bean.remark" style="width: 243px" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
      </Row>
    </i-form>
    <template #footer>
    <div>
      <Button @click="cancel">关闭</Button>
      <Button type="primary" :loading="saveLoading" @click="ok('roleForm')" v-if="showSaveBtn">保存</Button>
    </div>
    </template>
  </Modal>
</template>

<script>
  import { addRole, editRole } from '../../../../api/rbac/role-api'
  import { getList } from '../../../../api/rbac/res-manage'

  let $this
  export default {
    name: 'roleModal',
    components: {},
    props: {
      show: Boolean,
      value: Object
    },
    data () {
      return {
        systems: [],
        showModalRole: false,
        bean: {},
        initBean: {type: 1, isDefault: 0, state: 1, forSystemId: '0'},
        flag: '',
        saveLoading: false,
        modalTitle: '',
        disabled: false,
        showSaveBtn: true,
        rules: {
          forSystemId: [
            { required: true, message: '所属应用不能为空!', trigger: 'blur' },
          ],
          roleCode: [
            { required: true, message: '角色编码不能为空!', trigger: 'blur' },
          ],
          roleName: [
            { required: true, message: '角色名称不能为空!', trigger: 'blur' },
          ],
          type: [
            { type: 'number', required: true, message: '角色类型不能为空!', trigger: 'blur' },
          ],
          isDefault: [
            { type: 'number', required: true, message: '是否默认角色不能为空!', trigger: 'blur' },
          ],
          state: [
            { type: 'number', required: true, message: '状态不能为空!', trigger: 'blur' },
          ],
        },
        options: {
          trigger: '.ivu-modal-header',
          body: '.ivu-modal',
          recover: true
        }
      }
    }, methods: {
      ok (name) {
        this.$refs[name].validate((valid) => {
          if (!valid) {
            return
          }//表单校验失败则返回
          this.saveLoading = true
          //this.showModalRole = false;
          if (this.flag == 'edit') {
            let params = this.bean
            delete params['createTime']
            delete params['modifyTime']
            //执行修改操作
            editRole(params).then(data => {
              if (!data.status) {
                this.$Message.error(data.error.message)
                this.saveLoading = false
                console.log(data)
                return false
              }
              this.$Message.success('操作成功！')
              this.closeAndRefresh()
            }).catch(err => {
              this.$Message.error('操作失败！')
              this.saveLoading = false
              console.log(err)
            })
          } else if (this.flag == 'add') {
            delete this.bean['createTime']
            delete this.bean['modifyTime']
            //执行添加操作
            addRole(this.bean).then(data => {
              if (!data.status) {
                this.$Message.error(data.error.message)
                this.saveLoading = false
                console.log(data)
                return false
              }
              this.$Message.success('操作成功！')
              this.closeAndRefresh()
            }).catch(err => {
              this.$Message.error('操作失败！')
              this.saveLoading = false
              console.log(err)
            })
          }
        })

      },
      cancel () {
        this.showModalRole = false
        this.$emit('update:show', false)
        //重置校验结果
        this.$refs['roleForm'].resetFields()
      },
      closeAndRefresh () {
        this.saveLoading = false
        this.cancel()
        this.$emit('reload-data')
      },
      selectedSystem (e){//通过选择事件将系统编码赋值到对象
        this.bean.forSystemId = e.value;
        this.bean.forSystemCode = e.tag;
      }
    },
    mounted () {
      $this = this
      this.bean = this.initBean
      //初始化系统下拉框集合
      getList({type: 0}).then(res => {
        this.systems = res.data.result;
      });
    },
    watch: {
      show: {
        immediate: true,
        handler: function () {
          this.showModalRole = this.show
        }
      },
      value: {
        immediate: true,
        handler: function () {
          //赋值
          this.bean = this.value.bean == null ? Object.assign({}, this.initBean) : JSON.parse(JSON.stringify(this.value.bean))
          this.flag = this.value.flag
          this.modalTitle = this.value.title
          //如果时查看的功能，要将所有input框只读，并且去掉保存按钮
          if (this.flag == 'view') {
            this.disabled = true
            this.showSaveBtn = false
          } else {
            this.disabled = false
            this.showSaveBtn = true
          }
        }
      }
    }

  }
</script>

<style scoped>
  .ivu-form-item-search {
    margin-bottom: 21px;
  }
</style>
