<template>
  <div style="position: relative">
    <button v-if="isPreview" type="button" class="ant-btn button-preview"
      @click="handleOpenPreview"><span>流程预览</span></button>
    <el-link v-else :type="linkType" @click="handleOpen">{{ flow ? dictFormatter(flow.currentStatus) :
      dictFormatter(status) }}</el-link>
    <el-dialog ref="formDialog" append-to-body width="1000px" :visible.sync="visible" :close-on-click-modal="false"
      @close="handleClose">
      <div slot="title">
        <span style="color: white">审批明细</span>
      </div>
      <div v-loading="loading" class="fd-nav-content" style="height: 800px; top: 35px">
        <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 class="box-scale" id="box-scale"
            :style="'transform: scale(' + nowVal / 100 + '); transform-origin: 50% 0px 0px;'">
            <nodeWrap :nodeConfig.sync="nodeConfig" :isDetail="true"></nodeWrap>
            <div class="end-node">
              <div class="end-node-circle"></div>
              <div class="end-node-text">流程结束</div>
            </div>
          </div>
        </section>
      </div>
      <div slot="footer">
        <el-button class="back button" @click="handleClose">关闭</el-button>
      </div>
    </el-dialog>

    <el-dialog ref="previewDialog" append-to-body width="300px" :visible.sync="previewVisible"
      :close-on-click-modal="false" @close="handlePreviewClose">
      <div slot="title">
        <span style="color: white">流程预览</span>
      </div>
      <el-form ref="previewForm" :model="previewForm" label-width="100px" :inline="false">
        <select-item label="业务提交人" v-model="previewForm.userId" prop="userId" :data="userList" required />
        <input-item label="业务数据id" v-model="previewForm.businessId" prop="businessId" required :maxlength="50" />
      </el-form>
      <div slot="footer">
        <el-button type="primary" @click="handlePreview">提交</el-button>
        <el-button class="back button" @click="handlePreviewClose">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getFlowAuditDetail, preview } from '@/api/audit/flow'
import { mapGetters } from 'vuex'
import yid from 'yid'

const TaskStatus = {
  INACTIVE: 1,
  AUDITING: 2,
  PASS: 3,
  REJECT: 4,
}

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

export default {
  name: 'AuditStatusDialog',
  props: {
    auditFlow: {
      type: String,
      default: '',
    },
    isPreview: {
      type: Boolean,
      default: false,
    },
    nodeConfigData: {
      type: Object,
      default: () => { },
    },
    flow: {
      type: Object,
    },
    status: {},
  },
  data() {
    return {
      visible: false,
      previewVisible: false,
      previewForm: {
        userId: null,
        businessId: '',
      },
      loading: false,
      nodeConfig: defaultFlow(),
      nowVal: 100,
      userList: [],
    }
  },
  computed: {
    linkType() {
      const { INACTIVE, AUDITING, PASS, REJECT } = TaskStatus
      const { currentStatus } = this.flow || { currentStatus: this.status }
      switch (currentStatus) {
        case INACTIVE:
          return 'info'
        case AUDITING:
          return 'warning'
        case PASS:
          return 'success'
        case REJECT:
          return 'danger'
        default:
          return 'info'
      }
    },
    ...mapGetters(['dict']),
  },
  methods: {
    handleOpen() {
      if (!this.flow || !this.flow.id) {
        return
      }
      this.getDetail()
      this.visible = true
    },
    handleOpenPreview() {
      this.previewVisible = true
    },
    async handlePreview() {
      await this.$refs.previewForm.validate()

      const { auditFlow, nodeConfigData } = this
      const params = { ...this.previewForm, auditFlow, nodeConfig: nodeConfigData }
      this.loading = true
      preview(params)
        .then((res) => {
          this.nodeConfig = res.data ? res.data : defaultFlow()
          this.handlePreviewClose()
          this.visible = true
        })
        .finally(() => {
          this.loading = false
        })
    },
    handleClose() {
      this.visible = false
    },
    handlePreviewClose() {
      this.previewVisible = false
    },
    getDetail() {
      this.loading = true
      getFlowAuditDetail(this.flow.id)
        .then((res) => {
          this.nodeConfig = res.data ? res.data : defaultFlow()
        })
        .finally(() => {
          this.loading = false
        })
    },
    dictFormatter(cellValue) {
      const dicts = this.dict['audit_status'] || []
      const target = dicts.filter(({ value }) => String(value) === String(cellValue))
      return target && target.length > 0 ? target[0].label : ''
    },
    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-block;
  position: relative;
}

.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);
}

.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-block;
  line-height: 60px;
  width: 150px;
  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: #1583f2;
  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: #3296fa;
  display: flex;
  align-items: center;
  text-align: center;
}

.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);
}

.fd-nav .button-publish.ant-btn:active {
  color: #3296fa;
  background: #d6eaff;
  box-shadow: none;
}

.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;
}

.fd-nav .button-preview.ant-btn:active {
  color: #fff;
  border-color: #fff;
  background: #2186ef;
}

.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;
}

.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;
}

.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 {
  border-radius: 50%;
  font-size: 14px;
  color: rgba(25, 31, 37, 0.4);
  text-align: left;
}

.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>
