<template>
  <div>
    <a-card size="small" :bordered="false" class="salesDetailsList-wrap searchBoxNormal">
      <!-- 搜索条件 -->
      <div class="table-page-search-wrapper" ref="tableSearch">
        <a-form-model
          id="salesDetailsList-form"
          ref="ruleForm"
          class="form-model-con"
          layout="inline"
          :model="queryParam"
          :rules="rules"
          @keyup.enter.native="handleSearch">
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-model-item label="下推时间" prop="time">
                <rangeDate ref="rangeDate" id="salesDetailsList-time" :value="queryParam.time" @change="dateChange" />
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="产品编码">
                <a-input id="salesDetailsList-productCode" v-model.trim="queryParam.productCode" allowClear placeholder="请输入产品编码"/>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="客户名称">
                <a-input id="salesDetailsList-dealerName" v-model.trim="queryParam.dealerName" allowClear placeholder="请输入客户名称"/>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="销售单号">
                <a-input id="salesDetailsList-salesBillNo" v-model.trim="queryParam.salesBillNo" allowClear placeholder="请输入销售单号"/>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="出库仓库">
                <chooseWarehouse id="salesDetailsList-warehouseSn" ref="warehouse" isPermission v-model="queryParam.warehouseSn"></chooseWarehouse>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="产品名称">
                <a-input id="salesDetailsList-productName" v-model.trim="queryParam.productName" allowClear placeholder="请输入产品名称"/>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="客户级别">
                <v-select
                  v-model="queryParam.dealerLevel"
                  ref="dealerLevel"
                  id="salesDetailsList-dealerLevel"
                  code="DEALER_LEVEL"
                  placeholder="请选择客户级别"
                  allowClear></v-select>
              </a-form-model-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="24">
                <a-form-model-item label="备货单号">
                  <a-input id="salesDetailsList-dispatchBillNo" v-model.trim="queryParam.dispatchBillNo" allowClear placeholder="请输入备货单号"/>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="品牌分类">
                  <v-select code="BRAND_TYPE" id="salesDetailsList-productBrandTypeSn" v-model="queryParam.productBrandTypeSn" allowClear placeholder="请选择品牌分类"></v-select>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="产品品牌">
                  <ProductBrand id="salesDetailsList-productBrandSn" :brandType="queryParam.productBrandTypeSn" v-model="queryParam.productBrandSn"></ProductBrand>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="产品分类">
                  <ProductType id="salesDetailsList-productType" placeholder="请选择产品分类" @change="changeProductType" v-model="productType"></ProductType>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="产品类型" prop="promotionFlag">
                  <a-select id="salesDetailsList-promotionFlag" allowClear v-model.trim="queryParam.promotionFlag" :dropdownMatchSelectWidth="false" placeholder="请选择产品类型">
                    <a-select-option value="0">
                      正常产品
                    </a-select-option>
                    <a-select-option value="REGULAR">
                      正价产品（活动）
                    </a-select-option>
                    <a-select-option value="DISCOUNT">
                      特价产品
                    </a-select-option>
                    <a-select-option value="GIFT">
                      促销产品
                    </a-select-option>
                    <a-select-option value="GATE">
                      门槛产品
                    </a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="是否促销产品">
                  <a-select id="salesDetailsList-giftFlag" v-model="queryParam.giftFLag" placeholder="请选择是否促销产品" allowClear>
                    <a-select-option value="1">
                      是
                    </a-select-option>
                    <a-select-option value="0">
                      否
                    </a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="所在区域">
                  <subarea id="salesDetailsList-subareaSn" ref="subarea" @change="subareaChange"></subarea>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="区域负责人">
                  <BizUser id="salesDetailsList-bizUserSn" v-model="queryParam.subareaArea.bizUserSn"></BizUser>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="地区">
                  <AreaList id="salesDetailsList-areaList" changeOnSelect ref="areaList" @change="areaChange" defValKey="id"></AreaList>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24" v-show="isShowCustomerSearch">
                <a-form-model-item label="客服">
                  <customerService id="salesDetailsList-customerName" ref="customerName" v-model="queryParam.bizUserSn"></customerService>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="新/老客户">
                  <a-select id="salesDetailsList-dealerNewFlag" v-model="queryParam.dealerNewFlag" placeholder="请选择" allowClear>
                    <a-select-option value="1">新客户</a-select-option>
                    <a-select-option value="0">老客户</a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="新客户首笔订单">
                  <v-select
                    v-model="queryParam.newCustFlag"
                    ref="newCustFlag"
                    id="salesDetailsList-newCustFlag"
                    code="FLAG"
                    placeholder="请选择是否新客户首笔订单"
                    allowClear></v-select>
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="6" :sm="24" style="margin-bottom: 10px;">
              <a-button style="margin-left: 5px" type="primary" @click="handleSearch" :disabled="disabled" id="salesDetailsList-refresh">查询</a-button>
              <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="salesDetailsList-reset">重置</a-button>
              <a-button
                style="margin-left: 5px"
                type="primary"
                class="button-warning"
                @click="handleExport"
                :disabled="disabled"
                :loading="exportLoading"
                v-if="$hasPermissions('B_salesDetailsExport')"
                id="salesDetailsList-export">导出</a-button>
              <a @click="advanced=!advanced" style="margin-left: 5px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
    </a-card>
    <a-card size="small" :bordered="false">
      <a-spin :spinning="spinning" tip="Loading...">
        <div style="margin-bottom:12px;text-align: right;">
          <a-checkbox id="salesDetailsList-cityPrice" v-model="isAveragePrice" v-if="$hasPermissions('B_salesDetails_avgprice')"><span style="display: inline-block;margin-top: 1px;">平均公斤单价</span></a-checkbox>
        </div>
        <!-- 列表 -->
        <s-table
          class="sTable fixPagination"
          ref="table"
          size="small"
          :rowKey="(record) => record.no"
          rowKeyName="no"
          :columns="columns"
          :data="loadData"
          :pageSize="30"
          :style="{ height: tableHeight+84.5+'px' }"
          :scroll="{ x: 2480 , y: tableHeight - 50 }"
          :defaultLoadData="false"
          bordered>
          <!-- 产品编码 -->
          <template slot="productCode" slot-scope="text, record">
            <span style="padding-right: 15px;">{{ text }}</span>
            <a-badge count="促" v-if="record.promotionFlag=='GIFT'" :number-style="{ backgroundColor: '#52c41a', zoom:'80%' }"></a-badge>
            <a-badge count="特" v-if="record.promotionFlag=='DISCOUNT'" :number-style="{ backgroundColor: '#faad14', zoom:'80%' }"></a-badge>
            <a-badge count="槛" v-if="record.promotionFlag=='GATE'" :number-style="{ backgroundColor: '#108ee9', zoom:'80%' }"></a-badge>
          </template>
          <template slot="footer">
            <a-row :gutter="15">
              <a-col :md="4" :sm="24">下推数量：{{ (totalData && (totalData.qty || totalData.qty==0)) ? totalData.qty : '--' }}</a-col>
              <a-col :md="4" :sm="24" v-if="$hasPermissions('M_salesDetailsList_costPrice')">成本金额：{{ (totalData && (totalData.totalCost || totalData.totalCost==0)) ? toThousands(totalData.totalCost) : '--' }}</a-col>
              <a-col :md="4" :sm="24" v-if="$hasPermissions('M_salesDetailsList_salesPrice')">总优惠金额(含特价)：{{ (totalData && (totalData.discountAmount || totalData.discountAmount==0)) ? toThousands(totalData.discountAmount) : '--' }}</a-col>
              <a-col :md="4" :sm="24">毛利率（减促销费用）：{{ (totalData && (totalData.grossMargin || totalData.grossMargin==0)) ? (totalData.grossMargin*100).toFixed(2)+'%' : '--' }}</a-col>
              <a-col :md="4" :sm="24" v-if="isAveragePrice"><label>合计重量（kg）：</label>{{ (totalData && (totalData.totalWeight || totalData.totalWeight==0)) ? totalData.totalWeight : '--' }}</a-col>
              <a-col :md="4" :sm="24" v-if="$hasPermissions('M_salesDetailsList_costPrice')&&isAveragePrice"><label>平均成本公斤单价：</label>{{ (totalData && (totalData.weightAvgCost || totalData.weightAvgCost==0)) ? toThousands(totalData.weightAvgCost) : '--' }}</a-col>
            </a-row>
            <a-row :gutter="15">
              <a-col :md="4" :sm="24" v-if="$hasPermissions('M_salesDetailsList_salesPrice')">开单金额：{{ (totalData && (totalData.totalAmount || totalData.totalAmount==0)) ? toThousands(totalData.totalAmount) : '--' }}</a-col>
              <a-col :md="4" :sm="24" v-if="$hasPermissions('M_salesDetailsList_salesPrice')">实售金额：{{ (totalData && (totalData.totalRealAmount || totalData.totalRealAmount==0)) ? toThousands(totalData.totalRealAmount) : '--' }}</a-col>
              <a-col :md="4" :sm="24" v-if="$hasPermissions('M_salesDetailsList_salesPrice')">&nbsp;&nbsp;特价产品优惠金额：{{ (totalData && (totalData.specialDiscountAmount || totalData.specialDiscountAmount==0)) ? toThousands(totalData.specialDiscountAmount) : '--' }}</a-col>
              <a-col :md="4" :sm="24">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;实际毛利率：{{ (totalData && (totalData.realGrossMargin || totalData.realGrossMargin==0)) ? (totalData.realGrossMargin*100).toFixed(2)+'%' : '--' }}</a-col>
              <a-col :md="4" :sm="24" v-if="$hasPermissions('M_salesDetailsList_costPrice')&&isAveragePrice"><label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;返利金额：</label>{{ (totalData && (totalData.totalFactoryRebateAmount || totalData.totalFactoryRebateAmount==0)) ? toThousands(totalData.totalFactoryRebateAmount) : '--' }}</a-col>
              <a-col :md="4" :sm="24" v-if="$hasPermissions('M_salesDetailsList_salesPrice')&&isAveragePrice"><label>平均售价公斤单价：</label>{{ (totalData && (totalData.weightAvgPrice || totalData.weightAvgPrice==0)) ? toThousands(totalData.weightAvgPrice) : '--' }}</a-col>
            </a-row>
          </template></s-table>
      </a-spin>
      <!-- 导出提示框 -->
      <reportModal :visible="showExport" @close="showExport=false"></reportModal>
    </a-card>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import getDate from '@/libs/getDate.js'
import { hdExportExcel } from '@/libs/exportExcel'
// 组件
import { STable, VSelect } from '@/components'
import rangeDate from '@/views/common/rangeDate.vue'
import subarea from '@/views/common/subarea.js'
import custList from '@/views/common/custList.vue'
import AreaList from '@/views/common/areaList.js'
import reportModal from '@/views/common/reportModal.vue'
import ProductBrand from '@/views/common/productBrand.js'
import ProductType from '@/views/common/productType.js'
import chooseWarehouse from '@/views/common/chooseWarehouse'
import customerService from '@/views/common/customerService'
import BizUser from '@/views/common/bizUser.js'
// 接口
import { reportSalesBillDetailList, reportSalesBillDetailCount, reportSalesBillDetailExport } from '@/api/reportData'
export default {
  name: 'SalesDetailsList',
  mixins: [commonMixin],
  components: { STable, VSelect, rangeDate, custList, subarea, ProductBrand, ProductType, reportModal, AreaList, chooseWarehouse, BizUser, customerService },
  data () {
    return {
      spinning: false,
      advanced: false, // 高级搜索 展开/关闭
      disabled: false, //  查询、重置按钮是否可操作
      exportLoading: false, // 导出加载状态
      tableHeight: 0, // 表格高度
      showExport: false, // 导出提示弹窗
      //  查询条件
      queryParam: {
        // 默认时间
        time: [
          getDate.getCurrMonthDays().starttime,
          getDate.getCurrMonthDays().endtime
        ],
        beginDate: getDate.getCurrMonthDays().starttime, // 开始时间
        endDate: getDate.getCurrMonthDays().endtime, // 结束时间
        salesBillNo: '', // 销售单号
        dispatchBillNo: '', // 备货单号
        productCode: '', // 产品编码
        productName: '', // 产品名称
        dealerName: '', // 客户名称
        dealerLevel: undefined, // 客户等价
        productBrandTypeSn: undefined,
        productBrandSn: undefined, //  产品品牌
        productTypeSn1: '', //  产品一级分类
        productTypeSn2: '', //  产品二级分类
        productTypeSn3: '', //  产品三级分类
        subareaArea: {
          subareaSn: undefined, // 区域
          subareaAreaSn: undefined, // 分区
          bizUserSn: undefined// 区域负责人
        },
        dealerProvinceSn: undefined, // 省
        dealerCitySn: undefined, // 市
        dealerCountySn: undefined, // 区
        warehouseSn: undefined, // 出库仓库
        giftFLag: undefined, // 是否是促销产品  1是  0不是
        promotionFlag: undefined, // 产品类型
        bizUserSn: undefined, // 客服
        dealerNewFlag: undefined, // 客户类型
        newCustFlag: undefined
      },
      isAveragePrice: false, // 平均公斤单价
      productType: [], // 产品分类
      totalData: null, // 统计数据
      rules: {
        'time': [{ required: true, message: '请选择下推时间', trigger: 'change' }]
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        const params = Object.assign(parameter, this.queryParam)
        // 查询列表
        return reportSalesBillDetailList(params).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            // 统计查询
            this.getCount(params)
            // 计算列表序号
            const no = (data.pageNo - 1) * data.pageSize
            for (var i = 0; i < data.list.length; i++) {
              data.list[i].no = no + i + 1
            }
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      },
      // 列表列显示
      colsArr: [
        {
          title: '单重',
          value: 'directRebateAmountInfo',
          key: 'directRebateAmountInfo',
          disabled: !this.$hasPermissions('M_salesOrderTotalList_salesPrice')
        },
        {
          title: '平均公斤单价',
          value: 'indirectRebateAmountInfo',
          key: 'indirectRebateAmountInfo',
          disabled: !this.$hasPermissions('M_salesOrderTotalList_salesPrice')
        }
      ],
      showCols: []// 已勾选显示的列
    }
  },
  computed: {
    columns () {
      const _this = this
      const arr = [
        { title: '区域', dataIndex: 'subareaArea.subareaName', width: 80, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '分区', dataIndex: 'subareaArea.subareaAreaName', width: 80, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '区域负责人', dataIndex: 'subareaArea.bizUserName', width: 80, align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '销售单号', dataIndex: 'salesBillNo', width: 120, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '备货单号', dataIndex: 'dispatchBillNo', width: 120, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '出库仓库', dataIndex: 'warehouseName', width: 100, align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '省份', dataIndex: 'dealerProvinceName', width: 80, align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '客户名称', dataIndex: 'dealerName', width: 150, align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '客户级别', dataIndex: 'dealerLevelDictValue', width: 100, align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        // { title: '直接绑定客户名称', dataIndex: 'directDealerName', width: 120, align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        // { title: '间接绑定客户名称', dataIndex: 'indirectDealerName', width: 120, align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '创建时间', dataIndex: 'createDate', width: 130, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '销售审核时间', dataIndex: 'auditDate', width: 130, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '下推时间', dataIndex: 'pushedDate', width: 130, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '品牌', dataIndex: 'productBrandName', width: 100, align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '二级分类', dataIndex: 'productTypeName2', width: 120, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'productName', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '产品编码', dataIndex: 'productCode', width: 120, align: 'center', scopedSlots: { customRender: 'productCode' } },
        { title: '单位', dataIndex: 'productUnit', width: 50, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '下推数量', dataIndex: 'qty', width: 70, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } }
        // { title: '成本价', dataIndex: 'totalCost', width: 70, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        // { title: '实售价', dataIndex: 'totalRealAmount', width: 70, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        // { title: '开单价', dataIndex: 'totalAmount', width: 70, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        // { title: '市级价', dataIndex: 'totalCityAmount', width: 70, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        // { title: '直接差价', dataIndex: 'directRebateAmount', width: 70, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        // { title: '间接差价', dataIndex: 'indirectRebateAmount', width: 70, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        // { title: '折后单价', dataIndex: 'discountedPrice', width: 70, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        // { title: '折扣金额', dataIndex: 'discountAmount', width: 70, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
      ]
      if (this.isAveragePrice) {
        arr.push({ title: '单重（kg）', dataIndex: 'weight', width: 80, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } })
        arr.push({ title: '合计重量（kg）', dataIndex: 'totalWeight', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } })
      }
      if (this.$hasPermissions('M_salesDetailsList_costPrice')) { //  成本价权限
        arr.push({ title: '成本金额', dataIndex: 'totalCost', width: 70, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
        if (this.isAveragePrice) {
          arr.push({ title: '平均成本公斤单价', dataIndex: 'weightAvgCost', width: 70, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
        }
      }
      if (this.$hasPermissions('M_salesDetailsList_salesPrice')) { //  售价权限
        arr.push({ title: '实售金额', dataIndex: 'totalRealAmount', width: 70, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
        if (this.isAveragePrice) {
          arr.push({ title: '平均售价公斤单价', dataIndex: 'weightAvgPrice', width: 70, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
        }
      }
      if (this.$hasPermissions('M_salesDetailsList_costPrice') && this.isAveragePrice) {
        arr.push({ title: '返利金额', dataIndex: 'totalFactoryRebateAmount', width: 70, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
      }
      if (this.$hasPermissions('M_salesDetailsList_salesPrice')) {
        arr.push({ title: '开单金额', dataIndex: 'totalAmount', width: 70, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
      }
      if (this.$hasPermissions('M_salesDetailsList_cityPrice')) { //  市级价权限
        arr.push({ title: '市级金额', dataIndex: 'totalCityAmount', width: 70, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
      }
      if (this.$hasPermissions('M_salesDetailsList_salesPrice')) { //  售价权限
        arr.push({ title: '直接差金额', dataIndex: 'reportRebateAmountInfo.directRebateAmount', width: 90, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
        arr.push({ title: '间接差金额', dataIndex: 'reportRebateAmountInfo.indirectRebateAmount', width: 90, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
        arr.push({ title: '直接上级差金额', dataIndex: 'reportRebateAmountInfo.directRebateUpAmount', width: 120, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
        arr.push({ title: '间接上级差金额', dataIndex: 'reportRebateAmountInfo.indirectRebateUpAmount', width: 120, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
        arr.push({ title: '指定差价金额', dataIndex: 'reportRebateAmountInfo.assignRebateAmount', width: 100, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
        arr.push({ title: '总部差价金额', dataIndex: 'reportRebateAmountInfo.headRebateAmount', width: 100, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
        arr.push({ title: '优惠金额', dataIndex: 'discountAmount', width: 70, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
        // arr.push({ title: '折后单价', dataIndex: 'discountedPrice', width: 70, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
        // arr.push({ title: '折扣金额', dataIndex: 'discountAmount', width: 70, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
      }
      arr.push({ title: '操作员', dataIndex: 'operatorName', width: 150, align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true })
      return arr
    }
  },
  watch: {
    // 展开收起
    advanced (newValue, oldValue) {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  methods: {
    // 总计
    getCount (params) {
      reportSalesBillDetailCount(params).then(res => {
        if (res.status == 200 && res.data) {
          this.totalData = res.data
        } else {
          this.totalData = null
        }
      })
    },
    // 查询
    handleSearch () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          _this.$refs.table.refresh(true)
        } else {
          _this.$message.error('请选择下推时间')
          return false
        }
      })
    },
    //  下推时间  change
    dateChange (date) {
      if (date[0] && date[1]) {
        this.queryParam.time = date
      } else {
        this.queryParam.time = []
      }
      this.queryParam.beginDate = date[0] || ''
      this.queryParam.endDate = date[1] || ''
    },
    //  区域  change
    subareaChange (val) {
      this.queryParam.subareaArea.subareaSn = val[0] ? val[0] : undefined
      this.queryParam.subareaArea.subareaAreaSn = val[1] ? val[1] : undefined
    },
    //  重置
    resetSearchForm () {
      this.queryParam.time = [
        getDate.getCurrMonthDays().starttime,
        getDate.getCurrMonthDays().endtime
      ]
      this.$refs.rangeDate.resetDate(this.queryParam.time)
      this.queryParam.beginDate = getDate.getCurrMonthDays().starttime
      this.queryParam.endDate = getDate.getCurrMonthDays().endtime
      this.queryParam.salesBillNo = ''
      this.queryParam.dispatchBillNo = ''
      this.queryParam.productCode = ''
      this.queryParam.productName = ''
      this.queryParam.dealerName = ''
      this.queryParam.dealerLevel = undefined
      this.queryParam.productBrandTypeSn = undefined
      this.queryParam.productBrandSn = undefined
      this.queryParam.productTypeSn1 = ''
      this.queryParam.productTypeSn2 = ''
      this.queryParam.productTypeSn3 = ''
      this.queryParam.subareaArea.subareaSn = undefined
      this.queryParam.subareaArea.subareaAreaSn = undefined
      this.queryParam.subareaArea.bizUserSn = undefined
      this.queryParam.dealerProvinceSn = undefined
      this.queryParam.dealerCitySn = undefined
      this.queryParam.dealerCountySn = undefined
      this.queryParam.warehouseSn = undefined
      this.queryParam.giftFLag = undefined
      this.queryParam.promotionFlag = undefined
      this.queryParam.bizUserSn = undefined
      this.queryParam.dealerNewFlag = undefined
      this.queryParam.newCustFlag = undefined
      if (this.advanced) {
        this.$refs.subarea.clearData()
        this.$refs.areaList.clearData()
      }
      this.productType = []
      this.totalData = null
      this.$refs.ruleForm.resetFields()
      this.$refs.table.clearTable()
    },
    //  导出
    handleExport () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const params = _this.queryParam
          _this.$store.state.app.curActionPermission = 'B_salesDetailsExport'
          _this.showExport = true
          _this.exportLoading = true
          _this.spinning = true
          hdExportExcel(reportSalesBillDetailExport, params, '销售明细(开单统计)报表', function () {
            _this.exportLoading = false
            _this.spinning = false
            _this.$store.state.app.curActionPermission = ''
          })
        } else {
          return false
        }
      })
    },
    //  产品分类  change
    changeProductType (val, opt) {
      this.queryParam.productTypeSn1 = val[0] ? val[0] : ''
      this.queryParam.productTypeSn2 = val[1] ? val[1] : ''
      this.queryParam.productTypeSn3 = val[2] ? val[2] : ''
    },
    // 地区  change
    areaChange (val) {
      this.queryParam.dealerProvinceSn = val[0] ? val[0] : ''
      this.queryParam.dealerCitySn = val[1] ? val[1] : ''
      this.queryParam.dealerCountySn = val[2] ? val[2] : ''
    },
    // 初始化
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
      _this.resetSearchForm()
    },
    // 计算表格高度
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 245
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>
<style lang="less" scoped>
  .footerBox{
    /deep/ label{
      display: inline-block;
      width: 135px;
      text-align: right;
    }

  }
</style>
