<template>
  <div v-loading="loading">
    <el-button type="primary" style="margin-bottom: 20px" @click="onRestart">重启</el-button>
    <el-table :data="tableData" stripe border @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" />
      <el-table-column type="index" label="#" align="center" />
      <el-table-column prop="name" label="jar包名称" />
      <el-table-column prop="stats" label="状态" align="center" width="100px">
        <template #default="scope">
          <el-tag :type="scope.row.stats ? 'success' : ('error' as any)" disable-transitions>{{
            scope.row.stats ? '成功' : '失败'
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="exist" label="存在" align="center" width="100px">
        <template #default="scope">
          <el-tag :type="scope.row.exist ? 'success' : ('error' as any)" disable-transitions>{{
            scope.row.exist ? '是' : '否'
          }}</el-tag>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script setup lang="ts">
import { getJarList, jarRestart } from '@/api'

const loading = ref(true)
const tableData = ref()
const selectRows = ref()

const handleSelectionChange = (rows: any) => {
  selectRows.value = rows
}

const onRestart = () => {
  if (!selectRows.value.length) return ElMessage.warning('请先选择数据')
  if (selectRows.value.length > 1) return ElMessage.warning('一次只能重启一个jar包')
  if (!selectRows.value[0].exist) return ElMessage.warning('存在该jar包才能重启')
  ElMessageBox.confirm(`确认重启?`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      const res: any = await jarRestart({
        code: selectRows.value[0].code
      })
      if (res.code === 200) ElMessage.success('重启成功')
      else ElMessage.error(res.msg)
    } finally {
      init()
    }
  })
}

const init = async () => {
  loading.value = true
  const res: any = await getJarList()
  if (res.code === 200) {
    tableData.value = res.data
    loading.value = false
  }
}

init()
</script>

<style scoped></style>
