<template>
  <div class="app-container" style="height: 100%;">
    <div class="fd-nav" style="display: flex;align-items: center;">
      <div class="fd-nav-container">
        <div class="fd-nav-item active">
          <CustomForm :value="form" style="margin-top:36px;color: black;">
            <DictSelect prop="auditFlow" dictType="audit_flow" @change="item => getDetail(null, item.value)" :span="24" :lg-span="24" :md-span="24" :sm-span="24" :xs-span="24" :clearable="false" />
          </CustomForm>
        </div>
      </div>
      <div class="fd-nav-right" style="margin-top: -5px;">
        <!--        <CustomButton label="删除" class="ant-btn button-preview" :disabled="saving"  @handleClick="handleRemove"/>-->
        <CustomButton label="保存并提交" type="primary" class="ant-btn button-publish" :disabled="saving" @handleClick="saveSet"  style="color: white;"/>
        <!--        <button type="button" class="ant-btn button-preview" :disabled="saving"-->
        <!--          @click="handleRemove"><span>{{ $t('') }}</span></button>-->
        <!--        <button type="button" class="ant-btn button-publish" :disabled="saving"-->
        <!--          @click="saveSet"><span>{{ $t('audit.saveAndApply') }}</span></button>-->
      </div>
    </div>
    <div class="fd-nav-content">
      <section class="dingflow-design">
        <div class="zoom">
          <div :class="'zoom-out' + (nowVal == 50 ? ' disabled' : '')" @click="zoomSize(1)"></div>
          <span>{{ nowVal }}%</span>
          <div :class="'zoom-in' + (nowVal == 300 ? ' disabled' : '')" @click="zoomSize(2)"></div>
        </div>
        <div v-loading="loading" class="box-scale" id="box-scale" :style="'transform: scale(' + nowVal / 100 + '); transform-origin: 50% 0px 0px;'">
          <nodeWrap :nodeConfig.sync="nodeConfig" :flowPermission.sync="flowPermission" :isTried.sync="isTried" :tableId="tableId"></nodeWrap>
          <div class="end-node">
            <div class="end-node-text">流程结束</div>
          </div>
        </div>
      </section>
    </div>
    <errorDialog :visible.sync="tipVisible" :list="tipList" />
    <approverDrawer />
    <conditionDrawer :auditType="form.auditFlow" />
  </div>
</template>
<script>
import errorDialog from "./components/dialog/errorDialog";
import promoterDrawer from "./components/drawer/promoterDrawer";
import approverDrawer from "./components/drawer/approverDrawer";
import copyerDrawer from "./components/drawer/copyerDrawer";
import conditionDrawer from "./components/drawer/conditionDrawer";
import { getConfig, removeConfig, saveConfig } from "@/api/audit/flow";
import yid from "yid";
import nodeWrap from "./components/nodeWrap.vue";
import { mapGetters } from "vuex";
import CustomButton from "../../components/CustomElement/CustomButton.vue";
import CustomForm from "../../components/CustomElement/CustomForm/CustomForm.vue";
import DictSelect from "./components/form/DictSelect.vue";
import Form from "../../components/element/form/Form.vue";

const defaultFlow = () => ({
  nodeId: yid(),
  nodeName: "发起人",
  type: 0,
  childNode: null,
});

export default {
  name: "Audit",
  components: {
    Form,
    DictSelect,
    CustomForm,
    CustomButton,
    errorDialog,
    promoterDrawer,
    approverDrawer,
    copyerDrawer,
    conditionDrawer,
    nodeWrap,
  },
  data() {
    return {
      isTried: false,
      tipList: [],
      tipVisible: false,
      nowVal: 100,
      processConfig: {},
      nodeConfig: defaultFlow(),
      workFlowDef: {},
      flowPermission: [],
      tableId: "",
      form: {
        companyId: 1,
        auditFlow: { value: "1" },
      },
      loading: false,
      saving: false,
    };
  },
  computed: {
    ...mapGetters(["deptId"]),
  },
  created() {
    this.getDetail();
  },
  methods: {
    reErr({ childNode }) {
      if (childNode) {
        let { type, error, nodeName, conditionNodes } = childNode;
        if (type == 1 || type == 2) {
          if (error) {
            this.tipList.push({
              name: nodeName,
              type: ["", "审核人", "抄送人"][type],
            });
          }
          this.reErr(childNode);
        } else if (type == 3) {
          this.reErr(childNode);
        } else if (type == 4) {
          this.reErr(childNode);
          for (var i = 0; i < conditionNodes.length; i++) {
            if (conditionNodes[i].error) {
              this.tipList.push({
                name: conditionNodes[i].nodeName,
                type: "条件",
              });
            }
            this.reErr(conditionNodes[i]);
          }
        }
      } else {
        childNode = null;
      }
    },
    saveSet() {
      this.isTried = true;
      this.tipList = [];
      this.reErr(this.nodeConfig);
      if (this.tipList.length != 0) {
        this.tipVisible = true;
        return;
      }

      const data = {
        ...this.form,
        flowData: JSON.stringify(this.nodeConfig),
      };
      if (typeof data.auditFlow == "object") {
        data.auditFlow = data.auditFlow.value;
      }
      this.saving = true;
      saveConfig(data)
        .then((res) => {
          if (res.code === 200) {
            this.$message.success("保存成功");
            this.getDetail();
          }
        })
        .finally(() => {
          this.saving = false;
        });
    },
    getDetail(companyId, auditFlow) {
      const param = {};
      param.companyId = companyId || this.form.companyId;
      param.auditFlow = auditFlow || this.form.auditFlow.value;
      this.loading = true;
      getConfig(param)
        .then((res) => {
          if (res.data && String(res.data).trim().length > 0) {
            this.nodeConfig = JSON.parse(res.data.flowData);
          } else {
            this.nodeConfig = defaultFlow();
          }
        })
        .finally(() => {
          setTimeout(() => {
            this.loading = false;
          }, 500);
        });
    },
    handleRemove() {
      this.saving = true;
      removeConfig(this.form)
        .then((res) => {
          if (res.code === 200) {
            this.$message.success("清除成功");
            this.getDetail();
          }
        })
        .finally(() => {
          this.saving = false;
        });
    },
    zoomSize(type) {
      if (type == 1) {
        if (this.nowVal == 50) {
          return;
        }
        this.nowVal -= 10;
      } else {
        if (this.nowVal == 300) {
          return;
        }
        this.nowVal += 10;
      }
    },
  },
};
</script>
<style scoped>
.error-modal-list {
  width: 455px;
}

.fd-nav-container {
  display: inline-flex;
  position: relative;
  align-items: center;
  justify-content: center;
}

.fd-nav-container .ghost-bar {
  position: absolute;
  width: 150px;
  height: 100%;
  left: 0;
  background: #1583f2;
  -webkit-transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
  transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
}

::v-deep .node-wrap-box:active:after {
  border: 1px solid rgb(222, 147, 20);
}

::v-deep .node-wrap-box:hover:after {
  border: 1px solid rgb(222, 147, 20) !important;
}

.fd-nav-container .ghost-bar:after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 50%;
  margin-left: -5px;
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 0 5px 6px;
  border-color: transparent transparent #f6f6f6;
}

.fd-nav-item {
  position: relative;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  line-height: 60px;
  width: 100%;
  text-align: center;
  white-space: nowrap;
}

.fd-nav-item .order-num {
  display: inline-block;
  width: 20px;
  height: 20px;
  line-height: 20px;
  border: 1px solid #fff;
  border-radius: 50%;
  margin-right: 6px;
  -webkit-transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
  transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
}

.fd-nav-item.active .order-num {
  color: rgb(222, 147, 20);
  background: #fff;
}

.fd-nav {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 997;
  width: 100%;
  height: 60px;
  font-size: 14px;
  color: #fff;
  background: #fff;
  display: flex;
  align-items: center;
  text-align: right;
  padding-left: 40%;
}

:before .fd-nav .fd-nav-left {
  display: -webkit-box;
  display: flex;
  align-items: center;
}

.fd-nav .fd-nav-center {
  flex: none;
  width: 600px;
  text-align: center;
}

.fd-nav .fd-nav-right {
  float: right;
}

.fd-nav .fd-nav-back {
  display: inline-block;
  width: 60px;
  height: 60px;
  font-size: 22px;
  border-right: 1px solid rgb(222, 147, 20);
  text-align: center;
  cursor: pointer;
}

.fd-nav .fd-nav-back:hover {
  background: rgb(222, 147, 20);
}

.fd-nav .fd-nav-back:active {
  background: rgb(222, 147, 20);
}

.fd-nav .fd-nav-back .anticon {
  line-height: 60px;
}

.fd-nav .fd-nav-title {
  width: 0;
  flex: 1;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  padding: 0 15px;
}

.fd-nav a {
  color: #fff;
  margin-left: 12px;
}

.fd-nav .button-publish {
  min-width: 80px;
  margin-right: 15px;
  color: #3296fa;
  border-color: #fff;
}

.fd-nav .button-publish.ant-btn:focus,
.fd-nav .button-publish.ant-btn:hover {
  /*color: #3296fa;*/
  /*border-color: #fff;*/
  /*box-shadow: 0 10px 20px 0 rgba(0, 0, 0, 0.3);*/
  background-color: rgba(222, 147, 20, 0.1);
  color: rgb(222, 147, 20);
  border-color: rgb(222, 147, 20);
}

.fd-nav .button-publish.ant-btn:active {
  /*color: #3296fa;*/
  /*background: #d6eaff;*/
  /*box-shadow: none;*/
  background-color: rgba(222, 147, 20, 0.1);
  color: rgb(222, 147, 20);
  border-color: rgb(222, 147, 20);
}

.fd-nav .button-preview {
  min-width: 80px;
  margin-right: 8px;
  color: #fff;
  border-color: #fff;
  background: transparent;
}

.fd-nav .button-preview.ant-btn:focus,
.fd-nav .button-preview.ant-btn:hover {
  /*color: #fff;*/
  /*border-color: #fff;*/
  /*background: #59acfc;*/
  background-color: rgba(222, 147, 20, 0.1);
  color: rgb(222, 147, 20);
  border-color: rgb(222, 147, 20);
}

.fd-nav .button-preview.ant-btn:active {
  /*color: #fff;*/
  /*border-color: #fff;*/
  /*background: #2186ef;*/
  background-color: rgba(222, 147, 20, 0.1);
  color: rgb(222, 147, 20);
  border-color: rgb(222, 147, 20);
}

.fd-nav-content {
  position: absolute;
  top: 60px;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1;
  overflow-x: hidden;
  overflow-y: auto;
  padding-bottom: 30px;
  height: 100%;
}

.ant-btn {
  line-height: 1.5;
  display: inline-block;
  font-weight: 400;
  text-align: center;
  touch-action: manipulation;
  cursor: pointer;
  background-image: none;
  border: 1px solid transparent;
  white-space: nowrap;
  padding: 0 15px;
  font-size: 14px;
  border-radius: 4px;
  height: 32px;
  user-select: none;
  transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
  position: relative;
  color: rgba(0, 0, 0, 0.65);
  background-color: #fff;
  border-color: #d9d9d9;
}

.ant-btn > .anticon {
  line-height: 1;
}

.ant-btn,
.ant-btn:active,
.ant-btn:focus {
  outline: 0;
}

.ant-btn > a:only-child {
  color: currentColor;
}

.ant-btn > a:only-child:after {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  background: transparent;
}

.ant-btn:focus,
.ant-btn:hover {
  color: #40a9ff;
  background-color: #fff;
  border-color: #40a9ff;
}

.ant-btn:focus > a:only-child,
.ant-btn:hover > a:only-child {
  color: currentColor;
}

.ant-btn:focus > a:only-child:after,
.ant-btn:hover > a:only-child:after {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  background: transparent;
}

.ant-btn.active,
.ant-btn:active {
  /*color: #096dd9;*/
  /*background-color: #fff;*/
  /*border-color: #096dd9;*/
  background-color: rgba(222, 147, 20, 0.1);
  color: rgb(222, 147, 20);
  border-color: rgb(222, 147, 20);
}

.ant-btn.active > a:only-child,
.ant-btn:active > a:only-child {
  color: currentColor;
}

.ant-btn.active > a:only-child:after,
.ant-btn:active > a:only-child:after {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  background: transparent;
}

.ant-btn.active,
.ant-btn:active,
.ant-btn:focus,
.ant-btn:hover {
  background: #fff;
  text-decoration: none;
}

.ant-btn > i,
.ant-btn > span {
  pointer-events: none;
}

.ant-btn .anticon {
  transition: margin-left 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
}

.ant-btn:active > span,
.ant-btn:focus > span {
  position: relative;
}

.ant-btn > .anticon + span,
.ant-btn > span + .anticon {
  margin-left: 8px;
}

.zoom {
  display: flex;
  position: absolute;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  -webkit-box-pack: justify;
  -ms-flex-pack: justify;
  justify-content: space-between;
  height: 40px;
  width: 125px;
  right: 40px;
  margin-top: 30px;
  z-index: 10;
}

.zoom .zoom-in,
.zoom .zoom-out {
  width: 30px;
  height: 30px;
  background: #fff;
  color: #c1c1cd;
  cursor: pointer;
  background-size: 100%;
  background-repeat: no-repeat;
}

.zoom .zoom-out {
  background-image: url(https://gw.alicdn.com/tfs/TB1s0qhBHGYBuNjy0FoXXciBFXa-90-90.png);
}

.zoom .zoom-out.disabled {
  opacity: 0.5;
}

.zoom .zoom-in {
  background-image: url(https://gw.alicdn.com/tfs/TB1UIgJBTtYBeNjy1XdXXXXyVXa-90-90.png);
}

.zoom .zoom-in.disabled {
  opacity: 0.5;
}

.dingflow-design {
  width: 100%;
  background-color: #f5f5f7;
  overflow: auto;
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  top: 0;
}

.dingflow-design .box-scale {
  transform: scale(1);
  display: inline-block;
  position: relative;
  width: 100%;
  padding: 54.5px 0;
  -webkit-box-align: start;
  -ms-flex-align: start;
  align-items: flex-start;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
  min-width: -webkit-min-content;
  min-width: -moz-min-content;
  min-width: min-content;
  background-color: #f5f5f7;
  -webkit-transform-origin: 0 0 0;
  transform-origin: 0 0 0;
}

.dingflow-design .end-node {
  font-size: 14px;
  color: rgba(25, 31, 37, 0.4);
  text-align: left;
  width: 100px;
  height: 50px;
  margin: 0 auto 0 auto;
}

.dingflow-design .end-node:before {
  content: "*";
  position: absolute;
  top: -12px;
  left: 50%;
  -webkit-transform: translateX(-50%);
  transform: translateX(-50%);
  width: 0;
  height: 4px;
  border-style: solid;
  border-width: 8px 6px 4px;
  border-color: #cacaca transparent transparent;
  background: #f5f5f7;
}

/*.dingflow-design .end-node .end-node-circle {*/
/*  width: 10px;*/
/*  height: 10px;*/
/*  margin: auto;*/
/*  border-radius: 50%;*/
/*  background: #dbdcdc;*/
/*}*/

.dingflow-design .end-node .end-node-text {
  margin-top: 5px;
  text-align: center;
}
</style>
