<template>
  <div>
    <el-row class="handle-box" style="margin-bottom: 10px">
      <el-col :span="24">
        <el-input v-model="search.serverName" class="ch-input ch-input-size" placeholder="服务器名称" size="small" @keyup.enter.native="handleSearch()" />
        <el-select v-model="search.status" clearable filterable placeholder="状态" size="small" @change="handleSearch">
          <el-option v-for="item in dc_data.OPS_SERVER_STATUS" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
        <el-button class="ch-button-warning" size="small" @click="handleReset()"><i class="el-icon-search" />&nbsp;重置</el-button>
        <el-button class="ch-button" size="small" @click="handleSearch()"><i class="el-icon-search" />&nbsp;搜索</el-button>
        <el-button class="ch-button-export" size="small" @click="confirmOutput()"><i class="el-icon-download" />&nbsp;导出</el-button>
        <!--<el-button size="small" class="ch-button" style="float: right;margin-top: 4px" @click="handleAdd()"><i class="el-icon-menu" />&nbsp;新增</el-button>-->
      </el-col>
    </el-row>
    <el-row class="handle-box">
      <el-col :span="24">
        <el-table v-loading="loading" :data="AllData" row-class-name="g_table_row" stripe>
          <el-table-column type="index" width="60" />
          <el-table-column label="服务器IP" prop="serverIp" />
          <el-table-column label="服务器名称" prop="serverName" />
          <el-table-column label="状态" prop="status" />
          <el-table-column label="最近一次CPU状态" prop="currCpu" />
          <el-table-column label="最近一次内存状态" prop="currMemory" />
          <el-table-column label="最近上报时间" prop="lastReportAt" />
          <el-table-column align="center" header-align="center" label="操作" width="180">
            <template scope="scope">
              <!--<el-button size="mini" type="primary" @click="handleEdit(scope.row)">编辑</el-button>-->
              <!--<el-button size="mini" type="assistdanger" @click="handleDel(scope.row)">删除</el-button>-->
              <el-button plain size="mini" type="primary" @click="handleRecord(scope.row.id)">运行日志</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="table-page">
          <el-pagination
            :current-page.sync="currentPage"
            :page-size="pageSize"
            :total="allpage"
            background
            layout="total, prev, pager, next"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-col>
    </el-row>
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" top="50px" width="75%" @open="dlgOpen">
      <el-form ref="form" :model="form" :rules="rules" style="width: 100%;padding: 5px">
        <el-row>
          <el-col style="padding-bottom: 10px">
            <span class="card_title">基本信息</span>
            <el-card shadow="always" style="padding: 15px 5px 5px 15px">
              <el-row>
                <el-col :span="3" class="col-txt"><span>服务器IP</span></el-col>
                <el-col :span="20" class="col-input">
                  <el-form-item>
                    <el-input v-model="form.serverIp" />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="3" class="col-txt"><span>服务器名称</span></el-col>
                <el-col :span="20" class="col-input">
                  <el-form-item>
                    <el-input v-model="form.serverName" />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="3" class="col-txt"><span>状态</span></el-col>
                <el-col :span="20" class="col-input">
                  <el-form-item>
                    <el-select v-model="form.status" clearable filterable placeholder="状态">
                      <el-option
                        v-for="item in dc_data.OPS_SERVER_STATUS"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                      />
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="3" class="col-txt"><span>最近一次CPU状态</span></el-col>
                <el-col :span="20" class="col-input">
                  <el-form-item>
                    <el-input v-model="form.currCpu" />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="3" class="col-txt"><span>最近一次内存状态</span></el-col>
                <el-col :span="20" class="col-input">
                  <el-form-item>
                    <el-input v-model="form.currMemory" />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="3" class="col-txt"><span>最近上报时间</span></el-col>
                <el-col :span="20" class="col-input">
                  <el-form-item>
                    <el-date-picker
                      v-model="form.lastReportAt"
                      placeholder="最近上报时间"
                      type="datetime"
                      value-format="yyyy-MM-dd HH:mm:ss"
                    />
                  </el-form-item>
                </el-col>
              </el-row>
            </el-card>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmSubmit()">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="dialogRecordVisible" title="服务器运行日志" top="50px" width="75%">
      <el-card>
        <record v-if="dialogRecordVisible" :server-id="form.id" />
      </el-card>
      <div slot="footer">
        <el-button @click="dialogRecordVisible = false">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import Base from '@/views/base/base'
import BaseData from '@/views/base/baseData'
import record from './record'

export default {
    name: 'Server',
    components: { record },
    mixins: [Base, BaseData],
    data() {
        return {
            dc_key: ['OPS_SERVER_STATUS'],
            // 列表相关
            search: {
                serverName: '',
                status: ''
            },
            AllData: [],
            loading: false,
            // 弹框相关
            dialogVisible: false,
            dialogTitle: '新增',
            isAdd: true,
            form: this.initForm(),
            rules: {},
            // 日志记录
            dialogRecordVisible: false
        }
    },
    mounted() {
        this.initDict(this.dc_key).then((res) => {
            this.getData()
        })
    },
    methods: {
        getData: function() {
            const _this = this
            _this.loading = true
            _this.AllData = []

            this.search.pageNum = this.currentPage
            this.search.pageSize = this.pageSize
            this.baseRequest('list', this.search).then((res) => {
                if (res.data.rows) {
                    res.data.rows.forEach(function(item) {
                        const json = _this.getItemJson(item)
                        _this.AllData.push(json)
                    })
                    _this.allpage = res.data.total
                }
                _this.loading = false
            }).catch(() => {
            })
            this.initOutData()
        },
        handleSearch: function() {
            this.getData()
        },
        handleReset: function() {
            for (const i in this.search) {
                if (i !== 'pageNum' && i !== 'pageSize') {
                    this.search[i] = ''
                }
            }
            this.handleSearch()
        },
        initOutData: function() {
            const _this = this
            this.OutData = []
            const title = [' 服务器IP', ' 服务器名称', ' 状态', ' 最近一次CPU状态', ' 最近一次内存状态', ' 最近上报时间'
            ]
            this.OutData.push(title)

            this.baseRequest('listAll', this.search).then((res) => {
                if (res.data) {
                    res.data.forEach(function(item) {
                        const jsonMap = _this.getItemJson(item)
                        const jsonArray = []
                        jsonArray.push(jsonMap.serverIp)
                        jsonArray.push(jsonMap.serverName)
                        jsonArray.push(jsonMap.status)
                        jsonArray.push(jsonMap.currCpu)
                        jsonArray.push(jsonMap.currMemory)
                        jsonArray.push(jsonMap.lastReportAt)

                        _this.OutData.push(jsonArray)
                    })
                }
            }).catch(() => {
            })
        },
        getItemJson: function(item) {
            item.status = this.dc_map.OPS_SERVER_STATUS[item.status]
            item.lastReportAt = this.$common.transDate(item.lastReportAt, this.$constant.DATE_PATTERN.DATE_TIME_s_h)
            return item
        },
        initForm: function() {
            return {
                id: '',
                serverIp: '',
                serverName: '',
                status: '',
                currCpu: '',
                currMemory: '',
                lastReportAt: ''
            }
        },
        confirmOutput() {
            const OutSize = [{ wch: 15 }, { wch: 15 }, { wch: 15 }, { wch: 15 }, { wch: 15 }, { wch: 15 }
            ]
            const fileName = 'server导出 ' + new Date().Format('yyyyMMddhhmm')
            this.$outputXlsxFile(this.OutData, OutSize, fileName)
        },
        dlgOpen: function() {
            const _this = this

            if (_this.form.id !== '') {
                const postData = {
                    id: _this.form.id
                }
                this.baseRequest('getById', postData).then((res) => {
                    if (res.data) {
                        _this.form = Object.assign({}, _this.form, res.data)
                        _this.form.lastReportAt = _this.$common.transServDate(res.data.lastReportAt)
                    }
                }).catch(() => {
                })
            }
        },
        /* 编辑*/
        handleEdit: function(val) {
            this.isAdd = false
            this.form.id = val.id
            this.dialogVisible = true
            this.dialogTitle = '编辑'
        },
        /* 新增*/
        handleAdd: function() {
            this.form = this.initForm()
            this.isAdd = true
            this.dialogVisible = true
            this.dialogTitle = '新增'
        },
        confirmSubmit: function() {
            const _this = this

            this.$refs.form.validate(valid => {
                if (valid) {
                    let soaUrl = 'edit'
                    const extraData = {}
                    const postData = Object.assign({}, _this.form, extraData)

                    if (this.isAdd) {
                        soaUrl = 'add'
                    }
                    this.opRecord(postData, soaUrl)
                } else {
                    //console.log('error submit!!')
                    return false
                }
            })
        },
        /* 查看日志*/
        handleRecord: function(_id) {
            this.form.id = _id
            this.dialogRecordVisible = true
        },
        baseRequest(opUrl, postData) {
            return this.$channel.globeRequest('ServerMainController', opUrl, postData, 'project')
        }
    }
}
</script>

<style scoped>
  .ch-input .el-input__inner {
      border-color: #32323A;
  }
  .ch-input-size {
      width: 150px;
  }
  .ch-button {
      border-color: #32323A;
      background-color: #32323A;
      color: #fff;
  }
  .ch-button-warning {
      margin-left: 10px;
      border-color: #E6A23C;
      background-color: #E6A23C;
      color: #fff;
  }
  .ch-button-export {
      margin-left: 10px;
      border-color: #98CC1F;
      background-color: #98CC1F;
      color: #fff;
  }
  /deep/.el-dialog__header {
      padding: 10px 20px;
  }
  /deep/.el-dialog__body {
      padding: 10px 20px;
  }
</style>
