<script setup>
import { Page } from '@vben/common-ui';
import { requestClient } from '#/api/request';
import { Card, Button, Space, message } from 'ant-design-vue';
import { ref } from 'vue';

const buttonDisabled = ref(false);
const updateLoaderHide = ref(null);

const ControlAction = {
  async updateCode() {
    return requestClient.get('/control/update_code')
    .then(result => {
      message.success('更新代码成功');
      console.log('更新代码成功');
      console.log(result);
    });
  },

  async restartSportteryHot() {
    return requestClient.get('/control/restart_sporttery', { params: { hot: true } })
    .then(result => {
      message.success('重启 sporttery 服务成功');
      console.log('重启 sporttery 服务成功');
      console.log(result);
    });
  },

  async restartSportteryCold() {
    return requestClient.get('/control/restart_sporttery', { params: { hot: false } })
    .then(result => {
      message.success('重启 sporttery 服务成功');
      console.log('重启 sporttery 服务成功');
      console.log(result);
    });
  },

  async restartPinnacleHot() {
    return requestClient.get('/control/restart_pinnacle', { params: { hot: true } })
    .then(result => {
      message.success('重启 pinnacle 服务成功');
      console.log('重启 pinnacle 服务成功');
      console.log(result);
    });
  },

  async restartPinnacleCold() {
    return requestClient.get('/control/restart_pinnacle', { params: { hot: false } })
    .then(result => {
      message.success('重启 pinnacle 服务成功');
      console.log('重启 pinnacle 服务成功');
      console.log(result);
    });
  },

  async releaseWeb() {
    return requestClient.get('/control/release_web', { timeout: 90_000 })
    .then(result => {
      message.success('发布 web 服务成功');
      console.log('发布 web 服务成功');
      console.log(result);
    });
  },
}

const handleAction = (action) => {
  buttonDisabled.value = true;
  updateLoaderHide.value = message.loading('执行中...', 0);
  ControlAction[action]?.()
  .finally(() => {
    buttonDisabled.value = false;
    updateLoaderHide.value?.();
    updateLoaderHide.value = null;
  });
}

</script>

<template>
  <Page title="控制面板">
    <Space direction="vertical" size="large" style="width: 100%">
      <Card title="全局控制">
        <Space>
          <Button type="primary" @click="handleAction('updateCode')" ghost :disabled="buttonDisabled">
            更新代码
          </Button>
        </Space>
      </Card>

      <Card title="PS采集管理" v-if="false">
        <Space>
          <Button @click="handleAction('restartPinnacleHot')" type="primary" ghost :disabled="buttonDisabled">
            热重启服务
          </Button>

          <Button @click="handleAction('restartPinnacleCold')" ghost danger :disabled="buttonDisabled">
            冷重启服务
          </Button>
        </Space>
      </Card>

      <Card title="服务管理">
        <Space>
          <Button @click="handleAction('restartSportteryHot')" type="primary" ghost :disabled="buttonDisabled">
            热重启服务
          </Button>

          <Button @click="handleAction('restartSportteryCold')" ghost danger :disabled="buttonDisabled">
            冷重启服务
          </Button>
        </Space>
      </Card>

      <Card title="Web管理">
        <Space>
          <Button @click="handleAction('releaseWeb')" type="primary" ghost :disabled="buttonDisabled">
            发布Web服务
          </Button>
        </Space>
      </Card>

    </Space>
  </Page>
</template>

<style scoped>
</style>

