<template>
  <div class="auto select-overspread">
    <validator name='v'>
      <ul class="nav nav-tabs">
        <p class="bg-info text-center" style="padding: 8px;">员工信息</p>
      </ul>
      <div class="form-horizontal select-overspread container-fluid auto" style="margin-top:20px;flex: 1;">
        <div class="row">
          <div class="col-sm-6 form-group">
            <label class="font_normal_body" >员工编号</label>
            <input type="text"  v-model="model.userid"
                   disabled="true"
                   class="input_search" style="width:45%" placeholder="员工编号" v-next-el="name">
          </div>
          <div class="col-sm-6 form-group">
            <label class="font_normal_body" >员工姓名</label>
            <input type="text"  v-model="model.name"
                   disabled="true"
                   class="input_search" style="width:60%" placeholder="员工姓名" v-next-el="name">
          </div>
          <div class="col-sm-6 form-group" :class="[$v.f_department_name.required ? 'has-error' : '']">
            <label  class="font_normal_body" >所属部门</label>
            <input type="text" v-model="model.f_department_name"
                   v-validate:f_department_name="{required: true }"
                   class="input_search" style="width:45%" placeholder="所属部门" readonly >
            <button type="button" class="btn button_new btn-lg"
                    style="width:13%"
                    @click="openOrgModal">
              <span class="glyphicon glyphicon-search" ></span>
            </button>
          </div>
          <div class="col-sm-12 form-group" :class="[$v.f_reason.required ? 'has-error' : '']">
            <label  class="font_normal_body">调整原因</label>
            <textarea class="ver-textarea" style="width:80%"
                      v-validate:f_reason="{required: true }"
                      rows="2" v-model="data.f_reason" placeholder="调整原因"></textarea>
          </div><div class="col-sm-12 form-group">
            <label  class="font_normal_body">&nbsp;&nbsp;&nbsp;备注&nbsp;&nbsp;&nbsp;</label>
            <textarea class="ver-textarea" style="width:80%"
                      rows="2" v-model="data.f_comments" placeholder="备注"></textarea>
          </div>
        </div>
      </div>
      <div class="row">
        <div style="float:right;">
          <button @click="confirm()" class="btn button_search "
                  style="width: 70px" :disabled='!$v.valid'>
            <span class="glyphicon glyphicon-floppy-disk"></span>保存
          </button>
          <button @click='close()' class="btn button_clear"style="width: 70px">
            <span class=" glyphicon glyphicon-log-out"></span>取消
          </button>

        </div>
      </div>
    </validator>
    <department-modal :show.sync="orgModalShow" :data="data" @depart="getDepart"></department-modal>
  </div>
</template>
<script>
import co from 'co'
export default {
  title: '人员部门调整',
  data () {
    return {
      // 组织树模态框
      orgModalShow: false,
      // 权限
      depPermissionShow: false,
      depPermissions: [],
      disabled: true,
      model: {}
    }
  },
  props: ['data'],
  ready () {
    this.init()
  },
  watch: {
    'data' (val) {
      this.init()
    }
  },
  events: {
  },
  methods: {
    toStandardTimeString () {
      let dt = new Date()
      let month = dt.getMonth() + 1
      let date = dt.getDate()
      let hour = dt.getHours()
      let min = dt.getMinutes()
      let sec = dt.getSeconds()
      return dt.getFullYear() + '-' + (month < 10 ? '0' + month : month) + '-' + (date < 10 ? '0' + date : date) +
        ' ' + (hour < 10 ? '0' + hour : hour) + ':' + (min < 10 ? '0' + min : min) + ':' + (sec < 10 ? '0' + sec : sec)
    },
    async confirm () {
      // 保存用户
      try {
        let res = await this.$resetpost('rs/data/save', {data: this.model})
        // 验证是否包含数据权限，
        if (this.depPermissions.length>0) {
          console.log('保存用户。。', res)
          // 保存部门权限
          let userId = res.data + ''
          // 组织角色权限数据
          let param = {
            parentid: userId,
            loginid: this.$login.f.id,
            licenses:[]
          }
          this.depPermissions.forEach((item) => {
            item.parentid = userId
            item.resourceid = userId
            param.licenses.push(item)
          })
          await this.$resetpost('rs/license/save', param, {resolveMsg: '保存成功', rejectMsg: '保存权限失败！请您重试！！'})
        }
        let data = {
          f_old_depid: this.data.parentid,
          f_old_depname: this.data.f_department_name,
          f_reason: this.data.f_reason,
          f_comments: this.data.f_comments,
          model: this.model,
          f_operate_id: this.$login.f.id,
          f_operate: this.$login.f.name,
          f_operate_date: this.toStandardTimeString()
        }
        await this.$resetpost('rs/logic/ldap_userDepChange', {data: data}, {resolveMsg: '保存成功', rejectMsg: '保存权限失败！请您重试！！'})
        this.$showAlert('修改部门成功', 'success', 2000)
        this.$dispatch('success')
      } catch (error) {
        error = error.data ? error.data : error;
        this.$showAlert(`修改部门失败！！错误信息：${error}，请刷新后重试`, 'danger', 0)
        this.$dispatch('cancel')
      }
    },
    init() {
      this.model = Object.assign({}, this.model, this.data)
    },
    close () {
      this.$dispatch('cancel')
    },
    openOrgModal () {
      console.log('打开部门模态框')
      this.orgModalShow = true
    },
    async getDepart (val) {
      console.log('获取到所属部门', val)
      this.model.f_department_name = val.name
      this.model.parentid = val.id
      this.model.f_department_id = val.id
      let orgData  = await this.$resetpost('rs/search', {source: `this.getParentByType($organization$)`, userid: val.id}, {resolveMsg: null, rejectMsg: '获取分公司id错误'})
      this.model.f_organization_id = orgData.data && orgData.data.id ? orgData.data.id : ""
    }
  }
}

</script>
<style lang="less">
#img_self {
  div {
    height: auto;
  }
}
</style>
