<template>
  <div class="purchase-contract-review" v-loading="isLoading">
    <cy-up-down-drag>
      <div slot="top">
        <cy-steps :stepsData="stepList"></cy-steps>
        <el-collapse v-if="stepList.length" v-model="activeNames">
          <el-collapse-item v-for="(item, index) in stepList" :key="index" :name="index">
            <template slot="title">
              <cy-view-bar
                :view-bar-data="{ ...item.businessProcessingLogVo, formBusinessName: item.formBusinessName, index: index + 1 }">
              </cy-view-bar>
            </template>
            <template v-if="item.formBusinessName == '采购合同录入'">
              <step-1 :info="item.obj"></step-1>
            </template>
            <template v-if="item.formBusinessName == '采购合同审核'">
              <purchase-contract-review-content :info="item.obj" :isView="true" />
            </template>
            <template v-if="item.formBusinessName.includes('采购合同签署') || item.formBusinessName.includes('采购合同变更签署')">
              <step-2 :info="item.obj"></step-2>
            </template>
            <template v-if="item.formBusinessName == '采购合同变更终审' || item.formBusinessName == '采购合同终审'">
              <sign-review-content :info="item.obj" :isView="true" />
            </template>
            <template v-if="item.formBusinessName == '采购合同变更录入'">
              <step-3 :info="item.obj"></step-3>
            </template>
            <template v-if="item.formBusinessName == '采购合同变更审核'">
              <change-contract-review-content :info="item.obj" :isView="true" />
            </template>
          </el-collapse-item>
        </el-collapse>
      </div>
      <div slot="bar">
        <div>
          合同审核
        </div>
      </div>
      <div slot="bottom">
        <div class="tempalte-content" style="min-height: auto; padding: 0px 20px ">
          <purchase-contract-review-content ref="auditInfo" />
          <div class="submit-btn">
            <el-button style="width: 150px;" @click="() => { $router.go(-1) }">取消</el-button>
            <el-button style="width: 150px;" @click="submit(0)">暂存</el-button>
            <el-button type="primary" style="width: 150px;" @click="confirm">确定</el-button>
          </div>
        </div>
      </div>
    </cy-up-down-drag>
  </div>
</template>

<script>
import PurchaseContractReviewContent from './components/PurchaseContractReviewContent.vue'
import ChangeContractReviewContent from './components/ChangeContractReviewContent.vue'
import SignReviewContent from './components/SignReviewContent.vue'
import Step1 from './stepComponents/Step1.vue'
import Step2 from './stepComponents/Step2.vue'
import Step3 from './stepComponents/Step3.vue'
import request from '@/utils/request'
export default {
  name: 'PurchaseContractReviewContentPage',
  components: {
    PurchaseContractReviewContent, SignReviewContent, Step1, Step2, Step3, ChangeContractReviewContent
  },
  data() {
    return {
      stepList: [],
      activeNames: '',
      isLoading: false
    }
  },
  mounted() {
    this.getStepList()
  },
  methods: {
    getStepList() {
      this.isLoading = true
      request({
        url: '/businessProcessingTask/doBusinessHistoryView',
        method: 'get',
        params: { businessKey: this.$route.query.infoId, businessType: 'PurchContract', Loading: true }
      }).then(({ data }) => {
        data.forEach(item => {
          if(item.obj) {
            item.obj.mathId = Math.random()
          } else {
            item.obj = { mathId: Math.random() }
          }
        })
        this.stepList = data || []
        this.isLoading = false
      })
    },
    confirm() {
      let data = this.getParamas()
      if(data.purchContractApproval.approvalConclusion == 'back') {
        if(data.purchContractApproval.approvalComment) {
          this.submit(1)
        } else {
          this.$message.error('请输入审批结论')
        }
        return
      }
      this.$refs.auditInfo.validate().then(() => {
        this.submit(1)
      }, () => {
        console.log(this.getParamas())
      })
    },
    getParamas() {
      let data = { taskId: this.$route.query.taskId, businessKey: this.$route.query.infoId, ...this.$refs.auditInfo.ruleForm }
      return data
    },
    submit(type) {
      // this.isLoading = true
      let data = this.getParamas()
      data.visible = type
      delete data.purchContractApproval.fileStorage
      if(data.purchContractApproval.uploadPurchContracts && data.purchContractApproval.uploadPurchContracts.length > 0) {
        data.purchContractApproval.uploadPurchContracts.forEach(item => {
          item.uploadPurchContractSigners = item.personList.map(item => {
            return {
              signerName: item.split('。')[1],
              signerSocialCreditCode: item.split('。')[0]
            }
          })
          delete item.fileStorage
        })
      }
      data.Loading = true
      request({
        url: '/purchContract/approval/doAudit',
        data,
        method: 'post'
      }).then(() => {
        this.$message.success('提交成功')
        this.isLoading = false
        this.$router.go(-1)
      }, () => {
        this.isLoading = false
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  /* .template-examine .submit-btn {
    position: absolute;
    bottom: 40px;
    right: 40px;
  } */
</style>
