<template>
  <el-row class="el-row-container" style="height: 100%" v-loading="loading">
    <el-col
      :span="4"
      style="height:100%;padding-bottom:32px;"
    >
    <el-card style="margin:16px;height: 100%; overflow-y: auto">
      <data-report-left
        @setCurrentData="setCurrentData"
        :treeData="treeData"
      ></data-report-left>
    </el-card>
    </el-col>
    <el-col
      :span="20"
      style="padding: 16px 16px 16px 0; height: 100%"
    >
      <el-card style="height: 100%; overflow-y: auto">
        <el-col :span="24" class="elrow-main__col-top">
          <div>
            <el-input
              v-model="form.goodsName"
              style="width: 250px"
              placeholder="请输入机井编号/机井名称"
              @keyup.enter.native="handleSearch"
              clearable
            />
            <el-select placeholder="在线状态" style="margin: 0 10px">

            </el-select>
            <el-button
              type="primary"
              size="small"
              @click="handleSearch"
              >查询</el-button
            >
          </div>
        </el-col>
        <el-col :span="24" class="elrow-main__col-bottom">
          <b-table
            ref="tableRef"
            :args="{ 'highlight-current-row': true }"
            :data="loadData"
            :columns="columns"
            isShowIndex
            :showPagination="loadData.length"
          >
            <template #status="scope">
              <span style="display:flex;align-items:center;justify-content:center">
                <span class="status"></span>
                {{ scope.row.name }}
              </span>
            </template>
            <template #operate="scope">
              <el-link
                size="small"
                :underline="false"
                type="primary"
                style="margin-right: 10px"
                @click="irrigationShowHistory(scope.row)"
                >灌溉记录</el-link
              >
              <el-link
                size="small"
                :underline="false"
                type="primary"
                style="margin-right: 10px"
                @click="download(scope.row)"
              >修改</el-link>
              <el-link
                size="small"
                :underline="false"
                type="primary"
                style="margin-right: 10px"
                @click="showDetails(scope.row)"
              >详情</el-link>
              <el-link
                slot="reference"
                size="small"
                :underline="false"
                type="danger"
                style="margin-right: 10px"
                @click="deleteRow(scope.row)"
              >删除</el-link>
            </template>
          </b-table>
        </el-col>
      </el-card>
    </el-col>
    <set-preferencs
      :visible.sync="associationManageShow"
    />
  </el-row>
</template>

<script>
import { getTree } from '@/api/tree.js'
import BTable from '@/components/Table/index.vue';
import DataReportLeft from '@/components/DataReportLeft/index.vue'
import setPreferencs from './components/setPreferenc.vue'

export default {
  name: 'deviceSetup',
  components: { BTable,DataReportLeft,setPreferencs },
  data() {
    return {
      treeData: [],
      form: {
        productName: ''
      },
      loading: false,
      associationManageShow: false,
      currentClickId: '',
      columns: [
        {
          label: '水电双计编号',
          prop: 'person',
          customRender: '',
          align: 'center'
        },
        {
          label: '设备名称',
          prop: 'area',
          customRender: '',
          align: 'center'
        },
        {
          label: '水源名称',
          prop: 'name',
          customRender: '',
          align: 'center'
        },
        {
          label: '设备状态',
          prop: 'phone',
          customRender: 'status',
          align: 'center'
        },{
          label: '累计用水量',
          prop: 'phone',
          customRender: '',
          align: 'center'
        },{
          label: '累计用电量',
          prop: 'phone',
          customRender: '',
          align: 'center'
        },{
          label: '更新时间',
          prop: 'phone',
          customRender: '',
          align: 'center'
        },
        {
          label: '操作',
          customRender: 'operate',
          width: '200',
          align: 'center'
        }
      ]
    };
  },
  methods: {
    async searchHandler(farmerName, areaId) {
      const res = await getFarmerList({
        farmerName,
        areaId
      })
      this.userList = res.data
    },
    async loadData() {
      if (!this.currentClickId) {
        const treeList = this.$store.state.tree.treeList
        let res = []
        if (treeList.code === '000000') {
          res = treeList
        } else {
          res = await getTree()
          this.$store.dispatch('tree/setTree', res)
        }
        this.treeData = res?.data
        this.currentClick = res?.data[0]
        this.currentClickId = this.currentClick?.cusareaId
      }
      this.searchHandler(this.farmerName, this.currentClickId)
      return this.getTableData(this.currentClickId);
    },
    setCurrentData(areaId) {
      this.currentClickId = areaId
      this.$refs.tableRef.refresh(false);
      this.searchHandler(this.farmerName, areaId)
    },
    getTableData() {
      return Promise.resolve({
        data: [{
            person:"王大虎",
            area: '2016-05-02',
            name: '王小虎',
            phone: 1588888888,
            address: '上海市普陀区金沙江路 1518 弄'
          }, {
            person:"王大虎",
            area: '2016-05-04',
            name: '王小虎',
            phone: 1588888888,
            address: '上海市普陀区金沙江路 1517 弄'
          }, {
            person:"王大虎",
            area: '2016-05-01',
            name: '王小虎',
            phone: 1588888888,
            address: '上海市普陀区金沙江路 1519 弄'
          }, {
            person:"王大虎",
            area: '2016-05-03',
            name: '王小虎',
            phone: 1588888888,
            address: '上海市普陀区金沙江路 1516 弄'
          }]
      })
    },
    handleSearch() {
      this.$refs.tableRef.refresh(true);
    },
    goDetail(row) {
      this.$refs.sourceCodeDetail.open(row.tarcecodeapplyBatchno)
    },
    download(row) {
      alert(2)
    }
  }
};
</script>

<style lang="scss" scoped>
.el-row-container{
  height: 100%;
}
.elrow-main__col-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 15px;
  .top-left {
    height: 100%;
    display: flex;
    align-items: center;
    i {
      background-color: #40d5ec;
      height: 45%;
      width: 5px;
      margin-right: 5px;
    }
  }
}
.status{
  display: inline-block;
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background: red;
  margin-left: 5px;
}
</style>
