<template>
  <div class="inline-16">
    <el-button size="small" type="text" @click="onShow">编 辑</el-button>
    <el-dialog
      title="编辑用户"
      :visible.sync="drawer"
      width="864px">
      <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">
                <el-input v-model="form.accountno" readonly autocomplete="off" placeholder="输入账号"></el-input>
              </el-form-item>
            </el-col>
            <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">
                <el-input v-model="form.usertypename" readonly 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 {
  props:['data'],
  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":[]
      },
      showTips:false,
      options:[]
    }
  },
  methods:{
    onShow () {
      this.drawer = true
      this.form = Object.assign({},this.form,this.data)
      let arr = []
      this.form.userrole.forEach(e=>{
        arr.push(e.roleid)
      })
      this.form.roleids = arr
      this.rolelist()
      console.log(this.data,this.form)
    },
    
    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":{
            "userid": this.form.userid,
            "name": this.form.name,
            "phonenumber": this.form.phonenumber,
            "remarks":this.form.remarks,
            "roleids":this.form.roleids
          }
        }).then(res=>{
          this.tool.showMessage(res,()=>{
            let userid = JSON.parse(sessionStorage.getItem('account_list'))[0].userid
            let olddata = JSON.stringify((this.data.userrole.map(e=>{return e.roleid})).join(""))
            let newdata = JSON.stringify((this.form.roleids.map(e=>{return e})).join(""))
            if (olddata !== newdata && userid === this.form.userid) {
              this.$confirm('修改了当前账号的角色,是否重新登录?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
              }).then(() => {
                this.$router.push({path:'/'})
                sessionStorage.clear()
              }).catch(()=>{
                this.$emit('onSuccess')
                this.drawer = false
              })
            } else {
              this.$emit('onSuccess')
              this.drawer = false
            }
          })
        })
      });
    },
  }
}

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