<%- include('../partials/admin-header') %>
<h1>编辑产品 #<%= product.id %></h1>

<div class="form-card">
  <form method="POST" action="/admin/products/<%= product.id %>/edit">
    <div class="form-row">
      <label>商家
        <select name="merchant_id" required>
          <% merchants.forEach(m => { %>
          <option value="<%= m.id %>" <%= product.merchant_id === m.id ? 'selected' : '' %>><%= m.name %></option>
          <% }) %>
        </select>
      </label>
      <label>产品名 <input name="name" value="<%= product.name %>" required></label>
      <label>地区 <input name="location" value="<%= product.location || '' %>"></label>
    </div>
    <div class="form-row">
      <label style="flex:2">产品页 URL <input name="url" value="<%= product.url || '' %>"></label>
    </div>
    <div class="form-row">
      <label style="flex:2">购买链接 <input name="buy_url" id="edit_buy_url" value="<%= product.buy_url || '' %>" onchange="autoParseUrl(this)"></label>
      <label>Aff 参数名 <input name="aff_param" id="edit_aff_param" value="<%= product.aff_param || 'aff' %>" style="width:80px"></label>
      <label>Aff 值 <input name="aff_code" id="edit_aff_code" value="<%= product.aff_code || '' %>" placeholder="如 5" oninput="previewAffUrl()"></label>
    </div>
    <% if (product.generated_aff_url) { %>
    <div id="edit_aff_preview" style="padding:8px 12px;margin:-4px 0 8px;background:#e8f5e9;border-radius:4px;font-size:13px;word-break:break-all">
      <strong>🔗 推送用的 Aff 链接：</strong><span id="edit_aff_preview_url"><a href="<%= product.generated_aff_url %>" target="_blank"><%= product.generated_aff_url %></a></span>
      <span style="color:#28a745;font-size:11px;margin-left:8px">✓ 补货推送时使用此链接</span>
    </div>
    <% } else { %>
    <div id="edit_aff_preview" style="display:none;padding:8px 12px;margin:-4px 0 8px;background:#e8f5e9;border-radius:4px;font-size:13px;word-break:break-all">
      <strong>🔗 推送用的 Aff 链接：</strong><span id="edit_aff_preview_url"></span>
      <span style="color:#28a745;font-size:11px;margin-left:8px">✓ 补货推送时使用此链接</span>
    </div>
    <% } %>

    <!-- PID & Slug 字段 -->
    <div class="form-row" style="background:#f8f9fa;padding:8px 12px;border-radius:6px;margin:8px 0">
      <label>系统 PID
        <input value="<%= product.internal_pid || '(保存后自动生成)' %>" readonly style="background:#e9ecef;color:#6c757d;cursor:not-allowed">
      </label>
      <label>商家 PID
        <input name="provider_pid" id="edit_provider_pid" value="<%= product.provider_pid || '' %>" placeholder="从购买链接自动识别 / 手填">
      </label>
      <label style="flex:1">Slug
        <input name="slug" value="<%= product.slug || '' %>" placeholder="留空自动生成">
      </label>
    </div>

    <div class="form-row">
      <label>价格 <input name="price" value="<%= product.price || '' %>"></label>
      <label>年付价 <input name="annual_price" value="<%= product.annual_price || '' %>"></label>
      <label>配置摘要 <input name="spec_summary" value="<%= product.spec_summary || '' %>"></label>
      <label>流量 <input name="traffic" value="<%= product.traffic || '' %>"></label>
      <label>计费说明 <input name="billing_cycle" value="<%= product.billing_cycle || '' %>"></label>
    </div>
    <div class="form-row">
      <label>优惠码 <input name="coupon_code" value="<%= product.coupon_code || '' %>"></label>
      <label>标签 <input name="tags" value="<%= product.tags || '' %>"></label>
      <label>SKU <input name="sku" value="<%= product.sku || '' %>"></label>
      <label style="flex:1">推送开头
        <input name="push_intro" value="<%= product.push_intro || '' %>">
      </label>
    </div>
    <div class="form-row">
      <label>检测模式
        <select name="check_mode">
          <option value="keyword" <%= product.check_mode === 'keyword' ? 'selected' : '' %>>keyword</option>
          <option value="manual" <%= product.check_mode === 'manual' ? 'selected' : '' %>>manual</option>
        </select>
      </label>
      <label style="flex:1">有货关键词
        <input name="in_stock_keywords" value="<%= product.in_stock_keywords || '' %>">
      </label>
      <label style="flex:1">缺货关键词
        <input name="out_of_stock_keywords" value="<%= product.out_of_stock_keywords || '' %>">
      </label>
    </div>
    <div class="form-row">
      <label>
        <input type="checkbox" name="is_public" value="1" <%= product.is_public ? 'checked' : '' %>> 前台展示
      </label>
      <label>
        <input type="checkbox" name="is_featured" value="1" <%= product.is_featured ? 'checked' : '' %>> 推荐
      </label>
      <label>排序值 <input name="sort_order" type="number" value="<%= product.sort_order || 100 %>" style="width:80px"></label>
      <label style="flex:1">备注 <input name="notes" value="<%= product.notes || '' %>"></label>
    </div>
    <div class="form-row" style="justify-content:space-between;margin-top:12px">
      <a href="/admin/products" class="btn" style="background:#e9ecef;color:#333">← 返回列表</a>
      <button type="submit" class="btn btn-primary" style="padding:8px 24px">保存修改</button>
    </div>
  </form>
</div>

<script>
function autoParseUrl(input) {
  var url = input.value.trim();
  if (!url) return;
  fetch('/admin/products/api/parse-pid?url=' + encodeURIComponent(url))
    .then(function(r) { return r.json(); })
    .then(function(data) {
      if (data.provider_pid) {
        var el = document.getElementById('edit_provider_pid');
        if (el && !el.value) { el.value = data.provider_pid; flashGreen(el); }
      }
      if (data.aff_code) {
        var el2 = document.getElementById('edit_aff_code');
        if (el2 && !el2.value) { el2.value = data.aff_code; flashGreen(el2); }
      }
      if (data.aff_param) {
        var el3 = document.getElementById('edit_aff_param');
        if (el3) { el3.value = data.aff_param; }
      }
      previewAffUrl();
    })
    .catch(function() {});
}

function previewAffUrl() {
  var buyUrl = document.getElementById('edit_buy_url').value.trim();
  var affCode = document.getElementById('edit_aff_code').value.trim();
  var affParam = document.getElementById('edit_aff_param').value.trim() || 'aff';
  var preview = document.getElementById('edit_aff_preview');
  var previewUrl = document.getElementById('edit_aff_preview_url');

  if (buyUrl && affCode) {
    fetch('/admin/products/api/build-aff-url?base_url=' + encodeURIComponent(buyUrl) + '&aff_code=' + encodeURIComponent(affCode) + '&aff_param=' + encodeURIComponent(affParam))
      .then(function(r) { return r.json(); })
      .then(function(data) {
        if (data.aff_url) {
          previewUrl.innerHTML = '<a href="' + data.aff_url + '" target="_blank">' + data.aff_url + '</a>';
          preview.style.display = 'block';
        }
      })
      .catch(function() {});
  } else {
    preview.style.display = 'none';
  }
}

function flashGreen(el) {
  el.style.borderColor = '#28a745';
  setTimeout(function() { el.style.borderColor = ''; }, 2000);
}
</script>

<%- include('../partials/footer') %>
