<template>
  <div class="app-container">
    <el-form :inline="true" size="small" class="dark-el-input dark-el-button">
      <el-form-item label="时间">
        <el-date-picker
          :clearable="false"
          v-model="dateTime"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
          popper-class="cpp-popper"
        />
      </el-form-item>
      <el-form-item label="场站名称">
        <el-select v-model="stationCode" placeholder="请选择" style="width: 150px" popper-class="cpp-popper" clearable>
          <el-option
            v-for="item in stationList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="文件类型">
        <el-select v-model="fileTypeName" placeholder="请选择" style="width: 150px" popper-class="cpp-popper" clearable>
          <el-option
            v-for="item in fileType"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="数据源">
        <el-select v-model="dataSources" placeholder="请选择" style="width: 150px" popper-class="cpp-popper" clearable>
          <el-option
            v-for="item in dataSourcesList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="文件解析状态">
        <el-select v-model="fileStatus" placeholder="请选择" style="width: 100px" popper-class="cpp-popper" clearable>
          <el-option
            v-for="item in fileStatusList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="文件解析状态描述">
        <el-select v-model="fileDescribe" placeholder="请选择" style="width: 100px" popper-class="cpp-popper" clearable>
          <el-option
            v-for="item in fileDescribeList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="mini" style="margin-left: 5px" icon="el-icon-search"
                   @click="queryByStationCode">查询
        </el-button>
      </el-form-item>
    </el-form>
    <div style="padding-top: 10px">
      <vxe-table
        ref="xTable"
        align="center"
        class="mytable-style"
        auto-resize
        border
        resizable
        export-config
        highlight-current-row
        show-overflow
        max-height="700"
        :loading="loading"
        :data="tableData"
        :row-style="rowStyle">
        <vxe-table-column field="stationCode" title="场站名称" :formatter="codeChangeName"></vxe-table-column>
        <vxe-table-column field="dataSources" title="数据源" :formatter="changeDataSource"></vxe-table-column>
        <vxe-table-column field="fileType" title="文件类型" :formatter="changeFileName"></vxe-table-column>
        <vxe-table-column field="fileName" title="文件名称"></vxe-table-column>
        <vxe-table-column field="parsingTime" title="解析时间" :formatter="formatDateTime"></vxe-table-column>
        <vxe-table-column field="parsingFileStatus" title="文件解析状态"></vxe-table-column>
        <vxe-table-column field="parsingDescribe" title="文件解析状态描述"></vxe-table-column>
      </vxe-table>
      <vxe-pager
        background
        :loading="loading"
        :current-page.sync="currentPage"
        :page-size.sync="pageSize"
        :total="total"
        @page-change="handlePageChange"
        :layouts="['PrevJump', 'PrevPage', 'JumpNumber', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']">
      </vxe-pager>
    </div>
  </div>
</template>


<script>
export default {
  name: 'analyzeRecords',
  data() {
    return {
      dateTime: [new Date(new Date().toLocaleDateString()).getTime(), new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1],
      fileType: [],
      fileTypeName: '',
      fileStatus: '',
      fileDescribe: '',
      dataSources: '',
      dataSourcesList: [],
      fileDescribeList: [
        {
          value: 'null',
          label: '空'
        },
        {
          value: 'notNull',
          label: '非空'
        }
      ],
      fileStatusList: [
        {
          value: '成功',
          label: '成功'
        },
        {
          value: '失败',
          label: '失败'
        }
      ],
      title: "",
      total: 0,
      pageSize: 10,
      currentPage: 1,
      stationList: [],
      stationCode: '',
      stationName: undefined,
      tableData: [],
      loading: false,
      electricFieldTypeEnum: '',
      windFileTypeList: [],
      photovoltaicFileTypeList: [],
      allFileTypeList: []
    }
  },
  created() {
    this.getDataSourcesEnum()
    this.getAllFileType()
    this.getFileTypeInfo()

  },
  mounted() {
  },
  computed: {},
  methods: {
    getFileTypeInfo() {
      this.$axios.get('/enumSelect/allUseFileType').then(response => {
        this.photovoltaicFileTypeList = response.data.solar
        this.windFileTypeList = response.data.wind
        this.getStationCode()
      })
    },
    handlePageChange({currentPage, pageSize}) {
      this.currentPage = currentPage
      this.pageSize = pageSize
      this.queryByStationCode();
    },
    //日期转换器
    formatDateTime(cellValue) {
      if (cellValue.cellValue == null) {
        return ''
      }
      const date = new Date(cellValue.cellValue)
      const Y = date.getFullYear() + '-'
      const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
      const D = date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()
      const h = " " + (date.getHours() < 10 ? '0' : '') + date.getHours() + ':'
      const m = (date.getMinutes() < 10 ? '0' : '') + date.getMinutes() + ':'
      const s = (date.getSeconds() < 10 ? '0' : '') + date.getSeconds()
      return Y + M + D + h + m + s
    },
    getDataSourcesEnum() {
      this.$axios.get("/enumSelect/dataSourcesEnum").then(response => {
        this.dataSourcesList = response.data
      })
    },
    getStationCode() {
      this.$axios({url: '/electricfield/all', method: 'get'}).then(response => {
        this.stationList = response.data
        this.stationList.push({value: '未知', label: '未知'})
        if (this.stationList.length > 0) {
          this.stationCode = this.stationList[0].value
          this.queryByStationCode()
        }
      })
    },
    getAllFileType() {
      this.$axios.get('enumSelect/fileTypeEnum').then(response => {
        this.allFileTypeList = response.data
      })
    },
    queryByStationCode() {
      this.loading = true
      let startTime = Math.round(this.dateTime[0])
      let endTime = Math.round(this.dateTime[1])
      if (endTime <= startTime) {
        this.$message.warning("开始时间不能大于结束时间")
        return
      }
      const param = {
        "stationCode": this.stationCode,
        "fileType": this.fileTypeName,
        "startTime": startTime,
        "endTime": endTime,
        "dataSources": this.dataSources,
        "parsingDescribe": this.fileDescribe,
        "parsingFileStatus": this.fileStatus,
        "size": this.pageSize,
        "page": this.currentPage
      }
      this.$axios.get('/parsingLog/getByStationCodeAndCreateTimeAndFileType', {params: param}).then(response => {
        this.tableData = response.data.data.records
        this.total = response.data.data.total
        if (this.stationCode != '未知') {
          if (response.data.type === 'E1') {
            this.fileType = this.photovoltaicFileTypeList
          } else {
            this.fileType = this.windFileTypeList
          }
        } else {
          this.fileType = this.allFileTypeList
        }
        this.loading = false
      })
    },
    codeChangeName(row) {
      var codeList = this.stationList
      for (let i = 0; i < codeList.length; i++) {
        if (row.cellValue == codeList[i].value) {
          return codeList[i].label
        }
      }
    },
    changeDataSource(row) {
      for (let i = 0; i < this.dataSourcesList.length; i++) {
        if (row.cellValue === this.dataSourcesList[i].value) {
          return this.dataSourcesList[i].label
        }
      }
    },
    changeFileName(row) {
      for (let i = 0; i < this.allFileTypeList.length; i++) {
        if (row.cellValue === this.allFileTypeList[i].value) {
          return this.allFileTypeList[i].label
        }
      }
    },
    // 根据文件样式改变行样式
    rowStyle({row, rowIndex}) {
      if (row.parsingFileStatus === '失败') {
        return {
          background: 'linear-gradient(to bottom, #fa908f,#f53f3d)',
          color: '#ffffff'
        }
      }
    },
  }
}
</script>

<style scoped>
</style>
