<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%"
                    :disabled='hasChild'
                    @click="openOrgModal">
              <span class="glyphicon glyphicon-search"></span>
            </button>
          </div>
          <div class="col-sm-6">
            <label  class="font_normal_body" >&nbsp;&nbsp;&nbsp;类型&nbsp;&nbsp;&nbsp;</label>
            <v-select :value.sync="model.f_dep_type"
                      :options='userStates'
                      :value-single="true"
                      placeholder='类型'
                      close-on-select
                      v-model='model.f_dep_type'></v-select>
          </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 @click="confirm()" class="btn button_search " style="width: 70px"
                :disabled='!$v.valid'>
          <span class="glyphicon glyphicon-floppy-disk"></span>保存
        </button>
        <button @click='clear()' class="btn button_clear"style="width: 70px">
          <span class=" glyphicon glyphicon-log-out"></span>取消
        </button>
        <button @click='deleteDep()'v-if="model.id"  class="btn button_clear"style="width: 70px">
          <span class=" glyphicon glyphicon-trash"></span>删除
        </button>
      </div>
    </validator>
    <organization-modal :data="model" v-show="orgModalShow" :show.sync="orgModalShow" @depart="getDepart"></organization-modal>
  </div>
</template>
<script>


  export default {
    data () {
      return {
        model:{
          loginid: this.$login.f.id,
          f_description: '',
          id: '',
          selfid: '',
          f_dep_type:'部门',
          tablename: 't_department',
          entitytype: 't_department',
          position: '1',
          resourcetype: 'department',
          f_dep_id: '',
          f_remark: '',
          oname: '',
          name: '',
          parentid: ''
        },
        userStates: [{label:'部门', value: '部门'}, {label:'营业厅', value: '营业厅'}],
        // 组织树模态框
        orgModalShow: false
      }
    },
    props: ['data', 'hasChild'],
    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 = []
        this.orgModalShow = false
      },
      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>
