<template>
  <a-card size="small" :bordered="false" class="storeReceivedSendStorageReportList-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 搜索条件 -->
      <div class="table-page-search-wrapper">
        <a-form-model
          id="storeReceivedSendStorageReportList-form"
          ref="ruleForm"
          class="form-model-con"
          layout="inline"
          :model="queryParam"
          :rules="rules"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          @keyup.enter.native="handleSearch" >
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-model-item label="日期" prop="billDate">
                <a-month-picker
                  v-model="queryParam.billDate"
                  :defaultValue="moment().subtract(1, 'month')"
                  :disabled-date="disabledDate"
                  :allowClear="false"
                  @change="onChange"
                  style="width: 100%;" />
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24" style="margin-bottom: 10px;">
              <a-button type="primary" @click="handleSearch" :disabled="disabled" id="storeReceivedSendStorageReportList-refresh">查询</a-button>
              <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="storeReceivedSendStorageReportList-reset">重置</a-button>
              <a-button
                style="margin-left: 5px"
                type="primary"
                class="button-warning"
                @click="handleExport"
                :disabled="disabled"
                :loading="exportLoading"
                v-if="$hasPermissions('B_storeReceivedSendStorageReport_export')"
                id="storeReceivedSendStorageReportList-export">导出</a-button>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
      <!-- 列表 -->
      <s-table
        class="sTable"
        ref="table"
        size="small"
        :rowKey="(record) => record.no"
        :columns="columns"
        :data="loadData"
        :showPagination="false"
        :defaultLoadData="false"
        bordered>
        <!-- 结存余额-成本 -->
        <template slot="endAmount" slot-scope="text, record">
          <span v-if="record.endDetail" :style="{background: text ? 'chartreuse':'', display: 'block'}">{{ text ? '￥' + text : '--' }}</span>
        </template>
      </s-table>
    </a-spin>
  </a-card>
</template>

<script>
import moment from 'moment'
import { STable, VSelect } from '@/components'
import rangeDate from '@/views/common/rangeDate.vue'
import { downloadExcel } from '@/libs/JGPrint.js'
import { reportStockPutOutMonthList, reportStockPutOutMonthExport } from '@/api/reportData'
export default {
  components: { STable, VSelect, rangeDate },
  data () {
    return {
      moment,
      spinning: false,
      labelCol: { span: 8 },
      wrapperCol: { span: 16 },
      tableHeight: 0,
      queryParam: { //  查询条件
        billDate: moment().subtract(1, 'month')
      },
      rules: {
        'billDate': [{ required: true, message: '请选择月份', trigger: 'change' }]
      },
      disabled: false, //  查询、重置按钮是否可操作
      exportLoading: false,
      columns: [
        { title: '序号', dataIndex: 'no', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '期初余额',
          align: 'center',
          children: [
            {
              title: '明细',
              dataIndex: 'beginDetail',
              align: 'center',
              width: '9%'
            },
            {
              title: '成本',
              dataIndex: 'beginAmount',
              align: 'center',
              width: '14%',
              customRender: function (text, record) {
                let str
                if (record.beginDetail) {
                  if (text) {
                    str = '￥' + text
                  } else {
                    str = '--'
                  }
                } else {
                  str = ''
                }
                return str
              }
            }
          ]
        },
        { title: '入库',
          align: 'center',
          children: [
            {
              title: '明细',
              dataIndex: 'putDetail',
              align: 'center',
              width: '9%'
            },
            {
              title: '成本',
              dataIndex: 'putAmount',
              align: 'center',
              width: '14%',
              customRender: function (text, record) {
                let str
                if (record.putDetail) {
                  if (text) {
                    str = '￥' + text
                  } else {
                    str = '--'
                  }
                } else {
                  str = ''
                }
                return str
              }
            }
          ]
        },
        { title: '出库',
          align: 'center',
          children: [
            {
              title: '明细',
              dataIndex: 'outDetail',
              align: 'center',
              width: '9%'
            },
            {
              title: '成本',
              dataIndex: 'outAmount',
              align: 'center',
              width: '14%',
              customRender: function (text, record) {
                let str
                if (record.outDetail) {
                  if (text) {
                    str = '￥' + text
                  } else {
                    str = '--'
                  }
                } else {
                  str = ''
                }
                return str
              }
            }
          ]
        },
        { title: '结存余额',
          align: 'center',
          children: [
            {
              title: '明细',
              dataIndex: 'endDetail',
              align: 'center',
              width: '9%'
            },
            {
              title: '成本',
              dataIndex: 'endAmount',
              align: 'center',
              width: '14%',
              scopedSlots: { customRender: 'endAmount' }
            }
          ]
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        const params = JSON.parse(JSON.stringify(this.queryParam))
        params.billDate = params.billDate + '-01'
        this.spinning = true
        return reportStockPutOutMonthList(params).then(res => {
          let data
          if (res.status == 200) {
            if (res.data) {
              if (res.data.beginAmountHide) { // 是否隐藏期初金额
                res.data.beginAmount = null
              }
              if (res.data.endAmountHide) { // 是否隐藏结存金额
                res.data.endAmount = null
              }
              // 组装数据
              data = [
                { no: 1, beginDetail: '期初金额', beginAmount: res.data.beginAmount, putDetail: '采购入库', putAmount: res.data.putPurchaseAmount, outDetail: '采购退货', outAmount: res.data.outPurchaseReturnAmount, endDetail: null, endAmount: null },
                { no: 2, beginDetail: null, beginAmount: null, putDetail: '散件入库', putAmount: res.data.putSparePartsAmount, outDetail: '散件退货', outAmount: res.data.outSparePartsReturnAmount, endDetail: null, endAmount: null },
                { no: 3, beginDetail: null, beginAmount: null, putDetail: '销售退货', putAmount: res.data.putSalesReturnAmount, outDetail: '销售', outAmount: res.data.outSalesAmount, endDetail: null, endAmount: null },
                { no: 4, beginDetail: null, beginAmount: null, putDetail: '仓库调入', putAmount: res.data.putWarehouseCallInAmount, outDetail: '仓库调出', outAmount: res.data.outWarehouseCallOutAmount, endDetail: null, endAmount: null },
                { no: 5, beginDetail: null, beginAmount: null, putDetail: '库存盘点盘盈', putAmount: res.data.putStockCheckInAmount, outDetail: '库存盘点盘亏', outAmount: res.data.outCheckOrderOutAmount, endDetail: null, endAmount: null },
                { no: 6, beginDetail: null, beginAmount: null, putDetail: '连锁调入', putAmount: res.data.putLinkageCallInAmount, outDetail: '连锁调出', outAmount: res.data.outLinkageCallOutAmount, endDetail: null, endAmount: null },
                { no: 7, beginDetail: null, beginAmount: null, putDetail: '库存导入', putAmount: res.data.putStockImportAmount, outDetail: null, outAmount: null, endDetail: null, endAmount: null },
                { no: 8, beginDetail: null, beginAmount: null, putDetail: '散件导入', putAmount: res.data.putSparePartsImportAmount, outDetail: null, outAmount: null, endDetail: null, endAmount: null },
                { no: 9, beginDetail: null, beginAmount: null, putDetail: null, putAmount: null, outDetail: '店内调出', outAmount: res.data.outShopCallOutAmount, endDetail: null, endAmount: null },
                { no: 10, beginDetail: null, beginAmount: null, putDetail: null, putAmount: null, outDetail: '急件冲减', outAmount: res.data.outDispatchDeductAmount, endDetail: null, endAmount: null },
                { no: '成本合计', beginDetail: ' ', beginAmount: res.data.beginAmount, putDetail: ' ', putAmount: res.data.putAmount, outDetail: ' ', outAmount: res.data.outAmount, endDetail: '月末余额', endAmount: res.data.endAmount }
              ]
            } else {
              data = []
            }
          }
          this.disabled = false
          this.spinning = false
          return data
        })
      }
    }
  },
  methods: {
    disabledDate (current) {
      return current && current > moment().subtract(1, 'month')
    },
    // 月份  change
    onChange (date, dateString) {
      this.queryParam.billDate = dateString || null
    },
    //  查询
    handleSearch () {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.$refs.table.refresh(true)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    //  重置
    resetSearchForm () {
      this.queryParam.billDate = moment().subtract(1, 'month')
      this.$refs.ruleForm.resetFields()
      this.$refs.table.clearTable()
    },
    //  导出
    handleExport () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const params = JSON.parse(JSON.stringify(this.queryParam))
          params.billDate = params.billDate + '-01'
          _this.exportLoading = true
          _this.spinning = true
          reportStockPutOutMonthExport(params).then(res => {
            downloadExcel(res, '门店收发存汇总报表')
            _this.exportLoading = false
            _this.spinning = false
          })
        } else {
          return false
        }
      })
    },
    pageInit () {}
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
      this.resetSearchForm()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
      this.resetSearchForm()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>
