<template>
  <div>
    <el-button size="small" type="primary" icon="el-icon-plus" @click="onShow">新 建</el-button>
    <el-dialog
      title="创建用户"
      :visible.sync="drawer"
      width="864px"
      @close="onClose">
      <div>
        <el-row :gutter="50">
          <el-form :model="form" :rules="rules" ref="form"  size="mini" label-position="left">
            <el-col :span="12">
              <el-form-item label="手机号码" label-width="100px" prop="phonenumber">
                <el-input v-model="form.phonenumber" autocomplete="off" placeholder="输入手机号码"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="用户名称" label-width="100px" prop="name">
                <el-input  v-model="form.name" autocomplete="off" placeholder="输入用户名称"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="授权角色" label-width="100px" prop="roleids">
                <!-- <el-input v-model="form.sex" autocomplete="off" placeholder="输入性别"></el-input> -->
                <el-select v-model="form.roleids" style="width:100%" placeholder="请选择" multiple>
                  <el-option
                    v-for="item in options"
                    :key="item.index"
                    :label="item.rolename"
                    :value="item.roleid">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-form>
        </el-row>
      </div>
      <div class="dialog-footer">
        <el-button size="small" @click="drawer = false" class="normal-btn-width">取 消</el-button>
        <el-button size="small" type="warning" @click="onSubmit" class="normal-btn-width btn-warning">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

export default {
  data () {
    return {
      drawer:false,
      rules:{
        phonenumber: [
          { required: true, message: '请输入手机号码', trigger: 'blur' },
          { pattern:/^1[3-9]\d{9}$/, message: '请输入正确手机号码',trigger: 'blur' }
        ],
         name: [
          { required: true, message: '请输入用户名称', trigger: 'blur' },
        ],
        roleids: [
          { required: true, message: '请选择授权角色', trigger: 'change' }
        ],
      },
      form:{
        "userid": 0,
        "name": "",
        "phonenumber": "",
        "status": 1,
        "remarks":"",
        "roleids":[]
      },
      options:[]
    }
  },
  methods:{
    onShow () {
      this.drawer = true
      this.rolelist()
    },
    
    async rolelist () {
      let param = {
        "classname": "sysmanage.develop.optiontype.optiontype",
        "method": "optiontypeselect",
        "content": {
            "pageNumber": 1,
            "pageSize": 20,
            "typename": "role",
            "parameter": {
            }
        }
      }
      const res = await this.$api.requested(param)
      this.options = res.data
    },

    onSubmit () {
      this.$refs['form'].validate((valid) => {
        if (!valid) return false
        this.$api.requested({
          "classname": "webmanage.users.users",
          "method": "insertormodify_user",
          "content": this.form
        }).then(res=>{
          if (res.code === 1) {
            this.$notify({
              title: '成功',
              message: '账号添加成功',
              type: 'success'
            });
            this.drawer = false
            this.$emit('onSuccess')
          } else {
            this.$notify({
              title: '失败',
              message: res.data,
              type: 'error'
            });
          }
        })
      });
    },
    onClose () {
      this.$refs['form'].resetFields()
    }
  }
}

</script>
<style scoped>
.dialog-footer{
  margin-top:32px;
  text-align: center;
}
</style>