<template>
  <a-card size="small" :bordered="false" class="exportSalesList-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 搜索条件 -->
      <div ref="tableSearch">
        <a-form-model
          id="exportSalesList-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="exportSalesList-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="exportSalesList-dateArr" 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-row :gutter="15" v-if="queryParam.dateArr == '自主选择时间范围'">
            <a-col :md="12" :sm="24">
              <a-form-model-item :wrapper-col="{ span: 16, offset: 8 }" prop="timeDate">
                <rangeDate ref="rangeDate" :minYear="10" year id="exportSalesList-timeDate" @change="changeDate" />
              </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="exportSalesList-export">导出</a-button>
            <a-button @click="pageInit" style="margin-left: 15px" id="exportSalesList-reset">重置</a-button>
          </a-form-model-item>
        </a-form-model>
      </div>
    </a-spin>
  </a-card>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { hdExportExcel } from '@/libs/exportExcel'
// 组件
import { STable, VSelect } from '@/components'
import warehouse from '@/views/common/chooseWarehouse.js'
import rangeDate from '@/views/common/rangeDate.vue'
// 接口
import { checkWarehouseExcelList } from '@/api/checkWarehouse'
import { exportSalesOutProduct } from '@/api/stockOut.js'
export default {
  name: 'ExportSalesList',
  mixins: [commonMixin],
  components: { STable, VSelect, warehouse, rangeDate },
  data () {
    return {
      spinning: false,
      labelCol: { span: 8 }, // form表单label横向距离
      wrapperCol: { span: 16 }, // form表单行缩进距离
      disabled: false, //  查询、重置按钮是否可操作
      exportLoading: false, // 导出按钮加载状态
      //  查询条件
      queryParam: {
        dateArr: undefined, // 时间范围
        timeDate: undefined,
        warehouseSn: undefined// 仓库
      },
      rules: {
        dateArr: [{ required: true, message: '请选择时间范围', trigger: 'change' }],
        warehouseSn: [{ required: true, message: '请选择仓库', trigger: 'change' }],
        timeDate: [{ message: '请选择时间范围', trigger: 'change' }]
      },
      checkList: [] // 时间范围数据
    }
  },
  methods: {
    changeDate (v) {
      this.queryParam.timeDate = v.join(' 至 ')
    },
    // 获取时间范围数据
    getList (v) {
      this.queryParam.dateArr = undefined
      this.queryParam.timeDate = 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 = []
          }
          this.checkList.push('自主选择时间范围')
          if (this.checkList[0] == '自主选择时间范围') {
            this.queryParam.dateArr = this.checkList[0]
          }
        })
      }
    },
    //  导出
    handleExport () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (this.queryParam.dateArr == '自主选择时间范围' && !this.queryParam.timeDate) {
          this.$message.error('请自主选择时间范围')
          return
        }
        if (valid) {
          let params = _this.queryParam.dateArr.split(' 至 ')
          if (_this.queryParam.dateArr == '自主选择时间范围') {
            params = _this.queryParam.timeDate.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
        }
      })
    },
    // 选择仓库 change
    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">
  .exportSalesList-wrap{
    height: 99%;
    .form-model-con{
      margin-top: 50px;
    }
  }
</style>
