<template>
  <ifbp-page>
    <ifbp-breadcrumb name="新增部门"></ifbp-breadcrumb>
    <ifbp-main-area type="detail">
      <ifbp-panel title="组织-部门">
      <ifbp-template
        ref="deptFormRef"
        show-type="form"
        tpl-id="deptFormId"
        :funnode="deptFormFunnode"
        :nexuskey="deptFormNexuskey"
        :tpl-data="deptFormData">
        </ifbp-template>
        <div class="fr">
          <el-button type="default" @click="backInfo">返回</el-button>
          <el-button type="primary" @click="confirm">保存</el-button>        
        </div>
        <div style="clear:both;"></div>
      </ifbp-panel>
    </ifbp-main-area>
  </ifbp-page>
</template>

<script>
  export default{
    data() {
      return {
        deptFormFunnode: 'ZZGL',
        deptFormNexuskey: 'ZZGL_BM',
        deptFormData: {
          dept: {}
        },

        beanMaps:{},
        searchKey: '关键字',
        isEdit: true
      };
    },
    methods:{
      confirm(){
        const self = this;
        this.$refs.deptFormRef.getFormComp().validate((valid) => {
          var submitData = self.$refs.deptFormRef.getFormData();
          if(valid){
            self.$http({
              url: '/ifbp-app-bd/bd/org/dept/',
              method: 'post',
              data: submitData
            }).then((res) => {
              if (res.data.success === true) {
                if (window.env === 'dev') {
                  self.$message({
                    message: '新增成功！',
                    type: 'success'
                  });
                }
                self.isEdit = false;
                self.$context.destroyCurrentTile();
                self.$root.$router.push('/HR010003');
                window.localStorage.removeItem('beanMap');                
              } else {
                self.$message({
                  message: res.data.error.errorMessage,
                  type: 'error'
                });
              }
            }).catch(() => {
              self.$message({
                message: '请求接口失败！',
                type: 'error'
              });
            });
          }
          });
      },
      backInfo(){
        this.$context.destroyCurrentTile();
        this.$router.push('/HR010003');
      }
    },
    created() {
      var beanMap = JSON.parse(localStorage.getItem('beanMap'));
      this.beanMaps = beanMap;
      if(beanMap.orgtype !== 0){
        if(beanMap.orgtype === 2){
          this.beanMaps = {
            pk_fatherorg_ref: {},
          };
          this.beanMaps.pk_fatherorg_ref[beanMap.id] = {
            code: beanMap.code,
            name: beanMap.name,
            id: beanMap.id,
          };
          this.$set(this.deptFormData.dept,'pk_fatherorg',beanMap.id);
        } else if(beanMap.orgtype === 1){
          this.beanMaps = {
            pk_org_ref: {}
          },
          this.beanMaps.pk_org_ref[beanMap.id] = {
            code: beanMap.code,
            name: beanMap.name,
            id: beanMap.id, 
          };
          this.$set(this.deptFormData.dept,'pk_org',beanMap.id);
        };
        this.$set(this.deptFormData.dept,'beanMap',this.beanMaps);
      }else{
        return;
      }
    }
  };
</script>

<style scoped>
</style>
