<template>
  <div class="settings-view">
    <header class="view-header">
      <h1>软件设置</h1>
      <p class="subtitle">管理仓库地址与应用配置</p>
    </header>

    <div class="settings-content">
      <section class="settings-section">
        <h3 class="section-title">仓库配置</h3>
        <div class="settings-card">
          <div class="setting-item">
            <div class="setting-info">
              <label>仓库地址</label>
              <p>应用将从该地址同步“装机必备”软件清单</p>
            </div>
            <div class="setting-action">
              <input 
                type="text" 
                v-model="tempRepoUrl" 
                placeholder="https://..."
                class="settings-input"
              />
            </div>
          </div>
          
          <div class="setting-footer">
            <button 
              @click="handleSave" 
              class="btn-primary" 
              :disabled="isSaving || !isValidUrl"
            >
              {{ isSaving ? '正在保存...' : '保存配置' }}
            </button>
            <button 
              @click="handleSync" 
              class="btn-secondary" 
              :disabled="store.loading"
            >
              <span v-if="store.loading" class="spinner"></span>
              {{ store.loading ? '正在同步...' : '立即同步清单' }}
            </button>
          </div>
        </div>
      </section>

      <section class="settings-section">
        <h3 class="section-title">关于</h3>
        <div class="settings-card about-card">
          <p>Windows 软件管理 v{{ version }}</p>
          <p class="hint">基于 WinGet 构建的 Windows 软件管理工具</p>
        </div>
      </section>
    </div>

    <!-- 通知提示 -->
    <transition name="toast">
      <div v-if="toast" class="toast" :class="toast.type">
        {{ toast.message }}
      </div>
    </transition>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useSoftwareStore } from '../store/software'
import { version } from "../../package.json";

const store = useSoftwareStore()
const tempRepoUrl = ref('')
const isSaving = ref(false)
const toast = ref<{ message: string, type: 'success' | 'error' } | null>(null)

const isValidUrl = computed(() => {
  try {
    new URL(tempRepoUrl.value)
    return true
  } catch {
    return false
  }
})

onMounted(() => {
  tempRepoUrl.value = store.settings.repo_url
})

const showToast = (message: string, type: 'success' | 'error' = 'success') => {
  toast.value = { message, type }
  setTimeout(() => {
    toast.value = null
  }, 3000)
}

const handleSave = async () => {
  if (!isValidUrl.value) return
  isSaving.value = true
  try {
    await store.saveSettings({ repo_url: tempRepoUrl.value })
    showToast('配置已保存')
  } catch (err) {
    showToast('保存失败: ' + err, 'error')
  } finally {
    isSaving.value = false
  }
}

const handleSync = async () => {
  try {
    const result = await store.syncEssentials()
    showToast(result.message, result.status === 'updated' ? 'success' : 'error')
  } catch (err) {
    showToast('同步失败，请检查网络或地址', 'error')
  }
}
</script>

<style scoped>
.settings-view {
  padding: 60px;
  height: 100%;
  overflow-y: auto;
  background-color: var(--bg-light);
}

.view-header {
  margin-bottom: 40px;
}

.view-header h1 {
  font-size: 34px;
  font-weight: 700;
  letter-spacing: -0.5px;
  margin-bottom: 8px;
}

.subtitle {
  color: var(--text-sec);
  font-size: 17px;
}

.settings-content {
  max-width: 800px;
}

.settings-section {
  margin-bottom: 40px;
}

.section-title {
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  color: var(--text-sec);
  margin-bottom: 12px;
  padding-left: 4px;
}

.settings-card {
  background: white;
  border-radius: var(--radius-card);
  padding: 24px;
  box-shadow: var(--card-shadow);
  border: 1px solid var(--border-color);
}

.setting-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.setting-info label {
  display: block;
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 4px;
}

.setting-info p {
  font-size: 14px;
  color: var(--text-sec);
}

.settings-input {
  width: 320px;
  padding: 12px 16px;
  border-radius: 10px;
  border: 1px solid var(--border-color);
  font-size: 14px;
  transition: all 0.2s ease;
  background-color: #f5f5f7;
}

.settings-input:focus {
  outline: none;
  border-color: var(--primary-color);
  background-color: white;
  box-shadow: 0 0 0 4px rgba(0, 122, 255, 0.1);
}

.setting-footer {
  display: flex;
  gap: 12px;
  border-top: 1px solid var(--border-color);
  padding-top: 24px;
}

.btn-primary, .btn-secondary {
  padding: 10px 20px;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
  border: none;
  display: flex;
  align-items: center;
  gap: 8px;
}

.btn-primary {
  background-color: var(--primary-color);
  color: white;
}

.btn-primary:hover:not(:disabled) {
  background-color: var(--primary-hover);
  transform: translateY(-1px);
}

.btn-primary:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-secondary {
  background-color: #f5f5f7;
  color: var(--text-main);
}

.btn-secondary:hover:not(:disabled) {
  background-color: #e5e5e7;
}

.about-card p {
  margin-bottom: 4px;
  font-weight: 600;
}

.hint {
  font-size: 14px;
  color: var(--text-sec);
  font-weight: 400 !important;
}

/* Toast */
.toast {
  position: fixed;
  bottom: 40px;
  left: 50%;
  transform: translateX(-50%);
  padding: 12px 24px;
  border-radius: 12px;
  background: #323232;
  color: white;
  font-size: 14px;
  font-weight: 500;
  box-shadow: 0 8px 24px rgba(0,0,0,0.2);
  z-index: 1000;
}

.toast.error {
  background: #ff3b30;
}

.toast-enter-active, .toast-leave-active {
  transition: all 0.3s ease;
}

.toast-enter-from, .toast-leave-to {
  opacity: 0;
  transform: translate(-50%, 20px);
}

.spinner {
  width: 16px;
  height: 16px;
  border: 2px solid rgba(0,0,0,0.1);
  border-top-color: var(--text-main);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}
</style>
