<template>
  <div>
    <div class="leakage-top">
      <div style="color: #666666"></div>
      <div class="operate-btn">
        <delids :multipleSelection2="multipleSelection" @del="del()"
          >删除</delids
        >
        <el-button type="primary" @click="refresh">刷新</el-button>
        <el-button type="primary" @click="block = !block">查询</el-button>
        <el-button type="primary" @click="exportData">导出</el-button>
        <!-- <el-button type="primary" @click="handleAutomatic"
          >自动添加设备</el-button
        > -->
      </div>
    </div>
    <el-form
      class="search-div"
      v-if="block"
      :model="monitorForm"
      :inline="true"
      size="mini"
    >
      <el-form-item label="设备名称：">
        <el-input
          placeholder="请输入设备名称"
          v-model="monitorForm.equipName"
          clearable
        >
        </el-input>
      </el-form-item>
      <el-form-item label="设备身份编号：">
        <el-input
          placeholder="请输入设备身份编号"
          v-model="monitorForm.equipCode"
          clearable
        >
        </el-input>
      </el-form-item>
      <el-form-item label="IP地址：">
        <el-input placeholder="请输入IP地址" v-model="monitorForm.ip" clearable>
        </el-input>
      </el-form-item>
      <el-form-item label="所在铁路线：">
        <el-select
          placeholder="请选择所在铁路线"
          v-model="monitorForm.wayId"
          clearable
          @change="changerailWay()"
        >
          <el-option
            v-for="item in railWaySelect"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所在站点：">
        <el-select
          placeholder="请选择所在站点"
          v-model="monitorForm.siteId"
          @change="changesite()"
          clearable
        >
          <el-option
            v-for="item in stationSelect2"
            :key="item.id"
            :label="item.siteName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所在FSU：">
        <el-select
          placeholder="请选择所在FSU"
          v-model="monitorForm.fsuId"
          clearable
        >
          <el-option
            v-for="item in fsuSelect2"
            :key="item.id"
            :label="item.equipName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="监测设备厂家：">
        <el-select
          placeholder="请选择监测设备厂家"
          v-model="monitorForm.fsuId"
          clearable
        >
          <el-option
            v-for="item in fsuSelect2"
            :key="item.id"
            :label="item.equipName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="searchQuery()">查询</el-button>
        <el-button type="primary" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table
      ref="multipleTable"
      class="statistics-table"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      :row-class-name="tableRowClassName"
      :row-style="{ height: '50px' }"
      :header-cell-style="{
        background: '#eaf1fe',
        color: '#000',
        fontWeight: 700,
        height: '50px',
      }"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        prop="equipName"
        label="设备名"
        show-overflow-tooltip
        align="center"
      />
      <el-table-column prop="equipCode" label="设备身份编号" align="center" />
      <el-table-column prop="ip" label="IP" align="center" />
      <el-table-column prop="railWayName" label="所在铁路线" align="center" />
      <el-table-column prop="siteName" label="所在站点" align="center" />
      <el-table-column prop="fsuName" label="所在FSU" align="center" />
      <el-table-column prop="action" label="操作" align="center" width="300px">
        <template slot-scope="{ row }">
          <el-link type="primary" :underline="false" @click="handleView(row)"
            >查看</el-link
          >
          <el-link type="primary" :underline="false" @click="toEdit(row)"
            >编辑</el-link
          >
          <el-link
            type="primary"
            :underline="false"
            @click="handleGoParameter(row)"
            >设备数据</el-link
          >
          <el-link type="primary" :underline="false" @click="handleSetting(row)"
            >参数设置</el-link
          >
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :limit="monitorForm.size"
      :page="monitorForm.current"
      :total="total"
      class="pagination"
      @pagination="handlePageChange"
    />

    <el-dialog
      title="编辑-监测设备"
      :visible.sync="visible"
      width="40%"
      :close-on-click-modal="false"
    >
      <monitor :curInfo="curInfo" :isEdit="1" @update="update" />
    </el-dialog>
    <!-- <el-dialog
      title="自动添加设备"
      :visible.sync="open"
      width="40%"
      :close-on-click-modal="false"
    >
      <autoAdd />
    </el-dialog> -->
    <el-dialog
      title="监测设备参数设置"
      :visible.sync="openSetting"
      width="40%"
      :close-on-click-modal="false"
    >
      <autoAdd
        :rowData="rowData"
        :paramsJson="paramsJson"
        @update="handleUpdate"
      />
    </el-dialog>
  </div>
</template>
<script>
import monitor from "../../add/comp/monitorEquip.vue";
import autoAdd from "../../add/comp/autoAdd.vue";
import { selectParam, setParam } from "@/api/udp";
import {
  monitorEquiplist,
  monitorEquipbatchDelete,
  railWaylist,
  selectForSite,
  selectForFsu,
} from "../../api";
import { successAlert, warningAlert } from "@/utils/alert";
import download from "@/utils/download";
import { exportMonitorEquip } from "@/api/export";
export default {
  components: { monitor, autoAdd },
  data() {
    return {
      railWaySelect: [],
      stationSelect2: [],
      fsuSelect2: [],
      monitorForm: formInit(),
      tableData: [],
      params: {
        current: 1,
        size: 10,
      },
      total: 10,
      multipleSelection: [],
      ids: [],
      block: 0,
      istrue: 0,
      exids: [],
      visible: false,
      curInfo: {},
      open: false,
      openSetting: false,
      rowData: {},
      paramsJson: {},
    };
  },
  created() {
    var that = this;
    document.onkeydown = function (e) {
      var key = window.event.keyCode;
      if (key == 13) {
        that.searchQuery();
      }
    };
  },
  mounted() {
    this.getTableData();
    this.getAllWay();
  },
  methods: {
    handleGoParameter(row) {
      console.log(row);
      this.$router.push({
        path: "/setting/parameter",
        query: {
          id: row.id,
        },
      });
    },
    async handleUpdate(e) {
      if (e == false) {
        this.openSetting = false;
        return;
      }
      let data = { ...e };
      let res = await setParam(data);
      if (res.code == 200) {
        successAlert("保存成功");
        this.openSetting = false;
      }
    },
    async handleSelectParam(row) {
      let res = await selectParam({ equipId: row.id });
      this.paramsJson = JSON.parse(res.paramsJson);
      console.log("--------paramsJson------------");
      console.log(this.paramsJson);
    },
    async handleSetting(row) {
      this.rowData = row;
      this.handleSelectParam(row);
      this.openSetting = true;
    },
    handleAutomatic() {
      this.open = true;
    },
    toEdit(row) {
      this.visible = true;
      this.curInfo = row;
    },
    update(data) {
      this.visible = false;
      if (data) {
        this.getTableData();
      }
    },
    changerailWay() {
      selectForSite({ wayId: this.monitorForm.wayId }).then((res) => {
        this.stationSelect2 = res;
      });
    },
    changesite() {
      selectForFsu({ siteId: this.monitorForm.siteId }).then((res) => {
        this.fsuSelect2 = res;
      });
    },
    tableRowClassName({ row, rowIndex }) {
      return rowIndex % 2 === 0 ? "" : "single-row";
    },
    changeType(item) {
      this.activeName = item.key;
    },
    del() {
      let ids = this.ids;
      monitorEquipbatchDelete({ ids }).then((res) => {
        if (res.code == 200) {
          successAlert("删除成功");
          this.getTableData();
        } else {
          warningAlert("删除失败");
        }
      });
    },
    refresh() {
      this.reset();
    },
    searchQuery() {
      this.istrue = 1;
      this.getTableData();
    },
    reset() {
      this.monitorForm = formInit();
      this.searchQuery();
    },
    exportData() {
      if (this.exids.length == 0) {
        this.$message.warning("暂无数据");
        return false;
      } else {
        exportMonitorEquip({ ids: this.exids }).then((res) => {
          download(res, "vnd.ms-excel", `监测设备表.xls`);
        });
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      this.ids = this.multipleSelection.map((i) => i.id);
    },
    handleView(row) {
      this.$router.push({
        path: "/setting/statistics/detail",
        query: {
          id: row.id,
          type: 4,
        },
      });
    },
    handlePageChange(pageData) {
      this.monitorForm.size = pageData.size;
      this.monitorForm.current = pageData.page;
      this.getTableData();
    },
    getTableData() {
      monitorEquiplist(this.monitorForm).then((res) => {
        let list = res.records || [];
        this.tableData = list;
        this.total = res.total;
        this.exids = list.map((i) => i.id);
        if (this.istrue == 1) {
          if (this.tableData.length) {
            successAlert("操作成功");
          } else {
            warningAlert("查询结果为空");
          }
          this.istrue = 0;
        }
      });
    },
    getAllWay() {
      railWaylist(this.params).then((res) => {
        this.railWaySelect = res.records;
        if (res.total > this.params.size) {
          this.params.size = res.total;
          this.getAllWay();
        }
      });
    },
  },
};
function formInit(data = {}) {
  return {
    wayId: "",
    parentId: "",
    ip: "",
    siteId: "",
    fsuId: "",
    equipCode: "",
    equipName: "",
    current: 1,
    size: 10,
    ...data,
  };
}
</script>
<style lang="scss" scoped>
.el-link {
  margin-right: 20px;
}
.leakage-cable {
  .leakage-top {
    margin-bottom: 20px;
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
  }
  & ::v-deep .cell {
    color: #333333;
  }
  & ::v-deep .stripe {
    background-color: #eaf1fe;
  }
  & ::v-deep .red {
    background-color: #f00;
  }
  & ::v-deep .green {
    background-color: green;
  }
  .page {
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 20px 0;
    .pageNum {
      margin: 0 20px;
    }
  }
}
</style>
<style lang="scss">
.statistics-table {
  .single-row {
    background: #f1f6ff;
  }
  td {
    padding: 5px !important;
  }
}
</style>