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

      <vab-query-form-left-panel>
        <el-form
          ref="searchForm"
          :model="searchForm"
          :inline="true"
          @submit.native.prevent
        >
          <el-form-item>
            <el-date-picker
              v-model="times"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"

              value-format="timestamp"
            ></el-date-picker>
          </el-form-item>

          <el-form-item>
            <el-select
              v-model="searchForm.stationCode"
              clearable
              placeholder="场站"
            >
              <el-option
                v-for="item in stations"
                :key="item.stationCode"
                :label="item.name"
                :value="item.stationCode"
              ></el-option>
            </el-select>
          </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>

    <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  fixed  label="序号" width="95">
        <template #default="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column
        fixed

        label="一体化公司"
        prop="inCode"
        :formatter="formatCompany"
      />

      <el-table-column
        fixed
        width="150"
        label="场站名称"
        prop="stationCode"
        :formatter="formatStation"
      />
      <el-table-column  width="100" label="下载时间" prop="initTime" />

      <el-table-column width="100" label="上送时间" prop="uploadTime" />
      <el-table-column width="100" label="修正时间" prop="correctTime" />
      <el-table-column width="100" label="预测时间" prop="forecastTime" :formatter="formatDate"/>
      <el-table-column show-overflow-tooltip label="预测功率" prop="fpValue" />
      <el-table-column show-overflow-tooltip label="预测功率(修)" prop="fpValueCorrect" />
      <el-table-column show-overflow-tooltip label="总辐射" prop="swr" />
      <el-table-column show-overflow-tooltip label="总辐射(修)" prop="swrCorrect" />
      <el-table-column show-overflow-tooltip label="直辐射" prop="directRadiation" />
      <el-table-column show-overflow-tooltip label="直辐射(修)" prop="directRadiationCorrect" />
      <el-table-column show-overflow-tooltip label="散辐射" prop="diffuseRadiation" />
      <el-table-column show-overflow-tooltip label="散辐射(修)" prop="diffuseRadiationCorrect" />

      <el-table-column show-overflow-tooltip label="风速" prop="windSpeed" />
      <el-table-column show-overflow-tooltip label="风速(修)" prop="windSpeedCorrect" />

      <el-table-column show-overflow-tooltip label="风向" prop="windDir" />
      <el-table-column show-overflow-tooltip label="风向(修)" prop="windDirCorrect" />

      <el-table-column show-overflow-tooltip label="气温" prop="temperature" />
      <el-table-column show-overflow-tooltip label="气温(修)" prop="temperatureCorrect" />

      <el-table-column show-overflow-tooltip label="相对湿度" prop="humidity" />
      <el-table-column show-overflow-tooltip label="相对湿度(修)" prop="humidityCorrect" />

      <el-table-column show-overflow-tooltip label="压力" prop="pressure" />
      <el-table-column show-overflow-tooltip label="压力(修)" prop="pressureCorrect" />

    </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 ,delObj} from '@/api/forecastData'
  import { getCompanyAll } from '@/api/integrationCompany'
  import { getStationAll } from '@/api/station'


  export default {
    name: 'Record',

    data() {
      return {
        tableData: [],
        companys: [],
        types: [{label:"拉取原始数据",value:"PULL_INIT"},
          {label:"推送原始数据",value:"PUSH_INIT"},
          {label:"拉取修正数据",value:"PULL_CORRECT"},
          {label:"交互权限",value:"COM_PERMISSON"},],
        stations: [],
        searchForm: {},
        times: [
        new Date(new Date().setHours(0, 0, 0, 0)).getTime(),
        new Date(new Date().setHours(23, 59, 59, 59)).getTime()+1000*60*60*24*12,
        ],
        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.getCompany()
    },
    methods: {
      getCompany() {
        getCompanyAll()
          .then((response) => {
            this.companys = response.data
            this.getStation()
            this.listLoading = false
          })
          .catch(() => {
            this.listLoading = false
          })
      },

      getStation() {
        getStationAll()
          .then((response) => {
            this.stations = response.data
            this.fetchData()
            this.listLoading = false
          })
          .catch(() => {
            this.listLoading = false
          })
      },
      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()
      },
      handleDelete(row) {
        this.$baseConfirm('你确定要删除当前项吗', null, async () => {
          await delObj(row.id)
          this.$baseMessage('删除成功', 'success')
          this.fetchData()
        })
      },
      formatCompany(row, column) {
        for (let i = 0; i < this.companys.length; i++) {
          if (row.inCode == this.companys[i].code) {
            return this.companys[i].name
          }
        }
      },
      formatType(row, column) {
        for (let i = 0; i < this.types.length; i++) {
          if (row.type == this.types[i].value) {
            return this.types[i].label
          }
        }
      },
      formatStation(row, column) {
        const selectedItem = this.stations.find((item) => {
          return item.stationCode === row.stationCode
        })
        return selectedItem.name
      },
      formatDate(row, column) {
        //datetime是拿到的时间戳
        var date = new Date(row.forecastTime);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
        var year = date.getFullYear(),
          month = ("0" + (date.getMonth() + 1)).slice(-2),
          sdate = ("0" + date.getDate()).slice(-2),
          hour = ("0" + date.getHours()).slice(-2),
          minute = ("0" + date.getMinutes()).slice(-2),
          second = ("0" + date.getSeconds()).slice(-2);
// 拼接
        var result = year + "-"+ month +"-"+ sdate +" "+ hour +":"+ minute +":" + second;
// 返回
        return result;
      },

    },
  }
</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>
