<template>
  <div class="table-container">
    <div class="title">
      <div class="left">
        <div class="left-item">
          <div>场站：</div>
          <el-select v-model="selectValue" placeholder="请选择" clearable>
            <el-option v-for="item in stationList" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </div>
        <div class="left-item">
          <div style="width: 70px;">名称：</div>
          <el-input v-model="fuzzyQuery" placeholder="请输入" clearable />
        </div>
        <el-button icon="el-icon-search" type="primary" native-type="submit" @click="fetchData">
          查询
        </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">
      <!-- show-summary :summary-method="getSummaries" -->
      <el-table-column show-overflow-tooltip type="selection" width="65"></el-table-column>
      <el-table-column show-overflow-tooltip label="风场编号" prop="stationid" width="200" align="center" sortable>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="风场名称" prop="stationname" align="center" sortable></el-table-column>
      <el-table-column show-overflow-tooltip label="指标" prop="kay" align="center" sortable>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="值" prop="value" align="center" sortable>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="录入时间" prop="createtime" align="center" sortable>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="操作" width="180px">
        <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>
    <table-edit ref="edit" :stationList="stationList" @handleSuccess="fetchData"></table-edit>
  </div>
</template>

<script>
import api from '@/api/table'
import apis from '@/api/station'
import TableEdit from './components/TableEdit'
export default {
  name: 'ComprehensiveTable',
  components: {
    TableEdit
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger',
      }
      return statusMap[status]
    },
  },
  data() {
    return {
      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: [],
    }
  },
  computed: {
    height() {
      return this.$baseTableHeight() + 50
    },
  },
  created() {
    this.getStation()
    this.fetchData()
  },
  beforeDestroy() { },
  mounted() { },
  methods: {
    getStation() {
      apis.windpowerstationList({
        id: '',
        name: '',
        companyid: '',
        pageNum: 1,
        pageSize: 1000,
      }).then(res => {
        if (res.data) {
          this.listLoading = false
          this.stationList = res.data.records
        }
      })
    },
    setSelectRows(val) {
      this.selectRows = val
    },
    handleAdd() {
      this.$refs['edit'].showEdit()
    },
    handleEdit(row) {
      this.$refs['edit'].showEdit(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()
          }
          api.removeIndicators({
            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.queryForm.pageNo = 1
      this.fetchData()
    },
    fetchData() {
      this.listLoading = true
      api.indicators({
        windpowerstationid: this.selectValue,
        name: this.fuzzyQuery,
        pagenum: this.queryForm.pageNo,
        pagesize: this.queryForm.pageSize,
      }).then(res => {
        if (res.data) {
          this.total = res.data.total
          this.list = res.data.records
          setTimeout(() => {
            this.listLoading = false
          }, 500)
        }
      })
    },
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '总计';
          return;
        } else if (index === 4) {
          let totle = 0
          data.forEach(item => {
            totle = totle + Number(item.value)
          })
          sums[index] = totle;
        } else {
          sums[index] = '--';
        }
      });
      return sums;
    }
  },
}
</script>
<style lang="less" scoped>
.title {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
}

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

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

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