
<?php $__env->startSection('title', 'Nuevo Empleado'); ?>

<?php $__env->startSection('content'); ?>
<div class="d-flex justify-content-between align-items-center mb-3">
    <h5 class="mb-0"><i class="bi bi-person-plus me-2"></i>Nuevo Empleado</h5>
    <a href="<?php echo e(route('empleados.index')); ?>" class="btn btn-outline-secondary btn-sm">
        <i class="bi bi-arrow-left me-1"></i> Volver
    </a>
</div>

<form action="<?php echo e(route('empleados.store')); ?>" method="POST">
<?php echo csrf_field(); ?>
<div class="row g-3">


<div class="col-lg-8">

  
  <div class="card mb-3">
    <div class="card-header"><i class="bi bi-person me-1"></i> Datos Personales</div>
    <div class="card-body">
      <div class="row g-3">
        <div class="col-md-6">
          <label class="form-label fw-semibold">Nombre completo <span class="text-danger">*</span></label>
          <input type="text" name="nombre" class="form-control <?php $__errorArgs = ['nombre'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>"
                 value="<?php echo e(old('nombre')); ?>" required>
          <?php $__errorArgs = ['nombre'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?><div class="invalid-feedback"><?php echo e($message); ?></div><?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
        </div>
        <div class="col-md-6">
          <label class="form-label fw-semibold">Cédula <span class="text-danger">*</span></label>
          <input type="text" name="cedula" class="form-control <?php $__errorArgs = ['cedula'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>"
                 value="<?php echo e(old('cedula')); ?>" required>
          <?php $__errorArgs = ['cedula'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?><div class="invalid-feedback"><?php echo e($message); ?></div><?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
        </div>
        <div class="col-md-6">
          <label class="form-label fw-semibold">Cargo</label>
          <input type="text" name="cargo" class="form-control" value="<?php echo e(old('cargo')); ?>">
        </div>
        <div class="col-md-3">
          <label class="form-label fw-semibold">Tipo contrato</label>
          <select name="tipo_contrato" class="form-select">
            <option value="indefinido" <?php echo e(old('tipo_contrato','indefinido')==='indefinido'?'selected':''); ?>>Indefinido</option>
            <option value="fijo"       <?php echo e(old('tipo_contrato')==='fijo'?'selected':''); ?>>Término fijo</option>
            <option value="obra"       <?php echo e(old('tipo_contrato')==='obra'?'selected':''); ?>>Obra o labor</option>
            <option value="prestacion" <?php echo e(old('tipo_contrato')==='prestacion'?'selected':''); ?>>Prestación servicios</option>
          </select>
        </div>
        <div class="col-md-3">
          <label class="form-label fw-semibold">Fecha ingreso</label>
          <input type="date" name="fecha_ingreso" class="form-control" value="<?php echo e(old('fecha_ingreso')); ?>">
        </div>
      </div>
    </div>
  </div>

  
  <div class="card mb-3">
    <div class="card-header"><i class="bi bi-cash-coin me-1"></i> Salario y Beneficios</div>
    <div class="card-body">
      <div class="row g-3">
        <div class="col-md-4">
          <label class="form-label fw-semibold">Salario base <span class="text-danger">*</span></label>
          <div class="input-group">
            <span class="input-group-text">$</span>
            <input type="number" name="salario" class="form-control <?php $__errorArgs = ['salario'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>"
                   value="<?php echo e(old('salario',0)); ?>" min="0" step="1000" required>
          </div>
          <?php $__errorArgs = ['salario'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?><div class="invalid-feedback"><?php echo e($message); ?></div><?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
        </div>
        <div class="col-md-4">
          <label class="form-label fw-semibold">Medicina prepagada</label>
          <div class="input-group">
            <span class="input-group-text">$</span>
            <input type="number" name="medicina_prepagada" class="form-control"
                   value="<?php echo e(old('medicina_prepagada',0)); ?>" min="0" step="1000">
          </div>
        </div>
        <div class="col-md-4">
          <label class="form-label fw-semibold">Auxilio alimentación</label>
          <div class="input-group">
            <span class="input-group-text">$</span>
            <input type="number" name="alimentacion" class="form-control"
                   value="<?php echo e(old('alimentacion',0)); ?>" min="0" step="1000">
          </div>
        </div>
        <div class="col-md-4">
          <label class="form-label fw-semibold">Zafiro</label>
          <div class="input-group">
            <span class="input-group-text">$</span>
            <input type="number" name="zafiro" class="form-control"
                   value="<?php echo e(old('zafiro',0)); ?>" min="0" step="1000">
          </div>
        </div>
        <div class="col-md-4">
          <label class="form-label fw-semibold">ARL %
            <small class="text-muted fw-normal">(Riesgo I=0.522)</small>
          </label>
          <input type="number" name="arl_porcentaje" class="form-control"
                 value="<?php echo e(old('arl_porcentaje',0.5220)); ?>" min="0" max="10" step="0.001">
        </div>
        <div class="col-md-4 d-flex align-items-end">
          <div class="form-check form-switch mb-2">
            <input class="form-check-input" type="checkbox" name="aux_transporte" id="auxTransporte" value="1"
                   <?php echo e(old('aux_transporte') ? 'checked' : ''); ?>>
            <label class="form-check-label" for="auxTransporte">
              Auxilio de transporte<br>
              <small class="text-muted">SMLV ≤ 2 ($202.100)</small>
            </label>
          </div>
        </div>
      </div>

      
      <div class="alert alert-light border mt-3 mb-0 p-2" id="resumenCosto" style="font-size:.83rem">
        <div class="row text-center">
          <div class="col-4">
            <div class="fw-semibold text-primary" id="costoMensual">$ 0</div>
            <div class="text-muted">Costo real/mes</div>
          </div>
          <div class="col-4">
            <div class="fw-semibold text-success" id="valorHoraReal">$ 0</div>
            <div class="text-muted">Valor hora real</div>
          </div>
          <div class="col-4">
            <div class="fw-semibold text-secondary" id="cargaPct">51.85%</div>
            <div class="text-muted">Carga prestacional</div>
          </div>
        </div>
      </div>
    </div>
  </div>

  
  <div class="card mb-3">
    <div class="card-header"><i class="bi bi-shield-lock me-1"></i> Acceso al Sistema</div>
    <div class="card-body">
      <div class="row g-3">
        <div class="col-md-5">
          <label class="form-label fw-semibold">Correo electrónico</label>
          <input type="email" name="email" class="form-control <?php $__errorArgs = ['email'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>"
                 value="<?php echo e(old('email')); ?>" placeholder="correo@gessig.com">
          <div class="form-text">Dejar vacío si no tendrá acceso al sistema.</div>
          <?php $__errorArgs = ['email'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?><div class="invalid-feedback"><?php echo e($message); ?></div><?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
        </div>
        <div class="col-md-4">
          <label class="form-label fw-semibold">Contraseña</label>
          <input type="text" name="password" class="form-control" placeholder="Mínimo 6 caracteres">
          <div class="form-text">Vacío = usa la cédula como clave.</div>
        </div>
        <div class="col-md-3">
          <label class="form-label fw-semibold">Rol</label>
          <select name="rol" class="form-select">
            <option value="empleado"   <?php echo e(old('rol','empleado')==='empleado'?'selected':''); ?>>Empleado</option>
            <option value="supervisor" <?php echo e(old('rol')==='supervisor'?'selected':''); ?>>Supervisor</option>
            <option value="admin"      <?php echo e(old('rol')==='admin'?'selected':''); ?>>Administrador</option>
          </select>
        </div>
      </div>
    </div>
  </div>

</div>


<div class="col-lg-4">
  <div class="card mb-3 h-auto">
    <div class="card-header d-flex justify-content-between align-items-center">
      <span><i class="bi bi-diagram-3 me-1"></i> Centros de Costo</span>
      <div class="d-flex gap-1">
        <button type="button" class="btn btn-xs btn-outline-secondary" onclick="toggleCC(true)" style="font-size:11px;padding:1px 6px">Todos</button>
        <button type="button" class="btn btn-xs btn-outline-secondary" onclick="toggleCC(false)" style="font-size:11px;padding:1px 6px">Ninguno</button>
      </div>
    </div>
    <div class="card-body p-2" style="max-height:500px;overflow-y:auto">
      <div class="form-text mb-2 px-1">Todos vienen seleccionados. Desmarque los que NO apliquen para este empleado.</div>
      
      <input type="hidden" name="cc_enviado" value="1">
      <?php
        // Primer cargue: todos marcados. Si el formulario se reenvió por un
        // error de validación, se respeta lo que el usuario dejó marcado.
        $ccSeleccionados = old('cc_enviado') ? (array) old('centros_costo', []) : null;
      ?>
      <?php $__currentLoopData = $centrosCosto->groupBy('area'); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $area => $ccs): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
      <div class="mb-2">
        <div style="font-size:11px;font-weight:600;text-transform:uppercase;color:#999;padding:4px 4px 2px"><?php echo e($area ?? 'General'); ?></div>
        <?php $__currentLoopData = $ccs; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $cc): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
        <div class="form-check form-check-sm" style="padding-left:1.5rem">
          <input class="form-check-input cc-check" type="checkbox"
                 name="centros_costo[]" value="<?php echo e($cc->id); ?>"
                 id="cc_<?php echo e($cc->id); ?>"
                 <?php echo e(is_null($ccSeleccionados) || in_array($cc->id, $ccSeleccionados) ? 'checked' : ''); ?>>
          <label class="form-check-label" for="cc_<?php echo e($cc->id); ?>" style="font-size:13px">
            <?php echo e($cc->nombre); ?>

            <?php if($cc->es_exento): ?><span class="badge badge-exento" style="font-size:10px">ex</span><?php endif; ?>
          </label>
        </div>
        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
      </div>
      <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
    </div>
  </div>

  
  <div class="card">
    <div class="card-body">
      <div class="form-check form-switch">
        <input class="form-check-input" type="checkbox" name="activo" id="activo" value="1"
               <?php echo e(old('activo', '1') ? 'checked' : ''); ?>>
        <label class="form-check-label fw-semibold" for="activo">Empleado activo</label>
      </div>
    </div>
  </div>
</div>

</div>

<div class="d-flex gap-2 mt-2 mb-4">
  <button type="submit" class="btn btn-primary">
    <i class="bi bi-check-lg me-1"></i> Crear Empleado
  </button>
  <a href="<?php echo e(route('empleados.index')); ?>" class="btn btn-outline-secondary">Cancelar</a>
</div>
</form>

<?php $__env->startPush('scripts'); ?>
<script>
function toggleCC(check) {
    document.querySelectorAll('.cc-check').forEach(c => c.checked = check);
}

// Cálculo en tiempo real del costo laboral
const TASA = 0.5185; // 51.85% fijo + ARL variable
const HORAS_MES = 190;

function calcularCosto() {
    const salario = parseFloat(document.querySelector('[name=salario]').value) || 0;
    const medicina = parseFloat(document.querySelector('[name=medicina_prepagada]').value) || 0;
    const alim = parseFloat(document.querySelector('[name=alimentacion]').value) || 0;
    const zafiro = parseFloat(document.querySelector('[name=zafiro]').value) || 0;
    const arl = parseFloat(document.querySelector('[name=arl_porcentaje]').value) || 0.522;
    const auxTrans = document.getElementById('auxTransporte').checked ? 202100 : 0;

    const prestaciones = salario * (TASA - 0.0052 + arl/100); // reemplaza ARL base con el ingresado
    const total = salario + prestaciones + medicina + alim + zafiro + auxTrans;
    const horaReal = HORAS_MES > 0 ? total / HORAS_MES : 0;
    const pct = salario > 0 ? ((prestaciones / salario) * 100).toFixed(1) : '0.0';

    const fmt = v => '$ ' + Math.round(v).toLocaleString('es-CO');
    document.getElementById('costoMensual').textContent = fmt(total);
    document.getElementById('valorHoraReal').textContent = fmt(horaReal);
    document.getElementById('cargaPct').textContent = pct + '%';
}

document.querySelectorAll('[name=salario],[name=medicina_prepagada],[name=alimentacion],[name=zafiro],[name=arl_porcentaje]')
    .forEach(el => el.addEventListener('input', calcularCosto));
document.getElementById('auxTransporte').addEventListener('change', calcularCosto);
calcularCosto();
</script>
<?php $__env->stopPush(); ?>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.app', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /home/evolucionamos/public_html/ges-chronos/app/hoja-tiempo/resources/views/empleados/create.blade.php ENDPATH**/ ?>