<template>
  <div class="version-container">
    <el-card class="version-card">
      <div slot="header" class="card-header">
        <span>{{ $t('auto.version.releaseTitle') }}</span>
      </div>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item :label="$t('auto.version.versionNo')" prop="version">
          <el-input v-model="form.version" placeholder="例如：2.7.0" style="width: 300px" />
        </el-form-item>
        <el-form-item :label="$t('auto.version.noticeTitle')" prop="title">
          <el-input v-model="form.title" placeholder="例如：新增AGV多车调度功能" style="width: 500px" />
        </el-form-item>
        <el-form-item :label="$t('auto.version.content')" prop="content">
          <el-input
            v-model="form.content"
            type="textarea"
            :rows="8"
            placeholder="请输入更新内容，支持HTML格式"
            style="width: 600px"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="loading" @click="handleRelease">
            {{ $t('auto.version.release') }}
          </el-button>
          <el-button @click="handleReset">{{ $t('auto.common.Reset') }}</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 当前版本信息 -->
    <el-card class="version-card" style="margin-top: 20px">
      <div slot="header" class="card-header">
        <span>{{ $t('auto.version.currentInfo') }}</span>
      </div>
      <el-descriptions :column="2" border>
        <el-descriptions-item :label="$t('auto.version.versionNo')">
          <el-tag type="success">{{ currentVersion }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item :label="$t('auto.version.switch')">
          <el-tag :type="enabled ? 'success' : 'danger'">{{ enabled ? $t('auto.version.enabled') : $t('auto.version.disabled') }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item :label="$t('auto.version.pollInterval')">
          {{ pollInterval }}{{ $t('auto.version.seconds') }}
        </el-descriptions-item>
        <el-descriptions-item :label="$t('auto.version.releaseTime')">
          {{ releaseTime || '-' }}
        </el-descriptions-item>
      </el-descriptions>
    </el-card>
  </div>
</template>

<script>
import { getCurrentVersion, releaseVersion } from '@/api/system/version'

export default {
  name: 'VersionRelease',
  data() {
    return {
      loading: false,
      form: {
        version: '',
        title: '',
        content: ''
      },
      rules: {
        version: [
          { required: true, message: '请输入版本号', trigger: 'blur' }
        ],
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' }
        ]
      },
      currentVersion: '',
      enabled: false,
      pollInterval: 30,
      releaseTime: ''
    }
  },
  mounted() {
    this.fetchCurrentVersion()
  },
  methods: {
    fetchCurrentVersion() {
      getCurrentVersion().then(res => {
        this.currentVersion = res.version || '-'
        this.enabled = res.enabled
        this.pollInterval = res.pollInterval || 30
        this.releaseTime = res.releaseTime || ''
      })
    },
    handleRelease() {
      this.$refs.form.validate(valid => {
        if (!valid) return
        this.loading = true
        releaseVersion(this.form).then(() => {
          this.$message.success('版本更新通知已发布')
          this.handleReset()
          this.fetchCurrentVersion()
        }).catch(() => {
          // 错误已在 request 拦截器中处理
        }).finally(() => {
          this.loading = false
        })
      })
    },
    handleReset() {
      this.form = {
        version: '',
        title: '',
        content: ''
      }
    }
  }
}
</script>

<style scoped>
.version-container {
  padding: 20px;
}
.version-card {
  max-width: 800px;
}
.card-header {
  font-weight: bold;
}
</style>
