<template>
  <div style="padding: 0.5% 0.5%">
    <el-row>
      <el-col :span="24">
        <span style="font-weight: ; font-size: 14px">监测点等级:</span>
        <el-select
          v-model="lineGrade"
          placeholder="请选择监测点等级"
          style="width: 12%"
        >
          <el-option
            v-for="item in lineGradeList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
        <span style="font-weight: ; font-size: 14px">通讯状态:</span>
        <el-select
          v-model="comFlag"
          placeholder="请选择通讯状态"
          style="width: 12%"
        >
          <el-option
            v-for="item in statusList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
        <span style="font-weight: ; font-size: 14px">数据筛选:</span>
        <el-input
          v-model="searchValue"
          placeholder="请选择筛选数据"
          style="width: 10%"
          :clearable="true"
        ></el-input>
        <el-button type="primary" @click="query">查询</el-button>

        <el-button type="primary" style="margin-left: 25px" @click="exportCsv"
          >导出模板</el-button
        >

        <el-button type="primary" @click="exportData" style="float: right"
          >导出</el-button
        >
        <el-button type="primary" @click="importData" style="margin-light: 30px"
          >批量导入监测点评级</el-button
        >
      </el-col>
    </el-row>
    <el-row style="margin-top: 0.5%">
      <el-table
        
        stripe
        v-loading="loading"
        :data="tableData"
        border
        :height="height"
        style="width: 100%"
        :header-cell-style="{
          height: '25px',
          top: '0px',
          padding: '0px',
        }"
        :row-style="{ height: '25px' }"
        :cell-style="{ padding: '0px' }"
      >
        <el-table-column
          align="center"
          prop="index"
          type="index"
          label="序号"
          width="70"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="province"
          label="省份"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="gdName"
          label="供电公司"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="subName"
          label="所属变电站"
          :show-overflow-tooltip="true"
          width="350"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="devName"
          label="装置名称"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="lineName"
          label="监测点名称"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="comFlag"
          label="通讯状态"
          width="100"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.comFlag === 0" style="color: red">中断</span>
            <span v-if="scope.row.comFlag === 1" style="color: green"
              >正常</span
            >
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="runFlag"
          label="运行状态"
          width="100"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.runFlag === 0" style="color: green"
              >投运</span
            >
            <span v-if="scope.row.runFlag === 2" style="color: red">停运</span>
            <span v-if="scope.row.runFlag === 1" style="color: #cc9900"
              >热备用</span
            >
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="updateTime"
          label="数据更新时间"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="lineGrade"
          label="监测点评级"
          width="100"
        >
        </el-table-column>
        <el-table-column align="center" label="操作" fixed="right" width="180">
          <template slot-scope="scope">
            <el-button type="primary" @click="deilallData(scope.row)"
              >修改评级</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="float: right"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageData.currentPage"
        :page-sizes="[20, 50, 100, 200]"
        :page-size="pageData.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageData.total"
      >
      </el-pagination>

      <el-dialog
        :close-on-click-modal="false"
        title="修改评级"
        :visible.sync="dialogVisible"
        width="18%"
        :before-close="handleClose"
      >
        <span style="font-size: 14px">监测点评级:</span>
        <el-select v-model="lineGradeChoose" placeholder="请选择监测点评级">
          <el-option
            v-for="item in lineGradeListNoOne"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="submit()">提 交</el-button>
        </span>
      </el-dialog>

      <el-dialog
        :close-on-click-modal="false"
        title="导入文件"
        :visible.sync="dialogVisibleUpload"
        width="30%"
        :before-close="handleClose"
      >
        <el-upload
          class="upload-demo"
          ref="upload"
          action="#"
          :http-request="upload"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :before-remove="beforeRemove"
          :limit="1"
          accept=".xlsx,.xls"
          :on-exceed="handleExceed"
          :file-list="fileList"
        >
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">
            只能上传csv文件，且不超过500kb
          </div>
        </el-upload>
      </el-dialog>
    </el-row>
  </div>
</template>
<script>
import { tableheight } from "@/assets/commjs/common";
import lineMark from "@/api/device/lineMark.js";
import { dicData } from "../../../assets/commjs/dictypeData";
import node from "@/api/device/node";

export default {
  components: {
    // timepicker
  },
  data() {
    return {
      loading: false,
      searchValue: "", //模糊检索
      height: "",
      fileList: [],
      dialogVisible: false,
      dialogVisibleUpload: false,
      lineGradeList: [],
      lineGradeListNoOne: [],
      statusList: [
        { id: "", name: "全部" },
        { id: 1, name: "正常" },
        { id: 0, name: "中断" },
      ],
      tableData: [],
      lineGrade: "",
      comFlag: "",
      lineGradeChoose: "",
      lineId: "",

      //分页
      pageData: {
        currentPage: 1,
        pageSize: 20,
        total: 0,
      },
    };
  },
  created() {
    this.getLineGradeList();
    this.getList();
  },
  mounted() {
    this.height = tableheight() + "px";
  },
  methods: {
    handleSizeChange(val) {
      this.pageData.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.pageData.currentPage = val;
      this.getList();
    },
    //修改监测点评级
    deilallData(row) {
      this.dialogVisible = true;
      this.lineId = row.lineIndex;
      this.lineGradeChoose = row.lineGradeId;
    },
    //关闭
    handleClose() {
      this.dialogVisible = false;
      this.dialogVisibleUpload = false;
    },
    //导入
    importData() {
      this.dialogVisibleUpload = true;
    },
    //导出监测点台账
    exportData() {
      npm;
      lineMark
        .exportLineMark({})
        .then(function (response) {
          const blob = new Blob([response]);
          const fileName = "监测点评分台账.xls";
          const linkNode = document.createElement("a");

          linkNode.download = fileName; //a标签的download属性规定下载文件的名称
          linkNode.style.display = "none";
          linkNode.href = URL.createObjectURL(blob); //生成一个Blob URL
          document.body.appendChild(linkNode);
          linkNode.click(); //模拟在按钮上的一次鼠标单击

          URL.revokeObjectURL(linkNode.href); // 释放URL 对象
          document.body.removeChild(linkNode);
        })
        .catch(function (error) {
         // console.log(error);
        });
    },

    handleRemove(file, fileList) {
      //console.log(file, fileList);
    },
    handlePreview(file) {
    //  console.log(file);
    },
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${
          files.length + fileList.length
        } 个文件`
      );
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },

    //主列表
    getList() {
      let data = {
        comFlag: this.comFlag,
        lineGrade: this.lineGrade,
        pageNum: this.pageData.currentPage,
        pageSize: this.pageData.pageSize,
        searchValue: this.searchValue,
      };
      this.loading = true;
      lineMark.getList(data).then((response) => {
        if (response.code === "A0000") {
          var data = response.data;
          this.tableData = data.records;
          this.loading = false;
          this.pageData.currentPage = data.current;
          this.pageData.pageSize = data.size;
          this.pageData.total = data.total;
        }
      });
    },
    /**
     * 获取终端等级
     */
    getLineGradeList() {
      var code = "Dev_Level";
      this.lineGradeList = dicData(code, [], 1);
      this.lineGradeListNoOne = dicData(code, []);
    },

    query() {
      this.getList();
    },

    //单个修改监测点评分
    singleOperate(id, lineGrade) {
      let data = {
        id: id,
        lineGrade: lineGrade,
      };
      lineMark.singleOperate(data).then((response) => {
        if (response.code === "A0000") {
          this.$message.success("监测点等级成功");
          this.dialogVisible = false;
          this.getList();
        }
      });
    },

    //提交按钮
    submit() {
      this.$confirm("是否确认操作?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.singleOperate(this.lineId, this.lineGradeChoose);
      });
    },

    //导出模板
    exportCsv() {
      lineMark
        .heardExport()
        .then(function (response) {
          const blob = new Blob([response]);
          const fileName = "监测点评分模板.xls";
          const linkNode = document.createElement("a");

          linkNode.download = fileName; //a标签的download属性规定下载文件的名称
          linkNode.style.display = "none";
          linkNode.href = URL.createObjectURL(blob); //生成一个Blob URL
          document.body.appendChild(linkNode);
          linkNode.click(); //模拟在按钮上的一次鼠标单击

          URL.revokeObjectURL(linkNode.href); // 释放URL 对象
          document.body.removeChild(linkNode);
        })
        .catch(function (error) {
         // console.log(error);
        });
    },

    //文件上传

    upload(option) {
      let formData = new FormData();
      formData.set("file", option.file);
      lineMark.upload(formData).then((response) => {
       // console.log(response);
        if (response.type === "application/json") {
          const fileReader = new FileReader();
          fileReader.onloadend = () => {
            try {
              const jsonData = JSON.parse(fileReader.result);
              if (jsonData.code === "A0000") {
                this.$message.success("批量修改监测点评分成功");
                this.dialogVisibleUpload = false;
                this.getList();
              } else {
                this.$message.error(jsonData.data);
              }
            } catch (err) {
             // console.log(err);
            }
          };
          fileReader.readAsText(response);
        } else {
          const link = document.createElement("a"); // 创建a标签
          let blob = new Blob([response], {
            type: "application/vnd.ms-excel;charset=UTF-8",
          }); // 设置文件类型
          link.style.display = "none";
          link.href = URL.createObjectURL(blob); // 创建URL
          link.setAttribute("download", "监测点评分错误信息.xls");
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
        }

        this.$refs.upload.clearFiles();
      });
    },
  },
};
</script>
