<template>
  <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.procurementCode}}</e-desc-item>
        <e-desc-item label="付款方式" span="2">
          <el-checkbox-group v-model="idsPayType">
            <el-checkbox disabled class="my-cb"  v-for="(item,i) in optionsPayType" :label="item.code" :key="item.code"><span>{{item.value}}</span></el-checkbox>
          </el-checkbox-group>
        </e-desc-item>

        <e-desc-item label="申请人">{{dataForm.applier}}</e-desc-item>
        <e-desc-item label="申请部门" span="2">{{dataForm.orgName}}</e-desc-item>
      </e-desc>
      <e-desc title="采购物品明细">
        <el-table
          :data="dataForm.purchaseDetails"
          border
          style="width: 100%;">
          <el-table-column
            label="序号"
            type="index"
            width="50"
            align="center">
          </el-table-column>
          <el-table-column
            prop="detailId"
            label="ID"
            v-if="false">
          </el-table-column>
          <el-table-column
            prop="materialName"
            header-align="center"
            align="center"
            min-width="120"
            :show-tooltip-when-overflow="true"
            label="物品名称">
          </el-table-column>
          <el-table-column
            prop="specification"
            header-align="center"
            align="center"
            min-width="120"
            :show-tooltip-when-overflow="true"
            label="型号及规格">
          </el-table-column>
          <el-table-column
            prop="purchaseType"
            header-align="center"
            align="center"
            :formatter="formatType"
            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="deadline"
            header-align="center"
            align="center"
            min-width="160"
            :show-tooltip-when-overflow="true"
            label="采购期限">
          </el-table-column>
          <el-table-column
            prop="batchNumber"
            header-align="center"
            align="center"
            min-width="140"
            :show-tooltip-when-overflow="true"
            label="批次号/用途">
          </el-table-column>
          <el-table-column
            prop="price"
            header-align="center"
            align="center"
            min-width="100"
            label="不含税单价">
          </el-table-column>
          <el-table-column
            prop="taxPrice"
            header-align="center"
            align="center"
            label="含税单价">
          </el-table-column>
          <el-table-column
            prop="taxAmount"
            header-align="center"
            align="center"
            label="含税总价">
            <template slot-scope="scope">
              <span>{{ (scope.row.cnt*scope.row.taxPrice).toFixed(2) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="taxRate"
            header-align="center"
            align="center"
            label="税率">
            <template slot-scope="scope">
              <span>
                {{scope.row.taxRate}} %
              </span>
            </template>
          </el-table-column>
          <el-table-column
            prop="notes"
            header-align="center"
            align="center"
            min-width="160"
            :show-tooltip-when-overflow="true"
            label="备注">
          </el-table-column>
        </el-table>
      </e-desc>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="onChose">返回</el-button>
    </span>
  </div>
</template>

<script>
  import EDesc from '../common/e-desc'
  import EDescItem from '../common/e-desc-item'
  import { dealStepData, dealStepLogs } from '@/api/util'
  import { getPurchaseDetail } from '@/api/sale'
  import ApproveComponent from '../common/approve-component'
  import { getDictList } from '@/api/dict'
  export default {
    name: 'purchase-detail',
    components: {
      EDesc, EDescItem, ApproveComponent
    },
    data () {
      return {
        visible: false,
        isFlow: false,
        id: 0,
        dataForm: {},

        stepList: [],
        logList: [],
        idsPayType: [],
        optionsType: [],
        optionsPayType: [
          {
            code: '0', value: '对公转账'
          },
          {
            code: '1', value: '先行垫付'
          },
          {
            code: '2', value: '财务预支'
          }
        ]
      }
    },
    methods: {
      onChose () {
        this.$emit('onChose')
      },
      // 获取采购类别字典
      getTypeList () {
        getDictList({type: 'purchase_type'}).then(({data}) => {
          if (data) {
            this.optionsType = data
          }
        })
      },
      async init (id, businessType) {
        this.visible = true
        this.isFlow = !!(businessType && businessType !== '')
        this.id = id || 0
        this.dataForm = {}
        this.stepList = []
        this.logList = []
        this.idsPayType = []
        this.getTypeList()
        this.getDetails(businessType)
      },
      getDetails (businessType) {
        getPurchaseDetail(this.id).then(({data}) => {
          if (data && data.code === '200') {
            this.dataForm = data.data
            // 流程图展示
            dealStepData(data.data.workFlowBusinessExt.workFlowProcessStepList, this.stepList)
            dealStepLogs(data.data.workFlowBusinessExt.processLogList, this.logList)
            // 付款方式
            if (data.data.payType) {
              this.idsPayType.push(data.data.payType)
            }
            // 初始化审批Form
            this.showApproveForm(businessType, this.id)
          }
        })
      },
      // 百分比
      formatPercent (row) {
        if (!row.taxRate) return ''
        let str = (Number(row.taxRate)).toFixed(0)
        str += '%'
        return str
      },
      // 初始化审批Form
      showApproveForm (businessType, businessId) {
        if (this.isFlow) {
          this.$nextTick(() => {
            this.$refs.approve.init(businessType, businessId)
          })
        }
      },
      // 审批完成
      approveFinished () {
        this.onChose()
        this.$emit('approveFinished')
      },
      // 采购类型
      formatType (row) {
        if (!row.purchaseType || !this.optionsType) return ''
        const item1 = this.optionsType.find((item) => item.code === row.purchaseType.toString())
        return item1 ? item1.value : ''
      }
    }
  }
</script>

<style scoped>
.my-line{
  border-bottom: 1px solid #c0c4cc;
  margin-bottom: 10px;
}
.title{
  padding: 10px 0 ;
}
</style>
