<!--
  -    Copyright (c) 2018-2025, lengleng All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without
  - modification, are permitted provided that the following conditions are met:
  -
  - Redistributions of source code must retain the above copyright notice,
  - this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
  - notice, this list of conditions and the following disclaimer in the
  - documentation and/or other materials provided with the distribution.
  - Neither the name of the pig4cloud.com developer nor the names of its
  - contributors may be used to endorse or promote products derived from
  - this software without specific prior written permission.
  - Author: lengleng (wangiegie@gmail.com)
  -->
<template>
  <div class="execution">
    <div class="filter">
      <div class="startTime" style="display: inline-block">
        <span class="timeText" style="font-weight: bold;font-size: 14px">&#12288;预测起始时间:</span>
        <el-date-picker
          v-model="startTime"
          :clearable="false"
          type="datetime"
          value-format="timestamp"
          placeholder="选择日期">
        </el-date-picker>
      </div>
      <div class="endTime" style="display: inline-block">
        <span class="timeText" style="font-weight: bold;font-size: 14px">&#12288;预测截止时间:</span>
        <el-date-picker
          v-model="endTime"
          :clearable="false"
          type="datetime"
          value-format="timestamp"
          placeholder="选择日期">
        </el-date-picker>
      </div>
      <div style="display: inline-block">
        <span style="font-weight: bold;font-size: 14px">&#12288;场站名称：</span>
        <el-select style="width:250px" v-model="stationCode"
                   size="small" @change="getManufactor">
          <el-option
            v-for="item in stationList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
            <span style="float: left">{{ item.label }}</span>
            <span style="float: right; color: #8492a6;font-size: 13px">{{ item.value }}</span>
          </el-option>
        </el-select>
      </div>
      <div style="display: inline-block">
        <span style="font-weight: bold;font-size: 14px">&#12288;预测厂家：</span>
        <el-select style="width:250px" v-model="forecastManufactor" size="small">
          <el-option
            v-for="item in forecastManufactorList"
            :key="item.sign"
            :label="item.name"
            :value="item.sign">
            <span style="float: left">{{ item.name }}</span>
            <span style="float: right; color: #8492a6;font-size: 13px">{{ item.sign }}</span>
          </el-option>
        </el-select>
      </div>
      &nbsp;
      <div class="timeQuery" style="display: inline-block">
        <el-button size="small" :loading="tableLoading" @click="queryNWPDataByStationCode">查询</el-button>
      </div>
    </div>
    <basic-container>
      <avue-crud ref="crud"
                 :page.sync="page"
                 :data="tableData"
                 :table-loading="tableLoading"
                 :option="tableOption"
      >
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import {fetchList, getObj, addObj, putObj, delObj, getStation, queryTableData} from '@/api/nwp'
import {tableOption} from '@/const/crud/nwp'
import {mapGetters} from 'vuex'
import {getManufactorByStationCode} from '@/api/forecastmanufactor'

export default {
  name: 'nwp',
  data() {
    return {
      searchForm: {},
      tableData: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      startTime: new Date(new Date().toLocaleDateString()).getTime() + 60 * 60 * 24 * 1000,
      endTime: new Date(new Date().toLocaleDateString()).getTime() + 60 * 60 * 24 * 1000 * 4 - 1,
      stationCode: '',
      stationList: [],
      tableLoading: false,
      tableOption: tableOption,
      forecastManufactor: '',
      forecastManufactorList: [],
    }
  },
  mounted() {
    this.getStationCode();
    // this.getForecastManufactor(this.page)
  },
  computed: {
    ...mapGetters(['permissions']),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permissions.idp_nwp_add, false),
        delBtn: this.vaildData(this.permissions.idp_nwp_del, false),
        editBtn: this.vaildData(this.permissions.idp_nwp_edit, false)
      };
    }
  },
  methods: {
    getManufactor(val) {
      this.forecastManufactorList = []
      this.forecastManufactor = []
      getManufactorByStationCode(val).then(response => {
        this.forecastManufactorList = response.data.data
        if (this.forecastManufactorList != '') {
          this.forecastManufactor = this.forecastManufactorList[0].sign;
        }
        this.tableLoading = false
      })
    },
    queryNWPDataByStationCode() {
      const param = new URLSearchParams()
      param.append('currentPage', this.page.currentPage)
      param.append('pageSize', this.page.pageSize)
      param.append('stationCode', this.stationCode)
      param.append('startTime', this.startTime)
      param.append('endTime', this.endTime)
      param.append('forecastManufactor', this.forecastManufactor)
      queryTableData(param).then((res) => {
        this.tableData = res.data.data.records
        this.page.pageSize = res.data.data.size
        this.page.total = res.data.data.total
      })
    },
    getForecastManufactor(page, params) {
      this.tableLoading = true
      getForecastManufactor(Object.assign({
        // current: page.currentPage,
        // size: page.pageSize
      }, params, this.searchForm)).then(response => {
        console.log(response)
        this.forecastManufactorList = response.data.data.records
        if (this.forecastManufactorList.length > 0) {
          this.forecastManufactor = this.forecastManufactorList[0].sign
        }
      })
    },
    //获取所有场站
    getStationCode() {
      this.tableLoading = true
      getStation().then(response => {
        this.stationList = response.data.data
        if (this.stationList.length > 0) {
          this.stationCode = this.stationList[0].value
          this.getManufactor(this.stationCode)
        }
        this.tableLoading = false
      })
    },
    getList(page, params) {
      this.tableLoading = true
      fetchList(Object.assign({
        current: page.currentPage,
        size: page.pageSize
      }, params, this.searchForm)).then(response => {
        this.tableData = response.data.data.records
        this.page.total = response.data.data.total
        this.tableLoading = false
      }).catch(() => {
        this.tableLoading = false
      })
    },
    rowDel: function (row, index) {
      this.$confirm('是否确认删除ID为' + row.id, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return delObj(row.id)
      }).then(data => {
        this.$message.success('删除成功')
        this.getList(this.page)
      })
    },
    handleUpdate: function (row, index, done, loading) {
      putObj(row).then(data => {
        this.$message.success('修改成功')
        done()
        this.getList(this.page)
      }).catch(() => {
        loading();
      });
    },
    handleSave: function (row, done, loading) {
      addObj(row).then(data => {
        this.$message.success('添加成功')
        done()
        this.getList(this.page)
      }).catch(() => {
        loading();
      });
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize
    },
    currentChange(current) {
      this.page.currentPage = current
    },
    searchChange(form, done) {
      this.searchForm = form
      this.page.currentPage = 1
      this.getList(this.page, form)
      done()
    },
    refreshChange() {
      this.getList(this.page)
    }
  }
}
</script>
