<template>
  <div class="flex" @keyup.enter="search">
    <criteria-paged :model="model" v-ref:paged>
      <criteria partial='criteria' @condition-changed='$parent.selfSearch' v-ref:cri>
        <div novalidate class="form-horizontal select-overspread container-fluid auto" partial>
          <div class="row">
            <div class="col-sm-3 form-group">
              <label  class="font_normal_body">员工姓名</label>
              <input type="text" class="input_search" style="width:60%" v-model="model.name"
                     condition="name like '%{}%'" placeholder='员工姓名'>
            </div>
            <div class="col-sm-3 form-group">
              <label  class="font_normal_body">员工编号</label>
              <input type="text" class="input_search" style="width:60%" v-model="model.userid"
                     condition="userid like '%{}%'" placeholder='员工编号'>
            </div>
            <div class="col-sm-2 form-group button-range" style="float: right">
              <button class="button_search button_spacing" @click="$parent.$parent.search()" >查询</button>
              <button class="button_clear button_spacing" @click="clear">清空</button>
              <div style="float: right" class="button_spacing" :class="{'button_shrink_top': criteriaShow,'button_shrink_bottom': !criteriaShow}" @click="$parent.$parent.hidden"></div>
            </div>
          </div>
          <div class="row" v-show="$parent.$parent.criteriaShow">
            <div class="col-sm-3 form-group">
              <label  class="font_normal_body">部门编号</label>
              <input type="text" class="input_search" style="width:60%" v-model="model.f_dep_id"
                     condition="f_dep_id like '%{}%'" placeholder='部门编号'>
            </div>
            <div class="col-sm-3 form-group">

              <label  class="font_normal_body">所属公司</label>
              <v-select :options='$parent.$parent.orgoptions' :value.sync="model.organizationname"
                        close-on-select condition="organizationname like '%{}%'"
                        @change="$parent.$parent.orgOnChange"
                        placeholder='所属公司' v-model="model.organizationname"></v-select>
            </div>
            <div class="col-sm-3 form-group">
              <label  class="font_normal_body">所在部门</label>
              <v-select :options='$parent.$parent.depoptiones' :value.sync="model.f_department_name"
                        close-on-select condition="f_department_name like '%{}%'"
                        placeholder='所在部门' v-model="model.f_department_name"></v-select>
            </div>
            <div class="col-sm-3 form-group">
              <label  class="font_normal_body" >用户状态</label>
              <v-select :value.sync="model.state"
                        :options='$parent.$parent.userStates'
                        :value-single="true"
                        placeholder='用户状态'
                        condition="state = '{}'"
                        close-on-select
                        v-model='model.state'></v-select>
            </div>
          </div>
        </div>
      </criteria>
      <data-grid class="list_area table_sy" :model.sync="$parent.model" partial='list' v-ref:grid>
        <template partial='head'>
          <tr>
            <th style="text-align: center;"><nobr>序号</nobr></th>
            <th style="text-align: center;"><nobr>员工编号</nobr></th>
            <th style="text-align: center;"><nobr>员工名称</nobr></th>
            <th style="text-align: center;"><nobr>登录名</nobr></th>
            <th style="text-align: center;"><nobr>所属公司</nobr></th>
            <th style="text-align: center;"><nobr>所属部门</nobr></th>
            <th style="text-align: center;"><nobr>状态</nobr></th>
            <th style="text-align: center;">操作</th>
            <th style="text-align: center;">调整记录</th>
          </tr>
        </template>
        <template partial='body'  >
          <td style="text-align: center;height:50px;">{{$index+1}}</td>
          <td style="text-align: center;">{{row.userid}}</td>
          <td style="text-align: center;">{{row.name}}</td>
          <td style="text-align: center;">{{row.ename}}</td>
          <td style="text-align: center;">{{row.organizationname}}</td>
          <td style="text-align: center;">{{row.f_department_name}}</td>
          <td style="text-align: center;">{{row.state}}</td>
          <td style="text-align: center;">
            <button-link type="button" class="btn btn-link" @click.stop="$parent.$parent.$parent.modifyRes(row)">修改部门</button-link>
          </td>
          <td style="text-align: center;">
            <button-link type="button" class="btn btn-link" @click.stop="$parent.$parent.$parent.showRecord(row)">调整记录</button-link>
          </td>
        </template>
      </data-grid>
    </criteria-paged>
  </div>
  <modal :show.sync="editshow"  v-ref:modal large backdrop="false">
    <header slot="modal-header" class="modal-header">
      <button type="button" class="close" @click="cancel"><span class="glyphicon glyphicon-remove"></span></button>
    </header>
    <article slot="modal-body" class="modal-body">
      <person-dep-change  v-if="editshow" :data="row"></person-dep-change>
    </article>
    <footer slot="modal-footer" class="modal-footer">
    </footer>
  </modal>
  <modal :show.sync="depChangeHistoryShow" v-ref:depChangeHistoryShow backdrop="false" class="depChangeHistoryShow">
    <header slot="modal-header" class="modal-header" style="text-align: center">
      <h4 class="modal-title">人员部门调整记录</h4>
    </header>
    <article slot="modal-body" class="modal-body">
      <data-grid class="list_area table_sy" :model="depChangeHistorys" partial='list'>
        <template partial='head'>
          <th><nobr>姓名</nobr></th>
          <th><nobr>原部门</nobr></th>
          <th><nobr>新部门</nobr></th>
          <th><nobr>操作员</nobr></th>
          <th><nobr>操作原因</nobr></th>
          <th><nobr>操作时间</nobr></th>
        </template>
        <template partial='body'>
          <td >{{row.f_user_name}}</td>
          <td><nobr>{{row.f_old_depname}}</nobr></td>
          <td><nobr>{{row.f_depname}}</nobr></td>
          <td><nobr>{{row.f_operate}}</nobr></td>
          <td><nobr>{{row.f_reason}}</nobr></td>
          <td><nobr>{{row.f_operate_date}}</nobr></td>
        </template>
      </data-grid>
    </article>
    <footer slot="modal-footer" class="modal-footer">
      <button type="button" class="btn btn-default" @click='depChangeHistoryShow = false'>关闭</button>
    </footer>
  </modal>
</template>
<script>
import { PagedList } from 'vue-client'
import Util from '../../components/Util'
import PersonDepChange from './personDepChange'
export default {
  components: {PersonDepChange},
  title: '用户列表',
  data () {
    return {
      userStates: [{label:'全部', value: ''},{label:'在职', value: '在职'}, {label:'离职', value: '离职'}],
      model: new PagedList('rs/sql/ldap_getAllUser', 20,{},{online:0,count:0}),
      row: null,
      //控制修改按钮
      editshow:false,
      rowparent: null,
      criteriaShow:true,
      orgoptions:[],
      depoptiones:[],
      depChangeHistoryShow: false,
      depChangeHistorys: {rows:[]}
    }
  },
  async ready () {
    await this.search()
  },
  methods: {
    async orgOnChange(newVal){
      let getHasPermissionUsers = await this.$LdapService.getHasNodeByType('user',newVal[0])
      this.orgoptions =getHasPermissionUsers.orgoptions
      this.depoptiones = getHasPermissionUsers.depoptions
      this.roleoptiones = this.allrole.filter(item=>item.oname === newVal[0]).map(item=>{
        return {label:`${item.f_role_id}-${item.name}`,value:item.id}
      })
    },
    forceLoginout(row){
      //强制下线
      this.$showMessage(`是否强制${row.name}下线?`,['confirm','cancel']).then(res=> {
        if (res === 'confirm') {
          this.$resetpost(`rs/user/loginout/${row.id}`,{},{rejectMsg:"强制下线失败！",resolveMsg:'强制下线成功！'}).then(res=>{
            this.search();
          })
        }
      })
    },
    async showRecord(row){
      this.depChangeHistorys.rows = []
      let data = {
        condition: `f_user_id = ${row.tusid}`
      }
      let res = await this.$resetpost('rs/sql/ldap_getDepChangeRecord', {data: data}, {rejectMsg: '获取调整记录出错', resolveMsg: null})
      if (res){
        this.depChangeHistorys.rows = res.data
      }
      this.depChangeHistoryShow =true
    },
    hidden() {
      this.criteriaShow = !this.criteriaShow
    },
    cancel  () {
      this.row = null
      this.rowparent = null
      this.$parent.showselect=false
      this.editshow=false
      this.$refs.fileadd.uploadfile = ''
    },
    //修改资源
    modifyRes (obj) {
      delete obj.num
      delete obj.oname
      this.row = obj
      this.$parent.showselect=true
      this.editshow=true
    },
    search () {
      this.$refs.paged.$refs.cri.search()
    },
    async selfSearch (args) {
      let getHasPermissionUsers = await this.$LdapService.getHasNodeByType('user')
      this.orgoptions =getHasPermissionUsers.orgoptions
      this.depoptiones = getHasPermissionUsers.depoptions
      args.condition += `${getHasPermissionUsers.condition}`
      this.model.search(args.condition, args.model)
      this.$dispatch('search')
    },
    hasbtn (val) {
      if(Util.f.ldapReslist&&Util.f.ldapReslist.includes(val)){
        return true
      }else {
        return false
      }
    },

  },
  watch:{
  },
  computed: {
    getorgnames(){
      let ogr = [];
      if(this.model == null){
        return [];
      }
      return ogr;
    },

  },
  events: {
    //提交
    'success' () {
      this.row = null
      this.rowparent = null
      this.editshow=false
      this.$parent.showselect=false
      this.$showAlert('操作成功', 'success', 2000)
      this.search()
    },
    //返回
    'cancel' () {
      this.row = null
      this.rowparent = null
      this.editshow=false
      this.$parent.showselect=false
      this.$dispatch('cancel')
    },
  },
}
</script>

<style lang="less">
.depChangeHistoryShow {
  td {
    text-align: center !important;
  }
}
</style>
