<!-- @desc:销售汇总表查询  @auth:姜宁  @time:2024-07-16 16:28 -->
<template>
  <div class="main-div" ref="mainDiv">
    <!--加载中-->
    <loading :loading="loading" v-if="!modalVisible"></loading>
    <BaseIndexButtonGroup id="BaseIndexButtonGroup">
      <template #left>
        <!--    查询    -->
        <BaseIndexButton right-button="user-select" ref="search" name="search"></BaseIndexButton>
        <!--    清空条件    -->
        <BaseIndexButton ref="clear" name="clear"></BaseIndexButton>
      </template>

    </BaseIndexButtonGroup>

    <!--  卡片区  -->
    <div class="index-card">
      <!--  查询条件区域  -->
      <div id="search-cond-div" ref="search-cond-div" class="search-cond-class">
        <SearchCond ref="searchCond" v-model="searchCond"
                    @collapse-change="collapseChange" :setFlag="false"
                    :searchContent="searchContent"
                    @on-choice-select="handleChoiceSelect"
        ></SearchCond>
      </div>
      <!--  表格部分  -->
      <div slot="bottom">
        <DkTabs v-model="detailModel" :options="detailOptions" @on-click="detailTabsClick"></DkTabs>
        <div v-show="visGoodsDetailFlag">
          <!--商品汇总-->
          <DkTable v-show="collectType === 1" :id="'table-'+$options.name"
                   ref="table-select"
                   :data="tableData"
                   showFooter
                   :height="this.tableHeight-59"
                   primaryKey="skuId"
                   @pageChange="pageSizeChange"
                   :page-total="pageInfo.total"
                   :current-page="pageInfo.currentPage"
                   :choose-flag="false"
                   :page-size-opts="pageSizeOpts"
                   :page-size="pageInfo.pageSize"
                   name="table">
            <DkTableColumn field="skuCode"></DkTableColumn>
            <DkTableColumn field="skuName"></DkTableColumn>
            <DkTableColumn field="skuModel"></DkTableColumn>
            <DkTableColumn field="skuSpec"></DkTableColumn>
            <DkTableColumn field="seriesName"></DkTableColumn>
            <DkTableColumn field="brandName"></DkTableColumn>
            <DkTableColumn field="itemQty" :title="$t('salesQty')" dataType="number" type="link" @on-link="currentIntoRights()"
                           sum/>
            <DkTableColumn field="factAmt" :title="$t('salesAmt')" :digits="2" dataType="number" type="link" @on-link="currentIntoRights()"
                           sum/>
          </DkTable>
          <!--客户汇总-->
          <DkTable v-show="collectType === 2" :id="'table-'+$options.name"
                   ref="table-select-cus"
                   :data="tableData"
                   showFooter
                   :height="this.tableHeight-59"
                   primaryKey="cusId"
                   @pageChange="pageSizeChange"
                   :page-total="pageInfo.total"
                   :current-page="pageInfo.currentPage"
                   :choose-flag="false"
                   :page-size-opts="pageSizeOpts"
                   :page-size="pageInfo.pageSize"
                   name="table">
            <DkTableColumn field="cusCode"></DkTableColumn>
            <DkTableColumn field="cusName"></DkTableColumn>
            <DkTableColumn field="cusPhone"></DkTableColumn>
            <DkTableColumn field="channelName"></DkTableColumn>
            <DkTableColumn field="addressFull"></DkTableColumn>
            <DkTableColumn field="contactName"></DkTableColumn>
            <DkTableColumn field="contactPhone"></DkTableColumn>
            <DkTableColumn field="orgName"></DkTableColumn>
            <DkTableColumn field="itemQty" :title="$t('salesQty')" dataType="number" type="link" @on-link="currentIntoRightsCus()"
                           sum/>
            <DkTableColumn field="factAmt" :title="$t('salesAmt')" :digits="2" dataType="number" type="link" @on-link="currentIntoRightsCus()"
                           sum/>
          </DkTable>
        </div>
        <div v-show="visIntoDetailFlag">
          <DkTable :id="'table-'+$options.name" ref="detail-select" :data="intoDetailData" showFooter
                   :height="this.tableHeight -35"
                   primaryKey="invId"
                   @pageChange="pageSizeChange"
                   :page-total="pageInfo.total"
                   :current-page="pageInfo.currentPage"
                   :choose-flag="false"
                   :page-size-opts="pageSizeOpts"
                   :page-size="pageInfo.pageSize"
                   name="table">
            <DkTableColumn field="orderNo"/>
            <DkTableColumn field="cusName"/>
            <DkTableColumn field="cusPhone"/>
            <DkTableColumn field="contactName"/>
            <DkTableColumn field="statusName" :title="$t('outStatusName')" :color-cards="['待出库','出库中','已出库','已退货']"/>
            <DkTableColumn field="skuCode"></DkTableColumn>
            <DkTableColumn field="skuName"></DkTableColumn>
            <DkTableColumn field="skuModel"></DkTableColumn>
            <DkTableColumn field="skuSpec"></DkTableColumn>
            <DkTableColumn field="seriesName"></DkTableColumn>
            <DkTableColumn field="brandName"></DkTableColumn>
            <DkTableColumn field="itemQty" :title="$t('salesQty')" dataType="number" sum/>
            <DkTableColumn field="factAmt" :title="$t('salesAmt')" :digits="2" dataType="number" sum/>
            <DkTableColumn field="discount" :digits="2" dataType="number"/>
            <DkTableColumn field="outingQty" dataType="number" sum/>
            <DkTableColumn field="outQty" dataType="number" sum/>
            <DkTableColumn field="returnQty" dataType="number" sum/>
            <DkTableColumn field="nonStdCode"/>
            <DkTableColumn field="flgGift" type="switch"/>
<!--            <DkTableColumn field="makeStaffName"/>-->
            <DkTableColumn field="makeTime" data-type="dateTime"/>
            <DkTableColumn field="remarks"/>
          </DkTable>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {indexMixin} from '@/mixins'
import Config from "@/config";

export default {
  name: "order-sum-report",
  mixins: [indexMixin],
  data() {
    let self = this
    return {
      tableDataCus: [],
      tableDataGoods: [],
      detailModel: 'ordGoodsSumDetail',
      detailOptions: [
        {label: self.$t('ordGoodsSumDetail'), name: 'ordGoodsSumDetail'}, //销售汇总数据
        {label: self.$t('ordGoodsDetail'), name: 'ordGoodsDetail'}, //入库明细
      ],
      tableData: [],         // 库存商品
      intoDetailData: [],    // 入库明细
      visGoodsDetailFlag: true,
      visIntoDetailFlag: false,
      collectType: 1,
      searchContent:
        [
          {
            itemCode: '汇总类型-下拉',
            itemName: 'collectType',
            valueFormat: {
              code: 'type',
              data: [
                {collectType: 1, collectTypeName: '商品'},
                {collectType: 2, collectTypeName: '客户'}],
              valueKey: 'collectType',
              labelKey: 'collectTypeName',
              default: 1,
            },
            required: true,
            valueKind: 'S-CHOICE'
          },
          {
            itemCode: 'skuModel',
          },
          {
            itemCode: 'skuName',
          },
          {
            itemCode: 'brandName',
          },
          {
            itemCode: 'skuSpec',
          },
          {
            itemCode: 'seriesName',
          },
          {
            itemCode: 'cusName',
          },
          {
            itemCode: 'cusPhone',
          },
          {
            itemCode: 'nonStdCode',
          },
          {
            itemCode: 'accDate',
            valueFormat: {
              code: 'accDate',
              default: [new Date().addMonths(-1).format('yyyy-MM-dd'), new Date().format('yyyy-MM-dd')]
            },
            valueKind: 'DATE_RANGE',
          },
        ],
      // 查询条件
      searchCond: {}
    }
  },
  methods: {
    handleChoiceSelect(e) {
      this.tableData = []
      this.intoDetailData = []
      this.collectType = e
    },
    /**
     * @desc   : 查询数据
     * @author : 姜宁
     * @date   : 024/7/16 10:42
     */
    getData(params) {
      // 清空数据
      this.tableData = [];
      this.intoDetailData = [];
      this.outDetailData = [];
      // 自动显示收发存数据
      this.detailModel = 'ordGoodsSumDetail'
      this.visGoodsDetailFlag = true
      this.visIntoDetailFlag = false
      return this.excute(this.$service.detailReportService, this.$service.detailReportService.getOrderSummary, params);
      // let excute = this.excute(this.$service.detailReportService, this.$service.detailReportService.detailReportService, params).then(res => {
      //   if (res && res.code === this.$config.SUCCESS_CODE) {
      //     this.tableData = res.data.detail;
      //     this.loading = false;
      //   }
      // })
      // return excute
    },

    /**
     * @desc   : 点击Tab进行切换
     * @author : 姜宁
     * @date   : 2024/7/16 16:24
     */
    detailTabsClick(val) {
      if (val === 'ordGoodsSumDetail') {
        this.detailModel = val
        this.visGoodsDetailFlag = true
        this.visIntoDetailFlag = false
      } else if (val === 'ordGoodsDetail' && this.intoDetailData.length > 0) {
        this.detailModel = val
        this.visGoodsDetailFlag = false
        this.visIntoDetailFlag = true
      }
    },
    /**
     * @desc   : 赋值查询条件
     * @author : 姜宁
     * @date   : 2024/7/16 10:44
     */
    setSearchParams(params) {
      // 赋值JSON查询条件
      let skuquerys = {}
      if (this.searchCond && this.searchCond._value) {
        Object.assign(skuquerys, this.searchCond._value)
      }
      let skuQuerystr = JSON.stringify(skuquerys);
      if (skuQuerystr && skuQuerystr != '{}') {
        params['querys'] = skuQuerystr;
      }
      return params;
    },

    /**
     * @desc   : 销售汇总点击跳转事件
     * @author : 姜宁
     * @date   : 2024/7/16 13:22
     */
    currentIntoRights(row) {
      this.intoDetailData = [];
      if (!row) {
        row = [this.$refs['table-select'].getCurrentRow()]
      }
      let searchParams = {...this.searchParams};
      let params = {}
      if (row != null) {
        let sku = {
          skuId: row[0].skuId,
          skuModel:row[0].skuModel,
        }
        params = Object.assign(params,searchParams,sku)
        // 赋值JSON查询条件
        let skuQuerystr = JSON.stringify(params);
        if (skuQuerystr && skuQuerystr != '{}') {
          params['querys'] = skuQuerystr;
        }
      }
      // 查询入库明细
      this.excute(this.$service.detailReportService, this.$service.detailReportService.getOrderSummaryItem, params).then(res => {
        if (res.code === this.$config.SUCCESS_CODE) {
          this.intoDetailData = res.data.detail;
          // 显示入库明细信息页面
          this.detailModel = 'ordGoodsDetail'
          this.visGoodsDetailFlag = false
          this.visIntoDetailFlag = true
        }
      })
    },

    /**
     * @desc   : 销售客户汇总跳转明细
     * @author : 姜宁
     * @date   : 2024/7/17 16:38
     */
    currentIntoRightsCus(row) {
      this.intoDetailData = [];
      if (!row) {
        row = [this.$refs['table-select-cus'].getCurrentRow()]
      }
      let searchParams = {...this.searchParams};
      let params = {}
      if (row != null) {
        let sku = {
          cusId: row[0].cusId,
          skuModel:row[0].skuModel,
        }
        params = Object.assign(params,searchParams,sku)
        // 赋值JSON查询条件
        let skuQuerystr = JSON.stringify(params);
        if (skuQuerystr && skuQuerystr != '{}') {
          params['querys'] = skuQuerystr;
        }
      }
      // 查询入库明细
      this.excute(this.$service.detailReportService, this.$service.detailReportService.getOrderSummaryItem, params).then(res => {
        if (res.code === this.$config.SUCCESS_CODE) {
          this.intoDetailData = res.data.detail;
          // 显示入库明细信息页面
          this.detailModel = 'ordGoodsDetail'
          this.visGoodsDetailFlag = false
          this.visIntoDetailFlag = true
        }
      })
    },
  },
  created() {
    this.primaryKey = 'cusId'  // 设置主键Id
    this.routeObjName = 'detailReport'  // 设置路由名称
  }
}
</script>


