<template>
  <div class="form-wrap">
    <el-form
      class="search-box"
      ref="form"
      :model="form"
      :inline="true">
      <div class="block-title">
        查询条件
      </div>
      <el-form-item
        label="时间"
        prop="startDate">
        <el-date-picker
          v-model="form.startDate"
          type="datetime"
          placeholder="选择日期时间">
        </el-date-picker>
      </el-form-item>
      <div
        class="btn-wrap"
        style="margin-bottom:20px">
        <el-button
          @click="reset">
          重置
        </el-button>
        <el-button
          :disabled="loading"
          type="primary"
          @click="getDailySaleStatistic">
          搜索
        </el-button>
        <el-button
          type="primary"
          @click="reportExport">
          导出
        </el-button>
      </div>
    </el-form>

    <div class="table-box">
      <!-- <div class="block-title">
        统计结果
      </div> -->
      <el-table
        border
        stripe
        v-loading="loading"
        :data="tableData">
        <el-table-column
          v-for="(item,index) in tableRow"
          :key="index"
          :prop="String(index)"
          :label="item">
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>
<script>
import { getDailySale, exportChengzhanguanReport } from '@/api/queryReport'
import moment from 'moment'
export default {
  name: 'DailySale',
  props: {
    activeName: {
      type: String,
      default: ''
    }
  },
  watch: {
    activeName (val) {
      if (val === 'DailySale') {
        this.getDailySaleStatistic()
      }
    }
  },
  data () {
    return {
      form: {
        startDate: new Date(moment().format('YYYY-MM-DD') + ' 00:00:00')
      },
      tableData: [],
      tableRow: [], // 列表头部数据
      loading: false
    }
  },
  created () {
    this.getDailySaleStatistic()
  },
  methods: {
    reset () {
      this.$refs.form.resetFields()
    },

    // 获取日报数据
    getDailySaleStatistic () {
      this.loading = true
      getDailySale(this.form).then(res => {
        this.tableRow = res.shift()
        this.tableData = res.map(i => {
          return { ...i }
        })
      }).finally(() => {
        this.loading = false
      })
    },

    // 导出日报数据
    reportExport () {
      exportChengzhanguanReport(this.form).then(() => {
        this.$message.success('导出成功。')
        this.goToDownload()
      }, () => {
        this.$message.success('导出失败。')
      })
    }
  }
}
</script>

<style scoped>

</style>
