/**
 * Created by wang on 18/10/31.
 */
<template>
  <div class="index">
    <el-form ref="searchFrom" :model="searchFrom" label-width="80px">
      <el-row>
        <el-col :lg="10" :sm="24" :md="24">
          <el-form-item label="购买时间">
            <el-date-picker
              v-model="searchFrom.time"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="['00:00:00','23:59:59']">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :lg="4" :sm="24" :md="12">
          <el-form-item label="来源">
            <el-input v-model="searchFrom.source" placeholder="名称"
                      size="small"></el-input>
          </el-form-item>
        </el-col>
        <el-col :lg="4" :sm="24" :md="12">
          <el-form-item>
            <el-button type="primary" plain size="small" @click="initPage">
              搜索
            </el-button>
          </el-form-item>
        </el-col>
        <el-col :lg="4" :sm="24" :md="12">
          <el-form-item>
            <el-button type="primary" plain size="small" @click="doExport">
              导出
            </el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table
      :data="tableData"
      style="width: 100%">
      <el-table-column
        prop="name"
        label="宝贝名称">
      </el-table-column>
      <el-table-column
        prop="create_at"
        label="添加时间">
      </el-table-column>
      <el-table-column
        prop="source"
        label="来源">
      </el-table-column>
    </el-table>
    <page :total="total" :limit="limit" @pageChange="onPageChange" @sizeChange="onSizeChange"/>
  </div>
</template>

<script>
  import page from '../framework/page'
  import CommonJs from '../../util/common';
  import {getAdsListApi} from "../../service/api";
  export default {
    name: "index",
    components:{
      page
    },
    data(){
      return {
        total:0,
        nowPage:1,
        limit: 10,
        searchFrom: {
          source:''
        },
        tableData:[],
      }
    },
    created(){
      this.initPage()
    },
    methods:{
      initPage(){
        let json = {
          limit: this.limit,
          page: this.nowPage
        }
        if (this.searchFrom.time) {
          json.time = this.searchFrom.time
        }
        if (this.searchFrom.source) {
          json.source = this.searchFrom.source
        }
        if(this.searchFrom.time && this.searchFrom.time.length > 0){
          json.date_start = CommonJs.dateFmt(this.searchFrom.time[0],"yyyy-MM-dd hh:mm:ss");
          json.date_end = CommonJs.dateFmt(this.searchFrom.time[1],"yyyy-MM-dd hh:mm:ss")
        }
        getAdsListApi(json).then((res)=>{
          this.tableData = res.list;
          this.total = res.total
        })
      },
      onPageChange(val){
        this.nowPage = val
        this.initPage()
      },
      onSizeChange(val){
        this.nowPage = 1
        this.limit = val
        this.initPage()
      },
      doExport(){
        let query = `?type=export`;
        if(this.searchFrom.source){
          query = query + '&source=' + this.searchFrom.source
        }

        if(this.searchFrom.time && this.searchFrom.time.length > 0){
          query = query + '&date_start=' + CommonJs.dateFmt(this.searchFrom.time[0],"yyyy-MM-dd hh:mm:ss");
          query = query + '&date_end=' + CommonJs.dateFmt(this.searchFrom.time[1],"yyyy-MM-dd hh:mm:ss")
        }
        window.open(`/api/public/ads/export/all${query}`)
      }
    }
  }
</script>

<style scoped lang="less">
  .index {
    padding: 20px 0;
  }
</style>
