<template>
  <div class="app-container">
    <Form :formList="formList" :ruleFormData="ruleForm" @fnClick="callback"></Form>
  </div>
</template>

<script setup>
  import Form from '@/components/form/index.vue';
  import {addCpuInfo, getCpuInfo, updateCpuInfo} from "@/api/agentCollect/collectData"
  export default {
    name: 'CpuDataView',
    components: {Form},
    data() {
      return {
        ruleForm: {},
        formList: [],
        paramsData: {}
      }
    },
    created() {
      this.paramsData = this.$route && this.$route.query;
      if (this.paramsData && this.paramsData.id) {
        this.getFormDataList(this.paramsData.id);
      }
      this.fnFormList();
    },
    methods: {
      // formList集合
      fnFormList(objVal) {
        this.formList = [{
          config: {title: '基本信息'},
          controls: {
            id: {label: 'ID',hidden: true},
            clientId: {label: `客户端ID`, span: 12, type: 'input',},
            avg1: {label: `CPU1分钟负载`, span: 12, type: 'input',},
            avg5: {label: `CPU5分钟负载`, span: 12, type: 'input'},
            avg15: {label: `CPU15分钟负载`, span: 12, type: 'input'},
            interrupt: {label: `CPU硬件中断提供服务时间(秒)`, span: 12, type: 'input'},
            uti: {label: `CPU使用率(%)`, width: '150', span: 12, type: 'input'},
            num: {label: `CPU数量(核数)`, width: '150', span: 12, type: 'input'},
            normal: {label: `CPU正常运行时间(秒)`, span: 12, type: 'input'},
            idle: {label: `CPU空闲时间(秒)`, span: 12, type: 'input'},
            iowait: {label: `CPU等待响应时间(秒)`, span: 12, type: 'input'},
            system: {label: `CPU系统时间(秒)`,span: 12, type: 'input'},
            noresp: {label: `CPU软件无响应时间(秒)`, span: 12, type: 'input'},
            user: {label: `CPU用户进程所花费的时间(秒)`, span: 12, type: 'input'},
          }
        }];
      },
      // 获取详情
      getFormDataList(id) {
        getCpuInfo(id).then(val => {
          if (val && val.data) {
            val.data['includedDevices'] = val.data['includedDevices'].split(',');
            this.ruleForm = val.data;
          }
        }).catch(() => {
          this.$modal.msgError("操作失败")
        });
      },
      // 监听事件
      callback(result, dataVal, formVal) {
        if (result && result.fnCode) {
          switch (result.fnCode) {
            case 'submit':
              dataVal['includedDevices'] = dataVal['includedDevices'].join();
              let fnType = addCpuInfo;
              if (dataVal && dataVal.id) {
                fnType = updateCpuInfo;
              }
              fnType(dataVal).then(response => {
                this.$modal.msgSuccess(response.msg);
                this.$router.push("/resource/group")
              }).catch(() => {
                this.$modal.msgError("操作失败")
              });
              break;
            case 'cancel':
              this.$router.push("/resource/group");
              break;
            default:
          }
        }
      }
    }
  }
</script>
<style>
</style>
