<template xmlns:v-slot="http://www.w3.org/1999/XSL/Transform">
  <a-card :bordered="false" :title="modalTitle" v-show="visible">
    <a-row :gutter="48" slot="extra">
      <a-col :md="48" :sm="48">
        <span class="table-page-search-submitButtons" style="float: right">
          <a-button style="margin-left: 8px" @click="handleCancel()">返回</a-button>
        </span>
      </a-col>
    </a-row>
    <title-divider title="基本信息" width="90px"></title-divider>
    <detail-list title="" :col="2">
      <detail-list-item term="单号">{{ model.billNo }}</detail-list-item>
      <detail-list-item term="计划名称">{{ model.name }}</detail-list-item>
      <detail-list-item term="公司名称">{{ model.companyName }}</detail-list-item>
      <detail-list-item term="项目名称">{{ model.projectName }}</detail-list-item>
      <detail-list-item term="项目记账主币">{{ BaseTool.Object.getField(moneyTypeMap,model.projectMoneyType) }}</detail-list-item>
      <detail-list-item term="类型">{{ BaseTool.Object.getField(typeMap,model.type) }}</detail-list-item>
      <detail-list-item term="需求计划名称">{{ model.demandPlanName }}</detail-list-item>
      <detail-list-item term="呈报">{{ BaseTool.Object.getField(reportTypeMap,model.reportType) }}</detail-list-item>
      <detail-list-item term="需求日期">{{ model.demandDate }}</detail-list-item>
      <!--      <detail-list-item term="事项">{{ model.item }}</detail-list-item>-->
      <!--      <detail-list-item term="设备物资综述">{{ model.summary }}</detail-list-item>-->
      <!--      <detail-list-item term="特殊情况">{{ model.specialDescription }}</detail-list-item>-->
      <detail-list-item term="计划总金额">{{ BaseTool.Amount.formatter(model.planMoney)+ BaseTool.Number.divForDollar(model.planMoney, model.dollarExchangeRate, 2) }}</detail-list-item>
      <detail-list-item term="已询价金额">{{ BaseTool.Amount.formatter(model.money)+ BaseTool.Number.divForDollar(model.money, model.dollarExchangeRate, 2) }}</detail-list-item>
      <detail-list-item term="已用金额">{{ BaseTool.Amount.formatter(model.usedMoney)+ BaseTool.Number.divForDollar(model.usedMoney, model.dollarExchangeRate, 2) }}</detail-list-item>
      <detail-list-item term="进度">{{ BaseTool.Amount.formatter(model.progress) }}</detail-list-item>
      <detail-list-item term="数量">{{ BaseTool.Amount.formatter(model.quantity) }}</detail-list-item>
      <detail-list-item term="已购数量">{{ BaseTool.Amount.formatter(model.boughtQuantity) }}</detail-list-item>
      <detail-list-item term="已到数量">{{ BaseTool.Amount.formatter(model.arrivedQuantity) }}</detail-list-item>
      <detail-list-item term="未到数量">{{ BaseTool.Amount.formatter(model.notArriveQuantity) }}</detail-list-item>
      <detail-list-item term="总重">{{ BaseTool.Amount.formatter(model.totalWeight) }}</detail-list-item>
      <detail-list-item term="总体积">{{ BaseTool.Amount.formatter(model.totalVolume) }}</detail-list-item>
      <detail-list-item term="申请时间">{{ model.applyTime }}</detail-list-item>
      <detail-list-item term="审核时间">{{ model.auditTime }}</detail-list-item>
      <detail-list-item term="审核描述">{{ model.refuseReason }}</detail-list-item>
      <detail-list-item term="状态"><badge :status="DictCache.COLOR.PURCHASE_PLAN_STATUS[model.status]" :text="statusMap[model.status]"></badge></detail-list-item>
      <detail-list-item term="备注">{{ model.remark }}</detail-list-item>
      <detail-list-item term="更新人名称">{{ model.updateUserName }}</detail-list-item>
      <detail-list-item term="更新时间">{{ model.updateTime }}</detail-list-item>
    </detail-list>
    <title-divider title="计划明细" width="90px"></title-divider>
    <purchase-plan-list ref="planList" :viewFlag="true"/>
    <title-divider title="审批信息" width="90px" v-if="auditFlag"></title-divider>
    <a-card :bordered="false" title="" v-if="auditFlag">
      <a-steps
        :direction="'horizontal'"
        :current="current"
        :status="status"
        progressDot>

        <a-step
          v-for="item in currentNodes"
          :key="item.nodeValue"
          :title="item.nodeName"
        >
          <template v-slot:description v-if="item.userName">
            <div class="antd-pro-pages-profile-advanced-style-stepDescription">
              {{ item.userName }}
              <div>{{ item.createTime }}</div>
            </div>
          </template>
        </a-step>
      </a-steps>
    </a-card>
    <a-table
      v-if="auditFlag"
      :columns="auditColumns"
      :dataSource="auditData"
      :pagination="false"
    >
    </a-table>
  </a-card>
</template>

<script>
import DetailList from '@/components/tools/DetailList'
import PurchasePlanList from '../PurchasePlanListView'
import { getAuditRecord } from '@/api/activiti/activiti'
const DetailListItem = DetailList.Item

export default {
  name: 'PurchasePlanDetail',
  components: {
    DetailList,
    PurchasePlanList,
    DetailListItem
  },
  data () {
    return {
      confirmLoading: false,
      mdl: {},
      modalTitle: null,
      visible: false,
      nodeMap: {},
      moneyTypeMap: {},
      // 下拉框map
      typeMap: {},
      nodes: [],
      auditFlag: false,
      reportTypeMap: {},
      periodTypeMap: {},
      current: 0,
      status: 'process',
      statusMap: {},
      auditColumns: [
        {
          title: '节点',
          dataIndex: 'node',
          key: 'node',
          customRender: (text, record, index) => {
            return this.BaseTool.Table.statusCustomRenderDict(this, text, record,
              this.DictCache.COLOR.PURCHASE_AUDIT_NODE, this.nodeMap)
          }
        },
        {
          title: '审批人',
          dataIndex: 'userName',
          key: 'userName'
        },
        {
          title: '审批时间',
          dataIndex: 'createTime',
          key: 'auditTime'
        },
        {
          title: '审批结果',
          dataIndex: 'flag',
          key: 'flag',
          customRender: (text, row, index) => {
            return text ? '同意' : '不同意'
          }
        },
        {
          title: '审批意见',
          dataIndex: 'opinion',
          key: 'opinion'
        }
      ],
      auditData: [],
      reportType: this.DictCache.VALUE.REPORT_RANK.REPORT_RANK_XMB,
      currentNodes: [],
      model: {
        'billNo': null,
        'name': null,
        'demandPlanId': null,
        'demandPlanName': null,
        'companyId': null,
        'companyName': null,
        'projectId': null,
        'projectName': null,
        'projectMoneyType': null,
        'dollarExchangeRate': null,
        'type': null,
        'planDate': null,
        'planMoney': null,
        'reportType': null,
        'demandDate': null,
        'item': null,
        'year': null,
        'periodValue': null,
        'periodType': null,
        'summary': null,
        'specialDescription': null,
        'money': null,
        'usedMoney': null,
        'progress': null,
        'quantity': null,
        'boughtQuantity': null,
        'arrivedQuantity': null,
        'notArriveQuantity': null,
        'totalWeight': null,
        'totalVolume': null,
        'refuseReason': null,
        'applyTime': null,
        'auditTime': null,
        'status': null,
        'remark': '',
        'delFlag': null,
        'createdUserId': null,
        'updateUserId': null,
        'updateUserName': null,
        'updateTime': null
      }
    }
  },
  created () {
    // 下拉框map
    this.typeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.PURCHASE_TYPE)
    this.reportTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.REPORT_RANK)
    this.periodTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.PURCHASE_PERIOD_TYPE)
    this.statusMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.PURCHASE_PLAN_STATUS)
    this.nodeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.PURCHASE_AUDIT_NODE)
    this.moneyTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.MONEY_TYPE)
  },
  methods: {
    base (record) {
      this.visible = true
      this.modalTitle = '采购计划详情'
      this.model = record
      let dollarExchangeRate = 1
      if (record.projectMoneyType === this.DictCache.VALUE.MONEY_TYPE.RMB) {
        dollarExchangeRate = this.$store.getters.rmbRate
      }
      this.model.dollarExchangeRate = dollarExchangeRate
      const modal = this.$refs.planList
      modal.base(record.id)
      this.reportType = record.reportType
      this.getAuditRecords(record)
    },
    getAuditRecords (record) {
      const processInstanceId = record.processInstanceId
      if (processInstanceId) {
        this.auditFlag = true
        const currentNodes = []
        const reportType = record.reportType
        if (this.DictCache.VALUE.REPORT_RANK.REPORT_RANK_XMB === reportType) {
          for (const value in this.nodeMap) {
            if (value !== '20' && value !== '30' && value !== '40') {
              currentNodes.push({
                nodeValue: value,
                nodeName: this.nodeMap[value]
              })
            }
          }
        } else if (this.DictCache.VALUE.REPORT_RANK.REPORT_RANK_FGS === reportType) {
          for (const value in this.nodeMap) {
            if (value !== '30' && value !== '40') {
              currentNodes.push({
                nodeValue: value,
                nodeName: this.nodeMap[value]
              })
            }
          }
        } else if (this.DictCache.VALUE.REPORT_RANK.REPORT_RANK_QYZB === reportType) {
          for (const value in this.nodeMap) {
            if (value !== '40') {
              currentNodes.push({
                nodeValue: value,
                nodeName: this.nodeMap[value]
              })
            }
          }
        } else {
          for (const value in this.nodeMap) {
            if (value === '30' && !record.regionId) {
              continue
            }
            currentNodes.push({
              nodeValue: value,
              nodeName: this.nodeMap[value]
            })
          }
        }
        getAuditRecord(processInstanceId).then(res => {
          this.auditData = res.data
          for (let i = 0; i < this.auditData.length; i++) {
            const item = this.auditData[i]
            for (let j = 0; j < currentNodes.length; j++) {
              const elem = currentNodes[j]
              if (item.node === parseInt(elem.nodeValue)) {
                elem.createTime = item.createTime
                elem.userName = item.userName
                elem.flag = item.flag
                elem.opinion = item.opinion
                currentNodes[j] = elem
              }
            }
            if (item.node === 0) {
              break
            }
          }
          const first = this.auditData[0]
          const flag = first.flag
          const node = first.node
          let index = 1
          for (let j = 0; j < currentNodes.length; j++) {
            const elem = currentNodes[j]
            if (node === parseInt(elem.nodeValue)) {
              index = j
            }
          }
          if (!flag) {
            this.status = 'error'
            this.current = index
          } else {
            this.current = index + 1
            if (index === currentNodes.length - 1) {
              this.status = 'finish'
            } else {
              currentNodes[this.current].userName = this.model.currentAuditUserName
              this.status = 'process'
            }
          }
          this.currentNodes = currentNodes
        })
      }
    },
    handleCancel () {
      this.visible = false
      this.confirmLoading = false
      this.$emit('ok', {})
    }

  }
}
</script>
