<template>
  <div>
    <div v-for="(item) in datasource" style="margin-bottom: 10px">
      <div>
        <div style="margin-bottom: 10px;font-weight: bold;">
          <div>
            <el-divider direction="vertical"></el-divider>
            <span>采购订单：</span>
            <span>{{ item.orderNo }}</span>
            <el-divider direction="vertical"></el-divider>
            <span>计价方式：</span>
            <span> {{ item.pricingWay == 1 ? '按数量计费' : '按重量计费' }}</span>
            <el-divider direction="vertical"></el-divider>
            <span>总金额：</span>
            <span>{{item.amountTotalPrice}}</span>
            <el-divider direction="vertical"></el-divider>
            <span>应收金额：</span>
            <span>{{item.amountReceivablePrice}}</span>
            <el-divider direction="vertical"></el-divider>
            <span>应付金额：</span>
            <span>{{item.amountPayablePrice}}</span>


          </div>
        </div>
        <div  v-for="(j,i) in item.subList" :key="i">
          <ele-pro-table :show-summary="true" :summary-method="getSummaries" ref="table" row-key="id" :needPage="false"
                         :columns="getColumns(j)" max-height="500px" style="margin-bottom: 10px"
                         :sub-title="subTypeList[j.subType]+j.statementSubOrderCode" :toolkit="[]" :datasource="j.detailList"
                         cache-key="systemRoleTable17-11121" class="time-form">
            <template v-slot:action="{ row,$index }">
              <el-popconfirm class="ele-action" title="确定要删除吗？" @confirm="remove(index,$index,'line')">
                <template v-slot:reference>
                  <el-link type="danger" v-if="dialogType!=='view'" :underline="false" icon="el-icon-delete">
                    删除
                  </el-link>
                </template>
              </el-popconfirm>
            </template>
          </ele-pro-table>
        </div>
<!--        <template v-if="item.orderType!=6">-->
<!--        -->
<!--        </template>-->
<!--        <div v-else>-->
<!--          <ele-pro-table :show-summary="true" :summary-method="getSummaries" ref="table" row-key="id" :needPage="false"-->
<!--                         :columns="getColumns({subType:20})" sub-title="赔付订单" max-height="500px" style="margin-bottom: 10px"-->
<!--                         :toolkit="[]" :datasource="item.detailList"-->
<!--                         cache-key="systemRoleTable17-11121" class="time-form">-->
<!--            <template v-slot:action="{ row,$index }">-->
<!--              <el-popconfirm class="ele-action" title="确定要删除吗？" @confirm="remove(index,$index,'line')">-->
<!--                <template v-slot:reference>-->
<!--                  <el-link type="danger" v-if="dialogType!=='view'" :underline="false" icon="el-icon-delete">-->
<!--                    删除-->
<!--                  </el-link>-->
<!--                </template>-->
<!--              </el-popconfirm>-->
<!--            </template>-->
<!--            &lt;!&ndash;            <template v-slot:productName="{ row,$index }">&ndash;&gt;-->
<!--            &lt;!&ndash;              <el-popover&ndash;&gt;-->
<!--            &lt;!&ndash;                placement="right"&ndash;&gt;-->
<!--            &lt;!&ndash;                width="60%"&ndash;&gt;-->
<!--            &lt;!&ndash;                trigger="hover">&ndash;&gt;-->
<!--            &lt;!&ndash;                <ele-pro-table ref="childrenTable"&ndash;&gt;-->
<!--            &lt;!&ndash;                               row-key="id"&ndash;&gt;-->
<!--            &lt;!&ndash;                               max-height="300px"&ndash;&gt;-->
<!--            &lt;!&ndash;                               :needPage="false" :columns="childrenColumns" :toolkit="[]"&ndash;&gt;-->
<!--            &lt;!&ndash;                               :datasource="row.productList"&ndash;&gt;-->
<!--            &lt;!&ndash;                               cache-key="systemRoleTable17-222" class="time-form">&ndash;&gt;-->
<!--            &lt;!&ndash;                </ele-pro-table>&ndash;&gt;-->
<!--            &lt;!&ndash;                <el-button type="text" slot="reference">{{ row.productName }}</el-button>&ndash;&gt;-->
<!--            &lt;!&ndash;              </el-popover>&ndash;&gt;-->
<!--            &lt;!&ndash;            </template>&ndash;&gt;-->
<!--          </ele-pro-table>-->
<!--        </div>-->
      </div>
    </div>

    <!--    <el-row v-if="dialogType=='view'" style="margin-top: 10px">-->
    <!--      <el-col :span="6" style="float: right;">-->
    <!--        <el-descriptions class="margin-top" direction="vertical" :column="3" size="small" border>-->
    <!--          <el-descriptions-item label="本期应收款总额">{{ dataForm.amountReceivablePrice }}</el-descriptions-item>-->
    <!--          <el-descriptions-item label="截止上期欠款">-->
    <!--            <template>{{ dataForm.previousPeriodEndDebt }}</template>-->
    <!--          </el-descriptions-item>-->
    <!--        </el-descriptions>-->
    <!--      </el-col>-->
    <!--    </el-row>-->
  </div>

</template>
<script>

export default {
  components: {},
  props: ['datasource', 'dialogType','dataForm'],
  computed: {
    getColumns() {
      return (j)=>{
        let basicFields = [
          {
            width: 60,
            label: '序号',
            type: 'index',
            columnKey: 'index',
            align: 'center',
          },
          {
            minWidth: 130,
            prop: 'productOperateTime',
            label:  this.subColumnsTypeList[j.subType],
            align: 'center',
            slot: 'productOperateTime',
            showOverflowTooltip: true
          },
          {
            minWidth: 120,
            prop: 'productCode',
            label: '产品编码',
            slot: 'productCode',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            minWidth: 140,
            prop: 'productName',
            label: '产品名称',
            slot: 'productName',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            minWidth: 100,
            prop: 'specification',
            label: '规格',
            align: 'center',
            slot: 'specification',
            showOverflowTooltip: true
          },
          {
            minWidth: 100,
            prop: 'modelType',
            label: '型号',
            align: 'center',
            slot: 'modelType',
            showOverflowTooltip: true
          },
          // {
          //   minWidth: 100,
          //   prop: 'orderNo',
          //   label: '销售订单编码',
          //   align: 'center',
          //   slot: 'orderNo',
          //   showOverflowTooltip: true
          // },
          // {
          //   minWidth: 100,
          //   prop: 'pricingWay',
          //   label: '计价方式',
          //   align: 'center',
          //   slot: 'pricingWay',
          //   showOverflowTooltip: true,
          //   formatter: (_row, _column, cellValue) => {
          //     return _row.pricingWay == 1 ? '按数量计价' : '按重量计价'
          //   }
          // },
          {
            minWidth: 100,
            prop: 'totalCount',
            label: '数量',
            align: 'center',
            slot: 'totalCount'
          },
          {
            minWidth: 120,
            prop: 'measuringUnit',
            label: '计量单位',
            align: 'center',
            slot: 'measuringUnit'
          },
          {
            width: 100,
            prop: 'sendTotalWeight',
            label: '发货总重',
            slot: 'sendTotalWeight',
            align: "center",
            headerSlot: 'headerTotalCount'
          },
          {
            width: 100,
            prop: 'receiveTotalWeight',
            label: '收货总重',
            slot: 'receiveTotalWeight',
            align: "center"
          },
          {
            width: 100,
            prop: 'increaseTotalWeight',
            label: '增重重量',
            slot: 'increaseTotalWeight',
            align: "center"
          },
          {
            width: 100,
            prop: 'weightUnit',
            label: '重量单位',
            slot: 'weightUnit',
            align: "center"
          },
          // {
          //   minWidth: 90,
          //   prop: 'singlePrice',
          //   label: '单价',
          //   align: 'center',
          //   slot: 'singlePrice'
          // },
          {
            minWidth: 100,
            prop: 'discountSinglePrice',
            label: '单价',
            align: 'center',
            slot: 'discountSinglePrice'
          },
          {
            minWidth: 120,
            prop: 'discountTotalPrice',
            label: '合计金额',
            slot: 'discountTotalPrice',
            align: 'center',
          },
          // {
          //   minWidth: 100,
          //   prop: 'discountTotalPrice',
          //   label: '折让合计金额',
          //   align: 'center',
          //   slot: 'discountTotalPrice'
          // },
        ]
        let permissionType = {
          // 'add': [
          //   {
          //     columnKey: 'action',
          //     label: '操作',
          //     width: 120,
          //     align: 'center',
          //     resizable: false,
          //     slot: 'action',
          //     fixed: 'right',
          //     showOverflowTooltip: true
          //   }
          // ],
          // 'update': [
          //   {
          //     columnKey: 'action',
          //     label: '操作',
          //     width: 120,
          //     align: 'center',
          //     resizable: false,
          //     slot: 'action',
          //     fixed: 'right',
          //     showOverflowTooltip: true
          //   }
          // ],
          'view': [
            // {
            //   minWidth: 140,
            //   prop: 'paidDate',
            //   label: '付款日期',
            //   align: 'center',
            //   slot: 'paidDate',
            //   showOverflowTooltip: true
            // },
            // {
            //   minWidth: 130,
            //   prop: 'paidPrice',
            //   label: '已付款金额',
            //   align: 'center',
            //   slot: 'paidPrice'
            // },
            // {
            //   minWidth: 130,
            //   prop: 'notPaidPrice',
            //   label: '应付款金额',
            //   align: 'center',
            //   slot: 'notPaidPrice'
            // },
            // {
            //   minWidth: 130,
            //   prop: 'invoicedPrice',
            //   label: '已开票金额',
            //   align: 'center',
            //   slot: 'invoicedPrice'
            // },
            // {
            //   minWidth: 130,
            //   prop: 'notInvoicedPrice',
            //   label: '未开票金额',
            //   align: 'center',
            //   slot: 'notInvoicedPrice'
            // },
          ]
        }
        permissionType[this.dialogType] && basicFields.push(...permissionType[this.dialogType])
        return basicFields
      }

    },
  },
  data() {
    return {
      rules: {},
      form: {},
      subTypeList:{
        20:'收货单：',
        21:'退货单：',
      },
      subColumnsTypeList:{
        20:'收货日期',
        21:'退货日期',
      },
    };
  },
  methods: {
    remove(tableIndex, lineIndex, delType) {
      let params = {
        tableIndex,
        lineIndex,
        delType
      }
      this.$emit('handleRemove', params)
    },
    getSummaries(param) {
      const {columns, data} = param;
      const sums = [];
      let fields = ['totalPrice', 'paidPrice', 'notPaidPrice', 'invoicedPrice', 'notInvoicedPrice','discountTotalPrice']
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计';
          return;
        }
        const values = fields.includes(column.property) ? data.map(item => Number(item[column.property])) : []
        if (!values.every(value => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index] = (sums[index]).toFixed(2)
        } else {
          sums[index] = '';
        }
      });

      return sums;
    }
  }
};
</script>
<style lang="scss" scoped>
.headbox {
  display: flex;
  justify-content: space-between;
  align-items: center;

  .amount {
    font-size: 14px;
    font-weight: bold;
    padding-right: 30px;
  }
}

.time-form .el-form-item {
  margin-bottom: 0 !important;
}

::v-deep .period {
  display: flex;

  .borderleftnone {
    .el-input--medium .el-input__inner {
      border-top-right-radius: 0;
      border-bottom-right-radius: 0;
    }
  }

  .borderrightnone {
    .el-input--medium .el-input__inner {
      border-top-left-radius: 0;
      border-bottom-left-radius: 0;
    }
  }

}

::v-deep .time-form tbody > tr:hover > td {
  background-color: transparent !important;
}

::v-deep .time-form .el-table tr {
  background-color: #ffffff;
}

.pricebox {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  font-weight: bold;
}
::v-deep.el-divider {
  margin: 10px;
  font-weight: bold;
}

</style>
