<template>
  <el-row class="query">
    <div class="query__retrieve">
      <el-row type="flex" justify="start">
        <el-col :span="21"></el-col>
        <el-col :span="3">
          <el-button v-auth="50920040102" class="query__item" type="primary" @click="moreQuery">更多</el-button>
          <el-button
            v-auth="50920040101"
            icon="el-icon-refresh"
            class="query__item"
            type="primary"
            @click="handleRefresh"
            plain
            circle
          ></el-button>
           <el-button
              v-if="false"
              icon="el-icon-download"
              type="primary"
              @click="handleExcel"
              plain
              circle
            ></el-button>
        </el-col>
      </el-row>

      <!--<el-button class="query__item" type="primary" @click="toExport">导出</el-button>-->
      <div class="inputBox">
        <el-form inline :model="query" ref="query" label-position="left">
          <el-form-item label="入库单号" prop="asnNo">
            <el-input
              v-model="query.asnNo"
              placeholder="快捷查询（入库单号）"
              @keyup.enter.native="handleSearch"
            ></el-input>
          </el-form-item>
          <el-form-item label="入库单类型" prop="asnType">
            <el-select multiple v-model="asnTypeResults" @change="asnTypeChange">
              <template v-for="item in $keys.stockIn.asnType">
                <el-option :key="item.id" :value="item.key" :label="item.name"></el-option>
              </template>
            </el-select>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select multiple v-model="statusResults" @change="statusChange">
              <el-option value="00" label="创建"></el-option>
              <el-option value="10" label="部分收货"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="订单时间">
            <el-date-picker
              v-model="dateTime"
              type="datetimerange"
              :picker-options="pickerOptions"
              :default-time="['00:00:00', '23:59:59']"
              format="yyyy-MM-dd HH:mm"
              value-format="yyyy-MM-dd HH:mm:ss"
              @change="dateTimeChange"
            ></el-date-picker>
          </el-form-item>
        </el-form>
      </div>

      <MoreQuery
        :queryProp="query"
        :showMore="showMore"
        @close="showMore = false"
        @toGetList="toGetList"
      ></MoreQuery>
    </div>
  </el-row>
</template>

<script>
import MoreQuery from './MoreQuery'
import * as shortcuts from '@/config/shortcuts'
export default {
  components: { MoreQuery },
  created () {
  },
  mounted () {
  },

  data () {
    return {
      query: {}, // 查询条件
      showMore: false, // 显示更多查询
      asnTypeResults: [], // 入库单类型的多选value
      statusResults: [], // 状态的多选value
      dateTime: [],
      pickerOptions: {
        shortcuts: { ...shortcuts }
      },
      moreData: {}
    }
  },
  methods: {
    // 时间变化
    dateTimeChange (val) {
      this.query.orderTimeFm = val ? val[0] : ''
      this.query.orderTimeTo = val ? val[1] : ''
      this.handleSearch()
    },
    // 入库单多选变化
    asnTypeChange (val) {
      this.query.asnType = val.join(',')
      this.handleSearch()
    },
    // 入库单多选变化
    statusChange (val) {
      this.query.status = val.join(',')
      this.handleSearch()
    },
    handleRefresh () {
      this.dateTime = []
      this.asnTypeResults = []
      this.statusResults = []
      this.$refs.query.resetFields()
      this.$emit('getList')
    },
    // 搜索
    handleSearch () {
      const query = { ...this.query, asnNo: `%${this.query.asnNo || ''}%` }
      this.moreData = query
      this.$emit('getList', query)
    },
    // 点击更多查询
    moreQuery () {
      this.showMore = true
    },
    toGetList (query) {
      this.moreData = query
      this.$emit('getList', query)
    },

    // 导出
    toExport () {
      const url = '/dev/rep_web/repweb/reportController/getReportOfNoRcvAsnDetail.shtml'
      const param = {
        quickCondition: '',
        phonixIsExport: 'Y',
        phonixExportPageCode: 'reportControllergetReportOfNoRcvAsnDetail_mainTable',
      }
      this.$https.post(url, param)
        .then(data => {
          const fileName = '导出excel.xls'
          const blob = new Blob([data])
          if (typeof window.navigator.msSaveBlob !== 'undefined') {
            /**
             * IE workaround for "HTML7007: One or more blob URLs were revoked by closing
             * the blob for which they were created. These URLs will no longer resolve as
             * the data backing the URL has been freed."
             */
            window.navigator.msSaveBlob(blob, fileName)
          } else {
            let URL = window.URL || window.webkitURL
            let objectUrl = URL.createObjectURL(blob)
            if (fileName) {
              var a = document.createElement('a')
              // safari doesn't support this yet
              if (typeof a.download === 'undefined') {
                window.location = objectUrl
              } else {
                a.href = objectUrl
                a.download = fileName
                document.body.appendChild(a)
                a.click()
                a.remove()
              }
            } else {
              window.location = objectUrl
            }
          }
        })
    },
    handleExcel () {
      const url = this.$API.summaryReport.noRcvAsnList
      const options = this.moreData
      this.$exportExcel(url, options)
    },
  },
}
</script>

<style lang="scss" scoped>
.inputBox {
  width: 100%;
  border: 1px solid #d1d5db;
  margin-top: 10px;
  margin-bottom: 10px;
  border-radius: 10px;
  padding-bottom: 10px;
  padding-left: 10px;
  .el-form-item {
    margin-bottom: 0px;
    margin-top: 10px;
  }
}

</style>
