<template>
    <div>
        <el-form ref="form" label-width="90px" :model="searchParam">
          <el-row :gutter="20">
              <el-col :span="7">
                <el-form-item label="订单编号" prop="orderCode">
                    <el-input v-model="searchParam.orderCode" placeholder="订单编号" @keyup.enter.native="onSubmit('form')"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="下单时间段">
                   <el-date-picker
                    v-model="addDateVal"
                    type="daterange"
                    value-format="yyyy-MM-dd"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期">
                  </el-date-picker>
                </el-form-item>
              </el-col>
                 <el-col :span="11">
                <el-form-item class="btn">
                  <el-button type="primary" @click="onSubmit('form')">搜索</el-button>
                  <el-button @click="reset('form')">重置</el-button>
                  <!-- <a :href="exportDetail" v-if="orderIds">
                    <el-button type="primary" v-loading="downloadLoading">导出订单明细</el-button>
                  </a> -->
                  <a v-if="orderIds" :href="exportdetailUrl" @click.prevent="exportDetailFun(exportdetailUrl)">
                    <el-button type="primary" :loading="downloadLoading">导出订单明细</el-button>
                  </a>
                    <el-button style="margin-left:0px;" type="primary" v-else @click="exportMainBtnTips">导出订单明细</el-button>
                </el-form-item>
              </el-col>
          </el-row>
      </el-form>
    </div>
</template>
<script>
import qs from 'qs'

export default {
    data(){
        return{
           brandsList:[],
           addDateVal:'',
           downloadLoading:false
        }
    },
    props:{
      searchParam:{
        type:Object,
        default:{}
      },
      exportmainUrl:{
        type:String,
        default:''
      },
      exportdetailUrl:{
        type:String,
        default:''
      },
      brandType:{
        type:Number,
        default:1
      },
      source:{
        type:String,
        default:''
      },
      orderIds:{
        type:String,
        default:''
      },
      tableLoading:{
        type:Boolean,
        default:false
      }

    },
    mounted(){
      // this.getBrands()
    },
    methods:{
        // type 1-E淘  2-EDS 3-EDS2.0 4-家电馆 其他全部
        getBrands(){
          this.$axios.get(this.HOST +'/v1/select/brand',{
              params:{
                type:this.brandType
              }
            }).then((res) => {
              if(res.data.code==200){
                 this.brandsList = res.data.data;
              }else{
                this.$message.error(res.data.message);
              }
            }).catch(()=>{})
        },
        exportMainBtnTips(){
            this.$message.error('请先勾选需导出的订单！')
        },
        exportDetailFun(url){
          this.downloadLoading = true
          //  this.$emit("update:tableLoading", true);
          var info = localStorage.getItem('info') == "" ? "" : JSON.parse(localStorage.getItem('info'));
          var params = {};
            params.orderIds = this.orderIds
            params.merId = info.merId
          let dataparams = qs.stringify(params);
          this.$axios.get(url+'?'+dataparams,{ responseType: 'blob' }).then((res) => {
            let {data} = res
              let blob = new Blob([data], { type: 'application/vnd.ms-excel' })
              let link = document.createElement('a')
              link.href = window.URL.createObjectURL(blob)
              link.download = 'E淘家居' + this.getNowTime()
              link.click()
              this.downloadLoading = false
            }).catch((error)=>{
              console.error(error)
            })

        },
         // 获取当前时间
    getNowTime() {
      let dataTimeStr=''
      let yy = new Date().getFullYear()
      let mm = new Date().getMonth() + 1
      let dd = new Date().getDate()
      let hh = new Date().getHours()
      let mf = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes():new Date().getMinutes()
      let ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds():new Date().getSeconds()
      dataTimeStr = ''+yy+mm+dd+hh+mf+ss
      return dataTimeStr
    },
     onSubmit(formName) {
         this.searchParam.pageNum =1;
        var params = this.searchParam;
        params.addDateBegin= this.addDateVal && this.addDateVal.length?this.addDateVal[0]:'';
        params.addDateEnd= this.addDateVal && this.addDateVal.length?this.addDateVal[1]:'';
        this.$emit('backData',params);
      },
      reset(formName){
        this.$refs[formName].resetFields();
        var params = this.searchParam;
        params.addDateBegin= '';
        params.addDateEnd= '';

        this.addDateVal ='';

        this.$emit('backData',params);
      },
    },
    computed:{
        // exportDetail(){
        //   this.downloadLoading = true
        //   var info = localStorage.getItem('info') == "" ? "" : JSON.parse(localStorage.getItem('info'));
        //   var params = {};
        //     params.orderIds = this.orderIds
        //     params.merId = info.merId
        //     let dataparams = qs.stringify(params);
        //     this.downloadLoading = false
        //     return this.exportdetailUrl +'?' + dataparams;
            
        // }
    }
}
</script>
<style lang="scss" scoped>
.line{
  text-align: center;
}
.btn{text-align: right;}
.el-form-item{
  margin-bottom: 10px;
}
</style>

