<template>
  <ifbp-page>
    <ifbp-breadcrumb name="新增组织"></ifbp-breadcrumb>
    <ifbp-main-area type="detail">
      <ifbp-panel title="组织-公司">
      <ifbp-template
        ref="orgRef"
        show-type="form"
        tpl-id="orgId"
        :tpl-data="orgData"
        :funnode="funnode"
        :nexuskey="nexuskey"
        :editable="isEdit"
        :form-show-message="isEdit">
      </ifbp-template>
      <div class="fr">
        <el-button @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 {
        funnode: 'ZZGL',
        nexuskey: 'ZZGL_GS',
        orgData: {
          corp:{}
        },
        orgId:'', //组织的id
        fatherId:'',
        beanMap:{},
        beanMaps:{},
        searchKey: '关键字',
        isEdit: true,
      };
    },
    components:{
       // uitemplate:uitemplate
    },
    methods:{
      getOrgId(){
        var url = window.location.href;
        var orgId = url.split('?')[1].split('&')[0].split('=')[1];
        this.orgId = orgId;
      },
      confirm(){
        const self = this;
        this.$refs.orgRef.getFormComp().validate((valid) => {
          if(valid) {
            var submitData = self.$refs.orgRef.getFormData();
            self.$http({
              url: '/ifbp-app-bd/bd/org/corp/',
              method: 'post',
              data: submitData
            }).then((res) => {
              if (res.data.success === true) {
                self.isCreate = true;
                if (window.env === 'dev') {
                  self.$message({
                    message: '新增成功！',
                    type: 'success'
                  });
                }
                self.isEdit = false;
                self.$context.destroyCurrentTile();
                self.$router.push('/HR010003');
                window.localStorage.removeItem('beanMap');
              } else {
                self.$message({
                  message: res.data.error.errorMessage,
                  type: 'error'
                });
              }
            }).catch(() => {
              this.$message({
                message: '请求接口失败！',
                type: 'error'
              });
          });
          } else {
            // 验证失败
          }
        });
      },
      backInfo(){
        this.$context.destroyCurrentTile();
        this.$router.push('/HR010003');
      }
    },
    created() {
      debugger;
      this.getOrgId();
      var beanMaps = JSON.parse(localStorage.getItem('beanMap'));
      this.beanMaps = beanMaps;
      if(beanMaps.orgtype === 1){
        this.beanMap = {
          pk_fatherorg_ref: {}
        };
        this.beanMap.pk_fatherorg_ref[beanMaps.id] = {
          code: beanMaps.code,
          name: beanMaps.name,
          id: beanMaps.id,
        }
      }
      this.$set(this.orgData.corp,'beanMap',this.beanMap);
      if(beanMaps.orgtype !== 0){
      this.$set(this.orgData.corp,'pk_fatherorg',this.beanMaps.id);
      }
    }
  };
</script>

