<template>
  <div class="table-container">
    <div class="title">
      <div class="left">
        <div class="left-item">
          <div class="left-station">
            <div class="text">场站：</div>
            <el-select
              v-model="wpId"
              style="width: 200px"
              placeholder="请选择"
              clearable
              @change="handleChangeWpid"
            >
              <el-option
                v-for="item in wplist"
                :key="item.nemCode"
                :label="item.name"
                :value="item.nemCode"
              ></el-option>
            </el-select>
          </div>
          <div class="left-station">
            <div class="text">日期：</div>
            <el-date-picker
              v-model="date"
              style="width: 200px"
              type="date"
              placeholder="选择日期"
              @change="handleChangeDate"
            ></el-date-picker>
          </div>
        </div>
        <el-button
          icon="el-icon-search"
          type="primary"
          native-type="submit"
          @click="handleQuery"
        >
          查询
        </el-button>
      </div>
      <div class="right">
        <el-button icon="el-icon-plus" type="primary" @click="handleAdd">
          添加
        </el-button>
        <el-button icon="el-icon-delete" type="danger" @click="handleDelete">
          删除
        </el-button>
      </div>
    </div>
    <el-table
      ref="tableSort"
      v-loading="listLoading"
      :data="list"
      :element-loading-text="elementLoadingText"
      :height="height"
      @selection-change="setSelectRows"
    >
      <el-table-column
        width="65"
        show-overflow-tooltip
        type="selection"
      ></el-table-column>
      <el-table-column
        width="200"
        show-overflow-tooltip
        label="场站名称"
        prop="windpowerstationName"
        align="center"
        sortable
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="填报日期"
        prop="recordDate"
        align="center"
        sortable
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="日发电量(kWh)"
        prop="generationDay"
        align="center"
        sortable
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="月发电量(kWh)"
        prop="generationMonth"
        align="center"
        sortable
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="年发电量(kWh)"
        prop="generationYear"
        align="center"
        sortable
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="功率负荷(kW)"
        prop="power"
        align="center"
        sortable
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="风速/光照(m/s / W/m²)"
        prop="speedOrBeam"
        align="center"
        sortable
      ></el-table-column>

      <el-table-column
        show-overflow-tooltip
        label="操作"
        width="120px"
        align="center"
        fixed="right"
      >
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- <el-pagination
      :background="background"
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination> 
      :options="options"
      :station-list="stationList"
      :standard-list="standardList"
      @handleSuccess="fetchData"-->
    <table-edit
      ref="edit"
      :wplist="wplist"
      @handleSuccess="fetchData"
    ></table-edit>
  </div>
</template>

<script>
  import { getList, getWplist, doDelete } from '@/api/zhibiao.js'
  import { getWpid } from '@/api/user.js'
  import { mapGetters } from 'vuex'
  import TableEdit from './components/TableEdit'
  import dayjs from 'dayjs'
  export default {
    name: 'Target',
    components: {
      TableEdit,
    },
    filters: {
      statusFilter(status) {
        const statusMap = {
          published: 'success',
          draft: 'gray',
          deleted: 'danger',
        }
        return statusMap[status]
      },
    },
    data() {
      return {
        options: [],
        imgShow: true,
        list: [],
        fuzzyQuery: '',
        cascaderSel: [],
        selectValue: '',
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        background: true,
        selectRows: '',
        elementLoadingText: '正在加载...',
        queryForm: {
          pageNo: 1,
          pageSize: 20,
          title: '',
        },
        stationList: [],
        standardList: [], //指标列表
        date: '',
        wpid: '',
        wpId: '',
        wplist: [],
        windpowerstationId: '',
      }
    },
    computed: {
      height() {
        return this.$baseTableHeight() + 50
      },
      ...mapGetters({
        userInfo: 'user/userInfo',
      }),
    },
    created() {
      this.date = dayjs().format('YYYY-MM-DD')
      this.getWpid()
    },
    beforeDestroy() {},
    mounted() {},
    methods: {
      getWpid() {
        getWpid(this.userInfo).then(({ data }) => {
          if (Object.keys(data.data)) {
            this.windpowerstationId = data.data.windpowerstationId
            if (data.data.windpowerstationId == 0) {
              this.wpId = '0'
            } else {
              this.wpId = data.data.windpowerstationId
            }
            this.wpid = data.data.windpowerstationId
            this.fetchData()
            this.getWplist()
          }
        })
      },
      fetchData() {
        this.listLoading = true
        getList({
          date: this.date,
          wpid: this.wpid,
        }).then(({ data: res }) => {
          if (res.data) {
            this.list = res.data.map((item) => {
              return {
                ...item,
                recordDate: dayjs(item.recordDate).format('YYYY-MM-DD'),
              }
            })
            setTimeout(() => {
              this.listLoading = false
            }, 500)
          }
        })
      },
      getWplist() {
        getWplist({ wpid: this.wpid }).then(({ data: res }) => {
          if (this.wpid == 0) {
            this.wplist = [
              {
                name: '全部',
                nemCode: '0',
              },
              ...res.data,
            ]
          } else {
            this.wplist = res.data
          }
        })
      },
      handleChangeWpid(val) {
        if (!val) {
          this.windpowerstationId == 0
            ? ((this.wpid = 0), (this.wpId = '0'))
            : ((this.wpid = this.windpowerstationId),
              (this.wpId = this.windpowerstationId))
        } else {
          this.wpid = val
        }
        // this.fetchData()
      },
      handleChangeDate(val) {
        if (!val) {
          this.date = dayjs().format('YYYY-MM-DD')
        } else {
          this.date = dayjs(val).format('YYYY-MM-DD')
        }

        // this.fetchData()
      },
      setSelectRows(val) {
        this.selectRows = val
      },
      handleAdd() {
        if (this.wpId && this.date) {
          this.$refs['edit'].setFormItem(this.wpid, this.date)
          this.$refs['edit'].openDialog()
        } else {
          this.$refs['edit'].openDialog()
        }
      },
      handleEdit(row) {
        this.$refs['edit'].openDialog(row)
      },
      handleDelete(row) {
        if (row.id || this.selectRows.length > 0) {
          this.$baseConfirm('你确定要删除吗', null, async () => {
            let ids = ''
            if (this.selectRows.length > 0) {
              ids = this.selectRows.map((item) => item.id).join()
            }
            doDelete({
              id: row.id || ids,
            }).then((res) => {
              if (res.data) {
                this.$baseMessage('删除成功', 'success')
                this.fetchData()
              }
            })
          })
        } else {
          this.$baseMessage('未选中任何行', 'error')
          return false
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val
        this.fetchData()
      },
      handleQuery() {
        this.fetchData()
      },
    },
  }
</script>
<style lang="scss" scoped>
  .title {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 15px;
  }

  .left {
    display: flex;
    flex-direction: row;
    align-items: center;

    .left-item {
      display: flex;
      flex-direction: row;
      align-items: center;
      margin-right: 10px;
    }
    .left-station {
      display: flex;
      align-items: center;

      margin-right: 15px;
    }
  }

  .cascaders {
    width: 300px;
  }
</style>
