<template>
  <div>
    <!-- 搜索条件 -->
    <div class="table-page-search-wrapper">
      <a-form-model
        id="inventoryReportList-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="time">
              <rangeDate ref="rangeDate" @change="dateChange" />
            </a-form-model-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="产品编码">
              <a-input id="productPricingList-code" v-model.trim="queryParam.productEntity.code" allowClear placeholder="请输入产品编码"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="产品名称">
              <a-input id="productPricingList-name" v-model.trim="queryParam.productEntity.name" allowClear placeholder="请输入产品名称"/>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="24">
              <a-form-item label="产品品牌">
                <ProductBrand id="productPricingList-productBrandSn" v-model="queryParam.productEntity.productBrandSn"></ProductBrand>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="产品分类">
                <ProductType id="productPricingList-productType" @change="changeProductType" v-model="productType"></ProductType>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="6" :sm="24" style="margin-bottom: 10px;">
            <a-button type="primary" @click="handleSearch" :disabled="disabled" id="inventoryReportList-refresh">查询</a-button>
            <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="inventoryReportList-reset">重置</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-alert type="info" style="margin-bottom:10px">
      <div class="ftext" slot="message">
        库存总数量：<strong>{{ (totalData && (totalData.stockQty || totalData.stockQty==0)) ? totalData.stockQty : '--' }}</strong>；
        盘盈总数量：<strong>{{ (totalData && (totalData.checkProfitQty || totalData.checkProfitQty==0)) ? totalData.checkProfitQty : '--' }}</strong>；
        盘亏总数量：<strong>{{ (totalData && (totalData.checkLossQty || totalData.checkLossQty==0)) ? totalData.checkLossQty : '--' }}</strong>；
        盈亏总数量：<strong>{{ (totalData && (totalData.checkProfitLossQty || totalData.checkProfitLossQty==0)) ? totalData.checkProfitLossQty : '--' }}</strong>；
        盈亏总成本：<strong>{{ (totalData && (totalData.checkProfitLossCost || totalData.checkProfitLossCost==0)) ? '￥'+totalData.checkProfitLossCost : '--' }}</strong>；
      </div>
    </a-alert>
    <!-- 列表 -->
    <s-table
      class="sTable"
      ref="table"
      size="small"
      :rowKey="(record) => record.checkWarehouseSn"
      :columns="columns"
      :data="loadData"
      :defaultLoadData="false"
      bordered>
    </s-table>
  </div>
</template>

<script>
import { STable, VSelect } from '@/components'
import ProductType from '../../common/productType.js'
import ProductBrand from '../../common/productBrand.js'
import rangeDate from '@/views/common/rangeDate.vue'
import { reportCheckWarehouseDetailList, reportCheckWarehouseDetailCount } from '@/api/reportData'
export default {
  components: { STable, VSelect, rangeDate, ProductType, ProductBrand },
  data () {
    return {
      labelCol: { span: 8 },
      wrapperCol: { span: 16 },
      tableHeight: 0,
      advanced: true, // 高级搜索 展开/关闭
      queryParam: { //  查询条件
        time: [],
        beginDate: '',
        endDate: '',
        productEntity: {
          name: '',
          code: '',
          productBrandSn: undefined,
          productTypeSn1: '',
          productTypeSn2: '',
          productTypeSn3: ''
        }
      },
      productType: [],
      rules: {
        'time': [{ required: true, message: '请选择财务审核时间', trigger: 'change' }]
      },
      disabled: false, //  查询、重置按钮是否可操作
      exportLoading: false,
      columns: [
        { title: '盘点单号', dataIndex: 'checkWarehouseNo', width: '14%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品编码', dataIndex: 'productEntity.code', width: '15%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'productEntity.name', width: '19%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '单位', dataIndex: 'productEntity.unit', width: '4%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '仓库', dataIndex: 'warehouseName', width: '10%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '仓位', dataIndex: 'warehouseLocationName', width: '10%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '库存数量', dataIndex: 'stockQty', width: '7%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '盘点数量', dataIndex: 'checkQty', width: '7%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '盈亏数量', dataIndex: 'checkProfitLossQty', width: '7%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '盈亏总成本', dataIndex: 'checkProfitLossCost', width: '7%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        const params = Object.assign(parameter, this.queryParam)
        this.$emit('spinning', true)
        delete params.time
        if (params.beginDate) {
          return reportCheckWarehouseDetailList(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.$emit('spinning', false)
            return data
          })
        } else {
          const _this = this
          return new Promise(function (resolve, reject) {
            const data = {
              pageNo: 1,
              pageSize: 10,
              list: [],
              count: 0
            }
            _this.disabled = false
            _this.$emit('spinning', false)
            _this.$message.info('请选择财务审核时间')
            resolve(data)
          })
        }
      },
      totalData: null //  合计
    }
  },
  methods: {
    // 合计
    getCount (params) {
      reportCheckWarehouseDetailCount(params).then(res => {
        if (res.status == 200) {
          this.totalData = res.data
        } else {
          this.totalData = null
        }
      })
    },
    //  创建时间  change
    dateChange (date) {
      this.queryParam.time = date
      this.queryParam.beginDate = date[0] || ''
      this.queryParam.endDate = date[1] || ''
    },
    //  查询
    handleSearch () {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.$refs.table.refresh(true)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    //  重置
    resetSearchForm () {
      this.$refs.rangeDate.resetDate()
      this.queryParam.time = []
      this.queryParam.beginDate = ''
      this.queryParam.endDate = ''
      this.queryParam.productEntity.name = ''
      this.queryParam.productEntity.code = ''
      this.queryParam.productEntity.productBrandSn = undefined
      this.queryParam.productEntity.productTypeSn1 = ''
      this.queryParam.productEntity.productTypeSn2 = ''
      this.queryParam.productEntity.productTypeSn3 = ''
      this.productType = []
      this.$refs.ruleForm.resetFields()
      this.totalData = null
      this.$refs.table.clearTable()
    },
    //  产品分类  change
    changeProductType (val, opt) {
      this.queryParam.productEntity.productTypeSn1 = val[0] ? val[0] : ''
      this.queryParam.productEntity.productTypeSn2 = val[1] ? val[1] : ''
      this.queryParam.productEntity.productTypeSn3 = val[2] ? val[2] : ''
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.resetSearchForm()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.resetSearchForm()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>

<style>
</style>
