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

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">
    <PageTitle>
      <template #title>系统控制台</template>
    </PageTitle>
    <div class="app-container__body">
      <el-form-item label="" prop="">
        <div style="display: flex;width: 100%;">
          <el-input v-model="enterValue" placeholder="请输入命令"></el-input>
         <el-button type="primary" style="width: 120px; margin-left: 10px;" @click="handelExecute()">执行</el-button>
        </div>
      </el-form-item>
      <el-input type="textarea" v-model="requestOptions" rows="20" placeholder="返回结果..." disabled></el-input>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.app-container__body {
  :deep(.el-input__wrapper){
    background: #f3f5fa;
  }
  
}
</style>