<template>
  <!-- 投递进度卡片组件（新版4步骤） -->
  <div class="agent-apply-progress" :class="{ 'agent-apply-progress--completed': isCompleted }">
    <!-- 顶部：岗位信息 + 步骤条 -->
    <div class="agent-apply-progress__top">
      <!-- 左侧岗位名称和公司 -->
      <div class="agent-apply-progress__job-info">
        <span class="agent-apply-progress__job-title">{{ jobInfo?.title || '' }}</span>
        <div class="agent-apply-progress__company">
          <img
            v-if="(jobInfo as any)?.companyLogoUrl"
            :src="(jobInfo as any).companyLogoUrl"
            class="agent-apply-progress__company-logo"
            alt="logo"
          />
          <div v-else class="agent-apply-progress__company-logo agent-apply-progress__company-logo--placeholder">
            <svg viewBox="0 0 24 24" fill="none"><rect x="3" y="3" width="7" height="7" rx="1" stroke="currentColor" stroke-width="1.2"/><rect x="14" y="3" width="7" height="7" rx="1" stroke="currentColor" stroke-width="1.2"/><rect x="3" y="14" width="7" height="7" rx="1" stroke="currentColor" stroke-width="1.2"/><rect x="14" y="14" width="7" height="7" rx="1" stroke="currentColor" stroke-width="1.2"/></svg>
          </div>
          <span class="agent-apply-progress__company-name">{{ jobInfo?.companyShortName || jobInfo?.companyName || '' }}</span>
        </div>
      </div>

      <!-- 右侧步骤条 -->
      <div class="agent-apply-progress__steps">
        <!-- 第1步：优化简历 -->
        <div class="agent-apply-progress__step-item">
          <div class="agent-apply-progress__step-icon" :class="stepIconClass(1)">
            <!-- 已完成图标 -->
            <svg v-if="currentStep > 1" width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg">
              <g clip-path="url(#clip0_517_2052)">
                <path fill-rule="evenodd" clip-rule="evenodd" d="M11.875 2.08333C11.875 2.02808 11.8531 1.97509 11.814 1.93602C11.7749 1.89695 11.7219 1.875 11.6667 1.875H5.83335C5.22557 1.875 4.64267 2.11644 4.2129 2.54621C3.78313 2.97598 3.54169 3.55888 3.54169 4.16667V15.8333C3.54169 16.4411 3.78313 17.024 4.2129 17.4538C4.64267 17.8836 5.22557 18.125 5.83335 18.125H14.1667C14.7745 18.125 15.3574 17.8836 15.7871 17.4538C16.2169 17.024 16.4584 16.4411 16.4584 15.8333V7.6225C16.4584 7.56725 16.4364 7.51426 16.3973 7.47519C16.3583 7.43612 16.3053 7.41417 16.25 7.41417H12.5C12.3343 7.41417 12.1753 7.34832 12.0581 7.23111C11.9409 7.1139 11.875 6.95493 11.875 6.78917V2.08333ZM12.5 10.2083C12.6658 10.2083 12.8248 10.2742 12.942 10.3914C13.0592 10.5086 13.125 10.6676 13.125 10.8333C13.125 10.9991 13.0592 11.1581 12.942 11.2753C12.8248 11.3925 12.6658 11.4583 12.5 11.4583H7.50002C7.33426 11.4583 7.17529 11.3925 7.05808 11.2753C6.94087 11.1581 6.87502 10.9991 6.87502 10.8333C6.87502 10.6676 6.94087 10.5086 7.05808 10.3914C7.17529 10.2742 7.33426 10.2083 7.50002 10.2083H12.5ZM12.5 13.5417C12.6658 13.5417 12.8248 13.6075 12.942 13.7247C13.0592 13.8419 13.125 14.0009 13.125 14.1667C13.125 14.3324 13.0592 14.4914 12.942 14.6086C12.8248 14.7258 12.6658 14.7917 12.5 14.7917H7.50002C7.33426 14.7917 7.17529 14.7258 7.05808 14.6086C6.94087 14.4914 6.87502 14.3324 6.87502 14.1667C6.87502 14.0009 6.94087 13.8419 7.05808 13.7247C7.17529 13.6075 7.33426 13.5417 7.50002 13.5417H12.5Z" fill="white"/>
                <path d="M13.125 2.35322C13.125 2.19988 13.2858 2.10238 13.405 2.19822C13.5061 2.27988 13.5958 2.37488 13.6742 2.48322L16.185 5.98072C16.2417 6.06072 16.18 6.16405 16.0817 6.16405H13.3333C13.2781 6.16405 13.2251 6.1421 13.186 6.10303C13.1469 6.06396 13.125 6.01097 13.125 5.95572V2.35322Z" fill="white"/>
              </g>
              <defs>
                <clipPath id="clip0_517_2052">
                  <rect width="20" height="20" fill="white"/>
                </clipPath>
              </defs>
            </svg>
            <!-- 进行中/待处理图标：简历 -->
            <svg v-else  width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg">
              <g clip-path="url(#clip0_481_4042)">
                <path fill-rule="evenodd" clip-rule="evenodd" d="M11.875 2.08333C11.875 2.02808 11.8531 1.97509 11.814 1.93602C11.7749 1.89695 11.7219 1.875 11.6667 1.875H5.83335C5.22557 1.875 4.64267 2.11644 4.2129 2.54621C3.78313 2.97598 3.54169 3.55888 3.54169 4.16667V15.8333C3.54169 16.4411 3.78313 17.024 4.2129 17.4538C4.64267 17.8836 5.22557 18.125 5.83335 18.125H14.1667C14.7745 18.125 15.3574 17.8836 15.7871 17.4538C16.2169 17.024 16.4584 16.4411 16.4584 15.8333V7.6225C16.4584 7.56725 16.4364 7.51426 16.3973 7.47519C16.3583 7.43612 16.3053 7.41417 16.25 7.41417H12.5C12.3343 7.41417 12.1753 7.34832 12.0581 7.23111C11.9409 7.1139 11.875 6.95493 11.875 6.78917V2.08333ZM12.5 10.2083C12.6658 10.2083 12.8248 10.2742 12.942 10.3914C13.0592 10.5086 13.125 10.6676 13.125 10.8333C13.125 10.9991 13.0592 11.1581 12.942 11.2753C12.8248 11.3925 12.6658 11.4583 12.5 11.4583H7.50002C7.33426 11.4583 7.17529 11.3925 7.05808 11.2753C6.94087 11.1581 6.87502 10.9991 6.87502 10.8333C6.87502 10.6676 6.94087 10.5086 7.05808 10.3914C7.17529 10.2742 7.33426 10.2083 7.50002 10.2083H12.5ZM12.5 13.5417C12.6658 13.5417 12.8248 13.6075 12.942 13.7247C13.0592 13.8419 13.125 14.0009 13.125 14.1667C13.125 14.3324 13.0592 14.4914 12.942 14.6086C12.8248 14.7258 12.6658 14.7917 12.5 14.7917H7.50002C7.33426 14.7917 7.17529 14.7258 7.05808 14.6086C6.94087 14.4914 6.87502 14.3324 6.87502 14.1667C6.87502 14.0009 6.94087 13.8419 7.05808 13.7247C7.17529 13.6075 7.33426 13.5417 7.50002 13.5417H12.5Z" fill="currentColor"/>
                <path d="M13.125 2.35322C13.125 2.19988 13.2858 2.10238 13.405 2.19822C13.5061 2.27988 13.5958 2.37488 13.6742 2.48322L16.185 5.98072C16.2417 6.06072 16.18 6.16405 16.0817 6.16405H13.3333C13.2781 6.16405 13.2251 6.1421 13.186 6.10303C13.1469 6.06396 13.125 6.01097 13.125 5.95572V2.35322Z" fill="currentColor"/>
              </g>
              <defs>
                <clipPath id="clip0_481_4042">
                  <rect width="20" height="20" fill="white"/>
                </clipPath>
              </defs>
            </svg>
          </div>
          <div class="agent-apply-progress__step-text">
            <span class="agent-apply-progress__step-name" :class="stepNameClass(1)">优化简历</span>
            <span class="agent-apply-progress__step-status" :class="stepStatusClass(1)">{{ stepStatusText(1) }}</span>
          </div>
        </div>

        <!-- 连接线 -->
        <div class="agent-apply-progress__step-line"></div>

        <!-- 第2步：匹配分析 -->
        <div class="agent-apply-progress__step-item">
          <div class="agent-apply-progress__step-icon" :class="stepIconClass(2)">
            <svg v-if="currentStep > 2" width="17" height="17" viewBox="0 0 17 17" fill="none" xmlns="http://www.w3.org/2000/svg">
              <path d="M5.84365 2.87333C6.34198 1.415 8.35698 1.37083 8.94782 2.74083L8.99782 2.87417L9.67032 4.84083C9.82444 5.29186 10.0735 5.70459 10.4007 6.05119C10.7279 6.39778 11.1256 6.67018 11.567 6.85L11.7478 6.9175L13.7145 7.58917C15.1728 8.0875 15.217 10.1025 13.8478 10.6933L13.7145 10.7433L11.7478 11.4158C11.2966 11.5699 10.8837 11.8189 10.537 12.146C10.1903 12.4732 9.91773 12.871 9.73782 13.3125L9.67032 13.4925L8.99865 15.46C8.50032 16.9183 6.48531 16.9625 5.89531 15.5933L5.84365 15.46L5.17198 13.4933C5.01796 13.0422 4.76896 12.6293 4.44177 12.2825C4.11457 11.9358 3.71681 11.6632 3.27532 11.4833L3.09531 11.4158L1.12865 10.7442C-0.330519 10.2458 -0.374685 8.23083 0.995315 7.64083L1.12865 7.58917L3.09531 6.9175C3.54634 6.76338 3.95907 6.51433 4.30567 6.18714C4.65226 5.85995 4.92466 5.46224 5.10448 5.02083L5.17198 4.84083L5.84365 2.87333ZM14.0878 1.50573e-07C14.2437 -1.96643e-07 14.3965 0.0437319 14.5288 0.126226C14.6611 0.208721 14.7676 0.326669 14.8362 0.466667L14.8761 0.564167L15.1678 1.41917L16.0236 1.71083C16.1799 1.76391 16.3168 1.86218 16.4172 1.99318C16.5175 2.12418 16.5767 2.28202 16.5872 2.44668C16.5977 2.61135 16.5592 2.77544 16.4763 2.91816C16.3935 3.06087 16.2702 3.17578 16.122 3.24833L16.0236 3.28833L15.1686 3.58L14.877 4.43583C14.8238 4.59202 14.7255 4.72892 14.5944 4.82916C14.4634 4.92941 14.3055 4.98849 14.1409 4.99894C13.9762 5.00938 13.8121 4.9707 13.6695 4.88782C13.5268 4.80493 13.412 4.68156 13.3395 4.53333L13.2995 4.43583L13.0078 3.58083L12.152 3.28917C11.9957 3.23609 11.8588 3.13782 11.7585 3.00682C11.6581 2.87582 11.599 2.71799 11.5884 2.55332C11.5779 2.38865 11.6165 2.22456 11.6993 2.08184C11.7821 1.93913 11.9054 1.82422 12.0536 1.75167L12.152 1.71167L13.007 1.42L13.2986 0.564167C13.3548 0.399521 13.4612 0.256591 13.6027 0.155416C13.7442 0.0542408 13.9138 -0.000104401 14.0878 1.50573e-07Z" fill="white"/>
            </svg>
            <!-- 匹配分析图标 -->
            <svg v-else width="17" height="17" viewBox="0 0 17 17" fill="none" xmlns="http://www.w3.org/2000/svg">
              <path d="M5.84365 2.87333C6.34198 1.415 8.35698 1.37083 8.94782 2.74083L8.99782 2.87417L9.67032 4.84083C9.82444 5.29186 10.0735 5.70459 10.4007 6.05119C10.7279 6.39778 11.1256 6.67018 11.567 6.85L11.7478 6.9175L13.7145 7.58917C15.1728 8.0875 15.217 10.1025 13.8478 10.6933L13.7145 10.7433L11.7478 11.4158C11.2966 11.5699 10.8837 11.8189 10.537 12.146C10.1903 12.4732 9.91773 12.871 9.73782 13.3125L9.67032 13.4925L8.99865 15.46C8.50032 16.9183 6.48531 16.9625 5.89531 15.5933L5.84365 15.46L5.17198 13.4933C5.01796 13.0422 4.76896 12.6293 4.44177 12.2825C4.11457 11.9358 3.71681 11.6632 3.27532 11.4833L3.09531 11.4158L1.12865 10.7442C-0.330519 10.2458 -0.374685 8.23083 0.995315 7.64083L1.12865 7.58917L3.09531 6.9175C3.54634 6.76338 3.95907 6.51433 4.30567 6.18714C4.65226 5.85995 4.92466 5.46224 5.10448 5.02083L5.17198 4.84083L5.84365 2.87333ZM14.0878 1.50573e-07C14.2437 -1.96643e-07 14.3965 0.0437319 14.5288 0.126226C14.6611 0.208721 14.7676 0.326669 14.8362 0.466667L14.8761 0.564167L15.1678 1.41917L16.0236 1.71083C16.1799 1.76391 16.3168 1.86218 16.4172 1.99318C16.5175 2.12418 16.5767 2.28202 16.5872 2.44668C16.5977 2.61135 16.5592 2.77544 16.4763 2.91816C16.3935 3.06087 16.2702 3.17578 16.122 3.24833L16.0236 3.28833L15.1686 3.58L14.877 4.43583C14.8238 4.59202 14.7255 4.72892 14.5944 4.82916C14.4634 4.92941 14.3055 4.98849 14.1409 4.99894C13.9762 5.00938 13.8121 4.9707 13.6695 4.88782C13.5268 4.80493 13.412 4.68156 13.3395 4.53333L13.2995 4.43583L13.0078 3.58083L12.152 3.28917C11.9957 3.23609 11.8588 3.13782 11.7585 3.00682C11.6581 2.87582 11.599 2.71799 11.5884 2.55332C11.5779 2.38865 11.6165 2.22456 11.6993 2.08184C11.7821 1.93913 11.9054 1.82422 12.0536 1.75167L12.152 1.71167L13.007 1.42L13.2986 0.564167C13.3548 0.399521 13.4612 0.256591 13.6027 0.155416C13.7442 0.0542408 13.9138 -0.000104401 14.0878 1.50573e-07Z" fill="#6A7282"/>
            </svg>
          </div>
          <div class="agent-apply-progress__step-text">
            <span class="agent-apply-progress__step-name" :class="stepNameClass(2)">匹配分析</span>
            <span class="agent-apply-progress__step-status" :class="stepStatusClass(2)">{{ stepStatusText(2) }}</span>
          </div>
        </div>

        <!-- 连接线 -->
        <div class="agent-apply-progress__step-line"></div>

        <!-- 第3步：前往投递 -->
        <div class="agent-apply-progress__step-item">
          <div class="agent-apply-progress__step-icon" :class="stepIconClass(3)">
            <svg v-if="currentStep > 3" width="15" height="15" viewBox="0 0 15 15" fill="none" xmlns="http://www.w3.org/2000/svg">
              <path d="M14.5965 1.67729C14.9565 0.681458 13.9915 -0.283541 12.9957 0.0772919L0.824837 4.47896C-0.17433 4.84063 -0.295164 6.20396 0.624003 6.73646L4.509 8.98563L7.97817 5.51646C8.13534 5.36466 8.34584 5.28066 8.56434 5.28256C8.78283 5.28446 8.99185 5.3721 9.14635 5.52661C9.30086 5.68112 9.3885 5.89013 9.3904 6.10862C9.3923 6.32712 9.3083 6.53762 9.1565 6.69479L5.68734 10.164L7.93734 14.049C8.469 14.9681 9.83234 14.8465 10.194 13.8481L14.5965 1.67729Z" fill="white"/>
            </svg>
            <!-- 前往投递图标：箭头 -->
            <svg v-else width="15" height="15" viewBox="0 0 15 15" fill="none" xmlns="http://www.w3.org/2000/svg">
              <path d="M14.5965 1.67729C14.9565 0.681458 13.9915 -0.283541 12.9957 0.0772919L0.824837 4.47896C-0.17433 4.84063 -0.295164 6.20396 0.624003 6.73646L4.509 8.98563L7.97817 5.51646C8.13534 5.36466 8.34584 5.28066 8.56434 5.28256C8.78283 5.28446 8.99185 5.3721 9.14635 5.52661C9.30086 5.68112 9.3885 5.89013 9.3904 6.10862C9.3923 6.32712 9.3083 6.53762 9.1565 6.69479L5.68734 10.164L7.93734 14.049C8.469 14.9681 9.83234 14.8465 10.194 13.8481L14.5965 1.67729Z" fill="#6A7282"/>
            </svg>

          </div>
          <div class="agent-apply-progress__step-text">
            <span class="agent-apply-progress__step-name" :class="stepNameClass(3)">前往投递</span>
            <span class="agent-apply-progress__step-status" :class="stepStatusClass(3)">{{ stepStatusText(3) }}</span>
          </div>
        </div>

        <!-- 连接线 -->
        <div class="agent-apply-progress__step-line"></div>

        <!-- 第4步：结果确认 -->
        <div class="agent-apply-progress__step-item">
          <div class="agent-apply-progress__step-icon" :class="stepIconClass(4)">
            <svg v-if="currentStep > 4" width="15" height="17" viewBox="0 0 15 17" fill="none" xmlns="http://www.w3.org/2000/svg">
              <path d="M5.83333 13.3333L2.5 10L3.675 8.825L5.83333 10.975L11.325 5.48333L12.5 6.66667M7.5 1.66667C7.72101 1.66667 7.93297 1.75446 8.08926 1.91074C8.24554 2.06702 8.33333 2.27899 8.33333 2.5C8.33333 2.72101 8.24554 2.93298 8.08926 3.08926C7.93297 3.24554 7.72101 3.33333 7.5 3.33333C7.27899 3.33333 7.06702 3.24554 6.91074 3.08926C6.75446 2.93298 6.66667 2.72101 6.66667 2.5C6.66667 2.27899 6.75446 2.06702 6.91074 1.91074C7.06702 1.75446 7.27899 1.66667 7.5 1.66667ZM13.3333 1.66667H9.85C9.5 0.7 8.58333 0 7.5 0C6.41667 0 5.5 0.7 5.15 1.66667H1.66667C1.22464 1.66667 0.800716 1.84226 0.488155 2.15482C0.175595 2.46738 0 2.89131 0 3.33333V15C0 15.442 0.175595 15.8659 0.488155 16.1785C0.800716 16.4911 1.22464 16.6667 1.66667 16.6667H13.3333C13.7754 16.6667 14.1993 16.4911 14.5118 16.1785C14.8244 15.8659 15 15.442 15 15V3.33333C15 2.89131 14.8244 2.46738 14.5118 2.15482C14.1993 1.84226 13.7754 1.66667 13.3333 1.66667Z" fill="white"/>
            </svg>
            <!-- 结果确认图标：勾选框 -->
            <svg v-else width="15" height="17" viewBox="0 0 15 17" fill="none" xmlns="http://www.w3.org/2000/svg">
              <path d="M5.83333 13.3333L2.5 10L3.675 8.825L5.83333 10.975L11.325 5.48333L12.5 6.66667M7.5 1.66667C7.72101 1.66667 7.93297 1.75446 8.08926 1.91074C8.24554 2.06702 8.33333 2.27899 8.33333 2.5C8.33333 2.72101 8.24554 2.93298 8.08926 3.08926C7.93297 3.24554 7.72101 3.33333 7.5 3.33333C7.27899 3.33333 7.06702 3.24554 6.91074 3.08926C6.75446 2.93298 6.66667 2.72101 6.66667 2.5C6.66667 2.27899 6.75446 2.06702 6.91074 1.91074C7.06702 1.75446 7.27899 1.66667 7.5 1.66667ZM13.3333 1.66667H9.85C9.5 0.7 8.58333 0 7.5 0C6.41667 0 5.5 0.7 5.15 1.66667H1.66667C1.22464 1.66667 0.800716 1.84226 0.488155 2.15482C0.175595 2.46738 0 2.89131 0 3.33333V15C0 15.442 0.175595 15.8659 0.488155 16.1785C0.800716 16.4911 1.22464 16.6667 1.66667 16.6667H13.3333C13.7754 16.6667 14.1993 16.4911 14.5118 16.1785C14.8244 15.8659 15 15.442 15 15V3.33333C15 2.89131 14.8244 2.46738 14.5118 2.15482C14.1993 1.84226 13.7754 1.66667 13.3333 1.66667Z" fill="#6A7282"/>
            </svg>
          </div>
          <div class="agent-apply-progress__step-text">
            <span class="agent-apply-progress__step-name" :class="stepNameClass(4)">结果确认</span>
            <span class="agent-apply-progress__step-status" :class="stepStatusClass(4)">{{ stepStatusText(4) }}</span>
          </div>
        </div>
      </div>
    </div>

    <!-- 底部：提示文字 + 操作按钮 -->
    <div class="agent-apply-progress__bottom">
      <!-- 提示文字 -->
      <span class="agent-apply-progress__hint" :class="hintClass">{{ hintText }}</span>

      <!-- 操作按钮区域 -->
      <div v-if="showActions" class="agent-apply-progress__actions">
        <!-- 第1步完成：查看优化简历 + 确认简历 -->
        <template v-if="currentStep === 1 && stepPhase === 'done'">
          <button class="agent-apply-progress__btn agent-apply-progress__btn--outline" @click="handleViewResume">查看优化简历</button>
          <button class="agent-apply-progress__btn agent-apply-progress__btn--primary" @click="handleConfirmResume">确认简历</button>
        </template>

        <!-- 第2步匹配度偏低：不投递 + 继续投递 -->
        <template v-if="currentStep === 2 && stepPhase === 'low-match'">
          <button class="agent-apply-progress__btn agent-apply-progress__btn--outline" @click="handleNotApply">不投递</button>
          <button class="agent-apply-progress__btn agent-apply-progress__btn--primary" @click="handleContinueApply">继续投递</button>
        </template>

        <!-- 第3步：前往投递 -->
        <template v-if="currentStep === 3 && stepPhase === 'ready'">
          <button class="agent-apply-progress__btn agent-apply-progress__btn--primary" @click="handleGoApply">前往投递</button>
        </template>

        <!-- 第4步：不投递 + 已投递 -->
        <template v-if="currentStep === 4 && stepPhase === 'confirm'">
          <button class="agent-apply-progress__btn agent-apply-progress__btn--outline" @click="handleNotApply">不投递</button>
          <button class="agent-apply-progress__btn agent-apply-progress__btn--primary" @click="handleApplied">已投递</button>
        </template>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useStore } from 'vuex'
import { createChannelBridge } from '@/utils/channelBridge'
import type { JobListItem } from '@/api/jobs'

const store = useStore()

// ==================== Props ====================

const props = defineProps<{
  /** 岗位信息 */
  jobInfo: JobListItem | null
  /** 当前步骤（1-4，大于4表示全部完成） */
  currentStep: number
  /** 当前步骤的阶段：loading=请求中, done=完成可操作, low-match=匹配度偏低, ready=准备就绪, confirm=待确认 */
  stepPhase?: 'loading' | 'done' | 'low-match' | 'ready' | 'confirm'
  /** 匹配度分数（第2步用） */
  matchScore?: number
  /** 投递完成时间（第4步完成后显示） */
  completedTime?: string
}>()

// ==================== 计算属性 ====================

/** 是否为已完成状态 */
const isCompleted = computed(() => props.currentStep > 4)

/** 提示文字 */
const hintText = computed(() => {
  if (isCompleted.value && props.completedTime) {
    return `投递时间:${props.completedTime}`
  }
  if (props.currentStep === 1) {
    if (props.stepPhase === 'done') return '已完成岗位专属简历优化，请确认本次投递使用的简历'
    return '正在根据岗位信息优化简历...'
  }
  if (props.currentStep === 2) {
    if (props.stepPhase === 'low-match') return `当前匹配度偏低：${props.matchScore ?? 0}分，是否仍要继续投递？`
    return '正在根据最终投递简历复核岗位匹配度...'
  }
  if (props.currentStep === 3) {
    return '已准备好投递信息，请前往招聘官网完成申请'
  }
  if (props.currentStep === 4) {
    return '请确认该岗位是否已经完成投递'
  }
  return ''
})

/** 提示文字样式class */
const hintClass = computed(() => {
  if (isCompleted.value) return ''
  if (props.currentStep === 1 && props.stepPhase === 'done') return 'agent-apply-progress__hint--warning'
  if (props.currentStep === 2 && props.stepPhase === 'low-match') return 'agent-apply-progress__hint--low-match'
  if (props.currentStep === 3) return 'agent-apply-progress__hint--accent'
  if (props.currentStep === 4) return 'agent-apply-progress__hint--accent'
  return ''
})

/** 是否显示操作按钮 */
const showActions = computed(() => {
  if (isCompleted.value) return false
  if (props.currentStep === 1 && props.stepPhase === 'done') return true
  if (props.currentStep === 2 && props.stepPhase === 'low-match') return true
  if (props.currentStep === 3 && props.stepPhase === 'ready') return true
  if (props.currentStep === 4 && props.stepPhase === 'confirm') return true
  return false
})

/** 步骤图标样式class */
function stepIconClass(step: number) {
  if (props.currentStep > step) return 'agent-apply-progress__step-icon--done'
  if (props.currentStep === step) return 'agent-apply-progress__step-icon--active'
  return 'agent-apply-progress__step-icon--pending'
}

/** 步骤名称样式class */
function stepNameClass(step: number) {
  if (props.currentStep > step) return 'agent-apply-progress__step-name--done'
  if (props.currentStep === step) return 'agent-apply-progress__step-name--active'
  return 'agent-apply-progress__step-name--pending'
}

/** 步骤状态文字样式class */
function stepStatusClass(step: number) {
  if (props.currentStep > step) return 'agent-apply-progress__step-status--done'
  if (props.currentStep === step) return 'agent-apply-progress__step-status--active'
  return 'agent-apply-progress__step-status--pending'
}

/** 步骤状态文字 */
function stepStatusText(step: number) {
  if (props.currentStep > step) return '已完成'
  if (props.currentStep === step) return '进行中'
  return '待处理'
}

// ==================== 事件 ====================

const emit = defineEmits<{
  /** 查看优化简历（第1步完成） */
  (e: 'viewResume'): void
  /** 确认简历（第1步完成 → 进入第2步） */
  (e: 'confirmResume'): void
  /** 不投递（第2步匹配度低 或 第4步取消） */
  (e: 'notApply'): void
  /** 继续投递（第2步匹配度低时继续） */
  (e: 'continueApply'): void
  /** 前往投递（第3步） */
  (e: 'goApply'): void
  /** 已投递（第4步确认） */
  (e: 'applied'): void
}>()

// ==================== 事件处理 ====================

/** 查看优化简历 */
function handleViewResume() {
  emit('viewResume')
}

/** 确认简历 */
function handleConfirmResume() {
  emit('confirmResume')
}

/** 不投递 */
function handleNotApply() {
  emit('notApply')
}

/** 继续投递 */
function handleContinueApply() {
  emit('continueApply')
}

/** 前往投递 — 跨域 put 投递链接给插件 */
async function handleGoApply() {
  const link = props.jobInfo?.sourceUrl
  if (link) {
    const bridge = createChannelBridge({ domain: store.state.bridgeDomain })
    try {
      // 先 get 拿到已有的链接列表
      const existing = await bridge.get<{ linkList: string[] }>(store.state.bridgeDomain, 'offerpaiDeliveryLinkList', true)
      const linkList: string[] = existing?.linkList || []

      // 如果链接不在列表中则 push
      if (!linkList.includes(link)) {
        linkList.push(link)
      }

      // 跨域 put 数据给插件
      await bridge.put<{ linkList: string[] }>(store.state.bridgeDomain, 'offerpaiDeliveryLinkList', { linkList }, true)
    } catch (e) {
      console.error('[AgentApplyProgress] 跨域 put 投递链接失败', e)
    } finally {
      bridge.destroy()
    }
  }
  emit('goApply')
}

/** 已投递 */
function handleApplied() {
  emit('applied')
}
</script>
