<!--
 * @Date: 2021-01-05 13:40:58
 * @LastEditors: zwq
 * @LastEditTime: 2021-07-21 15:56:13
 * @FilePath: \basic-admin\src\views\RoleManager\AddForm.vue
 * @Description:
-->
<template>
  <div>
    <el-dialog v-bind="$attrs" :title="'formItem.role.add' | i18nFilter" v-on="$listeners" @open="onOpen" @close="onClose">
      <el-form ref="formData" :model="formData" :rules="rules" size="medium" label-width="100px">
        <el-form-item :label="$t('roleManage.roleCode')" prop="code">
          <el-input v-model="formData.code" :placeholder="['placeholder.input', $t('roleManage.roleCode')] | i18nFilterForm" clearable :style="{width: '100%'}" />
        </el-form-item>
        <el-form-item :label="$t('roleManage.roleName')" prop="name">
          <el-input v-model="formData.name" :placeholder="['placeholder.input', $t('roleManage.roleName')] | i18nFilterForm" clearable :style="{width: '100%'}" />
        </el-form-item>
        <el-form-item :label="$t('roleManage.roleType')" prop="category">
          <el-select v-model="formData.category">
            <el-option
              v-for="item in categoryOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('roleManage.remark')" prop="remark">
          <el-input v-model="formData.remark" :placeholder="['placeholder.input', $t('roleManage.remark')] | i18nFilterForm" clearable :style="{width: '100%'}" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="close">{{ 'btn.cancel' | i18nFilter }}</el-button>
        <el-button type="primary" @click="handelConfirm">{{ 'btn.confirm' | i18nFilter }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { addRole, RoleCode } from '@/api/role'
export default {
  components: {},
  inheritAttrs: false,
  props: [],
  data() {
    return {
      formData: {
        code: undefined,
        name: undefined,
        remark: undefined,
        category: 1
      },
      categoryOptions: [{
        value: 1,
        label: 'pc'
      }, {
        value: 5,
        label: 'pda'
      }],
      rules: {
        code: [{
          required: true,
          message: this.$i18nForm(['placeholder.input', this.$t('roleManage.roleCode')]),
          trigger: 'blur'
        }],
        name: [{
          required: true,
          message: this.$i18nForm(['placeholder.input', this.$t('roleManage.roleName')]),
          trigger: 'blur'
        }],
        category: [{
          required: true,
          message: this.$i18nForm(['placeholder.input', this.$t('roleManage.roleType')]),
          trigger: 'change'
        }]
      }
    }
  },
  computed: {},
  watch: {},
  created() {
  },
  mounted() {},
  methods: {
    onOpen() {
      this.$nextTick(() => {
        this.$refs['formData'].resetFields()
        RoleCode().then(res => {
          this.formData.code = res.data
        })
      })
    },
    onClose() {
      this.$refs['formData'].resetFields()
    },
    close() {
      this.$emit('update:visible', false)
    },
    handelConfirm() {
      this.$refs['formData'].validate(async valid => {
        if (!valid) return
        const result = await addRole(this.formData)
        if (result.code === 0) {
          this.$message({
            type: 'success',
            message: '添加成功！'
          })
          this.$emit('done')
          this.close()
        }
      })
    }
  }
}

</script>
<style>
</style>
