<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="checkWarehouseSn">
                <a-select id="exportCheckList-print" v-model="queryParam.checkWarehouseSn" placeholder="请选择盘点单" allowClear>
                  <a-select-option v-for="item in checkList" :value="item.checkWarehouseSn" :key="item.checkWarehouseSn">
                    <span>{{ item.financeAuditTime }} - {{ item.checkWarehouseNo }}</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 { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import { checkWarehouseExcelList, checkWarehouseExport } from '@/api/checkWarehouse'
import { hdExportExcel } from '@/libs/exportExcel'
export default {
  name: 'ExportCheckList',
  mixins: [commonMixin],
  components: { STable, VSelect },
  data () {
    return {
      spinning: false,
      tableHeight: 0,
      labelCol: { span: 8 },
      wrapperCol: { span: 16 },
      queryParam: { //  查询条件
        checkWarehouseSn: undefined
      },
      rules: {
        'checkWarehouseSn': [{ required: true, message: '请选择盘点单', trigger: 'change' }]
      },
      disabled: false, //  查询、重置按钮是否可操作
      exportLoading: false,
      checkList: []
    }
  },
  methods: {
    getList () {
      checkWarehouseExcelList().then(res => {
        if (res.status == 200) {
          this.checkList = res.data || []
        } else {
          this.checkList = []
        }
      })
    },
    //  导出
    handleExport () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const params = _this.queryParam
          _this.exportLoading = true
          _this.spinning = true
          hdExportExcel(checkWarehouseExport, params, '导出盘点', function () {
            _this.exportLoading = false
            _this.spinning = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
      this.queryParam.checkWarehouseSn = 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>
