<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">登&ensp;录&ensp;名</label>
              <input type="text" class="input_search" style="width:60%" v-model="model.ename"
                     condition="ename like '%{}%'" placeholder='登录名'>
            </div>
            <div class="col-sm-3 form-group" v-show="!$parent.$parent.$parent.row">
              <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_search button_spacing" v-if="$parent.$parent.hasbtn('新建人员')" @click="$parent.$parent.add()">新建人员</button>
              <button class="button_search button_spacing" v-if="$parent.$parent.hasbtn('新建人员')" @click="$parent.$parent.fileadd()">批量新增</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" v-show="$parent.$parent.$parent.row">
              <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-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>
              <!--<input type="text" class="input_search" style="width:60%" v-model="model.f_role_name"-->
              <!--condition="organizationname like '%{}%'" placeholder='所属公司'>-->
            </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>
              <!--<input type="text" class="input_search" style="width:60%" v-model="model.f_department_name"-->
                     <!--condition="f_department_name like '%{}%'" placeholder='所在部门'>-->
            </div>
            <div class="col-sm-3 form-group">
              <label  class="font_normal_body">所属角色</label>
              <v-select :options='$parent.$parent.roleoptiones' :value.sync="model.f_role_name"
                        close-on-select condition="roles like '%{}%'"
                        placeholder='所属角色' v-model="model.f_role_name"></v-select>
              <!--<input type="text" class="input_search" style="width:60%" v-model="model.f_role_name"-->
                     <!--condition="f_role_name like '%{}%'" placeholder='所属角色'>-->
            </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 class="col-sm-3 form-group">
              <label  class="font_normal_body" >登录状态</label>
              <v-select :value.sync="model.online"
                        :options='$parent.$parent.loginstate'
                        :value-single="true"
                        placeholder='登录状态'
                        condition="online = '{}'"
                        close-on-select
                        v-model='model.online'></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;"><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.f_user_telephone}}</td>
          <td style="text-align: center;">{{row.f_zone_name}}</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.f_role_name}}</td>
          <td style="text-align: center;">{{row.state}}</td>
          <td style="text-align: center;">{{row.online == 1? '在线':'离线'}}</td>
          <td style="text-align: center;">
            <button-link type="button" class="btn btn-link" @click.stop="$parent.$parent.$parent.deleteDep(row)">删除</button-link>
            <button-link type="button" class="btn btn-link" @click.stop="$parent.$parent.$parent.modifyRes(row)">修改</button-link>
          </td>
          <td>
            <button-link v-show="row.online == 1" type="button" class="btn btn-link" @click.stop="$parent.$parent.$parent.forceLoginout(row)">强制下线</button-link>
          </td>
        </template>
      </data-grid>
    </criteria-paged>
    <table class="table-hover">
      <tr class="table-bordered" style="position: relative">
        <td style="display: inline-block;width: auto;border-right: 1px solid #F2F6FA;padding: 0px 10px 0px 10px">
          当前在线用户:&emsp;{{sumsmodel.online}}/{{sumsmodel.count}}
        </td>
      </tr>
    </table>
  </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">
    <staff-compile-list  v-if="editshow" :data="row"></staff-compile-list>
  </article>
  <footer slot="modal-footer" class="modal-footer">
  </footer>
</modal>
  <modal :optional-width="200" :show.sync="fileadddshow"  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">
      <file-staff-compile-list  v-if="fileadddshow" :data="row" v-ref:fileadd></file-staff-compile-list>
    </article>
    <footer slot="modal-footer" class="modal-footer">
    </footer>
  </modal>
</template>
<script>
  import { PagedList } from 'vue-client'
  import Util from '../Util'
  export default {
    title: '用户列表',
    data () {
      return {
        userStates: [{label:'全部', value: ''},{label:'在职', value: '在职'}, {label:'离职', value: '离职'}],
        loginstate: [{label:'全部', value: ''},{label:'在线', value: '1'}, {label:'离线', value: '0'}],
        model: new PagedList('rs/sql/ldap_getAllUser', 20,{},{online:0,count:0}),
        row: null,
        //控制修改按钮
        editshow:false,
        rowparent: null,
        criteriaShow:true,
        fileadddshow:false, //多个人员新建控制
        orgoptions:[],
        depoptiones:[],
        roleoptiones:[],
        allrole:[],
        sumsmodel:{},
      }
    },
    async ready () {
      await this.getAllRole()
      await this.search()
      this.sumsmodel = this.$refs.paged.$refs.grid.model.sums;
    },
    methods: {
      async getAllRole(){
        let sql = {
          sql:'select * from t_role'
        };
        let req = await this.$resetpost('rs/logic/commonquery',{data:sql},{resolveMsg: null, rejectMsg: null});
        this.allrole = req.data
        if (this.roleoptiones.length===0){
          this.roleoptiones = this.allrole.map(item=>{
            return {label:`${item.f_role_id}-${item.name}`,value:item.id}
          })
        }
      },
      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();
            })
          }
        })
      },
      fileadd(){//多个人员新建
        this.$parent.showselect=true
        this.fileadddshow = true;
      },
      hidden() {
        this.criteriaShow = !this.criteriaShow
      },
      cancel  () {
        this.row = null
        this.rowparent = null
        this.$parent.showselect=false
        this.editshow=false
        this.fileadddshow = false;
        this.$refs.fileadd.uploadfile = ''
      },
      //添加部门
      add(){
        this.$refs.paged.$refs.grid.selectStore.selected = null
        this.$parent.showselect=true
        this.editshow=true
      },
      //删除员工
      async deleteDep(row) {
        let res = await this.$showMessage(`确认要删除员工: ${row.name} 吗？`, ['confirm', 'cancel'])
        if (res === 'confirm') {
          let param = []
          param.push(row)
          try {
            // 删除权限
            await this.$resetpost('rs/data/delete', {data: param}, {resolveMsg: '删除成功！！', rejectMsg: '删除出错'})
          } catch (error) {
            this.$showAlert(error.data, 'danger', 0)
          }
          this.$dispatch('refresh')
        }
      },
      //修改资源
      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.sumsmodel = this.$refs.paged.$refs.grid.model.sums;
        this.$dispatch('search')
      },
      async hasbtn(val) {
        if (Util.f.ldapReslist && Util.f.ldapReslist.includes(val)) {
          return true
        } else {
          await Util.load()
          return Util.f.ldapReslist && Util.f.ldapReslist.includes(val);
        }
      },
    },
    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.fileadddshow = false;
        this.$showAlert('操作成功', 'success', 2000)
        this.search()
      },
      //返回
      'cancel' () {
        this.row = null
        this.rowparent = null
        this.editshow=false
        this.$parent.showselect=false
        this.fileadddshow = false;
        this.$refs.fileadd.uploadfile = ''
        this.$dispatch('cancel')
      },
    },
  }
</script>
