<template>
  <a-card size="small" :bordered="false" class="exportCheckList-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 搜索条件 -->
      <div ref="tableSearch">
        <a-form-model
          id="exportCheckList-form"
          ref="ruleForm"
          class="form-model-con"
          :model="queryParam"
          :rules="rules"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          @keyup.enter.native="handleSearch" >
          <a-row :gutter="15" v-show="isShowWarehouse">
            <a-col :md="12" :sm="24">
              <a-form-model-item label="选择仓库" prop="warehouseSn">
                <warehouse
                  v-model="queryParam.warehouseSn"
                  isPermission
                  @load="loadWareHouse"
                  id="exportCheckList-warehouseSn"
                  placeholder="请选择仓库"
                  @change="getList"
                />
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row :gutter="15">
            <a-col :md="12" :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-row>
          <a-form-model-item :wrapper-col="{ span: 12, offset: 6 }">
            <a-button
                type="primary"
                class="button-warning"
                @click="handleExport"
                :disabled="disabled"
                :loading="exportLoading"
                id="exportCheckList-export">导出</a-button>
            <a-button @click="pageInit" style="margin-left: 15px" id="chooseCustom-btn-back">重置</a-button>
          </a-form-model-item>
        </a-form-model>
      </div>
    </a-spin>
  </a-card>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import { checkWarehouseExcelList } from '@/api/checkWarehouse'
import { exportSalesOutProduct } from '@/api/stockOut.js'
import warehouse from '@/views/common/chooseWarehouse.js'
import { hdExportExcel } from '@/libs/exportExcel'
export default {
  name: 'ExportSalesList',
  mixins: [commonMixin],
  components: { STable, VSelect, warehouse },
  data () {
    return {
      spinning: false,
      tableHeight: 0,
      labelCol: { span: 8 },
      wrapperCol: { span: 16 },
      queryParam: { //  查询条件
        dateArr: undefined,
        warehouseSn: undefined
      },
      rules: {
        'dateArr': [{ required: true, message: '请选择时间范围', trigger: 'change' }],
        warehouseSn: [{ required: true, message: '请选择仓库', trigger: 'change' }]
      },
      disabled: false, //  查询、重置按钮是否可操作
      exportLoading: false,
      checkList: []
    }
  },
  methods: {
    getList (v) {
      this.queryParam.dateArr = undefined
      this.checkList = []
      if(v){
        checkWarehouseExcelList({warehouseSn: this.queryParam.warehouseSn}).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
        }
      })
    },
    loadWareHouse(){
      const defWareHouse = this.$store.state.app.defWarehouse
      this.queryParam.dateArr = undefined
      this.queryParam.warehouseSn = this.isShowWarehouse ? undefined : (defWareHouse?defWareHouse.warehouseSn:undefined)

      if(!this.isShowWarehouse){
        this.getList(true)
      }
    },
    pageInit () {
      this.$refs.ruleForm.resetFields()
    }
  },
  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>
