<template>
  <div class="openModal" v-if="modal_visible">
    <Modal
      v-model="modal_visible"
      :title="' '"
      :mask-closable="false"
      :fullscreen="true"
      :class-name="'initHeight'"
    >
      <p slot="header">
        <a class="ivu-modal-close" @click="handleCancel">
          <Icon type="ios-close"
        /></a>
      </p>
      <gms-bill-form
        :templateName="templateName"
        :id="billId"
        :type="billId ? 'load' : 'init'"
        :state="state"
        @loadFinish="loadFinishFun"
        v-if="init"
      ></gms-bill-form>
      <div slot="footer" style="height: 0"></div>
    </Modal>
  </div>
</template>
<script>
import { compareData } from "./util/dataChange.js";
import Bridge from "./util/bridge.js";
export default {
  props: {},
  data() {
    return {
      init: false,
      modal_visible: false,
      templateName: null,
      billId: null,
      state: null,
      isReadOnly: false,
      billClass: null,
      oldBillData: null,
    };
  },
  watch: {
    templateName: {
      handler(newVal, oldVal) {
        this.templateName = newVal;
      },
    },
  },
  methods: {
    changeData(billId) {
      this.init = false;
      this.$nextTick(() => {
        this.billId = billId;
        this.init = true;
      });
    },
    loadFinishFun(bill) {
      let that = this;
      setTimeout(() => {
        that.billClass = bill;
        GMS.oldBillData = bill.getBuillData();
        that.oldBillData = bill.getBuillData();
      }, 100);
    },
    handleCancel() {
      //关闭弹出，刷新列表
      let newBill = this.isReadOnly
        ? this.oldBillData
        : this.billClass.getBuillData();
      let flag = compareData(this.oldBillData, newBill);
      if (flag) {
        delete GMS.editSubIndex;
        GMS.vbus.$off("cacheEcho");
        GMS.vbus.$emit("custom-query-refresh");
        GMS.vbus.$emit("tab-count-refresh");
        this.modal_visible = false;
      } else {
        //提示是否保存
        this.$Modal.confirm({
          title: "信息提示",
          content: "信息发生变化，确认关闭？",
          onOk: () => {
            this.modal_visible = false;
          },
          onCancel: () => {
            this.modal_visible = true;
          },
        });
      }
    },
    initBill(res, param, isSave) {
      this.billId = res.id;
      //this.state = "readOnly";
      if (
        res.BILLSTATEAUDIT &&
        res.BILLSTATEAUDIT.code == 2 &&
        param.param.approveViewName
      ) {
        //审核成功,
        this.templateName = param.param.approveViewName;
      } else if (param.param.viewName) {
        this.templateName = param.param.viewName;
      }
      if (isSave) {
        this.state = "readOnly";
      }
      this.init = false;
      this.$nextTick(() => {
        this.init = true;
      });
    },
  },
  created() {
    GMS.vbus.$off("saveSuccess");
  },
  mounted() {
    Bridge.vm = this;
    GMS.vbus.$on("change-form-state", (param) => {
      const { state, billId } = param;
      if (billId !== undefined) {
        this.billId = billId;
      }
      this.state = state;
      this.init = false;
      this.$nextTick(() => {
        this.init = true;
      });
    });
    GMS.vbus.$on("submitSuccess", (res, param) => {
      GMS.vbus.$emit("custom-query-refresh");
      GMS.vbus.$emit("tab-count-refresh");
      this.modal_visible = false;
    });
    GMS.vbus.$on("openEditBillForm", (res, param) => {
      //详情界面点击修改按钮
      this.isReadOnly = false;
      this.initBill(res, param);
    });
    GMS.vbus.$on("saveEcho", (res, param) => {
      this.initBill(res, param);
    });
    GMS.vbus.$on("saveSuccess", (res, param) => {
      //自己扩展的保存
      GMS.vbus.$Message.success("保存成功");
      this.initBill(res, param, true);
    });
    GMS.vbus.$on("resetBillData", this.changeData);
  },
  beforeDestroy() {
    delete GMS.editSubIndex;
    GMS.vbus.$off("cacheEcho");
    GMS.vbus.$off("openEditBillForm");
    GMS.vbus.$off("saveSuccess");
    GMS.vbus.$off("submitSuccess");
    GMS.vbus.$off("change-form-state");
  },
};
</script>
<style scoped lang="less">
.openModal {
  .ivu-modal-close .ivu-icon-ios-close {
    font-size: 31px;
    color: #999;
    -webkit-transition: color 0.2s ease;
    transition: color 0.2s ease;
    position: relative;
    top: 1px;
  }
}
</style>
