<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>
            <el-divider direction="vertical"></el-divider>
            <!--v-if="isOrderInList(item)"            -->
            <el-button v-if="!item.hasInvoiceApply" type="primary" :disabled="isButtonDisabled(item)" @click="handleChoose(item,true)">
              {{ (item.orderType == 6 || Number(item.amountTotalPrice) < 0) ? '收票' : '开票' }}
            </el-button>
            <span v-else style="color: red">{{'已申请' }}</span>
            <!--            <el-button type="danger" v-else @click="handleChoose(item,false)">取消选择</el-button>-->
          </div>
        </div>
        <template v-if="item.orderType!=6">
          <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" 
                           :datasource="j.detailList"
                           :toolkit="[]"  class="time-form">
            </ele-pro-table>
          </div>
        </template>
        <div v-else>
          <ele-pro-table :show-summary="true" :summary-method="getSummaries" ref="table" row-key="id" :needPage="false"
                         :columns="getColumns({subType:10})" sub-title="赔付订单" max-height="500px"
                         style="margin-bottom: 10px"
                          :datasource="item.detailList"
                          :toolkit="[]"  class="time-form">
          </ele-pro-table>
        </div>
      </div>
    </div>
  </div>

</template>
<script>

export default {
  components: {},
  inject:['invoiceType'],
  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: 'totalCount',
            label: '数量',
            align: 'center',
            slot: 'totalCount'
          },
          {
            minWidth: 120,
            prop: 'measuringUnit',
            label: '计量单位',
            align: 'center',
            slot: 'measuringUnit'
          },
          {
            width: 100,
            prop: 'receiveTotalWeight',
            label: '收货总重',
            slot: 'receiveTotalWeight',
            align: "center"
          },
          {
            width: 100,
            prop: 'sendTotalWeight',
            label: '发货总重',
            slot: 'sendTotalWeight',
            align: "center",
            headerSlot: 'headerTotalCount'
          },

          {
            width: 100,
            prop: 'increaseTotalWeight',
            label: '增重重量',
            slot: 'increaseTotalWeight',
            align: "center"
          },
          {
            width: 100,
            prop: 'weightUnit',
            label: '重量单位',
            slot: 'weightUnit',
            align: "center"
          },
          {
            minWidth: 100,
            prop: 'discountSinglePrice',
            label: '单价',
            align: 'center',
            slot: 'discountSinglePrice'
          },
          {
            minWidth: 120,
            prop: 'discountTotalPrice',
            label: '合计金额',
            slot: 'discountTotalPrice',
            align: 'center',
          },
        ]
        let permissionType = {}
        permissionType[this.taskDefinitionKey] && basicFields.push(...permissionType[this.taskDefinitionKey])
        return basicFields
      }
    },
    isButtonDisabled() {
      return (item) => {
        return (this.invoiceType() == 2 && ((item.orderType != 6 && Number(item.amountTotalPrice) >= 0))) || (this.invoiceType() == 1 && (item.orderType == 6 || Number(item.amountTotalPrice) <= 0));
      }
    },
    isOrderInList() {
      return (item) => {
        return !this.orderList.includes(item.orderNo);
      }

    },
  },
  data() {
    return {
      rules: {},
      form: {},
      subTypeList: {
        10: '发货单：',
        11: '退货单：',
      },
      subColumnsTypeList: {
        10: '发货日期',
        11: '退货日期',
      },
      orderList: [],
    };
  },
  mounted() {
  },
  methods: {
    handleChoose(val, flag) {
      this.$emit('getAccountInfo', val)
      return
      if (flag) {
        this.orderList.push(val.orderNo)
      } else {
        this.orderList = this.orderList.filter(item => item !== val.orderNo)
      }
    },
    remove(tableIndex, lineIndex, delType) {
      let params = {
        tableIndex,
        lineIndex,
        delType
      }
      this.$emit('handleRemove', params)
    },
    getSummaries(param) {
      const {columns, data} = param;
      const sums = [];
      let fields = ['totalPrice', 'notInvoicedPrice', 'invoicedPrice', 'receivablePrice', 'receivedPrice', '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>
