<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.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="资金预算">{{ BaseTool.Amount.formatter(model.planMoney) + BaseTool.Number.divForDollar(model.planMoney, model.dollarExchangeRate, 2) }}</detail-list-item>
      <detail-list-item term="明细控制总价">{{ BaseTool.Amount.formatter(model.planListControlMoney) + BaseTool.Number.divForDollar(model.planListControlMoney, 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="备注">{{ 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-demand-plan-list ref="purchaseDemandPlanList" :viewFlag="true"></purchase-demand-plan-list>
    <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 PurchaseDemandPlanList from '../PurchaseDemandPlanList'
import { getAuditRecord } from '@/api/activiti/activiti'
const DetailListItem = DetailList.Item

export default {
  name: 'PurchaseDemandPlanDetail',
  components: {
    DetailList,
    PurchaseDemandPlanList,
    DetailListItem
  },
  data () {
    return {
      confirmLoading: false,
      mdl: {},
      modalTitle: null,
      visible: false,
      nodeMap: {},
      moneyTypeMap: {},
      nodes: [],
      auditFlag: false,
      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: [],
      currentNodes: [],
      // 下拉框map
      typeMap: {},
      model: {
        'name': null,
        'companyId': null,
        'companyName': null,
        'projectId': null,
        'projectName': null,
        'type': null,
        'addMoney': null,
        'planMoney': null,
        'projectMoneyType': null,
        'planListControlMoney': null,
        'usedMoney': null,
        'progress': null,
        'quantity': null,
        'boughtQuantity': null,
        'arrivedQuantity': null,
        'notArriveQuantity': null,
        'currentAuditUserName': null,
        'dollarExchangeRate': null,
        'status': null,
        'remark': null,
        'delFlag': null,
        'createdUserId': null,
        'updateUserId': null,
        'updateUserName': null,
        'updateTime': null
      }
    }
  },
  created () {
    // 下拉框map
    this.typeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.PURCHASE_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.purchaseDemandPlanList
      modal.base(record.id)
      this.getAuditRecords(record)
    },
    getAuditRecords (record) {
      const processInstanceId = record.processInstanceId
      if (processInstanceId) {
        this.auditFlag = true
        const currentNodes = []
        // if (record.auditTime && (!record.addMoney || record.addMoney === 0)) {
        //   for (const value in this.nodeMap) {
        //     if (value !== '30' && value !== '20') {
        //       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>
