<template>
  <div class="pd10">
    <!-- 前置 -->
    <el-row>
      <el-col :span="12">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="前置等级:">
            <el-select
              v-model="formInline.fontdve"
              placeholder="请选择终端型号"
            >
              <el-option
                v-for="item in fontdveoption"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="状态:">
            <el-select v-model="formInline.status" placeholder="请选择终端状态">
              <el-option
                v-for="item in statusoption"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="query">查询</el-button>
          </el-form-item>
        </el-form></el-col
      >
      <el-col :span="12">
        <el-button
          style="float: right; margin-right: 20px"
          type="primary"
          icon="el-icon-check"
          >导出</el-button
        >
        <el-button
          style="float: right; margin-right: 20px"
          type="primary"
          icon="el-icon-upload2"
          @click="add"
          >新增
        </el-button>
      </el-col>
    </el-row>
    <el-row style="margin-top: 0.2%">
      <el-table
        stripe
        :data="tableData"
        border
        highlight-current-row
        :height="height"
        style="width: 100%"
      >
        <el-table-column
          align="center"
          prop="name"
          label="名称"
        ></el-table-column>
        <el-table-column align="center" prop="ip" label="IP"></el-table-column>
        <el-table-column align="center" prop="nodeGrade" label="等级">
          <template slot-scope="scope">
            <el-tag type="primary" v-if="scope.row.nodeGrade === 1"
              >普通</el-tag
            >
            <el-tag type="primary" v-if="scope.row.nodeGrade === 0"
              >极重要</el-tag
            >
            <el-tag type="primary" v-if="scope.row.nodeGrade === 2"
              >备用</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="nodeDevNum"
          label="最大监测点数量"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="sort"
          label="排序"
        ></el-table-column>
        <el-table-column align="center" prop="state" label="状态">
          <template slot-scope="scope">
            <el-tag type="primary" v-if="scope.row.state === 1">启用</el-tag>
            <el-tag type="primary" v-if="scope.row.state === 0">未启用</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="remark"
          label="描述"
        ></el-table-column>
        <el-table-column align="center" fixed="right" label="操作" width="200">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="edit(scope.row)"
              >修改</el-button
            >
            <el-button type="primary" size="mini" @click="Delete(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="[10, 20, 30, 40]"
        :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="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="formData" label-width="120px">
        <el-form-item label="名称">
          <el-input v-model="formData.name" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="IP">
          <el-input v-model="formData.ip" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="等级">
          <el-select
            v-model="formData.nodeGrade"
            placeholder="请选择等级"
            style="width: 80%"
          >
            <el-option
              v-for="item in fontdveoptionForm"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="最大装置数">
          <el-input v-model="formData.nodeDevNum" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="排序">
          <el-input v-model="formData.sort" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="formData.remark" style="width: 80%"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">提 交</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { tableheight } from "@/assets/commjs/common";
import node from "@/api/device/node.js";

export default {
  name: "premage",
  data() {
    return {
      dialogVisible: false,
      height: "",
      fontdve: "",
      fontdveoption: [
        { id: "", name: "全部" },
        { id: 0, name: "极重要" },
        { id: 1, name: "普通" },
        { id: 2, name: "备用" },
      ],
      fontdveoptionForm: [
        { id: 0, name: "极重要" },
        { id: 1, name: "普通" },
        { id: 2, name: "备用" },
      ],
      status: "",
      statusoption: [
        { id: "", name: "全部" },
        { id: 0, name: "未启用" },
        { id: 1, name: "启用" },
      ],
      //分页
      pageData: {
        currentPage: 1,
        pagesize: 20,
        total: 0,
      },
      operaType: 0, //0新增 1 修改 2 查看
      formData: {},
      //列表
      tableData: [],
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    this.height = tableheight() + "px";
  },
  methods: {
    //分页
    handleSizeChange(val) {
     // console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      //console.log(`当前页: ${val}`);
    },
    //查询按钮
    query() {
      this.getList();
    },
    //新增
    add() {
      this.dialogVisible = true;
      this.operaType = 0;
      this.title = "新增前置机";
    },
    //关闭
    handleClose() {
      this.dialogVisible = false;
    },
    //修改
    edit(row) {
      this.dialogVisible = true;
      this.operaType = 1;
      this.title = row.name + "修改";
      this.getOne(row.id);
    },
    //删除
    Delete(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          node.delete(row.id).then((response) => {
            if (response.code === "A0000") {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //获取主界面列表
    getList() {
      let data = {
        pageNum: this.pageData.currentPage,
        pageSize: this.pageData.pagesize,
        nodeGrade: this.fontdve,
        searchState: this.status,
      };
      node.getList(data).then((response) => {
        if (response.code === "A0000") {
          let res = response.data;
          this.pageData.currentPage = res.current;
          this.pageData.pagesize = res.size;
          this.pageData.total = res.total;
          this.tableData = res.records;
        }
      });
    },
    //获取单个前置机详情
    getOne(id) {
      node.getOne(id).then((response) => {
        if (response.code === "A0000") {
          this.formData = response.data;
        }
      });
    },
    //提交
    submit() {
      this.$confirm("是否确认操作?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        if (this.operaType === 0) {
          node.add(this.formData).then((response) => {
            if (response.code === "A0000") {
              this.$message({
                type: "success",
                message: "新增成功!",
              });
              this.dialogVisible = false;
              this.getList();
            }
          });
        } else {
          //修改
          node.update(this.formData).then((response) => {
            if (response.code === "A0000") {
              this.$message({
                type: "success",
                message: "修改成功!",
              });
              this.dialogVisible = false;
              this.getList();
            }
          });
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
@import url("../../../styles/comStyle.less");
::v-deep .el-form-item--small .el-form-item__content,
.el-form-item--small .el-form-item__label {
  height: 32px;
}
</style>
