<template>
  <div>
    <div class="my-title">查看</div>
    <div style="margin-left: 20px; margin-right: 20px">
      <!-- 工作流 -->
      <div v-show="dataForm.workFlowBusinessExt">
        <el-steps :active="activeNo" finish-status="success" align-center style="margin-bottom: 20px">
          <template v-for="(item, i) in stepList">
            <el-step
              :icon="item.icon"
              :title="item.title"
              :description="item.description"
            ></el-step>
          </template>
        </el-steps>
        <el-collapse style="margin-bottom: 20px">
          <el-collapse-item>
            <template slot="title">
              <span style="color: red">审批日志（展开查看更多）：</span>
            </template>
            <template v-for="(item, i) in logList">
              <div>
                {{ ++i }}：{{ item.approverName }} {{ item.createTime }}
                {{ item.approvalValue }}
              </div>
            </template>
          </el-collapse-item>
        </el-collapse>
      </div>
      <div v-if="isFlow">
        <approve-component ref="approve" @approveFinished="approveFinished" />
      </div>
      <e-desc title="基本信息" column="3">
        <e-desc-item label="首件鉴定编码">{{ dataForm.authenticateCode }}</e-desc-item>
        <e-desc-item label="任务单编码">{{ dataForm.orderCode }}</e-desc-item>
        <e-desc-item label="物料名称">{{ dataForm.productName }}</e-desc-item>

        <e-desc-item label="物料编号">{{ dataForm.productNumber }}</e-desc-item>
        <e-desc-item label="批次号">{{ dataForm.batchNumber }}</e-desc-item>
        <e-desc-item label="评审状态">{{dataForm.approveState?approveStateOption.find(item => item.value === dataForm.approveState).label:''}}</e-desc-item>
      </e-desc>
      <e-desc label="首件生产流程记录表">
        <e-desc-item span="3" label="首件生产流程记录表">
          <div v-for="(item, index) in dataForm.attachList1" style="display: inline">
            <span v-if="index > 0">, </span>
            <a :key="item.fileName + index" type="primary" href="#" @click="previewFile(item.fileName, item.url)">{{ item.fileName }}</a>
          </div>
        </e-desc-item>
      </e-desc>
      <e-desc label="首件生产过程原始记录">
        <e-desc-item span="3" label="首件生产过程原始记录">
          <div v-for="(item, index) in dataForm.attachList2" style="display: inline">
            <span v-if="index > 0">, </span>
            <a :key="item.fileName + index" type="primary" href="#" @click="previewFile(item.fileName, item.url)">{{ item.fileName }}</a>
          </div>
        </e-desc-item>
      </e-desc>
      <e-desc label="首件鉴定检验报告">
        <e-desc-item span="3" label="首件鉴定检验报告">
          <div v-for="(item, index) in dataForm.attachList3" style="display: inline">
            <span v-if="index > 0">, </span>
            <a :key="item.fileName + index" type="primary" href="#" @click="previewFile(item.fileName, item.url)">{{ item.fileName }}</a>
          </div>
        </e-desc-item>
      </e-desc>
      <e-desc label="首件鉴定审查报告">
        <e-desc-item span="3" label="首件鉴定审查报告">
          <div v-for="(item, index) in dataForm.attachList4" style="display: inline">
            <span v-if="index > 0">, </span>
            <a :key="item.fileName + index" type="primary" href="#" @click="previewFile(item.fileName, item.url)">{{ item.fileName }}</a>
          </div>
        </e-desc-item>
      </e-desc>
      <e-desc label="首件鉴定评审结论">
        <e-desc-item span="3" label="首件鉴定评审结论">
          <div v-for="(item, index) in dataForm.attachList5" style="display: inline">
            <span v-if="index > 0">, </span>
            <a :key="item.fileName + index" type="primary" href="#" @click="previewFile(item.fileName, item.url)">{{ item.fileName }}</a>
          </div>
        </e-desc-item>
      </e-desc>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="onChose">返回</el-button>
    </span>
    <!-- 文件预览 -->
    <preview-component v-if="previewVisible" ref="preview"/>
  </div>
</template>

<script>
import {getFirstDetail} from '@/api/quality'
import { dealStepData, dealStepLogs } from '@/api/util'
import EDesc from '../common/e-desc'
import EDescItem from '../common/e-desc-item'
import ApproveComponent from '../common/approve-component'
import UploadComponent from '../common/upload-component-v2'
import {approveStateOption} from '@/utils/enums'
import PreviewComponent from '@/views/modules/common/preview-component'
export default {
  name: 'first-detail',
  components: { PreviewComponent, EDesc, EDescItem, ApproveComponent, UploadComponent },
  data () {
    return {
      visible: false,
      isFlow: false,
      previewVisible: false,
      id: 0,
      approveStateOption: approveStateOption,
      dataForm: {},
      activeNo: 0,
      stepList: [],
      logList: []
    }
  },
  mounted () {},
  methods: {
    onChose () {
      this.$emit('onChose')
    },
    async init (id, businessType) {
      this.visible = true
      this.isFlow = !!(businessType && businessType !== '')
      this.id = id || 0
      this.dataForm = {
        workFlowBusinessExt: null
      }
      this.stepList = []
      this.logList = []
      this.getDetails(businessType)
    },
    getDetails (businessType) {
      getFirstDetail(this.id).then(({ data }) => {
        if (data && data.code === '200' && data.data) {
          this.dataForm = data.data
          // 流程图展示
          if (data.data.workFlowBusinessExt) {
            dealStepData(data.data.workFlowBusinessExt.workFlowProcessStepList, this.stepList)
            dealStepLogs(data.data.workFlowBusinessExt.processLogList, this.logList)
          }
          if (data.data.workFlowProcessStepList) {
            this.activeNo = Number(data.data.workFlowProcessStepList.activeNo)
          }
          // 初始化审批Form
          this.showApproveForm(businessType, this.id)
        }
      })
    },
      // 初始化审批Form
    showApproveForm (businessType, businessId) {
      if (this.isFlow) {
        this.$nextTick(() => {
          this.$refs.approve.init(businessType, businessId)
        })
      }
    },
    // 审批完成
    approveFinished () {
      this.onChose()
      this.$emit('approveFinished')
    },
    // 预览
    previewFile (fileName, url) {
      this.previewVisible = true
      this.$nextTick(() => {
        this.$refs.preview.init(fileName, url)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
