<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" @click="save()">提交审批</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"></title-divider>
      <a-table
        :columns="auditColumns"
        :dataSource="auditData"
        :pagination="false"
      >
      </a-table>
      <title-divider title="审核信息" width="90px"></title-divider>
      <a-row class="form-row" :gutter="16">
        <a-col :lg="12" :md="12" :sm="24">

          <a-radio-group v-model="auditFlag" @change="changeOpinion">
            <a-radio :value="1">通过</a-radio>
            <a-radio :value="0">拒绝</a-radio>
          </a-radio-group>
        </a-col>
      </a-row>
      <a-row class="form-row" :gutter="16">
        <a-col :lg="12" :md="12" :sm="24">
          <a-form-item
            label="审核描述"
          >
            <a-textarea
              v-model="refuseReason"/>
          </a-form-item>
        </a-col>
      </a-row>
    </div>
  </a-card>
</template>

<script>
import DetailList from '@/components/tools/DetailList'
import { fetchSbInfo, auditProcess, 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 { getAuditRecord } from '@/api/activiti/activiti'
import { formatDate } from '@/utils/util'
const DetailListItem = DetailList.Item

export default {
  name: 'DetailAuditScrap',
  components: {
    DetailList,
    DetailListItem,
    RepairApplicationFormTable,
    CheckJobTable,
    SparePartUsedSelectTable,
    SbOilPageTable,
    SbRunFillPageTable,
    SbInspectionFillPageTable,
    PrintInForm
  },
  data () {
    return {
      confirmLoading: false,
      mdl: {},
      modalTitle: null,
      visible: false,
      visible2: true,
      refuseReason: '同意',
      auditFlag: 1,
      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: [],
      model: {
        'no': null,
        'financingNo': null,
        'name': 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: {},
      firmProducerLevelMap: {},
      firmSupplier: {},
      firmProducer: {},
      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.firmProducerLevelMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.FIRM_PRODUCER_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)
  },
  computed: {
  },
  methods: {
    base (record) {
      this.visible = true
      this.modalTitle = '详情'
      this.model = {}
      this.taskId = record.taskId
      fetchSbInfo({ id: record.targetId }).then(res => {
        const data = res.data
        this.model = data
        this.getAuditRecords(data)
      })
    },
    getFormatOpinion (opinion) {
      if (opinion === '提交申请') {
        return '同意'
      } else {
        return opinion
      }
    },
    getAuditRecords (record) {
      const processInstanceId = record.processInstanceId
      if (processInstanceId) {
        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)
          }
          this.auditData = auditData
        })
      }
    },
    save () {
      this.confirmLoading = true
      auditProcess({ id: this.model.id, refuseReason: this.refuseReason, auditFlag: this.auditFlag, taskId: this.taskId })
        .then(() => {
          this.$message.info('操作成功')
          this.handleOk()
        }).catch(() => {
          this.confirmLoading = false
        })
    },
    handleCancel (values) {
      this.visible = false
      this.confirmLoading = false
      this.$emit('ok', values)
    },
    handleOk () {
      this.visible = true
      this.visible2 = true
      this.confirmLoading = false
      fetchSbInfo({ id: this.model.id }).then(res => {
        const data = res.data
        this.model = data
        this.getAuditRecords(data)
      })
    },
    updateSbInfoScrapForAudit () {
      updateSbInfoScrapForAudit(this.model)
        .then(() => {
          this.handleCancel(this.model)
        }).catch(() => {
          this.confirmLoading = false
        })
    },
    changeOpinion (e) {
      this.refuseReason = e.target.value ? '同意' : '不同意'
    }
  }
}
</script>
