<template>
  <div class="auto select-overspread" style="overflow-y: hidden">
    <validator name='v'>
      <ul class="nav nav-tabs">
        <p class="bg-info text-center" style="padding: 8px;">部门信息</p>
      </ul>
      <div 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"
                 :disabled="look"
                 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"
                 :disabled="look"
                 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:50%" placeholder="所属组织" readonly >
          <button type="button" class="btn button_new btn-lg" style="width:10%"
                  :disabled='look'
                  @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'
                    :disabled='look'
                    :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"
                    :disabled="look"
                    style="width:80%" rows="4" v-model="model.f_remark" placeholder="备注"></textarea>
        </div>
      </div>
      <div class="col-sm-12" style="text-align:right;height:auto;margin-top:6px;">
        <button @click="permissionOper()" v-if="!look" class="btn button_search "
                style="width: 70px"
                :disabled='!$v.valid' v-if="!depPermissionShow">
          数据授权
        </button>
        <button @click="confirm()" v-if="!look" 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>
      </div>
    </validator>
    <organization-modal v-if="!look" :data="model" v-show="orgModalShow" :show.sync="orgModalShow" @org="getDepart"></organization-modal>
    <data-permission-modal :show.sync="depPermissionShow"  v-if="!look" v-show="depPermissionShow":data="data" :roots.sync="depPermissions" ></data-permission-modal>
    <div class="row" v-if="look&&model.id&&model.id.length>0" style="height: 100px;overflow: auto;">
      <licensepolicy  v-if="look&&model.id&&model.id.length>0" :id="model.id"></licensepolicy>
    </div>  </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: this.$appdata.getParam('组织类型') ? this.$appdata.getParam('组织类型') : [{label:'部门', value: '部门'}, {label:'营业厅', value: '营业厅'}],
        // 组织树模态框
        orgModalShow: false,
        // 权限
        depPermissionShow: false,
        depPermissions: [],
      }
    },
    props: ['data','look'],
    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: {
      permissionOper () {
        this.depPermissionShow = !this.depPermissionShow
      },
      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 () {
          // 保存部门 资源服务的特殊保存程序，这里去掉除部门表外的其他字段
          let saveDep= await this.$resetpost('rs/data/save', {data: this.model}, {resolveMsg: '保存部门成功', rejectMsg: '保存部门失败'})
          let depId = this.model.id ? this.model.id : saveDep.data + ''
          // 组织角色权限数据
          let param = {
            parentid: depId,
            loginid: this.$login.f.id,
            licenses:[]
          }
          this.depPermissions.forEach((item) => {
            item.parentid = depId
            item.resourceid = depId
            param.licenses.push(item)
          })
          await this.$resetpost('rs/license/save', param, {resolveMsg: '保存成功', rejectMsg: '保存权限失败！请您重试！！'})
          this.$dispatch('success')
      },
      openOrgModal () {
        this.orgModalShow = true
      },
      getDepart (val) {
        this.model.oname = val.name
        this.model.parentid = val.id
      },
      clear () {
        this.$dispatch('cancel')
      },

      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>
