<template>
  <!-- <el-dialog
    title="查看"
    width="70%"
    :close-on-click-modal="false"
    :visible.sync="visible"> -->
  <div>
    <div class="my-title">查看</div>
    <div style="margin-left: 20px;margin-right: 20px">
      <!-- 工作流 -->
      <div v-show="dataForm.workFlowBusinessExt">
        <el-steps :active="dataForm.workFlowBusinessExt&&dataForm.workFlowBusinessExt.workFlowProcessStepList?dataForm.workFlowBusinessExt.workFlowProcessStepList.length + 2:0" 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.productName}}</e-desc-item>
        <e-desc-item label="产品规格">{{dataForm.productSpec}}</e-desc-item>
        <e-desc-item label="产品类别">{{dataForm.productTypeValue}}</e-desc-item>

        <e-desc-item label="产品来源">{{dataForm.sourceName}}</e-desc-item>
        <e-desc-item v-if="dataForm.noticeChangeRecord" label="产品变更描述">{{dataForm.noticeChangeRecord?dataForm.noticeChangeRecord.description:''}}</e-desc-item>

        <e-desc-item v-if="dataForm.noticeChangeRecord && dataForm.noticeChangeRecord.attachList" label="变更通知单" span="3">
          <div v-for="(item, index) in dataForm.noticeChangeRecord.attachList" 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-item v-if="fileList" label="产品图纸" span="3">
          <div v-for="(item, index) in fileList" 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-item v-if="dataForm.noticeChangeRecord && dataForm.noticeChangeRecord.attachList" label="所需文件资料" span="3">
          <div v-for="(item, index) in dataForm.noticeChangeRecord.attachList" 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-item v-if="dataForm.attachList" label="产品技术文件" span="3">
          <div v-for="(item, index) in dataForm.attachList" 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-item v-if="dataForm.listingList" label="产品配料清单" span="3">
          <div v-for="(item, index) in dataForm.listingList" 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-item label="备注说明" span="2" style="min-height: 80px">{{dataForm.notes}}</e-desc-item>
      </e-desc>
      <e-desc title="组合小产品">
        <el-table
          :data="productDetails"
          border
          style="width: 100%;">
          <el-table-column
            label="序号"
            type="index"
            width="50"
            align="center">
          </el-table-column>
          <el-table-column
            prop="productName"
            header-align="center"
            align="center"
            label="产品名称">
          </el-table-column>
          <el-table-column
            prop="materialName"
            header-align="center"
            align="center"
            label="物品名称">
          </el-table-column>
          <el-table-column
            prop="specifications"
            header-align="center"
            align="center"
            label="规格">
          </el-table-column>
          <el-table-column
            prop="cnt"
            header-align="center"
            align="center"
            label="数量">
          </el-table-column>
          <el-table-column
            prop="notes"
            header-align="center"
            align="center"
            label="备注">
          </el-table-column>
        </el-table>
      </e-desc>
<!--      <e-desc title="产品配料清单">-->
<!--        <el-table-->
<!--          :data="materialList"-->
<!--          border-->
<!--          style="width: 100%;">-->
<!--          <el-table-column-->
<!--            label="序号"-->
<!--            type="index"-->
<!--            width="50"-->
<!--            align="center">-->
<!--          </el-table-column>-->
<!--          <el-table-column-->
<!--            prop="materialName"-->
<!--            header-align="center"-->
<!--            align="center"-->
<!--            label="物品名称">-->
<!--          </el-table-column>-->
<!--          <el-table-column-->
<!--            prop="specifications"-->
<!--            header-align="center"-->
<!--            align="center"-->
<!--            label="规格">-->
<!--          </el-table-column>-->
<!--          <el-table-column-->
<!--            prop="cnt"-->
<!--            header-align="center"-->
<!--            align="center"-->
<!--            label="数量">-->
<!--          </el-table-column>-->
<!--          <el-table-column-->
<!--            prop="unitName"-->
<!--            header-align="center"-->
<!--            align="center"-->
<!--            label="单位">-->
<!--          </el-table-column>-->
<!--          <el-table-column-->
<!--            prop="notes"-->
<!--            header-align="center"-->
<!--            align="center"-->
<!--            label="备注">-->
<!--          </el-table-column>-->
<!--        </el-table>-->
<!--      </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 EDesc from '../common/e-desc'
  import EDescItem from '../common/e-desc-item'
  import { dealStepData, dealStepLogs } from '@/api/util'
  import uploadComponent from '../common/upload-component'
  import { getProductDetail } from '@/api/product'
  import ApproveComponent from '../common/approve-component'
  import PreviewComponent from '../common/preview-component'
export default {
    name: 'product-detail',
    components: {
      PreviewComponent,
      EDesc,
      EDescItem,
      uploadComponent,
      ApproveComponent
    },
    data () {
      return {
        visible: false,
        isFlow: false,
        previewVisible: false,
        id: 0,
        dataForm: {},
        materialList: [],
        productDetails: [],
        stepList: [],
        logList: [],
        fileList: []
      }
    },
    methods: {
      onChose () {
        this.$emit('onChose')
      },
      async init (id, businessType) {
        this.visible = true
        this.isFlow = !!(businessType && businessType !== '')
        this.id = id || 0
        this.dataForm = {}
        this.materialList = []
        this.productDetails = []
        this.stepList = []
        this.logList = []
        this.fileList = []
        this.getDetails(businessType)
      },
      getDetails (businessType) {
        getProductDetail(this.id).then(({data}) => {
          if (data && data.code === '200') {
            this.dataForm = data.data
            // 流程图展示
            if (data.data.workFlowBusinessExt) {
              dealStepData(data.data.workFlowBusinessExt.workFlowProcessStepList, this.stepList)
              dealStepLogs(data.data.workFlowBusinessExt.processLogList, this.logList)
            }
            // 组合小产品
            data.data.composeProductMaterialList.forEach((item) => {
              this.productDetails.push(item)
            })
            // 产品配料清单
            if (data.data.productMaterialList) {
              data.data.productMaterialList.forEach((item) => {
                this.materialList.push(item)
              })
            }
            // 产品图纸
            if (data.data.proDrawings) {
              // this.dataForm.drawingIdList = []
              data.data.proDrawings.forEach((item) => {
                if (item.attachList) {
                  item.attachList.forEach((item1) => {
                    this.fileList.push({
                      fileName: item1.fileName,
                      url: item1.url,
                      id: item1.url
                    })
                  })
                }
              })
            }
            // 初始化审批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 scoped>
.my-line{
  border-bottom: 1px solid #c0c4cc;
  margin-bottom: 10px;
}
.title{
  padding: 10px 0 ;
}
</style>
