<template>
  <a-card size="small" :bordered="false" class="receivedSendStorageReport-wrap">
    <div ref="tableSearch" class="table-page-search-wrapper newTableSearchName">
      <a-form layout="inline">
        <a-row :gutter="15">
          <a-col :md="5" :sm="24">
            <a-form-item label="选择月份">
              <div class="monthBox" style="display:flex;align-item:center;">
                <a-month-picker
                  v-model="startMonthVal"
                  :disabled-date="disabledStartDate"
                  format="YYYY-MM"
                  placeholder="开始月份"
                  @openChange="handleStartOpenChange"
                />
                <span>~</span>
                <a-month-picker
                  v-model="endMonthVal"
                  :disabled-date="disabledEndDate"
                  format="YYYY-MM"
                  placeholder="结束月份"
                  :open="endOpen"
                  @openChange="handleEndOpenChange"
                />
              </div>
              <!-- <a-range-picker
                :placeholder="['开始月份', '结束月份']"
                format="YYYY-MM"
                :value="monthVal"
                :ranges="{'上月':[moment().add(-1, 'M'),moment().add(-1, 'M')], '本月': [moment(), moment().endOf('month')] }"
                :disabled-date="disabledDate"
                :mode="['month', 'month']"
                @panelChange="handlePanelChange"
                @change="handleChange"
              >
                <template slot="renderExtraFooter">
                  <a-button type="primary">
                    确定
                  </a-button>
                </template>
              </a-range-picker> -->
            </a-form-item>
          </a-col>
          <a-col :md="5" :sm="24">
            <a-form-item label="产品名称">
              <a-input id="receivedSendStorageReport-name" v-model.trim="queryParam.name" allowClear placeholder="请输入产品名称"/>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="24">
            <a-form-item label="产品编码">
              <a-input id="receivedSendStorageReport-queryWord" v-model.trim="queryParam.queryWord" allowClear placeholder="请输入产品编码"/>
            </a-form-item>
          </a-col>
          <a-col :md="5" :sm="24">
            <a-form-item label="产品品牌">
              <ProductBrand id="receivedSendStorageReport-productBrandSn" v-model="queryParam.productBrandSn"></ProductBrand>
            </a-form-item>
          </a-col>
          <a-col :md="5" :sm="24">
            <a-form-item label="产品分类">
              <productTypeAll placeholder="请选择产品分类" @change="changeProductType" v-model="productType" id="receivedSendStorageReport-productType"></productTypeAll>
            </a-form-item>
          </a-col>
          <a-col :md="24" :sm="24" style="margin-bottom: 10px;display:flex;align-item:center;justify-content:center;">
            <a-button type="primary" @click="searchForm" :disabled="disabled" id="receivedSendStorageReport-refresh">查询</a-button>
            <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="receivedSendStorageReport-reset">重置</a-button>
            <a-button
              style="margin-left: 5px"
              type="primary"
              class="button-warning"
              @click="handleExport"
              :disabled="disabled"
              v-if="$hasPermissions('B_ProductPriceExoprt')"
              id="productInfoList-export">导出</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <div :style="{ height: tableHeight+84.5+'px' }">
      <a-tabs default-active-key="1">
        <a-tab-pane key="1" tab="明细">
          <div class="detailCon">
            <!-- 列表 -->
            <s-table
              class="sTable"
              ref="table"
              size="small"
              :rowKey="(record) => record.id"
              :columns="columns"
              :data="loadData"
              :customRow="handleClickRow"
              :scroll="{ y: 149 }"
              :defaultLoadData="false"
              bordered>
              <template slot="footer">
                <a-row>
                  <a-col span="2">合计：</a-col>
                  <a-col span="22">
                    <a-row>
                      <a-col span="4">初期结存数量：{{ (totalData && (totalData.jpLqqAmount || totalData.jpLqqAmount==0)) ? toThousands(totalData.jpLqqAmount) : '--' }}</a-col>
                      <a-col span="4">初期结存单价：{{ (totalData && (totalData.jpBsxLqqAmount || totalData.jpBsxLqqAmount==0)) ? toThousands(totalData.jpBsxLqqAmount) : '--' }}</a-col>
                      <a-col span="4">初期结存金额：{{ (totalData && (totalData.gpLqqAmount || totalData.gpLqqAmount==0)) ? toThousands(totalData.gpLqqAmount) : '--' }}</a-col>
                      <a-col span="4">本期收入数量：{{ (totalData && (totalData.gpLqqAmount || totalData.gpLqqAmount==0)) ? toThousands(totalData.gpLqqAmount) : '--' }}</a-col>
                      <a-col span="4">本期收入单价：{{ (totalData && (totalData.jpYgpAmount || totalData.jpYgpAmount==0)) ? toThousands(totalData.jpYgpAmount) : '--' }}</a-col>
                      <a-col span="4">本期收入金额：{{ (totalData && (totalData.jpYgpAmount || totalData.jpYgpAmount==0)) ? toThousands(totalData.jpYgpAmount) : '--' }}</a-col>
                      <a-col span="4">本期发出数量：{{ (totalData && (totalData.gpYgpAmount || totalData.gpYgpAmount==0)) ? toThousands(totalData.gpYgpAmount) : '--' }}</a-col>
                      <a-col span="4">本期发出单价：{{ (totalData && (totalData.gpYgpAmount || totalData.gpYgpAmount==0)) ? toThousands(totalData.gpYgpAmount) : '--' }}</a-col>
                      <a-col span="4">本期发出金额：{{ (totalData && (totalData.jpLbAmount || totalData.jpLbAmount==0)) ? toThousands(totalData.jpLbAmount) : '--' }}</a-col>
                      <a-col span="4">本期结存数量：{{ (totalData && (totalData.jpScpAmount || totalData.jpScpAmount==0)) ? toThousands(totalData.jpScpAmount) : '--' }}</a-col>
                      <a-col span="4">本期结存单价：{{ (totalData && (totalData.gpScpAmount || totalData.gpScpAmount==0)) ? toThousands(totalData.gpScpAmount) : '--' }}</a-col>
                      <a-col span="4">本期结存金额：{{ (totalData && (totalData.tbuAmount || totalData.tbuAmount==0)) ? toThousands(totalData.tbuAmount) : '--' }}</a-col>
                    </a-row>
                  </a-col>
                </a-row>
              </template>
            </s-table>
          </div>
        </a-tab-pane>
        <a-tab-pane key="2" tab="汇总" force-render>
          <gatherList></gatherList>
        </a-tab-pane>
      </a-tabs>
    </div>
  </a-card>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import ProductBrand from '@/views/common/productBrand.js'
import productTypeAll from '@/views/common/productTypeAll.js'
import { STable, VSelect } from '@/components'
import gatherList from './list'
import moment from 'moment'
export default {
  name: 'PriceDifferenceDetailReportList',
  mixins: [commonMixin],
  components: { STable, VSelect, ProductBrand, gatherList, productTypeAll },
  data () {
    return {
      moment,
      queryParam: {
        name: '',
        queryWord: '',
        productBrandSn: undefined,
        productTypeSn1: undefined,
        productTypeSn2: undefined,
        productTypeSn3: undefined
      },
      productType: undefined,
      disabled: false,
      tableHeight: 0,
      endOpen: false,
      startMonthVal: undefined,
      endMonthVal: undefined,
      columns: [
        { title: '产品编码', dataIndex: 'productCode', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'productName', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '原厂编码', dataIndex: 'productName', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '品牌', dataIndex: 'productBrandName', width: '20%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '二级分类', dataIndex: 'productName', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '三级分类', dataIndex: 'productName', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '单位', dataIndex: 'productName', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '期初结存数量', dataIndex: 'currentStockQty', width: '12%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '期初结存单价', dataIndex: 'productPrice', width: '12%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '期初结存金额', dataIndex: 'productPrice', width: '12%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '本期收入数量', dataIndex: 'productUnit', width: '12%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '本期收入单价', dataIndex: 'productPrice', width: '12%', align: 'right', customRender: text => ((text || text == 0) ? this.toThousands(text) : '--') },
        { title: '本期收入金额', dataIndex: 'productPrice', width: '12%', align: 'right', customRender: text => ((text || text == 0) ? this.toThousands(text) : '--') },
        { title: '本期发出数量', dataIndex: 'packQtyV', width: '12%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '本期发出单价', dataIndex: 'productPrice', width: '12%', align: 'right', customRender: text => ((text || text == 0) ? this.toThousands(text) : '--') },
        { title: '本期发出金额', dataIndex: 'productPrice', width: '12%', align: 'right', customRender: text => ((text || text == 0) ? this.toThousands(text) : '--') },
        { title: '期末结存数量', dataIndex: 'salesNums', scopedSlots: { customRender: 'nums' }, width: '12%', align: 'center' },
        { title: '期末结存单价', dataIndex: 'productPrice', width: '12%', align: 'right', customRender: text => ((text || text == 0) ? this.toThousands(text) : '--') },
        { title: '期末结存金额', dataIndex: 'productPrice', width: '12%', align: 'right', customRender: text => ((text || text == 0) ? this.toThousands(text) : '--') }
      ]
    }
  },
  methods: {
    // 选择月份限制
    disabledStartDate (startValue) {
      const endValue = this.endMonthVal
      if (!startValue || !endValue) {
        return startValue.valueOf() > this.moment().valueOf()
      }
      return startValue.valueOf() >= endValue.valueOf()
    },
    disabledEndDate (endValue) {
      const startValue = this.startMonthVal
      if (!startValue) {
        return endValue.valueOf() > this.moment().valueOf()
      }
      return endValue.valueOf() < startValue.valueOf() || endValue.valueOf() > this.moment().valueOf()
    },
    handleStartOpenChange (open) {
      if (!open) {
        this.endOpen = true
      }
    },
    handleEndOpenChange (open) {
      this.endOpen = open
    },

    //  产品分类  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] : ''
    },
    handleExport () {

    },
    searchForm () {
      console.log('1111:', this.startMonthVal)
    },
    resetSearchForm () {

    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 200
    }
  },
  mounted () {
    this.pageInit()
  }
}

</script>

<style lang="less" scoped>
  .monthBox{
    /deep/.ant-calendar-picker-icon{
      display:none !important;
    }
    /deep/.ant-calendar-picker input{
      text-align:center;
    }
  }
</style>
