<!--
  @description: 角色管理--角色添加、编辑弹框
  @Modifier: haofengxian
  @routerPath:
  @date: 2020-9-17-10:27
-->
<template>
  <el-dialog
    :title="textMap[dialogStatus]"
    :visible.sync="dialogFormVisible"
    :close-on-click-modal="false"
    width="650px"
  >
    <el-form
      ref="dataForm"
      :rules="rules"
      :model="temp"
      label-position="left"
      label-width="120px"
      style="width: 400px; margin-left:50px;"
    >
      <el-form-item label="角色名称" prop="roleName">
        <el-input v-model.trim="temp.roleName" />
      </el-form-item>
      <el-form-item label="角色编码" prop="roleCode">
        <el-input v-model.trim="temp.roleCode" />
      </el-form-item>
      <el-form-item label="是否可用" prop="roleType">
        <el-radio-group v-model="temp.roleType">
          <el-radio label="1">
            是
          </el-radio>
          <el-radio label="0">
            否
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注">
        <el-input
          v-model="temp.remarks"
          :autosize="{ minRows: 2, maxRows: 4 }"
          type="textarea"
          placeholder="请输入内容"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button v-if="dialogStatus == 'create'" type="primary" @click="createData">
        确 定
      </el-button>
      <el-button v-else type="primary" @click="updateData">
        确 定
      </el-button>
      <el-button @click="dialogFormVisible = false">
        取 消
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
/* 接口*/
import { addRole, updateRole } from '@/api/rolemanagment/role'
/* 公告方法*/
import { validatorCode } from '@/assets/js/validate'
export default {
  name: 'RoleAddDialog',
  data() {
    return {
      dialogFormVisible: false, // 弹框是否打开
      // 传的参数
      temp: {
        id: '',
        roleName: '',
        roleCode: '',
        roleType: '1',
        remarks: ''
      },
      dialogStatus: '',
      textMap: {
        update: '编辑角色',
        create: '添加角色'
      },
      // 校验
      rules: {
        roleCode: [{ required: true, validator: validatorCode, trigger: 'blur' }],
        roleName: [{ required: true, message: '角色名称必填', trigger: 'blur' }],
        roleType: [{ required: true, message: '是否可用必选', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 处理编辑或者新增
    handleUpdateOrCreate(dialogStatus, row) {
      this.dialogFormVisible = true // 打开弹框
      this.dialogStatus = dialogStatus
      // 编辑时执行
      if (row) {
        this.temp = Object.assign({}, row) // copy obj
      } else {
        this.resetTemp()
      }
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    resetTemp() {
      // 清空表单
      this.temp = {
        id: '',
        roleName: '',
        roleCode: '',
        roleType: '1',
        remarks: ''
      }
    },
    // 添加的确定（调用接口）
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          addRole(this.temp).then((res) => {
            this.dialogFormVisible = false
            this.$message.success(res.header.msg)
            this.$emit('getList')
          })
        }
      })
    },
    // 点击编辑确定（调接口）
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          updateRole(tempData).then(() => {
            this.dialogFormVisible = false
            this.$message({
              message: '更新成功',
              type: 'success'
            })
            this.$emit('getList')
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
