<template>
  <div class="auto" >
    <validator name='v'>
      <ul class="nav nav-tabs">
        <p class="bg-info text-center" style="padding: 8px;">用户信息</p>
      </ul>
      <div class="row">
        <div class="col-sm-4">
          <label class="control-label">原属信息</label>
        </div>
        <div class="col-sm-8">
          <p class="form-control-static">{{modelmsg.fullnames}}</p>
        </div>
        <div class="col-sm-6">
          <div class="col-sm-4">
            <label class="control-label">所属部门</label>
          </div>
          <div class="col-sm-8">
            <p class="form-control-static">{{oldmodel.f_department_name}}</p>
          </div>
        </div>
        <div class="col-sm-6">
          <div class="col-sm-4">
            <label class="control-label">所属角色</label>
          </div>
          <div class="col-sm-8">
            <p class="form-control-static">{{oldmodel.f_role_name}}</p>
          </div>
        </div>
      </div>
      <ul class="nav nav-tabs">
        <p class="bg-info text-center" style="padding: 8px;">调配信息</p>
      </ul>
      <div class="row" style="padding-bottom: 100px">
        <div class="col-sm-6" :class="[$v.f_org_id.required ? 'has-error' : '']">
          <label class="font_normal_body" >所属组织</label>
          <v-select :options='orgoptions'
                    placeholder='请选择'
                    :value.sync="model.f_org_id"
                    search="true"
                    :value-single="true"
                    @change="orgChange"
                    close-on-select></v-select>
          <input type="text"  v-model="model.f_org_id"
                 v-show="false"
                 v-validate:f_org_id='{required: true }'
                 class="input_search" style="width:60%"
          >

        </div>
        <div class="col-sm-6":class="[$v.f_department_id.required ? 'has-error' : '']">
          <label class="font_normal_body" >所属部门</label>
          <v-select :options='depoptions'
                    placeholder='请选择'
                    :value.sync="model.f_department_id"
                    search="true"
                    :value-single="true"
                    @change="depChange"
                    close-on-select></v-select>
          <input type="text"  v-model="model.f_department_id"
                 v-show="false"
                 v-validate:f_department_id='{required: true }'
                 class="input_search" style="width:60%"
          >
        </div>
      </div>
      <button @click="confirm()" class="btn button_search " style="width: 70px"
              :disabled='!$v.valid'>
        <span class="glyphicon glyphicon-floppy-disk"></span>保存
      </button>
      <button @click='close()' class="btn button_clear"style="width: 70px">
        <span class=" glyphicon glyphicon-log-out" v-if="!depPermissionShow"></span>取消
      </button>
    </validator>
  </div>
</template>

<script>
  import {HttpResetClass} from 'vue-client'
  let getUserMsg = async function (self, obj) {
    // 获取当前登录人所在组织下的角色，
    let http = new HttpResetClass()
    let geChildrens = await http.load('POST', '/rs/search',
      {source:`tool.getResUser($${obj}$)`,
        userid:self.$login.f.id},
      {resolveMsg:null, rejectMsg: '获取父级资源出错！！'})
    if (geChildrens.data) {
      //开始赋值
      self.modelmsg = Object.assign({}, self.modelmsg, geChildrens.data)
    }
  }
  let getdepList = async function (self) {
    // 获取当前登录人所在组织下的角色，
    let http = new HttpResetClass()
    if(self.model.f_org_id&&self.model.f_org_id.length>0){
      let deplist = await http.load('POST', '/rs/search',
        {
          source:`this.getAllChildrens().where(row.getType()==$department$)`,
          userid:self.model.f_org_id
        },
        {resolveMsg:null, rejectMsg: '获取父级资源出错！！'})
      if (deplist.data&&deplist.data.length>0) {
        let arry=[]
        deplist.data.forEach((item) => {
          arry.push({label: item.name, value: item.id, id: item.id, data: item})
        })
        self.depoptions =  arry
      }
    }

  }
  let getorgList = async function (self) {
    // 获取当前登录人所在组织下的角色，
    let http = new HttpResetClass()
    let orglist = await http.load('POST', '/rs/search',
      {
        source: `this.getRights().where(row.getType()==$organization$)`,
        userid: self.$login.f.id
      },
      {resolveMsg:null, rejectMsg: '获取父级资源出错！！'})
    if (orglist.data&&orglist.data.length>0) {
      let arry=[]
      orglist.data.forEach((item) => {
        arry.push({label: item.name, value: item.id, id: item.id, data: item})
      })
      self.orgoptions =  arry
    }
  }
  export default {
    title: '资源信息',
    props: ['data'],
    async ready() {
      this.initData()
    },
    data () {
      return {
        //部门数据
        depoptions:[],
        //部门数据
        orgoptions:[],
        //给定资源
        model: {},
        oldmodel: {},
        modelmsg: {},
        //子级资源
        children: []
      }
    },
    watch: {
      'model.f_org_id' (val) {
        this.getDepdata()
      },
      'model.f_department_id' (val) {
        let dd=this.findbyid(this.depoptions,this.model.f_department_id)
        if(dd&&dd.name){
          this.model.f_department_name=dd.name
          this.model.parentid=this.model.f_department_id
        }
      }
      /* data: {
         handler(n, o) {
            this.initData()
         },
         deep: true //对象内部属性的监听，关键。
       },*/
    },
    methods: {
      //组织改变
      orgChange(val){
        this.model.f_org_id=val
      },
      //部门改变
      depChange(val){

      },
      async initData () {
        this.model = Object.assign({}, this.model, this.data)
        this.oldmodel = Object.assign({}, this.oldmodel, this.data)
        if(this.oldmodel.id&&this.oldmodel.id.length>0){
          //获取当前用户信息
          await getUserMsg(this, this.oldmodel.id)
          await getorgList(this)
        }
      },
      async getDepdata () {
        await getdepList(this)
      },
      //提交修改
      async confirm () {
        // 保存用户
        try {
          let res = await this.$resetpost('rs/data/save', {data: this.model})
          console.log('保存用户。。', res)
          // 保存部门权限
          let userId = res.data + ''
          // 组织角色权限数据
          let param = {
            parentid: userId,
            loginid: this.$login.f.id,
            licenses:[]
          }
          let orglicense = {}
          orglicense.isinherit = "是"
          orglicense.byisinherit = "否"
          orglicense.resourceresult = ""
          orglicense.parentid = userId
          orglicense.resourceid = userId
          orglicense.byresourceid = this.model.f_org_id
          orglicense.righttype = 'org'
          orglicense.byresourceresult = ''
          orglicense.licensepolicy = ''
          //将组织数据放入
          param.licenses.push(orglicense)
          let deplicense = {}
          deplicense.isinherit = "是"
          deplicense.byisinherit = "是"
          deplicense.resourceresult = ""
          deplicense.parentid = userId
          deplicense.resourceid = userId
          deplicense.byresourceid = this.model.f_department_id
          deplicense.righttype = 'org'
          deplicense.byresourceresult = ''
          deplicense.licensepolicy = ''
          //将部门数据放入
          param.licenses.push(deplicense)
          await this.$resetpost('rs/license/save', param, {resolveMsg: '保存成功', rejectMsg: '保存权限失败！请您重试！！'})
          this.$showAlert('保存成功', 'success', 2000)
        } catch (error) {
          this.$showAlert(`用户保存失败！！错误信息：${error}`, 'danger', 0)
        }
        this.$dispatch('refresh')
      },
      //根据名字找数据
      findbyid(list,name){
        var result
        list.forEach((row, n) => {
          if(name==row.id){
            result= row.data
          }
        })
        return result
      },
      close () {
        this.$dispatch('refresh')
      },
    },

  }

</script>
