<template>
    <d2-container>
        <div :key="key">
            <el-card class="box-card" v-for="(item, index) in serverData" :key="index">
                <div slot="header" class="device-header">
                    <span>前置服务 {{ item.server_name }} ({{ item.url }}:{{ item.port }} )</span>
                </div>
                <el-row :gutter="12">
                    <el-col :span="6" v-for="(i, k) in item.devices" :key="k">
                        <el-popover placement="top" trigger="click">
                            <el-menu @select="(index) => setServeStatus(i.id, index)"
                                :style="{ 'borderRight': 'none' }">
                                <el-menu-item index="0">
                                    <span slot="title">暂停</span>
                                </el-menu-item>
                                <el-menu-item index="1">
                                    <span slot="title">重启</span>
                                </el-menu-item>
                                <el-menu-item index="2">
                                    <span slot="title">关闭</span>
                                </el-menu-item>
                            </el-menu>
                            <el-card slot="reference"
                                :class="{ 'box-card': true, 'online': i.deviceStatus, 'offline': !i.deviceStatus }">
                                <div slot="header" class="header-title">
                                    <span>{{ i.device_name }}</span>
                                </div>
                                <el-row :gutter="24">
                                    <el-col :span="5" style="padding-right:0px;margin-bottom: 5px;">
                                        <span>设备类型</span>
                                    </el-col>
                                    <el-col :span="14" style="padding-left:0px;">：{{ i.device_type }}</el-col>
                                </el-row>
                                <el-row :gutter="24">
                                    <el-col :span="5" style="padding-right:0px;margin-bottom: 5px;">
                                        <span>IP/端口</span>
                                    </el-col>
                                    <el-col :span="14" style="padding-left:0px;">：{{ i.config ? i.config[2] : "" }}
                                    </el-col>
                                </el-row>
                                <el-row :gutter="24">
                                    <el-col :span="5" style="padding-right:0px;margin-bottom: 5px;">
                                        <span>活动时间</span>
                                    </el-col>
                                    <el-col :span="14" style="padding-left:0px;">：{{ i.startTime }}</el-col>
                                </el-row>
                                <el-row :gutter="24">
                                    <el-col :span="5" style="padding-right:0px;margin-bottom: 5px;">
                                        <span>采集耗时</span>
                                    </el-col>
                                    <el-col :span="14" style="padding-left:0px;">：{{ i.captureSpendTime }}</el-col>
                                </el-row>
                                <el-row :gutter="24">
                                    <el-col :span="5" style="padding-right:0px;margin-bottom: 5px;">
                                        <span>成功次数</span>
                                    </el-col>
                                    <el-col :span="14" style="padding-left:0px;">：{{ i.success }}</el-col>
                                </el-row>
                                <el-row :gutter="24">
                                    <el-col :span="5" style="padding-right:0px;margin-bottom: 5px;">
                                        <span>失败次数</span>
                                    </el-col>
                                    <el-col :span="14" style="padding-left:0px;">：{{ i.failed }}</el-col>
                                </el-row>
                                <el-row :gutter="24">
                                    <el-alert v-if="i.failedMsg" :title="i.failedMsg" type="error" :closable="false">
                                    </el-alert>
                                    <div v-else style="height:35px;"></div>
                                </el-row>

                            </el-card>
                        </el-popover>
                    </el-col>
                </el-row>

            </el-card>
        </div>
    </d2-container>
</template>
<script>

export default {
  data () {
    return {

      serverData: [],
      deviceData: [],
      key: false
    }
  },

  methods: {
    async getServe () {
      try {
        this.serverData = Object.values(await this.$api.GET_ALL_DEVICES())
        this.serverData.forEach((element, index) => {
          element.devices.forEach((item, i) => {
            this.$api.GET_SERVE_DEVICE_MONITORING('http://' + item.url + ':' + item.port, 'admin', '123456', item.device_name).then(res => {
              if (res.IsSuccess) {
                const temp = {
                  config: res.Content.__config.split(' '),
                  startTime: res.Content.__startTime,
                  captureSpendTime: res.Content.__captureSpendTime,
                  success: res.Content.__success,
                  failed: res.Content.__failed,
                  failedMsg: res.Content.__failedMsg,
                  deviceStatus: res.Content.__deviceStatus,
                  device_name: item.device_name,
                  device_type: item.device_type
                }
                this.$set(this.serverData[index].devices, i, temp)
              }
            })
          })
        })
      } catch (e) {
        console.log(e)
      }
    }
  },
  mounted () {
    this.getServe()
  }
}
</script>
<style scoped>
.box-card {
    margin: 5px 0px;
    color: #fff;
    font-weight: bold;
}

.device-header {
    color: #000;
    font-weight: bold;
    font-size: 18px;
}

.online {
    background-color: #67c23a;
}

.offline {
    background-color: #666;
}

.text {
    font-size: 14px;
}

.item {
    margin-bottom: 18px;
}

.header-title {

    font-size: 18px;
}
</style>
