function togCloudType(type, cb){
  __cloudToggles[type] = cb.checked;
  api('/api/admin/system-configs', {
    method:'PUT', headers:{'Content-Type':'application/json'},
    body:JSON.stringify({entries:[{key:'cloud_type_'+type+'_enabled', value:cb.checked?'true':'false'}]})
  });
}

function togCloudAcc(id, cb){
  api('/api/admin/cloud-configs/'+id, {method:'PUT',headers:{'Content-Type':'application/json'},body:JSON.stringify({is_active:cb.checked})})
    .then(function(d){ if(d.error){ cb.checked=!cb.checked; toast(d.error,'error'); } });
}
function delCloudAcc(id){ var name = '账号#'+id;
  if(!confirm('确定删除 '+name+' 吗？')) return;
  api('/api/admin/cloud-configs/'+id, {method:'DELETE'}).then(function(d){
    toast(d.error?d.error:'已删除 '+name, d.error?'error':'success');
    nav('cloud-config');
  });
}
function doSaveAccount(){
  var body = {
    cloud_type: document.getElementById('dlg_type').value,
    // default off, will be set by manual toggle
  };
  var cookie = document.getElementById('dlg_cookie').value.trim();
  if(!cookie){ toast('请先输入或扫码获取 Cookie','error'); return; }
  body.cookie = cookie;
  body.promotion_account = document.getElementById('dlg_promo').value.trim() || '';
  body.nickname = document.getElementById('dlg_nick').value.trim() || '';
  body.storage_used = __qrStorageUsed || '';
  body.storage_total = __qrStorageTotal || '';
  toast('⏳ 正在保存...');
  api('/api/admin/cloud-configs', {
    method:'POST', headers:{'Content-Type':'application/json'}, body:JSON.stringify(body)
  }).then(function(d){
    if(d && d.error){ toast(d.error,'error'); return; }
    var savedId = d.id;
    toast('⏳ 正在验证 Cookie 并查询空间...');
    return api('/api/admin/cloud-configs/'+body.cloud_type+'/test', {
      method:'POST', headers:{'Content-Type':'application/json'},
      body:JSON.stringify({id: savedId})
    });
  }).then(function(r){
    if(r && r.success){
      toast('✅ 验证通过！空间: '+(r.storage_used||'?')+'/'+(r.storage_total||'?'));
    } else {
      toast('⚠️ 已保存但验证失败: '+(r?r.message:'未知错误'),'error');
    }
    closeDialog();
    nav('cloud-config');
  }).catch(function(e){
    toast('保存失败: '+(e.message||'网络错误'),'error');
    console.error('doSaveAccount error:', e);
  });
}

function doSaveAccount_OLD(){
  var body = {
    cloud_type: document.getElementById('dlg_type').value,
    // default off, will be set by verification
  };
  var cookie = document.getElementById('dlg_cookie').value.trim();
  if(!cookie){ toast('请先输入或扫码获取 Cookie','error'); return; }
  body.cookie = cookie;
  console.log("SAVE promo raw:", document.getElementById("dlg_promo").value); body.promotion_account = document.getElementById('dlg_promo').value.trim() || '';
  body.nickname = document.getElementById('dlg_nick').value.trim() || '';
  body.storage_used = __qrStorageUsed || '';
  body.storage_total = __qrStorageTotal || '';
  api('/api/admin/cloud-configs', {
    method:'POST', headers:{'Content-Type':'application/json'}, body:JSON.stringify(body)
  }).then(function(d){
    if(d && d.error){ toast(d.error,'error'); return; }
    toast('✅ 网盘配置更新成功');
    closeDialog();
    nav('cloud-config');
  }).catch(function(e){
    toast('保存失败: '+(e.message||'网络错误'),'error');
    console.error('doSaveAccount error:', e);
  });
}
