<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>
              <v-select :options='$parent.$parent.orgoptions' :value.sync="model.oname"
                        close-on-select condition="oname like '%{}%'"
                        placeholder='所属公司' v-model="model.oname"></v-select>
              <!--<input  type="text" class="input_search" style="width:60%" v-model="model.oname"-->
                      <!--condition="oname 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.depoptiones' :value.sync="model.name"
                        close-on-select condition="name like '%{}%'"
                        placeholder='部门名称' v-model="model.name"></v-select>
              <!--<input  type="text" class="input_search" style="width:60%" v-model="model.name"-->
                      <!--condition="name like '%{}%'" placeholder="部门名称"/>-->
            </div>
            <div class="col-sm-2 form-group button-range">
              <button class="button_search button_spacing" @click="search()">查询</button>
              <button class="button_new button_spacing" v-if="$parent.$parent.hasbtn('新建部门')" @click="$parent.$parent.add()">新建部门</button>
            </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;">序号</th>
            <th style="text-align: center;">部门编号</th>
            <th style="text-align: center;">部门名称</th>
            <th style="text-align: center;">所属组织/大区</th>
            <th style="text-align: center;">部门人数</th>
            <th style="text-align: center;">操作</th>
          </tr>
        </template>
        <template partial='body'>
          <tr>
            <td style="text-align: center;">{{$index+1}}</td>
            <td style="text-align: center;">{{row.f_dep_id}}</td>
            <td style="text-align: center;">{{row.name}}</td>
            <td style="text-align: center;">{{row.oname}}</td>
            <td style="text-align: center;">{{row.num}}</td>
            <td style="text-align: center;">
              <button-link type="button" v-if="row.num==0" 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>
          </tr>
        </template>
        <template partial='foot'></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">
      <dep-compile-list  v-if="editshow" :data="row"></dep-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 {
    data () {
      return {
        model: new PagedList('rs/sql/ldap_getAllDep',20, {}),
        row: null,
        //控制修改按钮
        editshow:false,
        rowparent: null,
        orgoptions:[],
        depoptiones:[]
      }
    },
    ready(){
      this.search()
    },
    methods: {
      //修改资源
      modifyRes (obj) {
        delete obj.num
        this.row = obj
        this.editshow=true
      },
      async deleteDep (val) {
        let res = await this.$showMessage(`确认要删除部门: ${val.name} 吗？`, ['confirm', 'cancel'])
        if (res === 'confirm') {
          let param = []
          param.push(val)
          try {
            await this.$resetpost('rs/data/delete', {data: param}, {resolveMsg: '删除成功！！', rejectMsg: null})
          } catch (error) {
            this.$showAlert(error.data, 'danger', 0)
          }
          this.$dispatch('refresh')
        }
      },
      cancel  () {
        this.row = null
        this.rowparent = null
        this.editshow=false
      },
      search () {
        this.$refs.paged.$refs.cri.search()
      },
      async selfSearch (args) {
        let getHasPermissionDepts = await this.$LdapService.getHasNodeByType('department')
        args.condition += `${getHasPermissionDepts.condition}`
        this.orgoptions =getHasPermissionDepts.orgoptions
        this.depoptiones = getHasPermissionDepts.depoptions
        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
        }
      },
      //添加部门
      add(){
        this.$refs.paged.$refs.grid.selectStore.selected = null
        this.editshow=true
        //this.$dispatch('add')
      },
    },
    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>
<style>
  .datapanel {
    color: #333;
    background-color: white;
    box-shadow: darkgrey 0.5px 0.5px 0.5px 0.5px;
    padding: 10px 10px 10px 10px;
    border-radius: 15px;
  }
</style>
