<template>
  <a-card :bordered="false" v-show="visible" class="card" :title="modalTitle">
    <div v-show="visible2" >
      <a-row :gutter="48" slot="extra">
        <a-col :md="48" :sm="48">
          <span class="table-page-search-submitButtons" style="float: right">
            <a-button type="error" :loading="confirmLoading" v-if="model.status != 3 && model.status != 4" @click="updateSbInfoScrapForAudit()">提交审批</a-button>
            <!--            <a-button style="margin-left: 8px" type="primary" @click="handlePrint()">打印</a-button>-->
            <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="3">
        <detail-list-item term="型号">{{ model.model }}</detail-list-item>
        <detail-list-item term="名称型号">{{ model.nameModel }}</detail-list-item>
        <detail-list-item term="设备名称">{{ model.name }}</detail-list-item>
        <detail-list-item term="设备类型">{{ model.typeName }}</detail-list-item>
        <detail-list-item term="自定义类型">{{ BaseTool.Object.getField(useTypeMap,model.useType) }}</detail-list-item>
        <detail-list-item term="设备等级">{{ BaseTool.Object.getField(levelMap,model.level) }}</detail-list-item>
        <detail-list-item term="生产商">{{ model.producerName }}</detail-list-item>
        <detail-list-item term="计量单位">{{ model.unit }}</detail-list-item>
        <detail-list-item term="设备原值">{{ BaseTool.Amount.formatter(model.initialValue) }}</detail-list-item>
        <detail-list-item term="当前价值">{{ BaseTool.Amount.formatter(model.currentValue) }}</detail-list-item>
        <detail-list-item term="已折旧价值">{{ BaseTool.Amount.formatter(model.cutValue) }}</detail-list-item>
        <detail-list-item term="净残值率">{{ BaseTool.Amount.formatter(model.rate) }}</detail-list-item>
        <detail-list-item term="折旧总计月份">{{ BaseTool.Amount.formatter(model.month) }}</detail-list-item>
        <detail-list-item term="已计提月份">{{ BaseTool.Amount.formatter(model.usedMonth) }}</detail-list-item>
      </detail-list>

      <title-divider title="基本信息" width="90px"></title-divider>
      <detail-list title="" :col="3">
        <detail-list-item term="设备编号">{{ model.no }}</detail-list-item>
        <detail-list-item term="财务编码">{{ model.financingNo }}</detail-list-item>
        <detail-list-item term="卡片编号">{{ model.cardNo }}</detail-list-item>
        <detail-list-item term="车牌号">{{ model.cph }}</detail-list-item>
        <detail-list-item term="资产现状说明">{{ model.remark }}</detail-list-item>
        <detail-list-item term="使用公司">{{ model.useCompanyName }}</detail-list-item>
        <detail-list-item term="使用项目部">{{ model.useProjectName }}</detail-list-item>
        <detail-list-item term="购置日期">{{ model.buyDate }}</detail-list-item>
        <detail-list-item term="投用日期">{{ model.startDate }}</detail-list-item>
        <detail-list-item term="状态"><badge :status="DictCache.COLOR.SB_INFO_STATUS[model.status]" :text="statusMap[model.status]"></badge></detail-list-item>
      </detail-list>
      <title-divider title="资产图片" width="90px"></title-divider>
      <detail-list title="" :col="6">
        <detail-list-item v-show="model.sbFileList!=null && model.sbFileList.length>0">
          <a-upload
            list-type="picture-card"
            :multiple="true"
            :fileList="BaseTool.UPLOAD.transImg(model.sbFileList)"
          >
          </a-upload>
        </detail-list-item>
      </detail-list>
      <title-divider title="审批信息" width="90px" v-if="auditFlag"></title-divider>
      <a-card :bordered="false" title="" v-if="auditFlag" class="audit-info">
        <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>
    </div>
    <print-in-form ref="basePrintModal" @ok="handleOk"/>
  </a-card>
</template>

<script>
import DetailList from '@/components/tools/DetailList'
import { fetchSbInfo, updateSbInfoScrapForAudit } from '@/api/sb/info'
import RepairApplicationFormTable from '@/views/repair/application-form/modules/RepairApplicationFormTable'
import CheckJobTable from '@/views/check/checkjob/modules/CheckJobTable'
import SparePartUsedSelectTable from '@/views/sqarepartmanage/sparepartused/modules/SparePartUsedSelectTable'
import SbOilPageTable from '@/views/sb/oil/modules/SbOilPageTable'
import SbRunFillPageTable from '@/views/sb/run-fill/modules/SbRunFillPageTable'
import SbInspectionFillPageTable from '@/views/sb/inspection-fill/modules/SbInspectionFillPageTable'
import PrintInForm from '@/views/sb/scrap/modules/PrintInForm'
import BaseTool from '@/utils/tool'
import { getAuditRecord } from '@/api/activiti/activiti'
import { formatDate } from '@/utils/util'
const DetailListItem = DetailList.Item

export default {
  name: 'SbInfoDetail',
  components: {
    DetailList,
    DetailListItem,
    RepairApplicationFormTable,
    CheckJobTable,
    SparePartUsedSelectTable,
    SbOilPageTable,
    SbRunFillPageTable,
    SbInspectionFillPageTable,
    PrintInForm
  },
  data () {
    return {
      confirmLoading: false,
      mdl: {},
      modalTitle: null,
      visible: false,
      visible2: true,
      auditColumns: [
        {
          title: '审批人',
          dataIndex: 'userName',
          key: 'userName',
          width: '20%'
        },
        {
          title: '审批时间',
          dataIndex: 'createTime',
          key: 'auditTime',
          width: '30%',
          scopedSlots: { customRender: 'keyword' }
        },
        {
          title: '审批结果',
          dataIndex: 'flag',
          key: 'flag',
          width: '30%',
          customRender: (text, row, index) => {
            return text ? '通过' : '不通过'
          }
        },
        {
          title: '审批意见',
          dataIndex: 'opinion',
          key: 'opinion'
        }
      ],
      auditData: [],
      nodeMap: {},
      nodes: [],
      auditFlag: false,
      current: 0,
      status: 'process',
      currentNodes: [],
      model: {
        'no': null,
        'financingNo': null,
        'name': null,
        'currentAuditUserName': null,
        'modelId': null,
        'typeId': null,
        'isShow': null,
        'isChild': null,
        'parentId': null,
        'level': null,
        'useType': null,
        'rate': null,
        'month': null,
        'usedMonth': null,
        'initialValue': null,
        'currentValue': null,
        'cutValue': null,
        'producerId': null,
        // 'sourceType': null,
        // 'sourceSbId': null,
        // 'parentId': null,
        'unit': null,
        // 'position': null,
        // 'guigeId': null,
        'useCompanyName': null,
        'useProjectName': null,
        'useDeptName': null,
        'useGroupName': null,
        'useUserName': null,
        'saveDeptName': null,
        'saveUserName': null,
        'repairUserName': null,
        'totalMiles': null,
        'totalHours': null,
        // 'lastBaoyangTime': null,
        // 'lastBaoyangMiles': null,
        // 'lastBaoyangHours': null,
        // 'baoyangTimes': null,
        'buyDate': null,
        'startDate': null,
        // 'nextCheckDate': null,
        // 'guaranteeDate': null,
        // 'workYear': null,
        // 'retirementDate': null,
        // 'depreciationType': null,
        'wbFile': null,
        'useFile': null,
        'checkFile': null,
        'sbImage': null,
        'qrCode': null,
        'status': null,
        'remark': null,
        'createdUserId': null,
        'updateTime': null,
        'parentName': null,
        'createdUserName': null,
        'typeName': null,
        'producerName': null,
        'zzh': null,
        'fdjxh': null,
        'fdjh': null,
        'cph': null,
        'dph': null,
        'zz': null,
        'rlType': null,
        'color': null,
        'seatNumber': null,
        'zjm': null,
        'jbdh': null,
        'sbdh': null,
        'bxgs': null
      },
      depreciationTypeMap: {},
      firmSupplier: {},
      statusMap: {},
      useTypeMap: {},
      levelMap: {},
      unitMap: {},
      colorMap: {},
      rlTypeMap: {},
      sourceTypeMap: {}
    }
  },
  created () {
    this.depreciationTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SBINFO_DEPRECIATIONTYPE)
    this.levelMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SBINFO_LEVEL)
    this.statusMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SB_INFO_STATUS)
    this.unitMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SBINFO_UNIT)
    this.colorMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SB_COLOR)
    this.rlTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.RANLIAO_TYPE)
    this.sourceTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SBTYPE_SOURCETYPE)
    this.useTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SB_USE_TYPE)
    this.nodeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SB_INFO_AUDIT_NODE)
  },
  computed: {
  },
  methods: {
    base (record) {
      this.visible = true
      this.modalTitle = '详情'
      if (record.targetId != null) {
        fetchSbInfo({ id: record.targetId }).then(res => {
          const data = res.data
          this.model = data
          this.getAuditRecords(data)
        })
      } else {
        this.model = record
        this.getAuditRecords(this.model)
      }
    },
    handlePrint (record) {
      const modal = this.$refs.basePrintModal
      this.visible2 = false
      modal.base(this.model)
    },
    handleCancel (values) {
      this.visible = false
      this.confirmLoading = false
      this.$emit('ok', values)
    },
    handleOk () {
      this.visible = true
      this.visible2 = true
      this.confirmLoading = false
    },
    filterCompany (flag, node) {
      if (!flag) {
        return parseInt(node) !== 5
      } else {
        return true
      }
    },
    getFormatOpinion (opinion) {
      if (opinion === '提交申请') {
        return '同意'
      } else {
        return opinion
      }
    },
    getAuditRecords (record) {
      const processInstanceId = record.processInstanceId
      if (processInstanceId) {
        this.auditFlag = true
        const currentNodes = []
        for (const value in this.nodeMap) {
          currentNodes.push({
            nodeValue: value,
            nodeName: this.nodeMap[value]
          })
        }
        getAuditRecord(processInstanceId).then(res => {
          const auditData = res.data
          for (let i = 0; i < auditData.length; i++) {
            const item = auditData[i]
            item.opinion = this.getFormatOpinion(item.opinion)
            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
            }
          }
          this.auditData = auditData
          this.currentNodes = currentNodes
          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'
            }
          }
        })
      }
    },
    updateSbInfoScrapForAudit () {
      this.confirmLoading = true
      updateSbInfoScrapForAudit(this.model)
        .then(() => {
          this.confirmLoading = false
          this.handleCancel(this.model)
        }).catch(() => {
          this.confirmLoading = false
        })
    }
  }
}
</script>
