(function(){
function fmt(n){return '$'+(n||0).toLocaleString('es-MX',{minimumFractionDigits:2,maximumFractionDigits:2});}
var params=new URLSearchParams(window.location.search);
var cid=parseInt(params.get('id'));
if(!cid){document.getElementById('credito-header').innerHTML='ID no proporcionado';return;}
fetch('/web/session/get_session_info',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({jsonrpc:'2.0',method:'call',id:1,params:{}})})
.then(function(r){return r.json();})
.then(function(sess){
fetch('/web/dataset/call_kw',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({jsonrpc:'2.0',method:'call',id:2,params:{model:'x_creditos',method:'read',args:[[cid],['x_name','x_studio_partner_id','x_studio_value','x_studio_saldo_insoluto','x_studio_tasa_anual_','x_studio_plazo_meses','x_studio_cuota_fija','x_studio_stage_id','x_studio_total_pagado','x_studio_fecha_desembolso','x_studio_fecha_vencimiento']],kwargs:{}}})})
.then(function(r){return r.json();})
.then(function(d){
if(!d.result||!d.result.length){document.getElementById('credito-header').innerHTML='Credito no encontrado';return;}
var cr=d.result[0];
var monto=cr.x_studio_value||0;
var saldo=cr.x_studio_saldo_insoluto||0;
var pagado=cr.x_studio_total_pagado||0;
var prog=monto>0?Math.round(((monto-saldo)/monto)*100):0;
if(prog<0)prog=0;if(prog>100)prog=100;
var estado=(cr.x_studio_stage_id&&cr.x_studio_stage_id[1])||'N/A';
var L='\x3c',R='\x3e';
var h=L+'div style="background:linear-gradient(135deg, #f1f5f9 0%, #e2e8f0 100%);border-radius:16px;padding:28px;margin-bottom:24px;"'+R;
h+=L+'h2 style="color: #1E3A5F;margin:0 0 4px;"'+R+cr.x_name+L+'/h2'+R;
h+=L+'p style="color:#90caf9;margin:0 0 20px;"'+R+((cr.x_studio_partner_id&&cr.x_studio_partner_id[1])||'')+L+'/p'+R;
h+=L+'div style="display:grid;grid-template-columns:repeat(3,1fr);gap:12px;"'+R;
function mc(label,value,color){return L+'div style="background:rgba(255,255,255,0.1);border-radius:10px;padding:14px;text-align:center;"'+R+L+'div style="color:#90caf9;font-size:12px;"'+R+label+L+'/div'+R+L+'div style="color:'+color+';font-size:20px;font-weight:700;"'+R+value+L+'/div'+R+L+'/div'+R;}
h+=mc('MONTO',fmt(monto),'#fff');
h+=mc('SALDO',fmt(saldo),'#ef5350');
h+=mc('TASA',(cr.x_studio_tasa_anual_||0)+'%','#fff');
h+=mc('PLAZO',(cr.x_studio_plazo_meses||0)+' meses','#fff');
h+=mc('CUOTA',fmt(cr.x_studio_cuota_fija),'#fff');
h+=mc('ESTADO',estado,'#4caf50');
h+=L+'/div'+R;
h+=L+'div style="margin-top:16px;"'+R+L+'div style="display:flex;justify-content:space-between;color:#90caf9;font-size:13px;margin-bottom:4px;"'+R+L+'span'+R+'Progreso de pago'+L+'/span'+R+L+'span'+R+prog+'%'+L+'/span'+R+L+'/div'+R;
h+=L+'div style="background:rgba(255,255,255,0.15);border-radius:8px;height:10px;overflow:hidden;"'+R+L+'div style="background:linear-gradient(90deg,#4caf50,#81c784);height:100%;border-radius:8px;width:'+prog+'%;"'+R+L+'/div'+R+L+'/div'+R+L+'/div'+R;
h+=L+'/div'+R;
document.getElementById('credito-header').innerHTML=h;
document.getElementById('credito-title').textContent=cr.x_name;
fetch('/web/dataset/call_kw',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({jsonrpc:'2.0',method:'call',id:3,params:{model:'x_amortizacion',method:'search_read',args:[[['x_studio_credito','=',cid]],['x_name','x_studio_numero_pago','x_studio_date','x_studio_cuota_total','x_studio_capital_programado','x_studio_interes_programado','x_studio_saldo_programado','x_studio_estado_linea']],kwargs:{order:'x_studio_numero_pago asc'}}})})
.then(function(r){return r.json();})
.then(function(ad){
var rows=ad.result||[];
if(!rows.length){document.getElementById('amort-table').innerHTML='Sin tabla de amortizacion';return;}
var today=new Date().toISOString().slice(0,10);
var L='\x3c',R='\x3e';
var tb=L+'table style="width:100%;border-collapse:collapse;font-size:13px;"'+R;
tb+=L+'thead'+R+L+'tr style="background: #f1f5f9;color: #1E3A5F;"'+R+L+'th style="padding:10px 8px;"'+R+'#'+L+'/th'+R+L+'th style="padding:10px 8px;"'+R+'Fecha'+L+'/th'+R+L+'th style="padding:10px 8px;text-align:right;"'+R+'Cuota'+L+'/th'+R+L+'th style="padding:10px 8px;text-align:right;"'+R+'Capital'+L+'/th'+R+L+'th style="padding:10px 8px;text-align:right;"'+R+'Interes'+L+'/th'+R+L+'th style="padding:10px 8px;text-align:right;"'+R+'Saldo'+L+'/th'+R+L+'th style="padding:10px 8px;text-align:center;"'+R+'Estado'+L+'/th'+R+L+'/tr'+R+L+'/thead'+R+L+'tbody'+R;
rows.forEach(function(r,i){
var est=(r.x_studio_estado_linea||'Pendiente');
var isPagado=est.toLowerCase().indexOf('pagado')>=0;
var isVencido=!isPagado&&r.x_studio_date&&today>r.x_studio_date;
var bg=isPagado?'rgba(76,175,80,0.15)':isVencido?'rgba(244,67,54,0.15)':(i%2===0?'transparent':'rgba(255,255,255,0.03)');
var badge=isPagado?L+'span style="background:#4caf50;color: #1E3A5F;padding:2px 10px;border-radius:12px;font-size:11px;"'+R+'Pagado'+L+'/span'+R:isVencido?L+'span style="background:#f44336;color: #1E3A5F;padding:2px 10px;border-radius:12px;font-size:11px;"'+R+'Vencido'+L+'/span'+R:L+'span style="background:#ff9800;color: #1E3A5F;padding:2px 10px;border-radius:12px;font-size:11px;"'+R+'Pendiente'+L+'/span'+R;
tb+=L+'tr style="background:'+bg+';border-bottom:1px solid rgba(255,255,255,0.06);"'+R+L+'td style="padding:8px;color: #1E3A5F;"'+R+r.x_studio_numero_pago+L+'/td'+R+L+'td style="padding:8px;color:#ccc;"'+R+(r.x_studio_date||'')+L+'/td'+R+L+'td style="padding:8px;color: #1E3A5F;text-align:right;"'+R+fmt(r.x_studio_cuota_total)+L+'/td'+R+L+'td style="padding:8px;color: #1E3A5F;text-align:right;"'+R+fmt(r.x_studio_capital_programado)+L+'/td'+R+L+'td style="padding:8px;color: #1E3A5F;text-align:right;"'+R+fmt(r.x_studio_interes_programado)+L+'/td'+R+L+'td style="padding:8px;color: #1E3A5F;text-align:right;"'+R+fmt(r.x_studio_saldo_programado)+L+'/td'+R+L+'td style="padding:8px;text-align:center;"'+R+badge+L+'/td'+R+L+'/tr'+R;
});
tb+=L+'/tbody'+R+L+'/table'+R;
document.getElementById('amort-table').innerHTML=tb;
});
});
});
})();