<template>
  <div class="table-container">
    <vab-query-form>
      <vab-query-form-left-panel>
        <el-form ref="form" :model="queryForm" :inline="true" @submit.native.prevent>
          <el-form-item label="公司名称">
            <el-input v-model="fuzzyQuery" placeholder="请输入" clearable />
            <!-- <el-cascader 
              v-model="fuzzyQuery" 
              :options="options" 
              style="width: 240px" 
              :props="{ checkStrictly: true, label: 'name', value: 'id' , children:'children1' }" 
              clearable /> -->
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" native-type="submit" @click="handleQuery">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel>
        <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>
      </vab-query-form-right-panel>
    </vab-query-form>
    <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"></el-table-column>
      <el-table-column show-overflow-tooltip label="编码" prop="id" align="center" sortable fixed="left"></el-table-column>
      <el-table-column width="120" show-overflow-tooltip label="公司名称" prop="name" align="center" sortable fixed="left"></el-table-column>
      <el-table-column width="120" show-overflow-tooltip label="别名" prop="aname" align="center" sortable></el-table-column>
      <el-table-column width="120" show-overflow-tooltip label="顺序" prop="ordernum" align="center" sortable ></el-table-column>
      <!--  -->
      <el-table-column width="130" show-overflow-tooltip label="风电装机容量" prop="windcapacity" align="center" sortable></el-table-column>
      <el-table-column width="130" show-overflow-tooltip label="风电容量单位" prop="windcapacityunit" align="center" sortable></el-table-column>
      <el-table-column width="130" show-overflow-tooltip label="风电装机数量" prop="windquantity" align="center" sortable></el-table-column>
      <el-table-column width="150" show-overflow-tooltip label="风电场站数量" prop="windnumber" align="center" sortable></el-table-column>
      <el-table-column width="190" show-overflow-tooltip label="风电场站数量" prop="windnumber" align="center" sortable ></el-table-column>
      <!--  -->
      <el-table-column width="150" show-overflow-tooltip label="光伏场站数量" prop="number" align="center" sortable></el-table-column>
      <el-table-column width="130" show-overflow-tooltip label="光伏装机容量" prop="capacity" align="center" sortable></el-table-column>
      <el-table-column width="130" show-overflow-tooltip label="光伏容量单位" prop="capacityunit" align="center" sortable></el-table-column>
      <el-table-column width="170" show-overflow-tooltip label="光伏装机数量集中" prop="quantityjz" align="center" sortable></el-table-column>
      <el-table-column width="170" show-overflow-tooltip label="光伏装机数量组串" prop="quantityzc" align="center" sortable></el-table-column>
      <el-table-column width="190" show-overflow-tooltip label="光伏场站数量" prop="number" align="center" sortable ></el-table-column>
      <!--  -->
      <el-table-column width="150" show-overflow-tooltip label="接入风电场站数" prop="jrwindnumber" align="center" sortable></el-table-column>
      <el-table-column width="150" show-overflow-tooltip label="接入光伏场站数" prop="jrnumber" align="center" sortable></el-table-column>
      <el-table-column width="170" show-overflow-tooltip label="接入风电装机容量" prop="jrwindcapacity" align="center" sortable></el-table-column>
      <el-table-column width="170" show-overflow-tooltip label="接入风电容量单位" prop="jrwindcapacityunit" align="center" sortable></el-table-column>
      <el-table-column width="170" show-overflow-tooltip label="接入风电装机数量" prop="jrwindquantity" align="center" sortable></el-table-column>
      <el-table-column width="170" show-overflow-tooltip label="接入光伏装机容量" prop="jrcapacity" align="center" sortable></el-table-column>
      <el-table-column width="170" show-overflow-tooltip label="接入光伏容量单位" prop="jrcapacityunit" align="center" sortable></el-table-column>
      <el-table-column width="190" show-overflow-tooltip label="接入光伏装机数量集中" prop="jrquantityjz" align="center" sortable></el-table-column>
      <el-table-column width="190" show-overflow-tooltip label="接入光伏装机数量组串" prop="jrquantityzc" align="center" sortable></el-table-column>
      <el-table-column width="190" show-overflow-tooltip label="接入风电场站数量" prop="jrwindnumber" align="center" sortable ></el-table-column>
      <el-table-column width="190" show-overflow-tooltip label="接入光伏场站数量" prop="jrnumber" align="center" sortable ></el-table-column>
      <el-table-column show-overflow-tooltip label="操作" width="100px" 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>
    <table-edit ref="edit" :options="options" @save-success="fetchData"></table-edit>
  </div>
</template>
<script>
import UploadExcel from '@/components/UploadExcel/index'
import ExportExcel from '@/components/ExportExcel/index'
import TableEdit from './components/TableEdit'
import api from '@/api/station'
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,
      },
    };
  },
  created() {
    this.fetchData()
    // this.getTree()
  },
  computed: {
    height() {
      return this.$baseTableHeight() + 50
    },
  },
  methods: {
    handleQuery() {
      this.queryForm.pageNo = 1
      this.fetchData()
    },
    getTree() {
      api.newtreeTreels({tag: 0}).then(res => {
        if (res.data) {
          this.options = res.data
        }
      })
    },
    getMyExcelData(val) {
      val.forEach(item => {
        api.addCompanys(item).then(res => {
          if (res.code == 200) {
            this.$message({
              type: 'success',
              message: '添加成功!'
            });
            this.fetchData()
          }
        })
      })

    },
    fetchData() {
      this.listLoading = true
      api.companys({
        id: '',
        name: 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: "",
              windcapacity: "",
              windcapacityunit: "",
              windquantity: "",
              windnumber: "",
              jrwindnumber: "",
              number: "",
              capacity: "",
              capacityunit: "",
              quantityjz: "",
              quantityzc: "",
              jrnumber: "",
              jrwindcapacity: "",
              jrwindcapacityunit: "",
              jrwindquantity: "",
              jrcapacity: "",
              jrcapacityunit: "",
              jrquantityjz: "",
              jrquantityzc: "",
              ordernum: "",
              windnumber: "",
              number: "",
              jrwindnumber: "",
              jrnumber: "",
            }
          ]
          let exportExcel = []
          res.data.records.forEach(item => {
            exportExcel.push({
              id: item.id,
              name: item.name,
              aname: item.aname,
              windcapacity: item.windcapacity,
              windcapacityunit: item.windcapacityunit,
              windquantity: item.windquantity,
              windnumber: item.windnumber,
              jrwindnumber: item.jrwindnumber,
              number: item.number,
              capacity: item.capacity,
              capacityunit: item.capacityunit,
              quantityjz: item.quantityjz,
              quantityzc: item.quantityzc,
              jrnumber: item.jrnumber,
              jrwindcapacity: item.jrwindcapacity,
              jrwindcapacityunit: item.jrwindcapacityunit,
              jrwindquantity: item.jrwindquantity,
              jrcapacity: item.jrcapacity,
              jrcapacityunit: item.jrcapacityunit,
              jrquantityjz: item.jrquantityjz,
              jrquantityzc: item.jrquantityzc,
              ordernum: item.ordernum,
              windnumber: item.windnumber,
              number: item.number,
              jrwindnumber: item.jrwindnumber,
              jrnumber: item.jrnumber,
            })
          })
          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.deleteCompanys({
            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>

</style>