<template>
  <ifbp-page>
    <ifbp-breadcrumb name="部门编辑"></ifbp-breadcrumb>
    <ifbp-main-area type="detail">
      <ifbp-panel title="组织-部门" :icons="deptIcons">
      <ifbp-template
        ref="deptFormRef"
        show-type="form"
        tpl-id="deptFormId"
        :funnode="deptFormFunnode"
        :nexuskey="deptFormNexuskey"
        :tpl-data="deptFormData"
        :editable="isEdit"
        :form-show-message="isEdit">
        </ifbp-template>
        <div class="fr">
          <el-button type="default" v-if="isEdit" @click="cancel">取消</el-button>
          <el-button type="primary" v-if="isEdit" @click="confirm">保存</el-button>
        </div>
        <div style="clear:both;"></div>
      </ifbp-panel>
    </ifbp-main-area>
  </ifbp-page>
</template>

<script>
  export default{
    data() {
      var vm = this;
      return {
        deptFormFunnode: 'ZZGL',
        deptFormNexuskey: 'ZZGL_BM',
        deptIcons: [{
          icon: 'edit',
          show: true,
          click: function() {
            vm.isEdit = true;
            vm.deptIcons[0].show = false;
          }
        }],
        deptFormData: {
          dept: {}
        },
        originalDeptFormData:{},
        isEdit: false
      };
    },
    methods:{
      request(){
        const self = this;
        this.$http({
          url: '/ifbp-app-bd/bd/org/dept/' + this.$root.$route.params.id,
          method: 'get'
        }).then((res) => {
          if(res.data.success === true){
            this.originalDeptFormData = JSON.parse(JSON.stringify(res.data.data));
            self.$nextTick(() => {
              self.$refs.deptFormRef.setFormData(res.data.data);
            });
          }
        }).catch(() => {
          self.$message({
            message: '获取部门信息失败！',
            type: 'error'
          });
        });
      },
      confirm(){
        const self = this;
        this.$refs.deptFormRef.getFormComp().validate((valid) => {
          var submitData = self.$refs.deptFormRef.getFormData();
          if(valid) {
            this.$http({
              url: '/ifbp-app-bd/bd/org/dept/',
              method: 'put',
              data: submitData
            }).then((res) => {
              if (res.data.success === true) {
                  self.$message({
                    message: '修改成功',
                    type: 'success'
                  });
                self.isEdit = false;
                self.deptIcons[0].show = true;
              } else {
                self.$message({
                  message: res.data.error.errorMessage,
                  type: 'error'
                });
              }
            }).catch(() => {
              this.$message({
                message: '接口调用失败！',
                type: 'error'
              });
            });
          } else {
          }
        });
      },
      cancel(){
        this.isEdit = false;
        this.deptIcons[0].show = true;
        this.$nextTick(() => {
          this.$refs.deptFormRef.setFormData(JSON.parse(JSON.stringify(this.originalDeptFormData)));
        });
      },
      backInfo(){
        this.$router.push('/HR010003');
      }
    },
    created() {
      this.request();
    }
  };
</script>