<template>
  <div class="ui-container">
    <el-form :model="queryParam" ref="queryForm" :inline="true">
      <el-form-item label="过滤筛选:" prop="searchValue">
        <el-input
          v-model="queryParam.searchValue"
          placeholder="请输入网关名称或编码ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="接入网络方式" prop="netType">
              <el-select v-model="queryParam.netType" placeholder="请选择接入网络方式" clearable size="small">
                <el-option
                  v-for="dict in netTypeList"
                  :key="dict.id"
                  :label="dict.name"
                  :value="dict.id"
                />
              </el-select>
            </el-form-item>-->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>

      <el-form-item style="float: right; margin-right: 20px">
        <el-button
          @click="handleAdd()"
          type="primary"
          size="mini"
          icon="el-icon-plus"
          >新增网关</el-button
        >
      </el-form-item>
    </el-form>

    <el-row>
      <el-table
        stripe
        id="deviceTable"
        ref="multipleTable"
        :data="tableData"
        @header-click="sort"
        v-loading="loading"
        border
        :header-cell-style="{
          //background:'#5E95E8',
          //color:'#FFFFFF',
          height: '25px',
          top: '0px',
          padding: '0px',
        }"
        :row-style="{ height: '25px' }"
        :cell-style="{ padding: '0px' }"
        style="top: 5px"
      >
        <el-table-column
          align="center"
          sortable
          prop="name"
          label="名称"
          column-key="date"
        ></el-table-column>
        <el-table-column
          align="center"
          sortable
          prop="ndid"
          label="网关ID"
        ></el-table-column>
        <el-table-column align="center" sortable prop="address" label="工程信息"
          >/</el-table-column
        >

        <el-table-column
          align="center"
          sortable
          prop="netType"
          label="接入网络方式"
        >
          <template slot-scope="scope">
            <span type="primary" size="small">{{
              scope.row.netType == null ? "/" : scope.row.netType
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          sortable
          prop="version"
          label="更新时间"
        >
          <template slot-scope="scope">
            <span type="primary" size="small">{{
              scope.row.updateTime == null ? "/" : scope.row.updateTime
            }}</span>
          </template>
        </el-table-column>

        <el-table-column align="center" fixed="right" label="操作" width="380">
          <template slot-scope="scope">
            <el-button
              @click="handleClick(scope.row)"
              type="primary"
              icon="el-icon-search"
              size="mini"
              >查看</el-button
            >
            <el-button
              @click="EditClick(scope.row)"
              type="primary"
              size="mini"
              icon="el-icon-edit"
              >编辑</el-button
            >
            <el-button
              @click="DeleteClick(scope.row)"
              type="danger"
              size="mini"
              icon="el-icon-delete"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-row>

    <el-row style="margin-top: 10px; text-align: right">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageData.pageNum"
        :page-sizes="[10, 20, 25, 30, 50]"
        :page-size="pageData.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageData.total"
      >
      </el-pagination>
    </el-row>

    <!--新增/修改-->
    <el-dialog
      :close-on-click-modal="false"
      :title="title"
      :visible.sync="open"
      width="20%"
      
    >
      <el-form
        :model="form"
        ref="form"
        :rules="rules"
        :inline="true"
        label-width="80px"
      >
        <el-form-item label="名称:" prop="name">
          <el-input style="width: 250px" v-model="form.name"></el-input>
        </el-form-item>

        <el-form-item label="网关ID:" prop="ndid">
          <el-input style="width: 250px" v-model="form.ndid"></el-input>
        </el-form-item>

        <el-form-item label="排序:" prop="sort">
          <el-input style="width: 250px" v-model="form.sort"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button :v-if="dialogFormVisible" @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog
      :close-on-click-modal="false"
      :title="title"
      :visible.sync="openInfo"
      width="80%"
      
    >
      <el-form
        :model="formInfo"
        ref="formInfo"
        :inline="true"
        label-width="180px"
      >
        <el-form-item label="联网设备名称:" prop="name">
          <el-input
            readOnly
            style="width: 250px"
            v-model="formInfo.name"
          ></el-input>
        </el-form-item>

        <el-form-item label="联网设备ID:" prop="ndid">
          <el-input
            readOnly
            style="width: 250px"
            v-model="formInfo.ndid"
          ></el-input>
        </el-form-item>

        <el-form-item label="版本信息" prop="versionId">
          <el-input
            readOnly
            style="width: 250px"
            v-model="formInfo.versionId"
          ></el-input>
        </el-form-item>

        <el-form-item label="接入网络方式" prop="netType">
          <el-input
            readOnly
            style="width: 250px"
            v-model="formInfo.netType"
          ></el-input>
        </el-form-item>

        <el-form-item label="设备厂商信息" prop="msgInfo">
          <el-input
            readOnly
            style="width: 250px"
            v-model="formInfo.msgInfo"
          ></el-input>
        </el-form-item>

        <el-form-item label="CPU核心数" prop="cpuCore">
          <el-input
            readOnly
            style="width: 250px"
            v-model="formInfo.cpuCore"
          ></el-input>
        </el-form-item>
        <el-form-item label="CPU主频（单位GHz）" prop="cpuFreq">
          <el-input
            readOnly
            style="width: 250px"
            v-model="formInfo.cpuFreq"
          ></el-input>
        </el-form-item>

        <el-form-item label="CPU架构" prop="arch">
          <el-input
            readOnly
            style="width: 250px"
            v-model="formInfo.arch"
          ></el-input>
        </el-form-item>

        <el-form-item label="CPU监控阈值（单位%）" prop="cpuLmt">
          <el-input
            readOnly
            style="width: 250px"
            v-model="formInfo.cpuLmt"
          ></el-input>
        </el-form-item>

        <el-form-item label="物理内存（单位MB）" prop="memPhy">
          <el-input
            readOnly
            style="width: 250px"
            v-model="formInfo.memPhy"
          ></el-input>
        </el-form-item>

        <!--        <el-form-item label="虚拟内存（单位MB）" prop="memVirt">
          <el-input readOnly style="width: 250px"  v-model="formInfo.memVirt "></el-input>
        </el-form-item>


        <el-form-item label="内存监控阈值（单位%）" prop="memLmt">
          <el-input readOnly style="width: 250px"  v-model="formInfo.memLmt"></el-input>
        </el-form-item>-->

        <el-form-item label="磁盘空间（单位MB）" prop="diskPhy">
          <el-input
            readOnly
            style="width: 250px"
            v-model="formInfo.diskPhy"
          ></el-input>
        </el-form-item>

        <!--        <el-form-item label="存储监控阈值（单位%）" prop="diskLmt">
          <el-input readOnly style="width: 250px"  v-model="formInfo.diskLmt"></el-input>
        </el-form-item>


        <el-form-item label="已使用磁盘空间（单位MB）" prop="diskUsePhy">
          <el-input readOnly style="width: 250px"  v-model="formInfo.diskUsePhy"></el-input>
        </el-form-item>-->

        <el-form-item label="操作系统名称" prop="osName">
          <el-input
            readOnly
            style="width: 250px"
            v-model="formInfo.osName"
          ></el-input>
        </el-form-item>

        <el-form-item label="操作系统版本" prop="osVersion">
          <el-input
            readOnly
            style="width: 250px"
            v-model="formInfo.osVersion"
          ></el-input>
        </el-form-item>

        <el-form-item label="应用程序版本号" prop="appVersion">
          <el-input
            readOnly
            style="width: 250px"
            v-model="formInfo.appVersion"
          ></el-input>
        </el-form-item>

        <el-form-item label="应用程序发布日期" prop="appDate">
          <el-input
            readOnly
            style="width: 250px"
            v-model="formInfo.appDate"
          ></el-input>
        </el-form-item>

        <el-form-item label="应用程序校验码" prop="appCheck">
          <el-input
            readOnly
            style="width: 250px"
            v-model="formInfo.appCheck"
          ></el-input>
        </el-form-item>

        <el-form-item label="是否支持远程升级程序" prop="softUpdate">
          <el-input
            readOnly
            style="width: 250px"
            v-model="formInfo.softUpdate"
          ></el-input>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  listNetDev,
  addNetDev,
  updateNetDev,
  delNetDev,
  getNetDevInfo,
} from "@/api/energy/netDev.js";

export default {
  name: "netDev",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,

      /*netTypeList:[{id:1,name:'4G'},{id:2,name:'无线'}],*/
      // 表格数据
      tableData: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openInfo: false, //只用于查看的dialog

      formInfo: {},

      dialogFormVisible: true,

      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParam: {
        searchValue: undefined,
      },
      pageData: {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      },
      // 表单参数
      form: {
        id: "",
        name: "",
        ndid: "",
        sort: 0,
      },
      formType: 0,
      // 表单校验
      rules: {
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        ndId: [{ required: true, message: "网关id不能为空", trigger: "blur" }],
        sort: [
          { required: true, message: "排序字段不可为空", trigger: "blur" },
        ],
      },
    };
  },

  created() {
    this.getList();
  },
  methods: {
    /** 查询网关列表 */
    getList() {
      this.loading = true;
      listNetDev(this.queryParam).then((response) => {
        this.tableData = response.data.records;
        this.pageData.total = response.data.total;
        this.pageData.pageSize = response.data.size;
        this.pageData.pageNum = response.data.current;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: "",
        name: "",
        ndid: "",
      };
      this.resetForm("form");
    },
    resetForm(form) {
      if (this.$refs[form]) {
        this.$refs[form].resetFields();
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.formType = 0; //新增
      this.title = "新增网关设备";
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.formType = 1;
      this.reset();
      updateNetDev(row.id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改网关设备";
      });
      this.getList();
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          console.log(this.form.id);
          if (this.formType == 1) {
            updateNetDev(this.form).then((response) => {
              this.$message({ message: "修改成功", type: "success" });
              this.open = false;
              this.getList();
            });
          } else {
            addNetDev(this.form).then((response) => {
              this.$message({ message: "新增成功", type: "success" });
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    //点击获取字典类型表头字段等属性
    sort(column, event) {
      this.orderBy = column.order;
      this.sortBy = column.property;
      console.log(this.orderBy + "=============" + this.sortBy);
      this.queryData();
    },
    //查看操作
    handleClick(row) {
      this.title = "联网设备详情";
      this.openInfo = true;
      this.getNetDevInfo(row.id);
    },

    //获取网关详细信息
    getNetDevInfo(id) {
      getNetDevInfo(id).then((response) => {
        if ((response.code = "A0000")) {
          this.formInfo = response.data;
        }
      });
    },
    //详情修改
    EditClick(row) {
      this.formType = 1;
      this.open = true;
      this.title = row.name + "详情修改";
      this.dialogFormVisible = true;

      this.form.id = row.id;
      this.form.name = row.name;
      this.form.ndid = row.ndid;
    },
    //详情删除
    DeleteClick(row) {
      this.$confirm("确认删除" + row.name + "吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let ids = [];
          ids.push(row.id);
          delNetDev(ids).then((response) => {
            if ((response.code = "A0000")) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    //分页
    handleSizeChange(val) {
      this.pageData.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.pageData.pageNum = val;
      this.getList();
    },
  },
};
</script>

<style scoped>
.ui-container {
  padding: 30px;
  height: 100%;
  width: 100%;
}
</style>
