<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-2 form-group button-range">
              <button class="button_search button_spacing" @click="$parent.$parent.search()" >查询</button>
              <button class="button_new button_spacing" v-if="$parent.$parent.hasbtn('新建人员')" @click="$parent.$parent.add()">新建人员</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="criteriaShow = !criteriaShow"></div>
            </div>
          </div>
          <div class="row" v-show="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>
              <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>
              <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" >状&emsp;&emsp;态</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="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;">操作</th>
          </tr>
        </template>
        <template partial='body'>
          <td style="text-align: center;">{{$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.f_org_name}}</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;">
            <button-link type="button" class="btn btn-link" @click="$parent.$parent.$parent.deleteDep(row)">删除</button-link>
            <button-link type="button" class="btn btn-link" @click="$parent.$parent.$parent.modifyRes(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>&times;</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>
</template>
<script>
  import { PagedList } from 'vue-client'
  import Util from '../Util'
  export default {
    title: '用户列表',
    data () {
      return {
        userStates: [{label:'全部', value: ''},{label:'在职', value: '在职'}, {label:'离职', value: '离职'}],
        model: new PagedList('rs/sql/ldap_getAllUser', 20),
        row: null,
        //控制修改按钮
        editshow:false,
        rowparent: null,
      }
    },
    ready () {
      this.search()
    },
    methods: {
      cancel  () {
        this.row = null
        this.rowparent = null
        this.editshow=false
      },
      //添加部门
      add(){
        this.$refs.paged.$refs.grid.selectStore.selected = null
        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.editshow=true
      },
      search () {
        this.$refs.paged.$refs.cri.search()
      },
      async selfSearch (args) {
        let getHasPermissionUsers = await this.$LdapService.getHasNodeByType('user')
        args.condition += `${getHasPermissionUsers}`
        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
        }
      },

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