<template>
  <a-card size="small" :bordered="false" class="exportCheckList-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 搜索条件 -->
      <div ref="tableSearch" class="table-page-search-wrapper">
        <a-form-model
          id="exportCheckList-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="10" :sm="24">
              <a-form-model-item label="选择时间范围" prop="dateArr">
                <a-select id="exportCheckList-print" v-model="queryParam.dateArr" placeholder="请选择时间范围" allowClear>
                  <a-select-option v-for="item in checkList" :value="item" :key="item">
                    <span>{{ item }}</span>
                  </a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-button
                type="primary"
                class="button-warning"
                @click="handleExport"
                :disabled="disabled"
                :loading="exportLoading"
                id="exportCheckList-export">导出</a-button>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
    </a-spin>
  </a-card>
</template>

<script>
import { STable, VSelect } from '@/components'
import { checkWarehouseExcelList } from '@/api/checkWarehouse'
import { exportSalesOutProduct } from '@/api/stockOut.js'
import { hdExportExcel } from '@/libs/exportExcel'
export default {
  components: { STable, VSelect },
  data () {
    return {
      spinning: false,
      tableHeight: 0,
      labelCol: { span: 8 },
      wrapperCol: { span: 16 },
      queryParam: { //  查询条件
        dateArr: undefined
      },
      rules: {
        'dateArr': [{ required: true, message: '请选择时间范围', trigger: 'change' }]
      },
      disabled: false, //  查询、重置按钮是否可操作
      exportLoading: false,
      checkList: []
    }
  },
  methods: {
    getList () {
      checkWarehouseExcelList().then(res => {
        if (res.status == 200) {
          const arr = res.data.reverse()
          for (let i = arr.length - 1; i > 0; i--) {
            this.checkList.push([arr[i - 1].financeAuditTime, arr[i].financeAuditTime].join(' 至 '))
          }
        } else {
          this.checkList = []
        }
      })
    },
    //  导出
    handleExport () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const params = _this.queryParam.dateArr.split(' 至 ')
          _this.exportLoading = true
          _this.spinning = true
          hdExportExcel(exportSalesOutProduct, { 'beginDate': params[0], 'endDate': params[1] }, '导出销售', function () {
            _this.exportLoading = false
            _this.spinning = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
      this.queryParam.dateArr = undefined
      this.$refs.ruleForm.resetFields()
      this.getList()
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 238
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
    }
    // 仅刷新列表，不重置页面
    if (this.$store.state.app.updateList) {
      this.pageInit()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>
<style lang="less">
  .exportCheckList-wrap{
    height: 99%;
    .form-model-con{
      margin-top: 50px;
    }
  }
</style>
