<template>
  <div>
    <div class="menu-title">
      节点管理
    </div>
    <div class="menu-content">
      <div>
        <el-form :inline="true" :model="qo" class="demo-form-inline">
          <el-form-item>
            <el-input v-model="qo.LIKES_name" placeholder="请输入节点名称"></el-input>
          </el-form-item>
          <el-form-item>
            <el-input v-model="qo.LIKES_name" placeholder="请输入节点地址"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="qo.pageNo=1;queryData()">搜 索</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div style="margin: 0px 0 20px 0">
        <el-button type="primary" @click="createRow()">新增节点</el-button>
      </div>
      <div>
        <el-table :data="result.records" style="width: 100%" ref="table">
          <el-table-column type="index" label="行号" width="100"></el-table-column>
          <el-table-column prop="name" label="节点名称"></el-table-column>
          <el-table-column label="节点地址">
            <template slot-scope="scope">
              {{ scope.row.host }}:{{ scope.row.port }}
            </template>
          </el-table-column>
          <el-table-column prop="modifyTimeMillis" label="修改时间">
            <template slot-scope="scope">
              {{ scope.row.modifyTimeMillis | timeFilter }}
            </template>
          </el-table-column>
          <el-table-column prop="createTimeMillis" label="创建时间">
            <template slot-scope="scope">
              {{ scope.row.createTimeMillis | timeFilter }}
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button @click.native.prevent="showXtermDialog(scope.row)" type="text" size="small">
                终端
              </el-button>
              <el-button @click.native.prevent="modifyRow(scope.row)" type="text" size="small">
                编辑
              </el-button>
              <el-button @click.native.prevent="removeRow(scope.row)" type="text" size="small">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="page-box">
        <el-pagination background
                       @current-change="handleCurrentChange"
                       :current-page="qo.pageNo"
                       :page-size="qo.pageSize"
                       layout="total, prev, pager, next"
                       :total="result.total">
        </el-pagination>
      </div>
    </div>
    <el-dialog
        :title="dialogName"
        :visible.sync="cmdDialogVisible"
        width="40%">
      <el-form ref="cmd" label-width="100px" :rules="rules" :model="cmd">
        <el-form-item label="节点名称" prop="name">
          <el-input v-model="cmd.name" placeholder="请输入节点名称"></el-input>
        </el-form-item>
        <el-form-item label="节点IP" prop="host">
          <el-input v-model="cmd.host" placeholder="请输入节点IP"></el-input>
        </el-form-item>
        <el-form-item label="节点端口" prop="port">
          <el-input v-model="cmd.port" placeholder="请输入节点端口"></el-input>
        </el-form-item>
        <el-form-item label="SSH账号" prop="user">
          <el-input v-model="cmd.user" placeholder="请输入SSH账号"></el-input>
        </el-form-item>
        <el-form-item label="SSH密码" prop="password">
          <el-input v-model="cmd.password" placeholder="请输入SSH密码"></el-input>
        </el-form-item>
        <!--        <el-form-item label="Host密码" prop="name">-->
        <!--          <el-input v-model="cmd.name" placeholder="请输入Host密码"></el-input>-->
        <!--        </el-form-item>-->
        <el-form-item label="数据目录" prop="dataPath">
          <el-input v-model="cmd.dataPath" placeholder="请输入数据目录"></el-input>
        </el-form-item>
<!--        <el-form-item label="超时时间">-->
<!--          <el-date-picker-->
<!--              v-model="cmd.time"-->
<!--              type="datetime"-->
<!--              placeholder="请选择超时时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="描述">-->
<!--          <el-input v-model="cmd.description" placeholder="请输入描述" type="textarea"></el-input>-->
<!--        </el-form-item>-->
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cmdDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitModify">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
        title="终端"
        :visible.sync="xtermDialogVisible"
        destroy-on-close
        @close="handleXtermDialogClose"
        width="60%">
      <xterm ref="xterm-item"></xterm>
    </el-dialog>
  </div>
</template>

<script>
import request from '@/utils/request2';
import xterm from '@/components/xterm/index';

var _this;
export default {
  name: "codeWarehouse",
  components: {
    xterm
  },
  data() {
    return {
      qo: {
        pageNo: 1,
        pageSize: 10,
        LIKES_name: '',
      },
      result: {
        records: [],
        total: 0
      },
      dialogName: '编辑',
      cmdDialogVisible: false,
      cmd: {},
      rules: {
        name: [
          {required: true, message: '请输入节点名称'}
        ],
        host: [
          {required: true, message: '请输入节点IP'}
        ],
        port: [
          {required: true, message: '请输入节点端口'}
        ],
        user: [
          {required: true, message: '请输入SSH账号'}
        ],
        password: [
          {required: true, message: '请输入SSH密码'}
        ],
      },
      xtermDialogVisible: false,
    }
  },
  mounted() {
    _this = this;
    _this.queryData();
  },
  filters: {
    timeFilter(val) {
      if (val > 0) {
        let dt = new Date(val);
        let year = dt.getFullYear();
        let month = dt.getMonth() + 1;
        let date = dt.getDate();
        let hour = dt.getHours();
        let minute = dt.getMinutes();
        let second = dt.getSeconds();
        return `${year}-${month}-${date} ${hour}:${minute}:${second}`;
      } else {
        return "-";
      }
    }
  },
  methods: {
    queryData() {
      request({
        url: '/server/node/list',
        method: 'post',
        data: _this.qo
      }).then(res => {
        _this.result.records = res.list;
        _this.result.total = res.total;
      });
    },
    handleCurrentChange(val) {
      _this.qo.pageNo = val;
      _this.queryData();
    },
    removeRow(item) {
      _this.$confirm('此操作将永久删除记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let id = item.id;
        request({
          url: `/server/node/delete/${id}`,
          method: 'post',
        }).then(res => {
          _this.$message.success("删除成功");
          _this.queryData();
        });
      }).catch(() => {
      });
    },
    createRow() {
      _this.cmdDialogVisible = true;
      _this.$nextTick(() => {
        _this.dialogName = "新增";
        _this.cmd = {
          "charset": "",
          "dataPath": "",
          "fileDirs": "",
          "host": "",
          "name": "",
          "notAllowedCommand": "",
          "password": "",
          "port": "",
          "user": ""
        };
        _this.$refs.cmd.resetFields();
      });
    },
    modifyRow(item) {
      _this.cmdDialogVisible = true;
      _this.$nextTick(() => {
        _this.dialogName = "编辑";
        _this.cmd = JSON.parse(JSON.stringify(item));
        _this.cmd.modifyTimeMillis = null;
        _this.$refs.cmd.resetFields();
      });
    },
    showXtermDialog(row) {
      _this.xtermDialogVisible = true;
      _this.$nextTick(() => {
        _this.$refs['xterm-item'].init(row.id);
      });
    },
    handleXtermDialogClose() {
      _this.$refs['xterm-item'].close();
    },
    submitModify() {
      this.$refs.cmd.validate((valid) => {
        if (valid) {
          if (_this.cmd.id) {
            request({
              url: '/server/node/update',
              method: 'post',
              data: _this.cmd
            }).then(res => {
              _this.$message.success("编辑成功");
              _this.queryData();
              _this.cmdDialogVisible = false;
            });
          } else { // 新建
            request({
              url: '/server/node/save',
              method: 'post',
              data: _this.cmd
            }).then(res => {
              _this.$message.success("新增成功");
              _this.queryData();
              _this.cmdDialogVisible = false;
            });
          }
        }
      });
    },
  }
}
</script>

<style lang="scss">
</style>
