<template lang="html">
  <div class="">
    <ifbp-panel-group>
      <ifbp-panel id="basePanel" title="基本信息" :icons="buttonIcons" class="titleLine">
        <ifbp-template ref="buttonRef"
                  tplId="buttonId"
                  :funnode="buttonFunnode"
                  :nexuskey="buttonNexuskey"
                  show-type="form"
                  :tplData="buttonData"
                  :tpl-reset-fun="btnFormResetFun"
                  @after-create="templateAfterCreate"
                  :editable="buttonEdit">
        </ifbp-template>
        <div class="form-button-div" v-if="buttonEdit">
          <el-button @click="buttonCancel">取消</el-button>
          <el-button type="primary"  @click="buttonConfirm">保存</el-button>
        </div>
      </ifbp-panel>
    </ifbp-panel-group>
  </div>
  <!-- <ifbp-page> -->

  <!-- </ifbp-page> -->
</template>

<script>
export default {
  data() {
    let vm = this;
    let id = this.$root.$router.currentRoute.params.id;
    let code = this.$root.$router.currentRoute.params.code;
    return {
      buttonFunnode: 'YMDZ',
      buttonNexuskey: 'YMDZ_BTN_DETAIL',
      buttonEdit: true,
      // btnBusiCode: false,
      buttonData: {
        btnBusiCode: false,
      },
      id: id,
      createCode: code,
      buttonIcons: [{
        icon: "edit",
        show: true,
        click: function() {
          vm.buttonEdit = !vm.buttonEdit;
          vm.buttonIcons[0].show = false;
        }
      }],
      btnFormResetFun: function($node) {
        var $busiCode = this.getNodeById($node, 'qc5xb4sbvks');
        $busiCode.attr(':disabled', "btnBusiCode");
        return $node[0].outerHTML;
      }
    }
  },
  methods: {
    templateAfterCreate(ucomp) {
      // if(this.id){
      ucomp.setData('btnBusiCode', true);
      // }
    },
    buttonCancel() {
      this.buttonEdit = false;
      this.buttonIcons[0].show = true;
      if (this.baseData) {
        this.$refs.buttonRef.setFormData(this.baseData);
      } else {
        this.$refs.buttonRef.resetFormData();
      }
    },
    buttonConfirm() {
      let vm = this;
      let url = "/ifbp-app-sm-infoset-web/page_operate/update";
      this.$refs.buttonRef.validate(valid => {
        if (valid) {
          let data = vm.$refs.buttonRef.getFormData();
          if (!vm.id) {
            url = "/ifbp-app-sm-infoset-web/page_operate/create";
          }
          vm
            .$http({
              url: url,
              method: "post",
              data: data,
              headers: {
                "Content-Type": "application/json"
              },
              dataType: "application/json"
            })
            .then(res => {
              if (res.data.success) {
                vm.$message({
                  message: "保存成功"
                });
                if (!vm.id) {
                  vm.id = res.data.data.id;
                }
                let resData = res.data.data || {};
                vm.$refs.buttonRef.setFormData(resData);
                vm.baseData = JSON.parse(JSON.stringify(resData));
                vm.buttonEdit = false;
                vm.buttonIcons[0].show = true;
                // debugger;
              } else {
                vm.$message({
                  message: res.data.error.errorMessage,
                  type: "error"
                });
              }
            })
            .catch(() => {
              vm.$message({
                message: "保存失败",
                type: "error"
              });
            });
        } else {
          this.$message("校验未通过");
        }
      });
    },
    requestButtonDetail() {
      // if (!this.id) return;
      var url;
      var data;
      if (!this.id) {
        url = "/ifbp-app-sm-infoset-web/page_operate/toCreate";
        data = this.createCode;
      } else {
        url = "/ifbp-app-sm-infoset-web/page_operate/getById";
        data = this.id;
      }
      this.$http({
          url: url,
          method: "post",
          data: data,
          headers: {
            "Content-Type": "application/json"
          },
          dataType: "application/json"
        })
        .then(res => {
          if (res.data.success === true) {
            let resData = res.data.data || {};
            this.$nextTick(() => {
              if (this.$refs.buttonRef) {
                this.$refs.buttonRef.setFormData(resData);
              } else {
                this.$set(this.buttonData, "uitemplateFormData", resData);
              }
            });
            this.baseData = JSON.parse(JSON.stringify(resData));
          } else {
            this.$message({
              message: res.data.msg,
              type: "error"
            });
          }
        })
        .catch(e => {
          this.$message({
            message: "基本信息详情获取失败",
            type: "error"
          });
        });
    },

  },
  mounted() {
    this.requestButtonDetail();
  },
  created() {

  }
}
</script>

<style lang="css">
</style>
