<template>
  <base-list>
    <template slot="n_header">
      <el-form v-model="pagerDto" inline>
        <el-form-item label="收货时间">
          <el-date-picker
            v-model="pagerDto._Range_ReceiveTime"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            value-format="yyyy-MM-dd"
            end-placeholder="结束日期"
          />
        </el-form-item>
        <el-form-item label="客户">
          <el-select
            v-model="pagerDto._Eq_CustomerId"
            placeholder="请选择客户"
            filterable
            clearable
          >
            <el-option
              v-for="item in Customers"
              :key="item.Id"
              :label="item.CustomerNumber"
              :value="item.Id"
            >
              <span style="float: left">{{ item.CompanyName }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{
                item.CustomerNumber
              }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="业务员">
          <el-select v-model="pagerDto._Eq_SalesmanUserId" placeholder="业务员" clearable filterable>
            <el-option
              v-for="item in salesmanUser"
              :key="item.Id"
              :label="item.NickName"
              :value="item.Id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="下单渠道">
          <el-select v-model="pagerDto._Eq_CreateOrderChannelId" placeholder="请选择" clearable>
            <el-option
              v-for="item in Channels"
              :key="item.Id"
              :label="item.Name"
              :value="item.Id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="dataLoading" @click="getDate()">
            搜索
          </el-button>
        </el-form-item>
      </el-form>
    </template>

    <template slot="n_option">
      <el-button size="small" :loading="outSumLoading" @click="shwoOut()">
        导出汇总项
      </el-button>
    </template>
    <template>
      <el-table
        v-loading="dataLoading"
        :data="data"
        :summary-method="getSummaries"
        show-summary
        :highlight-current-row="true"
        width="100%"
        border
      >
        <el-table-column label="客户" prop="客户" />
        <el-table-column label="结算金额" prop="结算金额" />
        <el-table-column label="收货票数" prop="收货票数" />
        <el-table-column label="业务员" prop="业务员" />
      </el-table>
    </template>
    <el-dialog
      title="分组方式"
      :visible.sync="dialogVisible"
    >
      <el-form inline label-width="100">
        <el-form-item label="渠道">
          <el-checkbox v-model="groupByChannel" />
        </el-form-item>
        <el-form-item label="客户">
          <el-checkbox v-model="groupByCustomer" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="outSum()">导出</el-button>
      </span>
    </el-dialog>
  </base-list>
</template>
<script>
import { _getCustomers, _getChannel } from '@/api/baseInfo'
import { _getSalesmanUsers } from '@/api/customer'
import { QueryCustomerIncome, _outFinanceReport } from '@/api/customerAccount/api'
import BaseList from '@/components/Xyy/baselist'
export default {
  name: 'CustomerIncome',
  components: { 'base-list': BaseList },
  data() {
    return {
      dialogVisible: false,
      Channels: [],
      Customers: [],
      salesmanUser: [],
      dataLoading: false,
      outSumLoading: false,
      data: [],
      groupByChannel: false,
      groupByCustomer: true,
      pagerDto: { PageIndex: 0, PageSize: 10, total: 0, _Range_ReceiveTime: [], GroupBy: [] }
    }
  },
  mounted() {
    _getCustomers().then(r => {
      this.Customers = r
    })
    _getChannel().then(r => {
      this.Channels = r
    })
    _getSalesmanUsers().then(response => {
      this.salesmanUser = response
    })
  },
  methods: {
    shwoOut() {
      this.dialogVisible = true
    },
    getDate() {
      if (this.pagerDto._Range_ReceiveTime.length == 0) {
        this.$alert('必须填入收货时间')
        return
      }
      this.dataLoading = true
      this.pagerDto.GroupBy = ['Customer']
      QueryCustomerIncome(this.pagerDto).then(r => {
        this.data = r
      }).finally(r => {
        this.dataLoading = false
      })
    },
    outSum() {
      this.pagerDto.GroupBy = []
      if (this.pagerDto._Range_ReceiveTime.length == 0) {
        this.$alert('必须填入收货时间')
        return
      }
      this.outSumLoading = false
      if (this.groupByChannel) {
        this.pagerDto.GroupBy.push('Channel')
      }
      if (this.groupByCustomer) {
        this.pagerDto.GroupBy.push('Customer')
      }
      _outFinanceReport(this.pagerDto, '公司小包收入汇总' + this.pagerDto._Range_ReceiveTime[0] + '至' + this.pagerDto._Range_ReceiveTime[1] + '.xlsx')
    },
    getSummaries() {
      const sums = []
      var SystemNoCount = 0
      var IncomeCount = 0
      for (var i = 0; i < this.data.length; i++) {
        SystemNoCount += this.data[i].收货票数
        IncomeCount += this.data[i].结算金额
      }
      sums[0] = '合计'
      sums[1] = '收入:' + Math.round(IncomeCount)
      sums[2] = SystemNoCount + '票'
      return sums
    }

  }
}
</script>
