<template>
  <div class="lsauthgroup-form">
    <el-form ref="infoForm" :model="infoForm" size="small" label-width="100px">
      <el-form-item label="角色名称" prop="name" :rules="[{
                    required: true, message: '角色名称名称不能为空', trigger: 'blur'
                  }]">
        <el-input :disabled="infoForm.disabled?true:false" maxlength="20" clearable v-model="infoForm.name" size="small" placeholder="请输入角色名称"/>
      </el-form-item>
      <el-form-item label="角色描述" prop="remark">
        <el-input :disabled="infoForm.disabled?true:false" maxlength="20" clearable v-model="infoForm.remark" size="small" placeholder="请输入角色描述"/>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input-number :disabled="infoForm.disabled?true:false" size="small" v-model="infoForm.sort"></el-input-number>
      </el-form-item>
      <el-form-item label="数据权限" prop="authData" :rules="[{
                    required: true, message: '数据权限不能为空', trigger: 'change'
                  }]">
        <el-radio-group @change="authDataChange" v-if="!infoForm.disabled" v-model="infoForm.authData">
          <el-radio :label="1">查看本人负责河湖</el-radio>
          <el-radio :label="2">查看本人组织及下属组织内河湖数据</el-radio>
          <el-radio :label="3">查看指定组织下河湖</el-radio>
        </el-radio-group>
        <div v-else>
          <el-tag v-if="infoForm.authData == 1" type="" size="small">查看本人负责河湖</el-tag>
          <el-tag v-if="infoForm.authData == 2" type="success" size="small">查看本人组织及下属组织内河湖数据</el-tag>
          <el-tag v-if="infoForm.authData == 3" type="warning" size="small">查看指定组织下河湖</el-tag>
        </div>
      </el-form-item>
      <el-form-item v-if="infoForm.authData == 3" label="区划" prop="driverArea" :rules="[{
                    required: true, message: '区划不能为空', trigger: 'change'
                  }]">
        <el-cascader
          filterable
          :disabled="infoForm.disabled?true:false"
          v-model="infoForm.driverArea"
          :options="cityList"
          size="small"
          placeholder="请选择区划"
          :props="{ checkStrictly: true,value:'id',
              label:'name' ,multiple: true ,emitPath:false}"
          clearable></el-cascader>
      </el-form-item>
    </el-form>
    <div style="text-align: center">
      <el-button v-if="!infoForm.disabled" icon="el-icon-document-add" :loading="addBtnLoading" type="primary" size="medium" @click="submitForm('infoForm')">提交
      </el-button>
      <el-button v-if="infoForm.id" icon="el-icon-back" style="margin-left: 15px" size="medium" @click="closeWin('infoForm')">关闭
      </el-button>
    </div>
  </div>
</template>

<script>
  import authGroupApi from '@/api/system/lsauthgroup'

  export default {
    name: "authGroup",
    data() {
      return {
        addBtnLoading: false,
      }
    },
    props: {
      infoForm: {
        type: Object,
        default() {
          return {};
        }
      },
      cityList: {
        type: Array,
        default() {
          return [];
        }
      }
    },
    created() {
    },
    mounted() {
    },
    methods: {
      authDataChange(){
        let infoForm=JSON.parse(JSON.stringify(this.infoForm))
        infoForm.driverArea=[]
        this.infoForm=Object.assign({},infoForm)
      },
      // 提交表单
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let method = 'authGroupAdd'
            if (this.infoForm.id) {
              method = 'authGroupEdit'
            }
            this.addBtnLoading = true
            let infoForm=JSON.parse(JSON.stringify(this.infoForm))
            if(infoForm.driverArea&&infoForm.driverArea.length>0){
              infoForm.driverArea=infoForm.driverArea.join(',')
            }else{
              infoForm.driverArea=''
            }
            authGroupApi[method](infoForm).then(res => {
              this.addBtnLoading = false
              this.$refs[formName].resetFields()
              this.$message({
                type: 'success',
                message: this.infoForm.id ? '修改成功' : '添加成功'
              })
              if (this.infoForm.id) {
                this.$emit('submit',2)
              }else{
                this.$emit('submit',1)
              }
              // 调动关闭窗体方法
              this.$emit('closeWin')
            }).catch(e => {
              this.addBtnLoading = false
            })
          }
        })
      },
      // 关闭窗体
      closeWin() {
        this.$refs['infoForm'].resetFields()
        this.$emit('closeWin')
      }
    }
  }
</script>

<style lang="less">
  .lsauthgroup-form{
    .el-radio {
      margin-top: 10px;
    }
  }

</style>
