<template>
  <!-- 求职助手准备向导 — 第1步到第4步 -->
  <div class="bg-f ptb16 prl24 mb16 fs18 fw600">投递准备</div>
  <div>
    <div class="agent-page__wizard bg-f prl32 ptb24 border-ra12 mrl24">

      <!-- 个人资料编辑抽屉 -->
      <ProfileEditDrawer
          v-model="showEditDrawer"
          :module="editModule"
          :initial-data="editInitialData"
          @save="handleSaveEdit"
      />

      <!-- 顶部步骤导航条 -->
      <div class="agent-page__steps">
        <template v-for="(step, index) in steps" :key="index">
          <div
              class="agent-page__step"
              :class="{ 'agent-page__step--active': currentStep === index + 1 }"
          >
            <span class="agent-page__step-number">{{ index + 1 }}</span>
            <span class="agent-page__step-label">{{ step }}</span>
          </div>
          <span v-if="index < steps.length - 1" class="agent-page__step-line"></span>
        </template>
      </div>

      <!-- 主体内容区域 -->
      <div class="agent-page__main">
        <!-- ========== 第1步：确认个人资料 ========== -->
        <template v-if="currentStep === 1">
          <div class="agent-page__right">
            <div class="agent-page__profile-wrapper">
              <ProfilePageContent :profile="profile" @edit="handleEdit" @save="handleInlineSave" @exp-update="handleExpUpdate" @exp-add="handleExpAdd" @exp-delete="handleExpDelete" />
            </div>
          </div>
          <div class="agent-page__left">
            <div class="agent-page__intro-card agent-page__intro-card--gradient">
              <!-- Nova 头像 + 名称 -->
              <div class="agent-page__intro-avatar-row">
                <img src="@/assets/images/nova-logo-2.png" alt="Nova" class="agent-page__intro-avatar-img" />
                <span class="agent-page__intro-avatar-name">Nova</span>
              </div>
              <!-- 标题 -->
              <h2 class="agent-page__intro-title">请先选择并确认你的投递简历</h2>
              <!-- 描述 -->
              <p class="agent-page__intro-desc">请选择智能投递使用的简历，并仔细核对简历内容，确保各项信息准确、完整。</p>
              <div class="agent-page__step2-filters">
                <!-- 自定义简历下拉 -->
                <div class="agent-page__step2-resume-dropdown" @click.stop="showResumeDropdown = !showResumeDropdown">
                  <span class="agent-page__step2-resume-dropdown-text">{{ selectedResumeName || '请选择简历' }}</span>
                  <svg class="agent-page__step2-resume-dropdown-arrow" viewBox="0 0 12 12" fill="none">
                    <path d="M3 4.5L6 7.5L9 4.5" stroke="currentColor" stroke-width="1.2" stroke-linecap="round" stroke-linejoin="round"/>
                  </svg>
                  <!-- 下拉菜单 -->
                  <div v-if="showResumeDropdown" class="agent-page__step2-resume-menu" @click.stop>
                    <div
                        v-for="r in resumeList"
                        :key="r.id"
                        class="agent-page__step2-resume-menu-item"
                        :class="{ 'agent-page__step2-resume-menu-item--active': selectedResumeId === r.id }"
                        @click.stop="selectResume(r)"
                    >{{ r.resumeName }}</div>
                  </div>
                </div>
              </div>
              <!-- 按钮 -->
              <button class="agent-page__intro-gradient-btn" @click="handleNext">下一步</button>
             
            </div>
          </div>

        </template>

        <!-- ========== 第2步：配置投递方案 ========== -->
        <template v-if="currentStep === 2">
          <!-- 左侧：求职偏好 + 简历选择 -->
          <div class="agent-page__right">
            <div class="agent-page__step2-panel">
              <!-- 求职偏好区域 -->
              <div class="agent-page__step2-section">
                <h3 class="agent-page__step2-section-title">求职偏好</h3>
                <div class="agent-page__step2-filters">
                  <!-- 城市选择 -->
                  <RegionSelector
                      :regionCodes="step2RegionCodes"
                      :level="2"
                      :maxSelect="3"
                      :triggerStyle="prefFilterTriggerStyle"
                      :displayStyle="prefFilterDisplayStyle"
                      @update:regionCodes="onStep2RegionChange"
                  />
                  <!-- 岗位选择 -->
                  <JobCategorySelector
                      :categoryIds="step2CategoryIds"
                      :maxSelect="3"
                      :level="3"
                      :allowParentSelect="false"
                      :allowParentSelectClick="true"
                      :triggerStyle="prefFilterTriggerStyle"
                      :displayStyle="prefFilterDisplayStyle"
                      @update:categoryIds="onStep2CategoryChange"
                  />
                  <!-- 行业选择 -->
                  <IndustrySelector
                      :industryIds="step2IndustryIds"
                      :maxSelect="3"
                      :level="2"
                      :allowParentSelect="false"
                      :allowParentSelectClick="true"
                      :triggerStyle="prefFilterTriggerStyle"
                      :displayStyle="prefFilterDisplayStyle"
                      @update:industryIds="onStep2IndustryChange"
                  />
                  <!-- 招聘分类下拉选择 -->
                  <div class="agent-page__pref-type-dropdown" @click.stop="showJobTypeDropdown = !showJobTypeDropdown">
                    <span>{{ intentionEmploymentLabel || '招聘类型' }}</span>
                    <svg class="agent-page__pref-type-arrow" viewBox="0 0 12 12" fill="none">
                      <path d="M3 4.5L6 7.5L9 4.5" stroke="currentColor" stroke-width="1.2" stroke-linecap="round" stroke-linejoin="round"/>
                    </svg>
                    <!-- 下拉菜单 -->
                    <div v-if="showJobTypeDropdown" class="agent-page__pref-type-menu" @click.stop>
                      <div
                          v-for="option in jobTypeOptions"
                          :key="option.value"
                          class="agent-page__pref-type-menu-item"
                          :class="{ 'agent-page__pref-type-menu-item--active': store.state.jobIntention.recruitCategory === option.value }"
                          @click.stop="selectJobType(option)"
                      >{{ option.label }}</div>
                    </div>
                  </div>
                </div>
              </div>
              <!-- 投递偏好区域 -->
              <div class="agent-page__step2-section">
                <h3 class="agent-page__step2-section-title">投递偏好</h3>
                <div class="agent-page__step2-pref-switches">
                  <!-- 开启简历针对性优化 -->
                  <div class="agent-page__step2-switch-item">
                    <div class="agent-page__step2-switch-text">
                      <span class="agent-page__step2-switch-label">开启简历针对性优化</span>
                      <span class="agent-page__step2-switch-desc">结合岗位要求，自动优化简历表达</span>
                    </div>
                    <div class="agent-page__step2-toggle" :class="{ 'agent-page__step2-toggle--on': autoOptimizeSwitch }" @click="toggleOptimize">
                      <div class="agent-page__step2-toggle-dot"></div>
                    </div>
                  </div>
                  <!-- 开启深度人岗匹配分析 -->
                  <div class="agent-page__step2-switch-item">
                    <div class="agent-page__step2-switch-text">
                      <span class="agent-page__step2-switch-label">开启深度人岗匹配分析</span>
                      <span class="agent-page__step2-switch-desc">分析你的匹配优势与差距</span>
                    </div>
                    <div class="agent-page__step2-toggle" :class="{ 'agent-page__step2-toggle--on': autoMatchAnalysisSwitch }" @click="toggleMatchAnalysis">
                      <div class="agent-page__step2-toggle-dot"></div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <!-- 右侧：确认卡片 -->
          <div class="agent-page__left">
            <div class="agent-page__intro-card agent-page__intro-card--gradient">
              <!-- Nova 头像 + 名称 -->
              <div class="agent-page__intro-avatar-row">
                <img src="@/assets/images/nova-logo-2.png" alt="Nova" class="agent-page__intro-avatar-img" />
                <span class="agent-page__intro-avatar-name">Nova</span>
              </div>
              <!-- 标题 -->
              <h2 class="agent-page__intro-title">请配置你的投递方案</h2>
              <!-- 描述 -->
              <p class="agent-page__intro-desc">我们会根据你的选择，为你推荐最合适的投递岗位</p>
              <!-- 按钮 -->
              <button class="agent-page__intro-gradient-btn" @click="handleNext">下一步</button>
              <button class="agent-page__intro-back-btn" @click="handleBack">上一步</button>
            </div>
          </div>
        </template>



        <!-- ========== 第3步：安装插件 ========== -->
        <template v-if="currentStep === 3">
          <!-- 左侧：安装教程内容 -->
          <div class="agent-page__right">
            <div class="agent-page__profile-wrapper bg-f">
              <AgentBrowserInstallGuide ref="browserInstallGuideRef" />
            </div>
          </div>
          <!-- 右侧：确认卡片 -->
          <div class="agent-page__left">
            <div class="agent-page__intro-card agent-page__intro-card--gradient">
              <!-- Nova 头像 + 名称 -->
              <div class="agent-page__intro-avatar-row">
                <img src="@/assets/images/nova-logo-2.png" alt="Nova" class="agent-page__intro-avatar-img" />
                <span class="agent-page__intro-avatar-name">Nova</span>
              </div>
              <!-- 标题 -->
              <h2 class="agent-page__intro-title">请按左侧步骤安装浏览器插件</h2>
              <!-- 描述 -->
              <p class="agent-page__intro-desc">只需4步，完成 Chrome / Edge 插件安装</p>
              <!-- 按钮：在线显示下一步，离线显示安装确认按钮 -->
              <button v-if="plugIsOnline" class="agent-page__intro-gradient-btn" @click="handleNext">下一步</button>
              <button v-else class="agent-page__intro-gradient-btn" @click="handleInstallConfirm">
                <svg width="15" height="15" viewBox="0 0 15 15" fill="none" xmlns="http://www.w3.org/2000/svg">
                 <path d="M0.625 10.2117V11.0417C0.625 11.7047 0.888392 12.3406 1.35723 12.8094C1.82607 13.2783 2.46196 13.5417 3.125 13.5417H11.4583C12.1214 13.5417 12.7573 13.2783 13.2261 12.8094C13.6949 12.3406 13.9583 11.7047 13.9583 11.0417V10.2083M7.29167 0.625V9.79167M4.375 6.875L7.29167 9.79167L10.2083 6.875" stroke="white" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round"/>
                </svg>
                下载插件
              </button>
              <button class="agent-page__intro-back-btn" @click="handleBack">上一步</button>
            </div>
          </div>
        </template>

        <!-- ========== 第4步：完成配置 ========== -->
        <template v-if="currentStep === 4">
          <div class="agent-page__complete">
            <div class="agent-page__complete-icon">
              <img class="w160 " src="@/assets/images/agent-setting/setting-success.gif" alt="">
            </div>
            <h2 class="agent-page__complete-title">恭喜！求职助手已经准备好</h2>
            <p class="agent-page__complete-desc">启用求职助手后，你可以随时调整求职偏好和简历信息</p>
            <button class="agent-page__complete-btn" @click="handleLaunchAgent">启用求职助手开始投递</button>
          </div>
        </template>
      </div>

      <!-- 求职目标设置弹窗 -->
      <JobGoalDialog v-model="showJobGoalDialog" />

      <!-- 插件安装确认弹窗 -->
      <Teleport to="body">
        <div v-if="showInstallDialog" class="agent-page__install-dialog-overlay" @click.self="showInstallDialog = false">
          <div class="agent-page__install-dialog">
            <!-- 渐变背景 -->
            <div class="agent-page__install-dialog-bg"></div>
            <!-- 关闭按钮 -->
            <div class="agent-page__install-dialog-close" @click="showInstallDialog = false">
              <svg width="24" height="24" viewBox="0 0 24 24" fill="none">
                <path d="M18 6L6 18M6 6l12 12" stroke="#6A7282" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
              </svg>
            </div>
            <!-- 标题 -->
            <h3 class="agent-page__install-dialog-title">插件是否已安装成功？</h3>
            <!-- 描述 -->
            <p class="agent-page__install-dialog-desc">智能投递助手需要配合浏览器插件使用<br/>请确认插件已成功安装并启用<br/>否则将无法正常进行自动填写和投递</p>
            <!-- 底部按钮 -->
            <div class="agent-page__install-dialog-btns">
              <button class="agent-page__install-dialog-btn agent-page__install-dialog-btn--outline" @click="showInstallDialog = false">还未安装</button>
              <button class="agent-page__install-dialog-btn agent-page__install-dialog-btn--primary" @click="handleInstallConfirmReload">已安装，继续</button>
            </div>
          </div>
        </div>
      </Teleport>
    </div>
    <!-- 功能说明 — 独立区块，仅第2步显示 -->
    <div v-if="currentStep === 2||currentStep === 3" class="agent-page__step2-faq mrl24 mt16">
      <h3 class="agent-page__step2-faq-title">功能说明</h3>
      <div class="agent-page__step2-faq-item">
        <div class="agent-page__step2-faq-item-title">为什么要选择求职偏好？</div>
        <div class="agent-page__step2-faq-item-desc">城市、岗位、行业和招聘类型会影响岗位推荐范围，系统会优先筛选更符合你目标的投递机会。</div>
      </div>
      <div class="agent-page__step2-faq-item">
        <div class="agent-page__step2-faq-item-title">为什么要选择简历？</div>
        <div class="agent-page__step2-faq-item-desc">AI会基于你选择的简历进行岗位匹配度分析，差距识别和岗位专属优化，帮助提升投递成功率。</div>
      </div>
      <div class="agent-page__step2-faq-item">
        <div class="agent-page__step2-faq-item-title">这些设置会影响后续哪些功能？</div>
        <div class="agent-page__step2-faq-item-desc">会影响推荐岗位、匹配度评分、简历优化方向、自动投递队列和投递进度记录。</div>
      </div>
      <div class="agent-page__step2-faq-item">
        <div class="agent-page__step2-faq-item-title">设置后还能修改吗？</div>
        <div class="agent-page__step2-faq-item-desc">可以随时调整，修改后，系统会根据新的偏好和简历重新推荐结果。</div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, watch, onMounted } from 'vue'
import { useStore } from 'vuex'
import { formatEmploymentType, JOB_TYPE_OPTIONS } from '@/stores/index'
import ProfilePageContent from '@/components/ProfilePageContent.vue'
import ProfileEditDrawer from '@/components/ProfileEditDrawer.vue'
import JobGoalDialog from '@/components/JobGoalDialog.vue'
import RegionSelector from '@/components/tools/RegionSelector.vue'
import JobCategorySelector from '@/components/tools/JobCategorySelector.vue'
import IndustrySelector from '@/components/tools/IndustrySelector.vue'
import AgentBrowserInstallGuide from '@/components/AgentBrowserInstallGuide.vue'
import { saveProfile, fetchProfile } from '@/api/profile'
import {
  fetchResumeList, setDefaultResume, fetchResumeMain, saveResumeMain,
  fetchResumeEducation, fetchResumeWork, fetchResumeInternship, fetchResumeProject, fetchResumeCompetition,
  addResumeEducation, updateResumeEducation, deleteResumeEducation,
  addResumeWork, updateResumeWork, deleteResumeWork,
  addResumeInternship, updateResumeInternship, deleteResumeInternship,
  addResumeProject, updateResumeProject, deleteResumeProject,
  addResumeCompetition, updateResumeCompetition, deleteResumeCompetition,
} from '@/api/resume'
import type { ResumeListItem } from '@/api/resume'
import type { AgentConfig } from '@/api/agent'
import { fetchAgentConfig, saveAgentConfig } from '@/api/agent'

const store = useStore()

/** 组件 Props — 接收父组件传入的初始配置数据 */
const props = defineProps<{
  /** 从接口查询到的初始配置，可能为空 */
  initialConfig?: AgentConfig | null
}>()

/** 组件事件 — complete: 设置完成时传回完整配置数据；launch: 点击启用按钮 */
const emit = defineEmits<{
  (e: 'complete', data: Record<string, any>): void
  (e: 'launch'): void
}>()

// ==================== 步骤导航 ====================
const steps = ['确认个人资料', '配置投递方案', '安装浏览器插件', '开启投递助手']
const currentStep = ref(1)

/** 进入下一步 */
function handleNext() {
  if (currentStep.value < steps.length) currentStep.value++
}

/** 返回上一步 */
function handleBack() {
  if (currentStep.value > 1) {
    currentStep.value--
  }
}

// ==================== 编辑抽屉状态 ====================
const showEditDrawer = ref(false)
const editModule = ref('info')
const editInitialData = ref<Record<string, any>>({})

// ==================== 个人档案数据 ====================

/** 默认简历 ID（从简历列表中确定） */
const defaultResumeId = ref('')

onMounted(async () => {
  // 恢复刷新前保存的步骤号
  const savedStep = sessionStorage.getItem('wizardStep')
  if (savedStep) {
    currentStep.value = Number(savedStep)
    sessionStorage.removeItem('wizardStep')
  }

  // 刷新后检测插件是否在线
  const needCheck = sessionStorage.getItem('wizardCheckPlug')
  if (needCheck) {
    sessionStorage.removeItem('wizardCheckPlug')
    // 立即打开弹窗
    showInstallDialog.value = true
    // 等待心跳检测完成（最多等 3.5 秒）
    await new Promise(resolve => setTimeout(resolve, 3500))
    if (store.state.plugIsOnline) {
      // 插件在线，关闭弹窗并直接进入下一步
      showInstallDialog.value = false
      handleNext()
    } else {
      // 插件离线，弹窗保持展示 + 提示
      ElMessage.warning('未检测到插件安装')
    }
  }

  if (!store.state.regions.length) store.dispatch('loadCommonData')
  store.dispatch('loadJobIntention')
  // 个人信息仍从 /user/profile 获取
  await loadProfile()
  // 其他数据（技能、证书、个人概述、5大经历）从默认简历获取
  await loadDefaultResumeData()
  //加载简历列表
  loadResumeList()
  // 加载投递偏好开关状态
  loadPreferenceSwitches()
})

// ==================== 通信桥接：插件在线状态从 store 全局获取 ====================
/** 插件是否在线（从 vuex 全局状态读取） */
const plugIsOnline = computed(() => store.state.plugIsOnline)

/** 加载个人资料主表 — 仅取 name/email/mobileNumber/regionCode/wechatNumber/portfolioUrl */
async function loadProfile() {
  try {
    const res = await fetchProfile()
    if (res.code === '0' && res.data) {
      const d = res.data
      profile.value.name = d.name || ''
      profile.value.phone = d.mobileNumber || ''
      profile.value.email = d.email || ''
      profile.value.regionCode = d.regionCode || ''
      profile.value.wechat = d.wechatNumber || ''
      profile.value.portfolioUrl = d.portfolioUrl || ''
    }
  } catch { console.error('[AgentSetupWizard] 加载个人资料失败') }
}

/**
 * 确定默认简历并加载简历主表 + 5大经历
 * 规则：优先 isDefault===1 的简历，否则取 createTime 最新的
 */
async function loadDefaultResumeData() {
  try {
    const listRes = await fetchResumeList()
    if (listRes.code !== '0' || !listRes.data || listRes.data.length === 0) {
      console.warn('[AgentSetupWizard] 无简历数据')
      return
    }
    const list = listRes.data
    // 确定默认简历
    let target = list.find(r => r.isDefault === 1)
    if (!target) {
      // 按 createTime 降序取第一个
      target = [...list].sort((a, b) => (b.createTime || 0) - (a.createTime || 0))[0]
    }
    if (!target || !target.id) return
    defaultResumeId.value = target.id

    // 并发加载简历主表 + 5大经历
    const [mainRes, eduRes, workRes, internRes, projRes, compRes] = await Promise.all([
      fetchResumeMain(defaultResumeId.value),
      fetchResumeEducation(defaultResumeId.value),
      fetchResumeWork(defaultResumeId.value),
      fetchResumeInternship(defaultResumeId.value),
      fetchResumeProject(defaultResumeId.value),
      fetchResumeCompetition(defaultResumeId.value),
    ])

    // 从简历主表获取技能、证书、个人概述
    if (mainRes.code === '0' && mainRes.data) {
      profile.value.skills = mainRes.data.skills || []
      profile.value.certificates = mainRes.data.certificates || []
      profile.value.summary = mainRes.data.summary || ''
    }
    // 教育经历
    if (eduRes.code === '0' && eduRes.data) {
      profile.value.education = eduRes.data.map(item => ({
        id: item.id || '',
        school: item.school || '', major: item.major || '',
        studyType: item.studyType || '',
        degree: item.degree || '',
        startDate: item.startDate || '', endDate: item.endDate || '',
        description: (item.description || []).map(d => ({ id: d.id || '', text: d.text || '' })),
      }))
    }
    // 工作经历
    if (workRes.code === '0' && workRes.data) {
      profile.value.works = workRes.data.map(item => ({
        id: item.id || '',
        companyName: item.companyName || '', position: item.position || '',
        startDate: item.startDate || '', endDate: item.endDate || '',
        description: (item.description || []).map(d => ({ id: d.id || '', text: d.text || '' })),
      }))
    }
    // 实习经历
    if (internRes.code === '0' && internRes.data) {
      profile.value.internships = internRes.data.map(item => ({
        id: item.id || '',
        companyName: item.companyName || '', position: item.position || '',
        startDate: item.startDate || '', endDate: item.endDate || '',
        description: (item.description || []).map(d => ({ id: d.id || '', text: d.text || '' })),
      }))
    }
    // 项目经历
    if (projRes.code === '0' && projRes.data) {
      profile.value.projects = projRes.data.map(item => ({
        id: item.id || '',
        projectName: item.projectName || '', companyName: item.companyName || '',
        role: item.role || '', startDate: item.startDate || '', endDate: item.endDate || '',
        description: (item.description || []).map(d => ({ id: d.id || '', text: d.text || '' })),
      }))
    }
    // 竞赛经历
    if (compRes.code === '0' && compRes.data) {
      profile.value.competitions = compRes.data.map(item => ({
        id: item.id || '',
        competitionName: item.competitionName || '', award: item.award || '',
        awardDate: item.awardDate || '',
        description: (item.description || []).map(d => ({ id: d.id || '', text: d.text || '' })),
      }))
    }
  } catch { console.error('[AgentSetupWizard] 加载默认简历数据失败') }
}

const profile = ref({
  name: '', phone: '', email: '', idNumber: '', regionCode: '', portfolioUrl: '', wechat: '',
  skills: [] as string[], certificates: [] as string[], summary: '',
  education: [] as Array<{ id: string; school: string; major: string; studyType: string | number; degree: string | number; startDate: string; endDate: string; description: Array<{ id: string; text: string }> }>,
  works: [] as Array<{ id: string; companyName: string; position: string; startDate: string; endDate: string; description: Array<{ id: string; text: string }> }>,
  internships: [] as Array<{ id: string; companyName: string; position: string; startDate: string; endDate: string; description: Array<{ id: string; text: string }> }>,
  projects: [] as Array<{ id: string; projectName: string; companyName: string; role: string; startDate: string; endDate: string; description: Array<{ id: string; text: string }> }>,
  competitions: [] as Array<{ id: string; competitionName: string; award: string; awardDate: string; description: Array<{ id: string; text: string }> }>,
})

// ==================== 编辑抽屉事件处理 ====================
function handleEdit(section: string) {
  editModule.value = section
  if (section === 'info') { editInitialData.value = { name: profile.value.name, email: profile.value.email, phone: profile.value.phone, location: profile.value.regionCode, wechat: profile.value.wechat } }
  else if (section === 'education') {
    // 记录当前教育经历ID快照，用于保存时做diff
    editSnapshotEducationIds.value = profile.value.education.map(e => e.id).filter(Boolean)
    editInitialData.value = { education: profile.value.education.map(edu => ({ ...edu, description: edu.description.map(d => ({ ...d })) })) }
  }
  else if (section === 'work') {
    editSnapshotWorkIds.value = profile.value.works.map(w => w.id).filter(Boolean)
    editInitialData.value = { works: profile.value.works.map(exp => ({ ...exp, description: exp.description.map(d => ({ ...d })) })) }
  }
  else if (section === 'internship') {
    editSnapshotInternshipIds.value = profile.value.internships.map(i => i.id).filter(Boolean)
    editInitialData.value = { internships: (profile.value.internships || []).map(exp => ({ ...exp, description: exp.description.map(d => ({ ...d })) })) }
  }
  else if (section === 'project') {
    editSnapshotProjectIds.value = profile.value.projects.map(p => p.id).filter(Boolean)
    editInitialData.value = { projects: (profile.value.projects || []).map(proj => ({ ...proj, description: proj.description.map(d => ({ ...d })) })) }
  }
  else if (section === 'competition') {
    editSnapshotCompetitionIds.value = profile.value.competitions.map(c => c.id).filter(Boolean)
    editInitialData.value = { competitions: profile.value.competitions.map(comp => ({ ...comp, description: comp.description.map(d => ({ ...d })) })) }
  }
  else if (section === 'portfolio') { editInitialData.value = { portfolioUrl: profile.value.portfolioUrl } }
  else if (section === 'summary') { editInitialData.value = { summary: profile.value.summary || '' } }
  else if (section === 'skills') { editInitialData.value = { skills: [...profile.value.skills] } }
  else if (section === 'certificate') { editInitialData.value = { certificates: [...(profile.value.certificates || [])] } }
  else { editInitialData.value = {} }
  showEditDrawer.value = true
}

/** 保存中的加载状态 */
const saving = ref(false)

/** 保存编辑数据 — 个人信息走 profile 接口，其余走简历接口 */
async function handleSaveEdit(data: Record<string, any>) {
  if (editModule.value === 'info') {
    // 个人信息 — 保存到 /user/profile
    try {
      saving.value = true
      await saveProfile({ name: data.name, email: data.email, mobileNumber: data.phone, regionCode: data.location, wechatNumber: data.wechat })
      profile.value.name = data.name; profile.value.email = data.email
      profile.value.phone = data.phone; profile.value.wechat = data.wechat
      profile.value.regionCode = data.location || ''
      ElMessage.success('个人信息保存成功')
    } catch { ElMessage.error('个人信息保存失败，请重试') } finally { saving.value = false }
  } else if (editModule.value === 'education') {
    // 教育经历 — diff增删改（简历接口）
    if (!defaultResumeId.value) { ElMessage.error('未找到默认简历'); return }
    try {
      saving.value = true
      const oldIds = new Set(editSnapshotEducationIds.value)
      const newItems: any[] = data.education
      const newIds = new Set(newItems.filter((e: any) => e.id).map((e: any) => e.id))
      const deletedIds = editSnapshotEducationIds.value.filter(id => !newIds.has(id))
      const addPromises: Promise<any>[] = []
      const updatePromises: Promise<any>[] = []
      const deletePromises = deletedIds.map(id => deleteResumeEducation(id))
      for (const edu of newItems) {
        const itemData = { school: edu.school, major: edu.major, degree: edu.degree, studyType: edu.studyType, startDate: edu.startDate, endDate: edu.endDate, description: edu.description }
        if (edu.id && oldIds.has(edu.id)) {
          updatePromises.push(updateResumeEducation({ ...itemData, id: edu.id }))
        } else {
          addPromises.push(addResumeEducation(defaultResumeId.value, itemData))
        }
      }
      await Promise.all([...addPromises, ...updatePromises, ...deletePromises])
      // 重新加载教育经历以获取最新ID
      const eduRes = await fetchResumeEducation(defaultResumeId.value)
      if (eduRes.code === '0' && eduRes.data) {
        profile.value.education = eduRes.data.map(item => ({ id: item.id || '', school: item.school || '', major: item.major || '', studyType: item.studyType || '', degree: item.degree || '', startDate: item.startDate || '', endDate: item.endDate || '', description: (item.description || []).map(d => ({ id: d.id || '', text: d.text || '' })) }))
      }
      ElMessage.success('教育经历保存成功')
    } catch { ElMessage.error('教育经历保存失败，请重试') } finally { saving.value = false }
  } else if (editModule.value === 'work') {
    // 工作经历 — diff增删改（简历接口）
    if (!defaultResumeId.value) { ElMessage.error('未找到默认简历'); return }
    try {
      saving.value = true
      const oldIds = new Set(editSnapshotWorkIds.value)
      const newItems: any[] = data.works
      const newIds = new Set(newItems.filter((w: any) => w.id).map((w: any) => w.id))
      const deletedIds = editSnapshotWorkIds.value.filter(id => !newIds.has(id))
      const addPromises: Promise<any>[] = []
      const updatePromises: Promise<any>[] = []
      const deletePromises = deletedIds.map(id => deleteResumeWork(id))
      for (const w of newItems) {
        const itemData = { companyName: w.companyName, position: w.position, startDate: w.startDate, endDate: w.endDate || '', description: w.description }
        if (w.id && oldIds.has(w.id)) {
          updatePromises.push(updateResumeWork({ ...itemData, id: w.id }))
        } else {
          addPromises.push(addResumeWork(defaultResumeId.value, itemData))
        }
      }
      await Promise.all([...addPromises, ...updatePromises, ...deletePromises])
      const workRes = await fetchResumeWork(defaultResumeId.value)
      if (workRes.code === '0' && workRes.data) {
        profile.value.works = workRes.data.map(item => ({ id: item.id || '', companyName: item.companyName || '', position: item.position || '', startDate: item.startDate || '', endDate: item.endDate || '', description: (item.description || []).map(d => ({ id: d.id || '', text: d.text || '' })) }))
      }
      ElMessage.success('工作经历保存成功')
    } catch { ElMessage.error('工作经历保存失败，请重试') } finally { saving.value = false }
  } else if (editModule.value === 'internship') {
    // 实习经历 — diff增删改（简历接口）
    if (!defaultResumeId.value) { ElMessage.error('未找到默认简历'); return }
    try {
      saving.value = true
      const oldIds = new Set(editSnapshotInternshipIds.value)
      const newItems: any[] = data.internships
      const newIds = new Set(newItems.filter((i: any) => i.id).map((i: any) => i.id))
      const deletedIds = editSnapshotInternshipIds.value.filter(id => !newIds.has(id))
      const addPromises: Promise<any>[] = []
      const updatePromises: Promise<any>[] = []
      const deletePromises = deletedIds.map(id => deleteResumeInternship(id))
      for (const i of newItems) {
        const itemData = { companyName: i.companyName, position: i.position, startDate: i.startDate, endDate: i.endDate || '', description: i.description }
        if (i.id && oldIds.has(i.id)) {
          updatePromises.push(updateResumeInternship({ ...itemData, id: i.id }))
        } else {
          addPromises.push(addResumeInternship(defaultResumeId.value, itemData))
        }
      }
      await Promise.all([...addPromises, ...updatePromises, ...deletePromises])
      const internRes = await fetchResumeInternship(defaultResumeId.value)
      if (internRes.code === '0' && internRes.data) {
        profile.value.internships = internRes.data.map(item => ({ id: item.id || '', companyName: item.companyName || '', position: item.position || '', startDate: item.startDate || '', endDate: item.endDate || '', description: (item.description || []).map(d => ({ id: d.id || '', text: d.text || '' })) }))
      }
      ElMessage.success('实习经历保存成功')
    } catch { ElMessage.error('实习经历保存失败，请重试') } finally { saving.value = false }
  } else if (editModule.value === 'project') {
    // 项目经历 — diff增删改（简历接口）
    if (!defaultResumeId.value) { ElMessage.error('未找到默认简历'); return }
    try {
      saving.value = true
      const oldIds = new Set(editSnapshotProjectIds.value)
      const newItems: any[] = data.projects
      const newIds = new Set(newItems.filter((p: any) => p.id).map((p: any) => p.id))
      const deletedIds = editSnapshotProjectIds.value.filter(id => !newIds.has(id))
      const addPromises: Promise<any>[] = []
      const updatePromises: Promise<any>[] = []
      const deletePromises = deletedIds.map(id => deleteResumeProject(id))
      for (const p of newItems) {
        const itemData = { projectName: p.projectName, companyName: p.companyName || '', role: p.role || '', startDate: p.startDate, endDate: p.endDate || '', description: p.description }
        if (p.id && oldIds.has(p.id)) {
          updatePromises.push(updateResumeProject({ ...itemData, id: p.id }))
        } else {
          addPromises.push(addResumeProject(defaultResumeId.value, itemData))
        }
      }
      await Promise.all([...addPromises, ...updatePromises, ...deletePromises])
      const projRes = await fetchResumeProject(defaultResumeId.value)
      if (projRes.code === '0' && projRes.data) {
        profile.value.projects = projRes.data.map(item => ({ id: item.id || '', projectName: item.projectName || '', companyName: item.companyName || '', role: item.role || '', startDate: item.startDate || '', endDate: item.endDate || '', description: (item.description || []).map(d => ({ id: d.id || '', text: d.text || '' })) }))
      }
      ElMessage.success('项目经历保存成功')
    } catch { ElMessage.error('项目经历保存失败，请重试') } finally { saving.value = false }
  } else if (editModule.value === 'competition') {
    // 竞赛经历 — diff增删改（简历接口）
    if (!defaultResumeId.value) { ElMessage.error('未找到默认简历'); return }
    try {
      saving.value = true
      const oldIds = new Set(editSnapshotCompetitionIds.value)
      const newItems: any[] = data.competitions
      const newIds = new Set(newItems.filter((c: any) => c.id).map((c: any) => c.id))
      const deletedIds = editSnapshotCompetitionIds.value.filter(id => !newIds.has(id))
      const addPromises: Promise<any>[] = []
      const updatePromises: Promise<any>[] = []
      const deletePromises = deletedIds.map(id => deleteResumeCompetition(id))
      for (const c of newItems) {
        const itemData = { competitionName: c.competitionName, award: c.award || '', awardDate: c.awardDate || '', description: c.description }
        if (c.id && oldIds.has(c.id)) {
          updatePromises.push(updateResumeCompetition({ ...itemData, id: c.id }))
        } else {
          addPromises.push(addResumeCompetition(defaultResumeId.value, itemData))
        }
      }
      await Promise.all([...addPromises, ...updatePromises, ...deletePromises])
      const compRes = await fetchResumeCompetition(defaultResumeId.value)
      if (compRes.code === '0' && compRes.data) {
        profile.value.competitions = compRes.data.map(item => ({ id: item.id || '', competitionName: item.competitionName || '', award: item.award || '', awardDate: item.awardDate || '', description: (item.description || []).map(d => ({ id: d.id || '', text: d.text || '' })) }))
      }
      ElMessage.success('竞赛经历保存成功')
    } catch { ElMessage.error('竞赛经历保存失败，请重试') } finally { saving.value = false }
  } else if (editModule.value === 'summary') {
    // 个人概述 — 保存到简历主表
    if (!defaultResumeId.value) { ElMessage.error('未找到默认简历'); return }
    try {
      saving.value = true
      await saveResumeMain({ resumeId: defaultResumeId.value, skills: profile.value.skills, certificates: profile.value.certificates, summary: data.summary })
      profile.value.summary = data.summary
      ElMessage.success('个人概述保存成功')
    } catch { ElMessage.error('个人概述保存失败，请重试') } finally { saving.value = false }
  } else if (editModule.value === 'portfolio') {
    // 作品集链接 — 保存到 /user/profile
    try {
      saving.value = true
      await saveProfile({ name: profile.value.name, email: profile.value.email, mobileNumber: profile.value.phone, regionCode: profile.value.regionCode, wechatNumber: profile.value.wechat, portfolioUrl: data.portfolioUrl })
      profile.value.portfolioUrl = data.portfolioUrl
      ElMessage.success('作品集保存成功')
    } catch { ElMessage.error('作品集保存失败，请重试') } finally { saving.value = false }
  } else if (editModule.value === 'skills') {
    // 技能 — 保存到简历主表
    if (!defaultResumeId.value) { ElMessage.error('未找到默认简历'); return }
    try {
      saving.value = true
      await saveResumeMain({ resumeId: defaultResumeId.value, skills: [...data.skills], certificates: profile.value.certificates, summary: profile.value.summary })
      profile.value.skills = [...data.skills]
      ElMessage.success('技能保存成功')
    } catch { ElMessage.error('技能保存失败，请重试') } finally { saving.value = false }
  } else if (editModule.value === 'certificate') {
    // 证书 — 保存到简历主表
    if (!defaultResumeId.value) { ElMessage.error('未找到默认简历'); return }
    try {
      saving.value = true
      await saveResumeMain({ resumeId: defaultResumeId.value, skills: profile.value.skills, certificates: [...data.certificates], summary: profile.value.summary })
      profile.value.certificates = [...data.certificates]
      ElMessage.success('证书保存成功')
    } catch { ElMessage.error('证书保存失败，请重试') } finally { saving.value = false }
  }
}

/** 编辑抽屉打开时的经历ID快照 — 用于保存时做增删改diff */
const editSnapshotEducationIds = ref<string[]>([])
const editSnapshotWorkIds = ref<string[]>([])
const editSnapshotInternshipIds = ref<string[]>([])
const editSnapshotProjectIds = ref<string[]>([])
const editSnapshotCompetitionIds = ref<string[]>([])

/** 内联保存处理（个人信息/技能/证书模块从组件内直接保存） */
async function handleInlineSave(section: string, data: Record<string, any>) {
  editModule.value = section
  await handleSaveEdit(data)
}

/** 单条经历更新 — 调用简历单条编辑接口 */
async function handleExpUpdate(moduleType: string, index: number, data: Record<string, any>) {
  if (!defaultResumeId.value) { ElMessage.error('未找到默认简历'); return }
  try {
    const record = (profile.value as any)[moduleType === 'work' ? 'works' : moduleType === 'internship' ? 'internships' : moduleType === 'project' ? 'projects' : moduleType === 'competition' ? 'competitions' : 'education'][index]
    const recordId = record?.id
    if (moduleType === 'education') {
      await updateResumeEducation({ id: recordId, school: data.school, major: data.major, degree: data.degree, studyType: data.studyType, startDate: data.startDate, endDate: data.endDate, description: data.description })
      profile.value.education[index] = { id: recordId, ...data } as any
    } else if (moduleType === 'work') {
      await updateResumeWork({ id: recordId, companyName: data.companyName, position: data.position, startDate: data.startDate, endDate: data.endDate, description: data.description })
      profile.value.works[index] = { id: recordId, ...data } as any
    } else if (moduleType === 'internship') {
      await updateResumeInternship({ id: recordId, companyName: data.companyName, position: data.position, startDate: data.startDate, endDate: data.endDate, description: data.description })
      profile.value.internships[index] = { id: recordId, ...data } as any
    } else if (moduleType === 'project') {
      await updateResumeProject({ id: recordId, projectName: data.projectName, companyName: data.companyName, role: data.role, startDate: data.startDate, endDate: data.endDate, description: data.description })
      profile.value.projects[index] = { id: recordId, ...data } as any
    } else if (moduleType === 'competition') {
      await updateResumeCompetition({ id: recordId, competitionName: data.competitionName, award: data.award, awardDate: data.awardDate, description: data.description })
      profile.value.competitions[index] = { id: recordId, ...data } as any
    }
    ElMessage.success('保存成功')
  } catch {
    ElMessage.error('保存失败')
  }
}

/** 新增经历 — 调用简历单条添加接口 */
async function handleExpAdd(moduleType: string, data: Record<string, any>) {
  if (!defaultResumeId.value) { ElMessage.error('未找到默认简历'); return }
  try {
    let newId = ''
    if (moduleType === 'education') {
      const res = await addResumeEducation(defaultResumeId.value, { school: data.school, major: data.major, degree: data.degree, studyType: data.studyType, startDate: data.startDate, endDate: data.endDate, description: data.description })
      newId = res.data || ''
      profile.value.education.push({ id: newId, ...data } as any)
    } else if (moduleType === 'work') {
      const res = await addResumeWork(defaultResumeId.value, { companyName: data.companyName, position: data.position, startDate: data.startDate, endDate: data.endDate, description: data.description })
      newId = res.data || ''
      profile.value.works.push({ id: newId, ...data } as any)
    } else if (moduleType === 'internship') {
      const res = await addResumeInternship(defaultResumeId.value, { companyName: data.companyName, position: data.position, startDate: data.startDate, endDate: data.endDate, description: data.description })
      newId = res.data || ''
      profile.value.internships.push({ id: newId, ...data } as any)
    } else if (moduleType === 'project') {
      const res = await addResumeProject(defaultResumeId.value, { projectName: data.projectName, companyName: data.companyName, role: data.role, startDate: data.startDate, endDate: data.endDate, description: data.description })
      newId = res.data || ''
      profile.value.projects.push({ id: newId, ...data } as any)
    } else if (moduleType === 'competition') {
      const res = await addResumeCompetition(defaultResumeId.value, { competitionName: data.competitionName, award: data.award, awardDate: data.awardDate, description: data.description })
      newId = res.data || ''
      profile.value.competitions.push({ id: newId, ...data } as any)
    }
    ElMessage.success('添加成功')
  } catch {
    ElMessage.error('添加失败')
  }
}

/** 删除单条经历 — 调用简历单条删除接口 */
async function handleExpDelete(moduleType: string, index: number) {
  try {
    await ElMessageBox.confirm('确定要删除这条经历吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    })
  } catch {
    return
  }
  if (!defaultResumeId.value) { ElMessage.error('未找到默认简历'); return }
  try {
    if (moduleType === 'education') {
      const id = profile.value.education[index]?.id
      if (id) await deleteResumeEducation(id)
      profile.value.education.splice(index, 1)
    } else if (moduleType === 'work') {
      const id = profile.value.works[index]?.id
      if (id) await deleteResumeWork(id)
      profile.value.works.splice(index, 1)
    } else if (moduleType === 'internship') {
      const id = profile.value.internships[index]?.id
      if (id) await deleteResumeInternship(id)
      profile.value.internships.splice(index, 1)
    } else if (moduleType === 'project') {
      const id = profile.value.projects[index]?.id
      if (id) await deleteResumeProject(id)
      profile.value.projects.splice(index, 1)
    } else if (moduleType === 'competition') {
      const id = profile.value.competitions[index]?.id
      if (id) await deleteResumeCompetition(id)
      profile.value.competitions.splice(index, 1)
    }
    ElMessage.success('删除成功')
  } catch {
    ElMessage.error('删除失败')
  }
}

// ==================== 第2步：确认目标 ====================
const showJobGoalDialog = ref(false)
const intentionEmploymentLabel = computed(() => formatEmploymentType(store.state.jobIntention.recruitCategory))

/** 招聘分类选项 */
const jobTypeOptions = JOB_TYPE_OPTIONS
/** 招聘分类下拉是否展开 */
const showJobTypeDropdown = ref(false)
/** 简历下拉是否展开 */
const showResumeDropdown = ref(false)

/** 当前选中简历名称 */
const selectedResumeName = computed(() => {
  const item = resumeList.value.find(r => r.id === selectedResumeId.value)
  return item ? item.resumeName : ''
})

/** 选中简历 */
function selectResume(r: { id?: string; resumeName?: string }) {
  selectedResumeId.value = r.id || ''
  showResumeDropdown.value = false

  // 个人信息仍从 /user/profile 获取
  loadProfile()
  // 其他数据（技能、证书、个人概述、5大经历）从默认简历获取
  loadDefaultResumeData()
}

/** 选中招聘分类 */
function selectJobType(option: { label: string; value: number }) {
  showJobTypeDropdown.value = false
  store.dispatch('saveJobIntention', {
    ...store.state.jobIntention,
    recruitCategory: option.value,
  })
}

/** 第2步：求职偏好筛选组件 — 读写 store.jobIntention */
const step2RegionCodes = computed<string[]>(() => store.state.jobIntention.regionCodes || [])
const step2CategoryIds = computed<number[]>(() => store.state.jobIntention.categoryIds || [])
const step2IndustryIds = computed<number[]>(() => store.state.jobIntention.industryIds || [])

/** 第2步：筛选组件统一触发按钮样式 — 大圆角胶囊按钮 */
const prefFilterTriggerStyle = {
  padding: '0.12rem 0.16rem',
  borderRadius: '0.12rem',
  fontSize: '0.14rem',
  background: '#F3F4F6',
  border: 'none',
  minWidth: '0.9rem',
  justifyContent: 'center',
  height: '0.43rem',
  boxSizing: 'border-box',
}
/** 第2步：筛选组件显示文字样式 */
const prefFilterDisplayStyle = {
  maxWidth: 'none',
  color: '#6A7282',
}

/** 第2步：地区选择变更 */
function onStep2RegionChange(codes: string[]) {
  store.dispatch('saveJobIntention', { ...store.state.jobIntention, regionCodes: codes })
}

/** 第2步：岗位选择变更 */
function onStep2CategoryChange(ids: number[]) {
  store.dispatch('saveJobIntention', { ...store.state.jobIntention, categoryIds: ids })
}

/** 第2步：行业选择变更 */
function onStep2IndustryChange(ids: number[]) {
  store.dispatch('saveJobIntention', { ...store.state.jobIntention, industryIds: ids })
}

/** 第2步：简历列表 */
const resumeList = ref<ResumeListItem[]>([])
/** 选中的简历 ID */
const selectedResumeId = ref('')
/** 自动优化简历开关 */
const autoOptimizeSwitch = ref(true)
/** 岗位匹配分析开关 */
const autoMatchAnalysisSwitch = ref(true)

/** 切换简历针对性优化开关 */
function toggleOptimize() {
  autoOptimizeSwitch.value = !autoOptimizeSwitch.value
  savePreferenceSwitches()
}

/** 切换深度人岗匹配分析开关 */
function toggleMatchAnalysis() {
  autoMatchAnalysisSwitch.value = !autoMatchAnalysisSwitch.value
  savePreferenceSwitches()
}

/** 保存投递偏好开关到接口 */
async function savePreferenceSwitches() {
  try {
    await saveAgentConfig({
      autoOptimizeResume: autoOptimizeSwitch.value ? 1 : 0,
      acceptDeptTransfer: autoMatchAnalysisSwitch.value ? '是' : '否',
    })
  } catch { /* 静默 */ }
}

/** 加载投递偏好开关状态 */
async function loadPreferenceSwitches() {
  try {
    const res = await fetchAgentConfig()
    if (res.code === '0' && res.data) {
      if (res.data.autoOptimizeResume !== undefined) autoOptimizeSwitch.value = res.data.autoOptimizeResume === 1
      autoMatchAnalysisSwitch.value = res.data.acceptDeptTransfer === '是'
    }
  } catch { /* 静默 */ }
}

/** 加载简历列表 */
async function loadResumeList() {
  try {
    const res = await fetchResumeList()
    if (res.code === '0' && res.data) {
      resumeList.value = res.data
      let defaultResume = res.data.find(r => r.isDefault === 1)
      if(!defaultResume){
        // 按 createTime 降序取第一个
        defaultResume = [...resumeList.value].sort((a, b) => (b.createTime || 0) - (a.createTime || 0))[0]
      }
      if (defaultResume && defaultResume.id) {
        selectedResumeId.value = defaultResume.id
      } else if (res.data.length > 0 && res.data[0].id) {
        selectedResumeId.value = res.data[0].id
      }
    }
  } catch { console.error('[AgentSetupWizard] 加载简历列表失败') }
}

/** 进入第2步时加载简历列表 */
// watch(currentStep, (val) => { if (val === 1) loadResumeList() })

/** 选择简历后设为默认简历 */
watch(selectedResumeId, async (newId, oldId) => {
  // 仅在用户主动切换时调用接口（非初始化加载）
  if (!newId || !oldId) return
  try {
    await setDefaultResume(newId)
  } catch { console.error('[AgentSetupWizard] 设置默认简历失败') }
})

// ==================== 第3步：安装插件 ====================

/** 浏览器插件安装指引组件引用 */
const browserInstallGuideRef = ref<InstanceType<typeof AgentBrowserInstallGuide> | null>(null)

/** 插件安装确认弹窗 */
const showInstallDialog = ref(false)

/** 点击"下载插件"：下载插件文件 + 弹出安装确认弹窗 */
function handleInstallConfirm() {
  // 触发下载插件文件
  browserInstallGuideRef.value?.downloadExtension()
  // 显示安装确认弹窗
  showInstallDialog.value = true
}

/** 点击"已安装，继续"：保存步骤 + 标记需要检测 + 刷新页面 */
function handleInstallConfirmReload() {
  sessionStorage.setItem('wizardStep', String(currentStep.value))
  sessionStorage.setItem('wizardCheckPlug', '1')
  location.reload()
}

// ==================== 第4步：配置求职助手 ====================

/** 启用求职助手 — 收集第2步数据，通过 emit 传回父组件并触发 launch */
function handleLaunchAgent() {
  const allSettings = {
    jobType: store.state.jobIntention.recruitCategory === 2 ? 1 : 2,
    agentMode: 1,
    weeklyTarget: 2,
    autoOptimizeResume: autoOptimizeSwitch.value ? 1 : 0,
    autoMatchAnalysis: autoMatchAnalysisSwitch.value ? 1 : 0,
    defaultResumeId: selectedResumeId.value,
    status: 1,
  }
  emit('complete', allSettings)
  emit('launch')
}
</script>

<style lang="scss">
@use '../assets/styles/pages/agent';
</style>
