<template>
  <div class="table-container">
    <vab-query-form>
      <div class="title">
        <div class="left">
          <div class="left-item">
            <div style="width: 70px;">公司：</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: 100px;">风场编码：</div>
            <el-input v-model="fuzzyQuery" placeholder="请输入" clearable />
          </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>
          <UploadExcel style="margin-left: 10px;" @getResult="getMyExcelData"></UploadExcel>
          <ExportExcel :exportList="exportExcel" :useType="'export'" partsName="场站"></ExportExcel>
          <ExportExcel :exportList="templateExcel" :useType="'template'" partsName="场站模板"></ExportExcel>
        </div>
      </div>
      <el-table ref="tableSort" v-loading="listLoading" :data="list" :element-loading-text="elementLoadingText"
        header-cell-class-name="table_header_style" border
        :height="height" @selection-change="setSelectRows">
        <el-table-column show-overflow-tooltip type="selection" width="55" align="center" fixed></el-table-column>
        <el-table-column show-overflow-tooltip label="编码" prop="id" align="center" sortable fixed width="100">
        </el-table-column>
        <el-table-column show-overflow-tooltip label="风场名称" prop="name" align="center" sortable fixed width="150">
        </el-table-column>
        <el-table-column show-overflow-tooltip label="别名" prop="aname" align="center" sortable width="160">
        </el-table-column>
        <el-table-column show-overflow-tooltip label="区域编号" prop="regionid" align="center" sortable width="160">
        </el-table-column>
        <el-table-column show-overflow-tooltip label="风场编码" prop="code" align="center" sortable width="150">
        </el-table-column>
        <el-table-column show-overflow-tooltip label="地址" prop="address" align="center" sortable width="180">
        </el-table-column>
        <el-table-column show-overflow-tooltip label="电话" prop="telephone" align="center" sortable width="120">
        </el-table-column>
        <el-table-column show-overflow-tooltip label="装机容量" prop="capacity" align="center" sortable width="150">
        </el-table-column>
        <el-table-column show-overflow-tooltip label="容量单位" prop="capacityunit" align="center" sortable width="150">
        </el-table-column>
        <el-table-column show-overflow-tooltip label="装机数量" prop="quantity" align="center" sortable width="150">
        </el-table-column>
        <el-table-column show-overflow-tooltip label="地球经度" prop="longitude" align="center" sortable width="150">
        </el-table-column>
        <el-table-column show-overflow-tooltip label="地球纬度" prop="latitude" align="center" sortable width="150">
        </el-table-column>
        <el-table-column show-overflow-tooltip label="图片" prop="photo" align="center" sortable></el-table-column>
        <el-table-column show-overflow-tooltip label="公司编号" prop="companyid" align="center" sortable width="150">
        </el-table-column>
        <el-table-column show-overflow-tooltip label="系统厂家" prop="systemmanufact" align="center" sortable width="150">
        </el-table-column>
        <el-table-column show-overflow-tooltip label="场长" prop="headfarm" align="center" sortable></el-table-column>
        <el-table-column show-overflow-tooltip label="场长电话" prop="headfarmphone" align="center" sortable width="120">
        </el-table-column>
        <el-table-column show-overflow-tooltip label="机型" prop="model" align="center" sortable width="120">
        </el-table-column>
        <el-table-column show-overflow-tooltip label="顺序" prop="ordernum" align="center" sortable></el-table-column>
        <el-table-column show-overflow-tooltip label="海拔高度" prop="height" align="center" sortable width="150">
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
          <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" :options="options" :stationList="stationList" @save-success="fetchData"></table-edit>
    </vab-query-form>
  </div>
</template>
<script>
import UploadExcel from '@/components/UploadExcel/index'
import ExportExcel from '@/components/ExportExcel/index'
import api from '@/api/station'
import TableEdit from './components/TableEdit'
export default {
  components: {
    UploadExcel,
    ExportExcel,
    TableEdit,
  },
  data() {
    return {
      fuzzyQuery: '',
      listLoading: true,
      list: [],
      options: [],
      exportExcel: [],
      templateExcel: [],
      elementLoadingText: '正在加载...',
      selectRows: '',
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      background: true,
      exportList: [],
      queryForm: {
        pageNo: 1,
        pageSize: 20,
      },
      stationList: [],
      selectValue: ''
    };
  },
  computed: {
    height() {
      return this.$baseTableHeight() + 50 + 50
    },
  },
  created() {
    this.getStation()
    this.fetchData()
    this.getTree()
  },
  methods: {
    handleQuery() {
      this.queryForm.pageNo = 1
      this.fetchData()
    },
    getTree() {
      api.newtreeTreels({tag:1}).then(res => {
        if (res.data) {
          this.options = res.data
        }
      })
    },
    getMyExcelData(val) {
      val.forEach(item => {
        api.addWindpowerstation(item).then(res => {
          if (res.code == 200) {
            this.$message({
              type: 'success',
              message: '添加成功!'
            });
            this.fetchData()
          }
        })
      })
    },
    getStation() {
      api.companys({
        id: '',
        name: '',
        pageNum: 1,
        pageSize: 1000,
      }).then(res => {
        if (res.data) {
          this.listLoading = false
          this.stationList = res.data.records
        }
      })
    },
    fetchData() {
      api.windpowerstationList({
        id: '',
        companyid: this.selectValue,
        code: this.fuzzyQuery,
        pageNum: this.queryForm.pageNo,
        pageSize: this.queryForm.pageSize,
      }).then(res => {
        if (res.data) {
          this.listLoading = false
          this.total = res.data.total
          this.list = res.data.records
          this.templateExcel = [
            {
              id: "",
              name: "",
              aname: "",
              regionid: "",
              code: "",
              address: "",
              telephone: "",
              capacity: "",
              capacityunit: "",
              quantity: "",
              longitude: "",
              latitude: "",
              photo: "",
              companyid: "",
              systemmanufact: "",
              headfarm: "",
              headfarmphone: "",
              model: "",
              ordernum: "",
              height: "",
            }
          ]
          // 导出
          let exportExcel = []
          res.data.records.forEach(item => {
            exportExcel.push({
              id: item.id,
              name: item.name,
              aname: item.aname,
              regionid: item.regionid,
              code: item.code,
              address: item.address,
              telephone: item.telephone,
              capacity: item.capacity,
              capacityunit: item.capacityunit,
              quantity: item.quantity,
              longitude: item.longitude,
              latitude: item.latitude,
              photo: item.photo,
              companyid: item.companyid,
              systemmanufact: item.systemmanufact,
              headfarm: item.headfarm,
              headfarmphone: item.headfarmphone,
              model: item.model,
              ordernum: item.ordernum,
              height: item.height,
            })
          })
          this.exportExcel = exportExcel
        }
      })
    },
    handleSizeChange(val) {
      this.queryForm.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.queryForm.pageNo = val
      this.fetchData()
    },
    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.deleteWindpowerstation({
            id: row.id || ids
          }).then(res => {
            if (res.data) {
              this.$baseMessage('删除成功', 'success')
              this.fetchData()
            }
          })
        })
      } else {
        this.$baseMessage('未选中任何行', 'error')
        return false
      }
    },
    setSelectRows(val) {
      this.selectRows = val
    },
  },
}
</script>
<style lang="less" scoped>
.title {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
}

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

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