<script setup name="SystemConsole">
import { onMounted, ref, toRefs, onBeforeUnmount } from 'vue'
import { ElMessage } from "element-plus";
import { command, commandlog } from '@/api/systemConfig/systemConsole.js'
import pageIcon from '@/assets/images/systemConfig/systemParamPage.png'

const enterValue = ref('') // 命令内容
const requestOptions = ref('') // 返回结果
const timeRef = ref()

// 执行命令
const handelExecute = async () => {
  if (!enterValue.value) {
    ElMessage.warning('请输入命令')
    return false
  }
  const { flag, msg } = await command({ command: enterValue.value })
  if (flag) {
    IntervalRequest()
  } else { 
    ElMessage.error(msg)
  }
}

const IntervalRequest = async () => {
  const { flag, data, msg } = await commandlog({})
  if (flag) {
    requestOptions.value = data
    timeRef.value = setTimeout(() => {
      IntervalRequest()
    }, 5000)
  } else {
    timeRef.value && clearTimeout(timeRef.value)
    ElMessage.error(msg)
  }
}

onBeforeUnmount(() => {
  timeRef.value && clearTimeout(timeRef.value)
})
</script>

<template>
  <div class="app-container scroller">
    <div class="app-container__body">
      <IconTitle :icon="pageIcon" title="系统控制台" />
      <div class="box">
        <div class="line"></div>
        <div class="content">
          <el-form-item label="请输入命令" label-position="right" label-width="100" prop="">
            <div style="display: flex;width: 100%;">
              <el-input v-model="enterValue" placeholder="请输入命令"></el-input>
            <el-button type="primary" style="width: 60px; margin-left: 10px;border-radius: 0;" @click="handelExecute()">执行</el-button>
            </div>
          </el-form-item>
          <el-form-item label="执行结果" label-position="right" label-width="100" prop="">
            <div style="display: flex;width: 100%;">{{ requestOptions }}</div>
          </el-form-item>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.app-container__body {
  .box {
    display: flex;
    flex-direction: column;
    flex: 1;
    overflow: hidden;
    background: #fff;

    .line {
      width: 100%;
      height: 2px;
      border-radius: 5px 5px 0px 0px;
      background: linear-gradient(90deg,
          rgba(33, 103, 217, 1) 0%,
          rgba(17, 200, 250, 1) 34.03%,
          rgba(33, 103, 217, 1) 64.58%,
          rgba(48, 128, 255, 1) 100%);
    }

    .content {
      padding: 52px 280px;
      flex: 1;
      overflow: hidden;
      display: flex;
      flex-direction: column;
      :deep(.el-form-item__label){
        font-size: 12px;
        font-weight: 400;
        color: rgba(148, 148, 148, 1);
      }
    }
  }
}
</style>