

















(function () {
  'use strict';

  var T = function (k, d) { return window.VizI18n ? VizI18n.tr(k, d) : d; };

  var MAX_CHARS = 2000;        

  var el = {};
  var open = false;
  var sending = false;

  function injectStyle() {
    var css = [
      '#fb-backdrop{position:fixed;top:0;right:0;bottom:0;left:0;',
      '  z-index:46;background:rgba(0,0,0,.45);display:none;}',
      '#fb-pop{position:fixed;top:0;right:0;bottom:0;left:0;',
      '  margin:auto;height:fit-content;max-height:88vh;overflow:auto;',
      '  z-index:47;width:520px;max-width:92vw;',
      '  background:rgba(28,31,36,.98);border:1px solid #4da3ff;border-radius:6px;',
      '  box-shadow:0 6px 18px rgba(0,0,0,.5);display:none;font-size:12px;color:#c5c8c6;}',
      '#fb-pop .hd{display:flex;align-items:center;gap:8px;padding:12px 14px 6px;}',
      '#fb-pop h3{margin:0;flex:1 1 auto;font-size:13px;color:#ffcc44;font-weight:600;}',
      '#fb-pop .x{flex:0 0 auto;background:none;border:0;color:#8f979e;font-size:16px;',
      '  line-height:1;padding:0 4px;cursor:pointer;}',
      '#fb-pop .x:hover{color:#c5c8c6;}',
      '#fb-pop .body{padding:0 14px;}',
      '#fb-pop .hint{color:#8f979e;font-size:11px;line-height:1.6;margin:0 0 8px;}',
      

      '#fb-pop textarea{width:100%;box-sizing:border-box;min-height:170px;resize:vertical;',
      '  background:#23272c;color:#c5c8c6;border:1px solid #47505a;border-radius:4px;',
      '  padding:8px;font-size:12px;line-height:1.6;font-family:inherit;}',
      '#fb-pop textarea:focus{outline:none;border-color:#4da3ff;}',
      '#fb-pop .row{display:flex;align-items:center;gap:8px;margin:8px 0 0;}',
      '#fb-pop .row label{flex:0 0 auto;color:#8f979e;}',
      '#fb-pop .row input{flex:1 1 auto;background:#23272c;color:#c5c8c6;',
      '  border:1px solid #47505a;border-radius:4px;padding:5px 7px;font-size:12px;}',
      '#fb-pop .row input:focus{outline:none;border-color:#4da3ff;}',
      '#fb-pop .meta{display:flex;align-items:center;margin:8px 0 0;color:#8f979e;font-size:11px;}',
      '#fb-pop .meta .spacer{flex:1 1 auto;}',
      '#fb-pop .meta .over{color:#ff9a90;}',
      '#fb-pop .btns{display:flex;justify-content:flex-end;gap:8px;padding:10px 14px 12px;}',
      '#fb-pop .btns .go{background:#2d5f8b;border-color:#4da3ff;color:#fff;}',
      '#fb-pop .btns .go:hover{background:#3a76ab;}',
      '#fb-pop .btns .go:disabled{background:#2b3138;border-color:#47505a;color:#8f979e;}'
    ].join('\n');
    var s = document.createElement('style');
    s.textContent = css;
    document.head.appendChild(s);
  }

  function mk(tag, cls, text) {
    var e = document.createElement(tag);
    if (cls) e.className = cls;
    if (text !== undefined && text !== null) e.textContent = text;
    return e;
  }

  function build() {
    injectStyle();

    el.backdrop = mk('div');
    el.backdrop.id = 'fb-backdrop';
    el.backdrop.addEventListener('click', close);

    el.pop = mk('div');
    el.pop.id = 'fb-pop';

    var hd = mk('div', 'hd');
    el.title = mk('h3');
    var x = mk('button', 'x', '×');
    x.addEventListener('click', close);
    hd.appendChild(el.title);
    hd.appendChild(x);

    var body = mk('div', 'body');
    el.hint = mk('div', 'hint');
    el.text = document.createElement('textarea');
    el.text.maxLength = MAX_CHARS;
    el.text.addEventListener('input', paintMeta);

    var row = mk('div', 'row');
    el.contactLabel = mk('label');
    el.contact = document.createElement('input');
    el.contact.type = 'text';
    el.contact.maxLength = 120;
    row.appendChild(el.contactLabel);
    row.appendChild(el.contact);

    var meta = mk('div', 'meta');
    el.who = mk('span');
    meta.appendChild(el.who);
    meta.appendChild(mk('span', 'spacer'));
    el.count = mk('span');
    meta.appendChild(el.count);

    body.appendChild(el.hint);
    body.appendChild(el.text);
    body.appendChild(row);
    body.appendChild(meta);

    var btns = mk('div', 'btns');
    el.cancel = document.createElement('button');
    el.cancel.addEventListener('click', close);
    el.send = document.createElement('button');
    el.send.className = 'go';
    el.send.addEventListener('click', submit);
    btns.appendChild(el.cancel);
    btns.appendChild(el.send);

    el.pop.appendChild(hd);
    el.pop.appendChild(body);
    el.pop.appendChild(btns);

    document.body.appendChild(el.backdrop);
    document.body.appendChild(el.pop);

    
    document.addEventListener('keydown', function (ev) {
      if (!open) return;
      if (ev.key === 'Escape') {
        ev.stopPropagation();
        ev.preventDefault();
        close();
      }
    }, true);
  }

  function paintMeta() {
    var n = el.text.value.length;
    el.count.textContent = n + ' / ' + MAX_CHARS;
    el.count.className = (n >= MAX_CHARS) ? 'over' : '';
    el.send.disabled = sending || !el.text.value.trim();
  }

  


  function paintWho() {
    var yes = false;
    try {
      yes = !!(window.CloudUI && CloudUI.loggedIn && CloudUI.loggedIn());
    } catch (e) { yes = false; }
    el.who.textContent = yes ? T('viz_fb_as_user', '已登录，会带上你的账号')
                             : T('viz_fb_anonymous', '未登录，将匿名提交');
  }

  function render() {
    if (!el.pop) return;
    el.title.textContent = T('viz_fb_title', '意见反馈');
    el.hint.textContent = T('viz_fb_hint',
      '遇到的问题或想要的功能都可以写在这里。说清「你在做什么、期望怎样、实际怎样」，' +
      '我们才复现得出来。');
    el.text.placeholder = T('viz_fb_placeholder',
      '例：点开 3D 页之后拖动末端，J4 总是报需翻腕，换个角度就好了…');
    el.contactLabel.textContent = T('viz_fb_contact', '联系方式（选填）：');
    el.contact.placeholder = T('viz_fb_contact_ph', '想收到回复就留个邮箱或论坛 ID');
    el.cancel.textContent = T('viz_fb_cancel', '取消');
    el.send.textContent = sending ? T('viz_fb_sending', '提交中…')
                                  : T('viz_fb_send', '提交');
    paintWho();
    paintMeta();
  }

  function submit() {
    if (sending) return;
    var text = el.text.value.trim();
    if (!text) return;
    if (!VizChannel.cloud || !VizChannel.cloud.submit_feedback) {
      
      window.Toast && Toast.show({ id: 'fb', level: 'error',
        text: T('viz_fb_unavailable', '反馈通道不可用（未连接到 enpeicv.com）') });
      return;
    }
    sending = true;
    render();
    VizChannel.cloud.submit_feedback(JSON.stringify({
      req_id: 0, text: text, contact: el.contact.value.trim()
    }));
    
    
  }

  function show() {
    if (!el.pop) return;
    sending = false;
    render();
    el.backdrop.style.display = 'block';
    el.pop.style.display = 'block';
    open = true;
    el.text.focus();
  }

  function close() {
    if (!el.pop) return;
    el.backdrop.style.display = 'none';
    el.pop.style.display = 'none';
    open = false;
  }

  window.FeedbackUI = {
    init: function () {
      build();
      var btn = document.getElementById('btn-feedback');
      if (btn) btn.addEventListener('click', function () { open ? close() : show(); });
    },
    
    refresh: function () { if (open) render(); },
    open: show,
    close: close,
    isOpen: function () { return open; },
    

    onDone: function (p) {
      if (!sending) return;
      sending = false;
      if (p && p.ok) {
        el.text.value = '';
        close();
      } else {
        render();
      }
    }
  };
})();
