<template>
  <div class="auto select-overspread" id="rolesTree">
    <form class="datapanel form-horizontal" style="min-height: 400px">
      <h3 class="txtline">&nbsp;&nbsp;用户信息</h3>
      <h3 class="txtline">&nbsp;&nbsp;上一级【{{parentname}}】</h3>
      <div class="row">
        <div class="col-sm-3"></div>
        <div class="col-sm-3 form-group form-input-group">
          <label class="font_normal_body">姓名：</label>
          <input type="text" required="required" class="input_search" style="width:60%"
                 placeholder="请输入姓名" v-model="model.name">
        </div>
        <div class="col-sm-3 form-group form-input-group">
          <label class="font_normal_body">登录名：</label>
          <input type="text" required="required" class="input_search" style="width:60%"
                 placeholder="请输入登录名" v-model="model.ename">
        </div>
      </div>
      <div class="row">
        <div class="col-sm-3"></div>
        <div class="col-sm-3 form-group form-input-group">
          <label class="font_normal_body">电话号码：</label>
          <input type="text" required class="input_search" style="width:60%"
                 placeholder="请输入电话号码" v-model="model.f_user_telephone">
        </div>
        <div class="col-sm-3 form-group form-input-group">
          <label class="font_normal_body">所属角色：</label>
          <v-select :value.sync="model.rolelist"
                    :multiple="true"
                    :options='initrolslist' placeholder='请选择用户角色'
          ></v-select>
        </div>
      </div>
      <!-- 设置用户角色结束 -->
      <button @click.stop.prevent="confirm()" class="btn btn-success col-sm-1 col-sm-offset-4" :disabled="!$v.valid">
        <span class="glyphicon glyphicon-floppy-disk"></span>保存
      </button>
      <button @click='$back()' class="btn btn-default col-sm-1 col-sm-offset-1">
        <span class=" glyphicon glyphicon-log-out"></span>返回
      </button>
      <button @click.stop.prevent="initpassword()" class="btn btn-warning col-sm-1 col-sm-offset-1" :disabled="!$v.valid">
        <span class="glyphiconglyphicon-alert"></span>初始化密码
      </button>
    </form>
  </div>
</template>

<script>
import co from 'co'
// 获取上级组织有权限角色
let getRoles = function * (self) {
  let ret = yield self.$http.post('rs/data/getRoles',{parentid:self.parent.id,loginid:self.$login.f.id})
  console.log('roles====>',ret)
  let rolearry=[]
  ret.data.forEach((item) => {
    let  json={label: item.name, value: {id:item.id}}
    rolearry.push(json)
  })
  self.initrolslist = rolearry
  let arry1=[]
  if(self.model.roles&&self.model.roles.length>0){
    self.initrolslist.forEach((item) => {
      if(self.model.roles.indexOf(item.value.id)>-1){
        arry1.push(item.value)
      }
    })
  }
  self.model.rolelist=arry1
}

let saveGen = async function (self) {
  self.deliver.f_userid = self.model.f_userid
  self.deliver.lastinputdate = self.model.lastinputdate
  if (typeof self.model.f_bankname === 'object' ) {
    if (self.model.f_bankname.length === 0) {
      self.model.f_bankname = ''
    } else {
      self.model.f_bankname = self.model.f_bankname[0]
    }
  }
  self.$http.post('rs/data/save', {data: self.model}).then((msg)=>{
    if(Number.isNaN(Number(msg.bodyText))){
      self.$showMessage(msg.bodyText)
    }else {
      self.$showMessage("添加成功")
    }
    self.refersh()
    self.$back('refresh')
  })
}
let passwodGen = function * (self) {
  var json={
    password : self.model.password ,
    newpassword : 1? '000000':self.$login.f.initpassword,
    affirmpassword : 1? '000000':self.$login.f.initpassword,
    ename : self.model.ename
  }
  let res = self.$resetpost('/rs/user/modifypwd', {data: json })
  console.log(res)
  self.$back('refresh')
}
export default {
  name:'user',
  title: '用户信息',
  props: ['parent', 'data'],
  data () {
    return {
      initrolslist:[],
      model: {
        loginid:this.$login.f.id,
        parentid: '',
        id: '',
        name: '',
        ename: '',
        touxiang: '',
        f_fengongsi: '',
        f_fengongsinum: '',
        f_fapiaotaitu: '',
        rolestr:'',
        f_fgsshuihao: '',
        f_fgsdizhi: '',
        f_fgsdianhua: '',
        gonghao: '',
        f_invoicenum: '',
        operpage: '',
        f_bankname: '',
        maxgassellnum: '',
        myxgmax: '',
        hemax: '',
        gyxgmax: '',
        f_description: '',
        rolelist: [],
        f_user_telephone: '',
        resourcetype: 'user'
      },
      deliver: {}, // 业务逻辑需要的参数
      img: '',
      parentname: ''
    }
  },
  methods: {
    //获取非功能弹窗
    async getRoleList() {
      let getRoleco = getRoles(this)
      co(getRoleco)
    },
    confirm () {
      if (this.model.name.replace(/\s*/g,"").length==0) {
        this.$showMessage('姓名不能为空')
      } else if (this.model.ename.replace(/\s*/g,"").length==0) {
        this.$showMessage('登录名不能为空')
      } else {
        if(this.model.f_user_telephone.length != 0){
          if(!(/^1[3|4|5|7|8][0-9]\d{8,11}$/.test(this.model.f_user_telephone))){
            this.$showMessage("联系方式输入有误，请核实后更正")
          } else {
            saveGen(this)
          }
        } else {
          saveGen(this)
        }
      }
      //this.$parent.$parent.$parent.$parent.flush()
    },
    refersh(){
      this.$parent.$parent.$parent.$parent.flush()
    },
    initpassword () {
      let gen = passwodGen(this)
      return co(gen)
    },
  },
  computed: {
    selects () {
      return [{label: '请选择', value: ''}, ...this.$appdata.getParam('银行名称')]
      // [{label: '', value: ''},
      //    {label: '湖北和芯科技', value: '湖北和芯科技'},
      //    {label: '奥枫科技', value: '奥枫科技'}
      // ]
    },
  },
  ready () {
    //准备默认数据
    // 将要修改的值设置默认值
    if (this.parent) {
      this.getRoleList()
      this.model.parentid = this.parent.id
      this.parentname = this.parent.name
      this.model = Object.assign({}, this.model, this.data)
    } else {
      console.log('!this.parent======>',this.data)
      this.parentname = this.data.parentname
      this.$resetpost('rs/data/getRoles',{parentid:this.data.parentid,loginid:this.$login.f.id},{resolveMsg:'',rejectMsg:''}).then(ret => {
        console.log('roles====>',ret)
        let rolearry=[]
        ret.data.forEach((item) => {
          let  json={label: item.name, value: {id:item.id}}
          rolearry.push(json)
        })
        this.initrolslist = rolearry
        let arry1=[]
        if(this.model.roles&&this.model.roles.length>0){
          this.initrolslist.forEach((item) => {
            if(this.model.roles.indexOf(item.value.id)>-1){
              arry1.push(item.value)
            }
          })
        }
        this.model.rolelist=arry1
      })
      this.model= Object.assign({}, this.model, this.data)

    }
  }
}
</script>
<style>
  #rolesTree span {
    color: #000;
  }
  #rolesTree .list-group-item {
    background-color: #fff;
    border: none;
  }
  #rolesTree .list-group-item.active,
  #rolesTree  .list-group-item.active:hover {
    width: fit-content;
  }
  #rolesTree  .list-group-item.active:focus {
    background-color: #fff;
  }
</style>
