#!/usr/bin/env bash
set -euo pipefail
cd /home/iotcuboc/pyme || exit 1

echo "=============================================="
echo " ElaboraPro Presupuesto Editar V4.17"
echo " Quitar -> Eliminar rojo"
echo "=============================================="

F="public/presupuesto_editar_v3.php"
[ -f "$F" ] || { echo "ERROR: no existe $F"; exit 1; }

TS=$(date +%Y%m%d_%H%M%S)
B="backups/elaborapro_presupuesto_editar_v4_17_$TS"
mkdir -p "$B"; cp -p "$F" "$B/"
echo "Backup: /home/iotcuboc/pyme/$B"

TMP=$(mktemp); trap 'rm -f "$TMP"' EXIT; cp -p "$F" "$TMP"

if ! TMP="$TMP" php <<'PHP'
<?php
$f=getenv('TMP'); $s=file_get_contents($f);
if($s===false){fwrite(STDERR,"ERROR leyendo archivo.\n");exit(10);}
if(strpos($s,'ELABORAPRO_PRESUPUESTO_EDITAR_V4_17')!==false){
 fwrite(STDERR,"ERROR: V4.17 ya aplicado.\n");exit(11);
}

/* Busca el control Quitar, sea enlace o botón, y preserva todos sus atributos/lógica. */
$pat='~<(a|button)\b([^>]*)>\s*Quitar\s*</\1>~isu';
$n=preg_match_all($pat,$s);
if($n!==1){
 fwrite(STDERR,"ERROR: se esperaba 1 control Quitar; encontrados=$n\n");exit(20);
}
$s=preg_replace_callback($pat,function($m){
 $tag=$m[1]; $attrs=$m[2];
 if(preg_match('~\bclass=(["\'])(.*?)\1~is',$attrs)){
   $attrs=preg_replace('~\bclass=(["\'])(.*?)\1~is','class=$1btn ep-budget-delete$1',$attrs,1);
 } else {
   $attrs=' class="btn ep-budget-delete"'.$attrs;
 }
 return '<'.$tag.$attrs.'>Eliminar</'.$tag.'>';
},$s,1,$changed);
if($changed!==1){fwrite(STDERR,"ERROR modificando Quitar.\n");exit(21);}

$css=<<<'CSS'

<style>
/* ELABORAPRO_PRESUPUESTO_EDITAR_V4_17 */
body.op3-body .op3-main .ep-budget-delete{
display:inline-flex!important;align-items:center!important;justify-content:center!important;
min-height:34px!important;padding:6px 12px!important;border-radius:8px!important;
background:#F3C2BE!important;border:1px solid #EAB0AB!important;color:#8B302C!important;
text-decoration:none!important;white-space:nowrap!important;box-shadow:none!important;font-weight:600!important}
body.op3-body .op3-main .ep-budget-delete:hover{
background:#ECB3AE!important;border-color:#DFA39E!important;color:#7A2926!important}
</style>

CSS;
$footer='~<\?php\s+render_footer_v3\(\);\s*\?>~';
if(preg_match_all($footer,$s)!==1){fwrite(STDERR,"ERROR: footer inesperado.\n");exit(30);}
$s=preg_replace($footer,$css.'<?php render_footer_v3(); ?>',$s,1);

foreach(['ELABORAPRO_PRESUPUESTO_EDITAR_V4_17','ep-budget-delete','>Eliminar<'] as $x){
 if(strpos($s,$x)===false){fwrite(STDERR,"ERROR validación: $x\n");exit(40);}
}
if(preg_match('~>\s*Quitar\s*<~iu',$s)){fwrite(STDERR,"ERROR: todavía existe Quitar.\n");exit(41);}
if(file_put_contents($f,$s)===false){fwrite(STDERR,"ERROR escribiendo temporal.\n");exit(50);}
echo "OK: V4.17 preparado en temporal.\n";
PHP
then
 echo "ERROR: parche abortado. No se instaló ningún cambio."
 exit 1
fi

echo "===== LINT TEMPORAL ====="
php -l "$TMP"
echo "===== VALIDACION ====="
grep -n -B3 -A4 -E 'Eliminar|ep-budget-delete|ELABORAPRO_PRESUPUESTO_EDITAR_V4_17' "$TMP" | tail -50

cp -p "$TMP" "$F"
if ! php -l "$F"; then
 echo "ERROR: restaurando respaldo..."
 cp -p "$B/presupuesto_editar_v3.php" "$F"; exit 1
fi

echo "=============================================="
echo " OK - Presupuesto Editar V4.17 instalado"
echo " Quitar -> Eliminar"
echo " Eliminar = rojo pastel"
echo " Lógica original preservada"
echo "=============================================="
