<template>
  <div class="auto">
    <validator name='v'>
      <ul class="nav nav-tabs">
        <p class="bg-info text-center" style="padding: 8px;">部门信息</p>
        <!--<li class="active"><a href="#">部门信息</a></li>-->
      </ul>
      <form class="form-horizontal" style="margin-top:20px;flex: 1;">

          <div class="col-sm-6"  :class="[$v.f_dep_id.required ? 'has-error' : '']">
            <label class="font_normal_body" >部门编码</label>
            <input type="text"  v-model="model.f_dep_id" @blur="validateDul" v-validate:f_dep_id='{required: true }' class="input_search" style="width:60%" placeholder="部门编码" v-next-el="name">
          </div>

          <div class="col-sm-6"   :class="[$v.name.required ? 'has-error' : '']">
            <label for="f_adjustable_name" class="font_normal_body" >部门名称</label>
            <input type="text"  v-model="model.name"
                   v-validate:name="{required: true }" class="input_search" style="width:60%" placeholder="部门名称">
          </div>


          <div class="col-sm-6"  :class="[$v.oname.required ? 'has-error' : '']">
            <label for="f_area" class="font_normal_body" >所属组织</label>
            <input type="text" v-model="model.oname"
                   v-validate:oname="{required: true }" class="input_search" style="width:45%" placeholder="所属组织" readonly >
            <button type="button" class="btn button_new btn-lg" style="width:10%">
              <span class="glyphicon glyphicon-search" @click="openOrgModal"></span>
            </button>
          </div>
          <div class="col-sm-6"></div>
          <div class="col-sm-12">
            <label for="f_area" class="font_normal_body" >&nbsp;&nbsp;&nbsp;备注&nbsp;&nbsp;&nbsp;</label>
            <textarea class="ver-textarea" style="width:80%" rows="4" v-model="model.f_remark" placeholder="备注"></textarea>
          </div>

      </form>
      <div class="col-sm-12" style="text-align:right;height:auto;margin-top:6px;">
        <button class="button_search button_spacing" @click="confirm()" :disabled='!$v.valid'>确认</button>
        <button class="button_clear button_spacing" @click="deleteDep()" v-if="model.id">删除</button>
        <button class="button_clear button_spacing" @click="clear()">取消</button>
      </div>
    </validator>
    <organization-modal :show.sync="orgModalShow" @depart="getDepart"></organization-modal>
  </div>
</template>
<script>


  export default {
    data () {
      return {
        model:{
          loginid: this.$login.f.id,
          f_description: '',
          id: '',
          selfid: '',
          tablename: 't_department',
          entitytype: 't_department',
          position: '1',
          resourcetype: 'department',
          f_dep_id: '',
          f_remark: '',
          oname: '',
          name: '',
          parentid: ''
        },
        // 组织树模态框
        orgModalShow: false
      }
    },
    props: ['data'],
    ready () {
      this.model = Object.assign({}, this.model, this.data)
    },
    watch: {
      'data' (val) {
        if (val) {
          this.model = Object.assign({}, this.model, this.data)
        } else {
          this.initModel()
        }
      }
    },
    methods: {
      initModel () {
        this.model = {
          loginid: this.$login.f.id,
          f_description: '',
          id: '',
          selfid: '',
          tablename: 't_department',
          entitytype: 't_department',
          position: '1',
          resourcetype: 'department',
          f_dep_id: '',
          f_remark: '',
          oname: '',
          name: '',
          parentid: ''
        }
        this.rolePermissions = []
      },
      async confirm () {
        try {
          // 保存部门 资源服务的特殊保存程序，这里去掉除部门表外的其他字段
          delete this.model.onumber
          await this.$resetpost('rs/data/save', {data: this.model}, {resolveMsg: '保存部门成功', rejectMsg: '保存部门失败'})
          this.$dispatch('refresh')
        } catch (error) {
          this.$showAlert(error, 'danger', 0)
        }
      },
      openOrgModal () {
        this.orgModalShow = true
      },
      getDepart (val) {
        this.model.oname = val.name
        this.model.parentid = val.id
      },
      clear () {
        this.$dispatch('refresh')
      },
      async deleteDep () {
        let res = await this.$showMessage(`确认要删除部门: ${this.model.name} 吗？`, ['confirm', 'cancel'])
        if (res === 'confirm') {
          let param = []
          param.push(this.model)
          try {
            await this.$resetpost('rs/data/delete', {data: param}, {resolveMsg: '删除成功！！', rejectMsg: null})
          } catch (error) {
            this.$showAlert(error.data, 'danger', 0)
          }
          this.$dispatch('refresh')
        }
      },
      async validateDul () {
        // 判断是修改还是新增
        if (this.data) {
          if (this.model.f_dep_id !== this.data.f_dep_id ) {
            let validate = await this.$resetpost('rs/sql/ldap_getAllDep', {data: {condition: `f_dep_id = '${this.model.f_dep_id}'`}}, {rejectMsg: '验证部门编号重复出错！！', resolveMsg: null})
            if (validate.data.length > 0) {
              this.model.f_dep_id = this.data.f_dep_id
              this.$showAlert('编号重复，请重新输入！！', 'warning', 2000)
            }
          }
        } else {
          let validate = await this.$resetpost('rs/sql/ldap_getAllDep', {data: {condition: `f_dep_id = '${this.model.f_dep_id}'`}}, {rejectMsg: '验证部门编号重复出错！！', resolveMsg: null})
          if (validate.data.length > 0) {
            this.model.f_dep_id = ''
            this.$showAlert('编号重复，请重新输入！！', 'warning', 2000)
          }
        }
      }
    }
  }
</script>
<style>

</style>
