<template>
  <div>
    <a-card size="small" :bordered="false" class="salesReturnDetailList-wrap searchBoxNormal">
      <!-- 搜索条件 -->
      <div class="table-page-search-wrapper" ref="tableSearch">
        <a-form-model
          id="salesReturnDetailList-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="salesReturnDetailList-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="salesReturnDetailList-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="salesReturnDetailList-salesReturnBillNo" v-model.trim="queryParam.salesReturnBillNo" allowClear placeholder="请输入退货单号"/>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24" v-if="isShowCustomerSearch">
              <a-form-model-item label="产品名称">
                <a-input id="salesReturnDetailList-productName" v-model.trim="queryParam.productName" allowClear placeholder="请输入产品名称"/>
              </a-form-model-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="24" v-if="!isShowCustomerSearch">
                <a-form-model-item label="产品名称">
                  <a-input id="salesReturnDetailList-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="客户名称">
                  <a-input id="salesReturnDetailList-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="客户级别">
                  <v-select
                    v-model="queryParam.dealerLevel"
                    ref="dealerLevel"
                    id="salesReturnDetailList-dealerLevel"
                    code="DEALER_LEVEL"
                    placeholder="请选择客户级别"
                    allowClear></v-select>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="品牌分类">
                  <v-select code="BRAND_TYPE" id="salesReturnDetailList-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="salesReturnDetailList-productBrandSn" v-model="queryParam.productBrandSn"></ProductBrand>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="产品分类">
                  <ProductType id="salesReturnDetailList-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="退货仓库">
                  <chooseWarehouse ref="warehouse" id="salesReturnDetailList-warehouse" v-model="queryParam.warehouseSn"></chooseWarehouse>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="所在区域">
                  <subarea id="salesReturnDetailList-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="salesReturnDetailList-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="salesReturnDetailList-areaList" changeOnSelect ref="areaList" @change="areaChange" defValKey="id"></AreaList>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="退货类别" prop="goodFlag">
                  <v-select
                    v-model="queryParam.goodFlag"
                    ref="goodFlag"
                    id="salesReturnDetailList-goodFlag"
                    code="GOOD_FLAG"
                    placeholder="请选择退货类别"
                    allowClear></v-select>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="退货类型">
                  <v-select
                    v-model="queryParam.salesReturnType"
                    id="salesReturnDetailList-salesReturnType"
                    code="SALES_RETURN_TYPE"
                    placeholder="请选择退货类型"
                    allowClear></v-select>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24" v-show="isShowCustomerSearch">
                <a-form-model-item label="客服">
                  <customerService id="salesReturnDetailList-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="salesReturnDetailList-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>
            </template>
            <a-col :md="!isShowCustomerSearch?24:(advanced?6:24)" :sm="24" style="margin-bottom: 10px;" :style="{textAlign: !isShowCustomerSearch?'center':(advanced?'':'center')}">
              <a-button style="margin-left: 5px" type="primary" @click="handleSearch" :disabled="disabled" id="salesReturnDetailList-refresh">查询</a-button>
              <a-button style="margin-left: 8px" @click="resetSearchForm" :disabled="disabled" id="salesReturnDetailList-reset">重置</a-button>
              <a-button
                style="margin-left: 10px"
                type="primary"
                class="button-warning"
                @click="handleExport"
                :disabled="disabled"
                :loading="exportLoading"
                v-if="$hasPermissions('B_salerReturnDetailTable_export')"
                id="salesReturnDetailList-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="text-align:right;margin-bottom:10px;">
          <span>显示：</span>
          <a-tree-select
            size="small"
            v-model="showCols"
            style="min-width: 200px"
            dropdownMatchSelectWidth
            :maxTagCount="3"
            :tree-data="colsArr"
            id="salesReturnDetailList-isShowColumn"
            tree-checkable
            placeholder="请选择要显示的列（多选）"
          />
        </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+70+'px' }"
          :scroll="{ x: 2960, y: tableHeight - 100 }"
          :defaultLoadData="false"
          bordered>
          <!-- 产品编码 -->
          <template slot="productCode" slot-scope="text, record">
            <span style="margin-right:15px;">{{ record.productCode }}</span>
            <a-badge count="折" :number-style="{ zoom:'80%' }" v-if="record.priceSetFlag==='PRICE_SET_FLAG_DISCOUNT'"></a-badge>
          </template>
          <template slot="footer">
            <a-row :gutter="15">
              <a-col :md="4" :sm="24" v-if="$hasPermissions('M_salesReturnDetailReportList_salesPrice')">开单退货金额：{{ (totalData && (totalData.totalPrice || totalData.totalPrice==0)) ? toThousands(totalData.totalPrice) : '--' }}</a-col>
              <a-col :md="4" :sm="24" v-if="$hasPermissions('M_salesReturnDetailReportList_salesPrice')&&showCols.includes('priceOrig')">退货开单金额（折前）：{{ (totalData && (totalData.totalAmountOrig || totalData.totalAmountOrig==0)) ? toThousands(totalData.totalAmountOrig) : '--' }}</a-col>
              <a-col :md="4" :sm="24" v-if="$hasPermissions('M_salesReturnDetailReportList_salesPrice')">实售退货金额：{{ (totalData && (totalData.totalRealAmount || totalData.totalRealAmount==0)) ? toThousands(totalData.totalRealAmount) : '--' }}</a-col>
              <a-col :md="4" :sm="24" v-if="$hasPermissions('M_salesReturnDetailReportList_salesPrice')&&showCols.includes('realSalePriceOrig')">退货实售金额（折前）：{{ (totalData && (totalData.totalRealAmountOrig || totalData.totalRealAmountOrig==0)) ? toThousands(totalData.totalRealAmountOrig) : '--' }}</a-col>
              <a-col :md="4" :sm="24" v-if="$hasPermissions('M_salesReturnDetailReportList_salesPrice')">直接差价金额：{{ (totalData && (totalData.directRebateAmount || totalData.directRebateAmount==0)) ? toThousands(totalData.directRebateAmount) : '--' }}</a-col>
              <a-col :md="4" :sm="24" v-if="$hasPermissions('M_salesReturnDetailReportList_salesPrice')">间接差价金额：{{ (totalData && (totalData.indirectRebateAmount || totalData.indirectRebateAmount==0)) ? toThousands(totalData.indirectRebateAmount) : '--' }}</a-col>
              <a-col :md="4" :sm="24">仓库实收数量：{{ (totalData && (totalData.receiveQty || totalData.receiveQty==0)) ? totalData.receiveQty : '--' }}</a-col>
              <a-col :md="4" :sm="24">坏件数量：{{ (totalData && (totalData.totalBadQty || totalData.totalBadQty==0)) ? totalData.totalBadQty : '--' }}</a-col>
              <a-col :md="4" :sm="24">良品数量：{{ (totalData && (totalData.goodQty || totalData.goodQty==0)) ? totalData.goodQty : '--' }}</a-col>
              <a-col :md="4" :sm="24">返库数量：{{ (totalData && (totalData.totalBackStockQty || totalData.totalBackStockQty==0)) ? totalData.totalBackStockQty : '--' }}</a-col>
              <a-col :md="4" :sm="24">正常退货数量：{{ (totalData && (totalData.totalNormalQty || totalData.totalNormalQty==0)) ? totalData.totalNormalQty : '--' }}</a-col>
              <a-col :md="4" :sm="24" v-if="$hasPermissions('M_salesReturnDetailReportList_costPrice')">成本金额：{{ (totalData && (totalData.totalCost || totalData.totalCost==0)) ? toThousands(totalData.totalCost) : '--' }}</a-col>
              <a-col :md="4" :sm="24" v-if="$hasPermissions('M_salesReturnDetailReportList_salesPrice')">正常退货金额：{{ (totalData && (totalData.totalNormalPrice || totalData.totalNormalPrice==0)) ? toThousands(totalData.totalNormalPrice) : '--' }}</a-col>
              <a-col :md="4" :sm="24" v-if="$hasPermissions('M_salesReturnDetailReportList_costPrice')">正常再入库金额：{{ (totalData && (totalData.totalNormalCost || totalData.totalNormalCost==0)) ? toThousands(totalData.totalNormalCost) : '--' }}</a-col>
              <a-col :md="4" :sm="24" v-if="$hasPermissions('M_salesReturnDetailReportList_salesPrice')">正常退货入库差额：{{ (totalData && (totalData.totalNormalBalance || totalData.totalNormalBalance==0)) ? toThousands(totalData.totalNormalBalance) : '--' }}</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 reportModal from '@/views/common/reportModal.vue'
import AreaList from '@/views/common/areaList.js'
import chooseWarehouse from '@/views/common/chooseWarehouse'
import customerService from '@/views/common/customerService'
import ProductBrand from '@/views/common/productBrand.js'
import ProductType from '@/views/common/productType.js'
import BizUser from '@/views/common/bizUser.js'
// 接口
import { reportSalesReturnDetailList, reportSalesReturnDetailCount, reportSalesReturnDetailExport } from '@/api/reportData'
export default {
  name: 'SalesReturnDetailReportList',
  mixins: [commonMixin],
  components: { STable, VSelect, rangeDate, subarea, ProductBrand, ProductType, reportModal, AreaList, chooseWarehouse, BizUser, customerService },
  data () {
    return {
      spinning: false,
      advanced: false, // 高级搜索 展开/关闭
      tableHeight: 0, // 表格高度
      showExport: false, // 导出弹窗
      disabled: false, //  查询、重置按钮是否可操作
      exportLoading: false, // 导出按钮加载状态
      //  查询条件
      queryParam: {
        // 退货完成日期 默认值
        time: [
          getDate.getCurrMonthDays().starttime,
          getDate.getCurrMonthDays().endtime
        ],
        beginDate: getDate.getCurrMonthDays().starttime, // 开始日期
        endDate: getDate.getCurrMonthDays().endtime, // 结束日期
        salesReturnBillNo: '', // 退货单号
        productCode: '', // 产品编码
        productName: '', // 产品名称
        dealerName: '', // 客户名称
        dealerLevel: undefined, // 客户等级
        goodFlag: undefined, // 退货类别
        salesReturnType: undefined, // 退货类型
        productBrandTypeSn: undefined, // 品牌分类
        productBrandSn: undefined, //  产品品牌
        productTypeSn1: '', //  产品一级分类
        productTypeSn2: '', //  产品二级分类
        productTypeSn3: '', //  产品三级分类
        warehouseSn: undefined, // 出库仓库
        subareaArea: {
          subareaSn: undefined, // 区域
          subareaAreaSn: undefined, // 分区
          bizUserSn: undefined// 区域负责人
        },
        dealerProvinceSn: undefined, // 省
        dealerCitySn: undefined, // 市
        dealerCountySn: undefined, // 区
        bizUserSn: undefined, // 客服
        dealerNewFlag: undefined // 客户类型
      },
      rules: {
        'time': [{ required: true, message: '请选择退货完成日期', trigger: 'change' }]
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        const params = Object.assign(parameter, this.queryParam)
        delete params.time
        // 获取列表数据 有分页
        return reportSalesReturnDetailList(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
        })
      },
      productType: [], // 产品分类
      totalData: null, // 统计数据
      // 列表列显示
      showCols: [], // 已勾选的列
      colsArr: [
        {
          title: '退货开单价（折前）',
          value: 'priceOrig',
          key: 'priceOrig',
          disabled: !this.$hasPermissions('M_salesReturnDetailReportList_salesPrice')
        },
        {
          title: '退货实售价（折前）',
          value: 'realSalePriceOrig',
          key: 'realSalePriceOrig',
          disabled: !this.$hasPermissions('M_salesReturnDetailReportList_salesPrice')
        },
        {
          title: '直接差价（折前）',
          value: 'directRebatePriceOrig',
          key: 'directRebatePriceOrig',
          disabled: !this.$hasPermissions('M_salesReturnDetailReportList_salesPrice')
        },
        {
          title: '间接差价（折前）',
          value: 'indirectRebatePriceOrig',
          key: 'indirectRebatePriceOrig',
          disabled: !this.$hasPermissions('M_salesReturnDetailReportList_salesPrice')
        }
      ]
    }
  },
  computed: {
    columns () {
      const _this = this
      const arr = [
        { title: '区域', dataIndex: 'subareaArea.subareaName', width: 100, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '分区', dataIndex: 'subareaArea.subareaAreaName', width: 100, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '区域负责人', dataIndex: 'subareaArea.bizUserName', width: 100, align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '退货单号', dataIndex: 'salesReturnBillNo', width: 160, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '退货仓库', dataIndex: 'warehouseName', width: 150, align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '省份', dataIndex: 'dealerProvinceName', width: 90, align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '客户名称', dataIndex: 'dealerName', width: 120, align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '客户级别', dataIndex: 'dealerLevelDictValue', width: 120, align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '退货类别', dataIndex: 'goodFlagDictValue', width: 100, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '退货类型', dataIndex: 'salesReturnTypeDictValue', width: 100, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '退货完成日期', dataIndex: 'salesReturnDate', width: 130, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '品牌', dataIndex: 'productBrandName', width: 130, align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '二级分类', dataIndex: 'productTypeName2', width: 130, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'productName', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '产品编码', scopedSlots: { customRender: 'productCode' }, width: 150, align: 'center' },

        { title: '单位', dataIndex: 'productUnit', width: 50, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '退货原因', dataIndex: 'returnReason', width: 100, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '申请退货数量', dataIndex: 'initialQty', width: 70, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '仓库实收数量', dataIndex: 'receiveQty', width: 70, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '坏件数量', dataIndex: 'badQty', width: 80, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '良品数量', dataIndex: 'goodQty', width: 80, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '返库数量', dataIndex: 'backStockQty', width: 80, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } }
        // { title: '退货开单价', dataIndex: 'price', width: 80, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        // { title: '退货开单金额', dataIndex: 'totalPrice', width: 90, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        // { title: '退货实售价', dataIndex: 'realPrice', width: 90, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        // { title: '退货实售金额', dataIndex: 'totalRealAmount', width: 90, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        // { title: '市级价金额', dataIndex: 'totalWholesalePrice2', width: 90, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        // { title: '直接差价', dataIndex: 'directRebateAmount', width: 80, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        // { title: '间接差价', dataIndex: 'indirectRebateAmount', width: 80, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        // { title: '再入库单价', dataIndex: 'cost', width: 90, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        // { title: '正常退货数量', dataIndex: 'normalQty', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } }
        // { title: '正常退货金额', dataIndex: 'totalNormalPrice', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        // { title: '正常再入库金额', dataIndex: 'totalNormalCost', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        // { title: '正常退货入库差额', dataIndex: 'totalNormalBalance', width: 110, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } }
      ]
      if (this.$hasPermissions('M_salesReturnDetailReportList_costPrice')) {
        arr.splice(17, 0, { title: '成本金额', dataIndex: 'totalCost', width: 80, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
      }
      if (this.$hasPermissions('M_salesReturnDetailReportList_salesPrice')) { //  售价权限
        arr.push({ title: '退货开单价', dataIndex: 'price', width: 80, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
        if (this.showCols.includes('priceOrig')) {
          arr.push({ title: '退货开单价（折前）', dataIndex: 'priceOrig', width: 80, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
        }
        arr.push({ title: '退货开单金额', dataIndex: 'totalPrice', width: 90, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
        arr.push({ title: '退货实售价', dataIndex: 'realPrice', width: 90, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
        if (this.showCols.includes('realSalePriceOrig')) {
          arr.push({ title: '退货实售价（折前）', dataIndex: 'realSalePriceOrig', width: 90, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
        }
        arr.push({ title: '退货实售金额', dataIndex: 'totalRealAmount', width: 90, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
      }
      if (this.$hasPermissions('M_salesReturnDetailReportList_cityPrice')) {
        arr.push({ title: '市级价金额', dataIndex: 'totalWholesalePrice2', width: 90, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
      }
      if (this.$hasPermissions('M_salesReturnDetailReportList_salesPrice')) {
        arr.push({ title: '直接差价', dataIndex: 'reportRebateAmountInfo.directRebateAmount', width: 70, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
        if (this.showCols.includes('directRebatePriceOrig')) {
          arr.push({ title: '直接差价（折前）', dataIndex: 'directRebateAmountOrig', width: 70, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
        }
        arr.push({ title: '间接差价', dataIndex: 'reportRebateAmountInfo.indirectRebateAmount', width: 70, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
        if (this.showCols.includes('indirectRebatePriceOrig')) {
          arr.push({ title: '间接差价（折前）', dataIndex: 'indirectRebateAmountOrig', width: 70, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
        }
        arr.push({ title: '直接上级差价金额', dataIndex: 'reportRebateAmountInfo.directRebateUpAmount', width: 70, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
        arr.push({ title: '间接上级差价金额', dataIndex: 'reportRebateAmountInfo.indirectRebateUpAmount', width: 70, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
        arr.push({ title: '指定差价金额', dataIndex: 'reportRebateAmountInfo.assignRebateAmount', width: 70, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
        arr.push({ title: '总部差价金额', dataIndex: 'reportRebateAmountInfo.headRebateAmount', width: 70, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
      }
      if (this.$hasPermissions('M_salesReturnDetailReportList_costPrice')) {
        arr.push({ title: '再入库单价', dataIndex: 'cost', width: 90, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
      }
      arr.push({ title: '正常退货数量', dataIndex: 'normalQty', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } })
      if (this.$hasPermissions('M_salesReturnDetailReportList_salesPrice')) { //  价权限
        arr.push({ title: '正常退货金额', dataIndex: 'totalNormalPrice', width: 100, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
      }
      if (this.$hasPermissions('M_salesReturnDetailReportList_costPrice')) {
        arr.push({ title: '正常再入库金额', dataIndex: 'totalNormalCost', width: 100, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
      }
      if (this.$hasPermissions('M_salesReturnDetailReportList_salesPrice')) { //  价权限
        arr.push({ title: '正常退货入库差额', dataIndex: 'totalNormalBalance', width: 110, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
      }
      return arr
    }
  },
  watch: {
    // 展开关闭
    advanced (newValue, oldValue) {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  methods: {
    // 盘点库存日期
    handleStock () {
      this.$message.info('无盘点区间的起始/终止时间，请自行选择日期区间查询！')
    },
    // 总计
    getCount (params) {
      reportSalesReturnDetailCount(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
    custChange (val) {
      this.queryParam.dealerSn = val.key
    },
    //  导出
    handleExport () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const params = _this.queryParam
          _this.$store.state.app.curActionPermission = 'B_salerReturnDetailTable_export'
          _this.showExport = true
          _this.exportLoading = true
          _this.spinning = true
          hdExportExcel(reportSalesReturnDetailExport, 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
    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.salesReturnBillNo = ''
      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.goodFlag = undefined
      this.queryParam.salesReturnType = undefined
      this.queryParam.bizUserSn = undefined
      this.queryParam.dealerNewFlag = undefined
      this.productType = []
      if (this.advanced) {
        this.$refs.subarea.clearData()
        this.$refs.areaList.clearData()
      }
      this.totalData = null
      this.$refs.ruleForm.resetFields()
      this.$refs.table.clearTable()
    },
    // 地区
    areaChange (val) {
      this.queryParam.dealerProvinceSn = val[0] ? val[0] : ''
      this.queryParam.dealerCitySn = val[1] ? val[1] : ''
      this.queryParam.dealerCountySn = val[2] ? val[2] : ''
    },
    // 初始化
    pageInit () {
      this.resetSearchForm()
      this.setTableH()
    },
    // 计算表格高度
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 230
    }
  },
  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>
