<template>
  <div class="table-container">
    <vab-query-form>

      <vab-query-form-left-panel>
        <el-form
          ref="searchForm"
          :inline="true"
          @submit.native.prevent
        >
          <el-form-item>
            标记时间：
<!--            <el-date-picker
              v-model="searchForm.signTime"
              type="datetime"
              format="yyyy-MM-dd HH:mm:ss"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择日期">
            </el-date-picker>-->

            <el-date-picker
              v-model="times"
              type="datetimerange"
              format="yyyy-MM-dd HH:mm:ss"
              value-format="yyyy-MM-dd HH:mm:ss"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
          </el-form-item>

          <el-form-item>
            <el-button
              icon="el-icon-search"
              type="primary"
              native-type="submit"
              @click="handleQuery"
            >
              查询
            </el-button>
          </el-form-item>

        </el-form>

      </vab-query-form-left-panel>


      <vab-query-form-right-panel :span="1">

      </vab-query-form-right-panel>


    </vab-query-form>

    <el-table
      ref="table"
      v-loading="listLoading"
      :data="tableData"
      :element-loading-text="elementLoadingText"
      :height="height"
      :header-cell-style="{ 'text-align': 'center' }"
      :cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column show-overflow-tooltip label="序号" width="95">
        <template #default="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column width="150" show-overflow-tooltip label="标记时间" prop="signTime" />
      <el-table-column  width="150" show-overflow-tooltip label="解析时间" prop="analysisTime"/>
      <el-table-column width="150" show-overflow-tooltip label="实际功率" prop="realPower" />
      <el-table-column width="150" show-overflow-tooltip label="场站编码" prop="stationCode" />
      <el-table-column show-overflow-tooltip label="总辐射" prop="globalR" />
      <el-table-column show-overflow-tooltip label="直辐射" prop="directR" />
      <el-table-column show-overflow-tooltip label="散辐射" prop="diffuseR" />
      <el-table-column show-overflow-tooltip label="风速" prop="ws" />
      <el-table-column show-overflow-tooltip label="风向" prop="wd" />
      <el-table-column show-overflow-tooltip label="电池温度" prop="cellT" />

      <el-table-column show-overflow-tooltip label="温度" prop="temperature" />
      <el-table-column show-overflow-tooltip label="湿度" prop="humidity" />
      <el-table-column show-overflow-tooltip label="气压" prop="pressure" />
      <el-table-column show-overflow-tooltip label="超短期上报" prop="reportStateForecastshort" />
      <el-table-column show-overflow-tooltip label="短期上报" prop="reportStateForecast" />
      <el-table-column show-overflow-tooltip label="理论功率" prop="theoryPower" />
      <el-table-column show-overflow-tooltip label="可用功率" prop="availablePower" />
      <el-table-column show-overflow-tooltip label="限电信号" prop="powerLimitSign" />
      <el-table-column show-overflow-tooltip label="两则准确率" prop="accuracy" />

    </el-table>
    <el-pagination
      :background="background"
      :current-page="page.currentPage"
      :layout="layout"
      :page-size="page.pageSize"
      :total="page.total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>
  </div>
</template>

<script>
import { fetchList } from '@/api/backstat'

export default {
  name: 'Record',

  data() {
    return {
      tableData: [],
      searchForm: {

        },

      times:[ this.dateFormat("yyyy-MM-dd HH:mm:ss",new Date(new Date().setHours(0, 0, 0, 0))),
        this.dateFormat("yyyy-MM-dd HH:mm:ss",this.dateFor15(new Date()))],
      listLoading: true,
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      background: true,
      elementLoadingText: '正在加载...',
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20, // 每页显示多少条
      },
    }
  },
  computed: {
    height() {
      return this.$baseTableHeight()
    },
  },
  created() {
    this.fetchData()
  },
  methods: {
    async fetchData() {
      this.listLoading = true
      this.searchForm.startTime = null
      this.searchForm.endTime = null
      if(this.times !=null){
        this.searchForm.startTime = this.times[0]
        this.searchForm.endTime = this.times[1]
      }
      fetchList(
        Object.assign(
          {
            current: this.page.currentPage,
            size: this.page.pageSize,
          },
          this.searchForm
        )
      )
        .then((response) => {
          this.tableData = response.data.records
          this.page.total = response.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.listLoading = false
        })
    },

    handleSizeChange(val) {
      this.page.pageSize = val
      this.page.currentPage = 1
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.page.currentPage = val
      this.fetchData()
    },
    handleQuery() {
      for (var v in this.searchForm) {
        if (this.searchForm[v] == '') {
          delete this.searchForm[v]
        }
      }

      this.page.currentPage = 1
      this.fetchData()
    },

  dateFor15(date){
   return  new Date(Math.floor(date.getTime()/900000) *900000)
  },

    dateFormat(fmt, date) {
      let ret
      const opt = {
        'y+': date.getFullYear().toString(), // 年
        'M+': (date.getMonth() + 1).toString(), // 月
        'd+': date.getDate().toString(), // 日
        'H+': date.getHours().toString(), // 时
        'm+': date.getMinutes().toString(), // 分
        's+': date.getSeconds().toString(), // 秒
        // 有其他格式化字符需求可以继续添加，必须转化成字符串
      }
      for (let k in opt) {
        ret = new RegExp('(' + k + ')').exec(fmt)
        if (ret) {
          fmt = fmt.replace(
            ret[1],
            ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, '0')
          )
        }
      }
      return fmt
    },
  },
}
</script>

<style>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>
