<script>
import Decimal from 'decimal.js'
import { parseTime } from '@/utils/ruoyi'
export default {
  name: 'CheckDetailProd',
  props: {
    unloadTime: {
      type: String,
      required: false
    },
    list: {
      type: Array,
      required: true
    },
    attrList: {
      type: Array,
      required: true
    },
    prodTypeList: {
      type: Array,
      required: true
    },
    currencyMap: {
      type: Object,
      required: true
    },
    unitMap:{
      type: Object,
      required: true
    },
    transportId: {
      type: Number,
      required: false
    },
  },
  data(){
    return {
      feeDetail: null,
      showFeeDetailDialog: false
    }
  },
  computed:{
    getProdTypeName() {
      return (prodType) => {
        for (let index in this.prodTypeList) {
          let prodTypeItem = this.prodTypeList[index];
          if (prodTypeItem.id == prodType) {
            return this.$l(prodTypeItem, "title");
          }
        }
      };
    },
    getUnloadTime() {
      return this.unloadTime;
    },
    // 显示特性
    showAttrText(){
      return ids => {
        if(!ids) return ''
        ids = !Array.isArray(ids) ? ids.split(',') : ids
        const attrMap = {}
        this.attrList.forEach(item => {
          attrMap[item.id] = this.$l(item, 'attrName')
        })
        return ids.map(id => attrMap[id]).join(',')
      }
    },
  },
  methods:{

    // 处理异常
    handleError(row) {
      this.$emit("closeStart", "close");
      this.$router.push({
        path: `/order/pending?id=${row.orderId}`,
      });
    },

    // 获取储位名称
    getLocationName(locationArr){
      if(!locationArr || !locationArr.length) return ''
      let arr = []
      locationArr.forEach(item => {
        arr.push(`${item.areaName}${item.locationName || ''}`)
      })
      return Array.from(new Set(arr)).join(",")
    },
    // 获取品名汇总数据
    getProdSummary(e){
      console.log("getProdSummart", e)
      // 货值
      let worth = new Decimal(0)
      // 填单数据
      let fillData = {
        num: new Decimal(0),
        weight: new Decimal(0),
        volume: new Decimal(0),
        quantity: new Decimal(0)
      }
      // 装柜/出货数据
      let warehouseInData = {
        num: new Decimal(0),
        weight: new Decimal(0),
        volume: new Decimal(0),
        quantity: new Decimal(0)
      }
      // 到仓数据
      let toWarehouseData = {
        checkNum: new Decimal(0),
        checkWeight: new Decimal(0),
        checkVolume: new Decimal(0),
        checkQuantity: new Decimal(0)
      }
      // 收费数据
      let chargeData = {
        weight: new Decimal(0),
        volume: new Decimal(0),
      }
      this.list?.forEach(item => {
        worth = worth.plus(item.worth || 0)
        fillData.num = fillData.num.plus(item.num || 0)
        fillData.weight = fillData.weight.plus(item.weight || 0)
        fillData.volume = fillData.volume.plus(item.volume || 0)
        fillData.quantity = fillData.quantity.plus(item.quantity || 0)
        if(item.warehouseInInfoVO){
          warehouseInData.num = warehouseInData.num.plus(item.warehouseInInfoVO.cartonsNum || 0)
          warehouseInData.weight = warehouseInData.weight.plus(item.warehouseInInfoVO.weight || 0)
          warehouseInData.volume = warehouseInData.volume.plus(item.warehouseInInfoVO.volume || 0)
          warehouseInData.quantity = warehouseInData.quantity.plus(item.warehouseInInfoVO.quantityAll || 0)
        }
        if (item.warehouseCheckInfoVO){
          toWarehouseData.checkNum = toWarehouseData.checkNum.plus(item.warehouseCheckInfoVO.cartonsNum || 0)
          toWarehouseData.checkWeight = toWarehouseData.checkWeight.plus(item.warehouseCheckInfoVO.weight || 0)
          toWarehouseData.checkVolume = toWarehouseData.checkVolume.plus(item.warehouseCheckInfoVO.volume || 0)
          toWarehouseData.checkQuantity = toWarehouseData.checkQuantity.plus(item.warehouseCheckInfoVO.quantity || 0)
        }
        chargeData.weight = chargeData.weight.plus(item.chargeWeight || 0)
        chargeData.volume = chargeData.volume.plus(item.chargeVolume || 0)
      })
      const summary = Array(8).fill(null)
      summary[3] = this.$t("合计")
      // 入仓
      summary[4] = `${warehouseInData.num}${this.$t('箱')} ${warehouseInData.weight}KG ${warehouseInData.volume}m³ ${warehouseInData.quantity}${this.$t('个')}`
      // 到仓
      summary[5] = `${toWarehouseData.checkNum}${this.$t('箱')} ${toWarehouseData.checkWeight}KG ${toWarehouseData.checkVolume}m³ ${toWarehouseData.checkQuantity}${this.$t('个')}`
      return summary
    }
  }
}
</script>

<template>
  <div>
    <el-table
      :data="list"
      border
      show-summary
      :summary-method="getProdSummary"
      style="width: 100%">
      <el-table-column :label="$t('序号')" width="90px">
        <template slot-scope="scope">{{scope.$index + 1}}</template>
      </el-table-column>
      <el-table-column :label="$t('品名')" width="180px">
          <template slot-scope="{row}">
            <div>{{ getProdTypeName(row.prodType) }}</div>
            <div>{{row.prodTitleZh}}/{{row.prodTitleEn}}</div>
          </template>
      </el-table-column>
      <!--<el-table-column prop="prodTitleEn" :label="$t('品名')" />-->
      <el-table-column :label="$t('品牌')"  width="90px">
        <template slot-scope="{row}">
          <template v-if="row.checkBrandName">{{row.checkBrandName}}</template>
          <dict-tag v-else :type="DICT_TYPE.ECW_IS_BRAND" :value="row.brandType" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('特性')"  width="120px">
        <template slot-scope="{row}">
          {{showAttrText(row.warehouseInInfoVO ? row.warehouseInProdAttrIds : row.prodAttrIds)}}
        </template>
      </el-table-column>
      <el-table-column  :label="$t('装柜/出货信息')"  width="150px">
        <template slot-scope="{row}">
          <div v-if="row.warehouseInInfoVO">
            {{row.warehouseInInfoVO.cartonsNum}}{{$t('箱')}}
            {{row.warehouseInInfoVO.weight}}Kg
            {{row.warehouseInInfoVO.volume}}m³
            {{row.warehouseInInfoVO.quantityAll}}{{$t('个')}}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="sumNum" :label="$t('到仓信息')"  width="150px">
        <template slot-scope="{row}">
          <div v-if="row.warehouseCheckInfoVO">
            {{row.warehouseCheckInfoVO.cartonsNum}}{{$t('箱')}}
            {{row.warehouseCheckInfoVO.weight}}Kg
            {{row.warehouseCheckInfoVO.volume}}m³
            {{row.warehouseCheckInfoVO.quantity}}{{$t('个')}}
          </div>
        </template>
      </el-table-column>
      <el-table-column :label="$t('到仓时间')" width="150px">
        <template slot-scope="{row}">
          {{parseTime(row.warehouseCheckInfoVO.checkTime)}}
        </template>
      </el-table-column>
      <el-table-column :label="$t('异常类型')" >
        <template slot-scope="{row}">
          <div v-for="(itemException, i) in row.itemExceptionList" :key="i" :src="row.itemExceptionList" >
            <el-button type="text" size="small" @click="handleError(row)">{{itemException.exceptionType}}-{{itemException.result}}</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>