<template>
  <div style="padding: 0px 100px;" class="flex">
    <div class="btn-group auto" style="margin-bottom: 30px">
      <button type="button" class="button_clear button_spacing" style="width: 70px" v-if="hasbtn('批量删除')"@click="removeData(ids)">批量删除</button>
    </div>
    <div class="datapanel">
      <h3 class="txtline">&nbsp;&nbsp;【{{data.name}}】部门管理</h3>
      <data-grid class="list_area table_sy"  partial='list' v-ref:grid :model="model">
        <template partial='head'>
          <tr>
            <th>全选
              <input type="checkbox" class="all-checked" v-model="checked" @click="$parent.$parent.isAll(checked)">
            </th>
            <th></th>
            <th>员工编号</th>
            <th>员工名称</th>
          </tr>
        </template>
        <template partial='body'>
          <tr>
            <td style="text-align: center;">
              <input type="checkbox"  v-model="$parent.$parent.ids" :value="row">
            </td>
            <td style="text-align: center;"><img :src="$parent.$parent.imgsrc(row.f_icon)" alt=""></td>
            <td style="text-align: center;">{{row.userid}}</td>
            <td style="text-align: center;">{{row.name}}</td>
          </tr>
        </template>
      </data-grid>
    </div>
    <modal :show.sync="showFile" v-ref:modal backdrop="true">
      <header slot="modal-header" class="modal-header">
        <button type="button" class="close" @click="closeFile"><span>&times;</span></button>
        <h4 class="modal-title">选择文件</h4>
      </header>
      <article slot="modal-body" class="modal-body">
        <div class="form-group">
          <file-upload class="my-file-uploader" name="userUploadFile" action="rs/file/uploadFile" tagname="确定" multiple v-ref:file></file-upload>
        </div>
      </article>
      <footer slot="modal-footer" class="modal-footer"></footer>
    </modal>
  </div>
</template>

<script>
  import { PagedList } from 'vue-client'
  import co from 'co'
  import * as Util from '../Util'
  import * as service from '../../service/LicenseService'

  let gen = function * (self) {
    //self.model = yield self.$post('rs/search', {source: `tool.getChildren($${self.data.id}$)`, userid: self.userid},{resolveMsg: null, rejectMsg: null})
    self.model = yield self.$http.post('rs/search',{source: `tool.getChildren($${self.data.id}$).where(true)`, userid: self.userid})
    if(self.data.id == "332240") {
      self.batchImport = true
    }
    // console.log(self.model)
  }
  let deleteData = function * (self,data) {
    let msg = yield self.$http.post('rs/data/delete',{data: data})
    //yield self.$post('rs/data/delete', {data: data},{resolveMsg:null, rejectMsg:null})
    yield self.$showMessage(msg.bodyText)
    self.ids = []
    self.refresh()
    self.flush()

  }
  export default {
    title: '部门',
    props: ['data', 'userid'],
    ready() {
      this.$parent.setTitle(this.data.name)
      this.refresh()
    },
    data() {
      return {
        ids: [], // 处理全选和部分选择使用
        model: '',

        //文件上传弹框
        showFile: false,
        batchImport: false
      }
    },
    methods: {
      flush(){
        this.$parent.$parent.$parent.$parent.flush()
      },
      moveres(name) {
        this.$goto(name, {data: this.data}, 'self', this.isRefresh)
      },
      // 刷新数据
      refresh () {
        let g = gen(this)
        co(g)
      },
      add(name) {
        console.log(name)
        let parent = this.data
        delete parent.children
        this.$goto(name, {parent: parent}, 'self', this.isRefresh)
      },
      // 批量导入
      importUser(){
        this.showFile = true
      },
      hasbtn (val) {
        if(Util.f.ldapReslist&&Util.f.ldapReslist.includes(val)){
          return true
        }else {
          return false
        }
      },
      // 关闭文件上传对话框
      closeFile() {
        this.showFile = false
        // 将选的文件清空
        this.$refs.file.$el.querySelector('input').value = ''
      },
      isAll (checked) {
        this.ids = []
        if (!checked) {
          this.model.data.forEach((row) => {
            this.ids.push(row)
          })
        }
      },
      imgsrc (name) {
        return Util.getImgsrc(name)
      },
      removeData (data) {
        var removeName = ''
        data.forEach((item) => {
          removeName = removeName + item.name + ';'
        })
        if(!removeName){
          this.$showMessage("请选择需要删除项！")
        } else {
          this.$showMessage('确定要删除【'+removeName+'】', ['confirm', 'cancel']).then((res) => {
            if (res === 'confirm') {
              let d = deleteData(this,data)
              co(d)
            }
          })
        }

      },
      isRefresh (param) {
        if (param === 'refresh') {
          this.refresh()
        }
      }
    },
    events: {
      'onFileUpload': async  function(file, res){
        console.log("上传完毕"+JSON.stringify(res))
        let data = {
          filepath: res.f_downloadpath,
          parents : this.data
        }
        console.log("filepath+parents="+JSON.stringify(data))
        await this.$resetpost(`rs/logic/userBatchImport`,data, {resolveMsg: null, rejectMsg: '导入失败', silent: true}, 0).then((result)=>{
          let msg = ""
          if(parseInt(result.data.fail) > 0) {
            msg = "失败原因："+ result.data.msg
          }
          this.$showAlert("成功导入"+result.data.success+"条,失败导入"+result.data.fail+"条用户信息"+msg, 'success', 2000)
          //关闭上传框
          this.closeFile()
          //重新查询
          this.init()
        })
      },
      'onFileError'(error) {
        this.$showAlert(error, 'warning', 2000)
      }
    }
  }
</script>
<style>
  .all-checked {
    vertical-align: text-top;
    margin-left: 10px !important;
  }
  td > .glyphicon {
    cursor: pointer;
  }
  th > input[type="checkbox"], td > input[type="checkbox"] {
    cursor: pointer;
  }
</style>
