<template>
  <el-dialog
    title="权限配置"
    :visible.sync="dialogVisible"
    width="500px"
    @closed="closedHandler"
    :close-on-click-modal="false"
  >
    <el-form
      ref="dataForm"
      :model="dataForm"
      size="small"
      label-width="150px"
      :rules="rules"
    >
      <el-form-item
        label="角色类型："
        prop="roleId"
      >
        <el-radio-group
          class="radio-group-type1"
          v-model="dataForm.roleId"
        >
          <el-radio :label="1">管理员</el-radio>
          <el-radio :label="2">机构用户</el-radio>
          <el-radio :label="3">二级管理员</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        label="管理区域："
        prop="area"
        v-if="dataForm.roleId === 3"
      >
        <el-select
          v-model="dataForm.area"
          placeholder="请选择管理区域"
          style="width: 100%"
          :clearable="true"
        >
          <el-option
            v-for="item in areaList"
            :label="item.name"
            :key="item.id"
            :value="item.val"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <span
      slot="footer"
      class="dialog-footer"
    >
      <el-button
        size="small"
        @click="dialogVisible = false"
      >取 消</el-button>
      <el-button
        type="primary"
        size="small"
        @click="submitForm()"
      >确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { updateInsideRole } from '@/api/user'
import { getDic } from '@/util/dict'
export default {
  data() {
    return {
      dialogVisible: false,
      areaList: [],
      dataForm: {
        roleId: '',
        area: ''
      },
      rules: {
        roleId: [
          { required: true, message: '请选择角色类型' },
        ],
        area: [
          { required: true, message: '请选择管理区域' },
        ]
      }
    }
  },
  mounted() {
    // 获取字典
    getDic(['area']).then(res => {
      this.areaList = res.area
    })
  },
  methods: {
    init(userId, roleId, area = '') {
      this.dialogVisible = true
      this.dataForm.roleId = roleId
      this.dataForm.userId = userId
      this.dataForm.area = area
    },
    submitForm() {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          const params = {
            ...this.dataForm
          }
          updateInsideRole(params).then(res => {
            if (res.msg === 201) {
              this.dialogVisible = false
              this.$message.success('配置成功')
              this.$emit('subsucc')
            } else {
              this.$message.error(res.content)
            }
          })
        }
      })
    },
    closedHandler() {
      this.$refs.dataForm.resetFields()
    }
  }
}
</script>

<style>
</style>