// js/components/widgets/DenyCloseRequestModal.jsx
// [Wave 13 v223.38 20260520] Extraído de AprovacoesFrete.jsx L546-568
// Modal negar request com razão obrigatória
//
// Props: selectedRequest, selectedNote, denyReason, setDenyReason, inFlight, onConfirm, onClose
// Deps runtime: window.Modal, window.FreightTypeBadge
(function(){
  'use strict';

  function DenyCloseRequestModal(props){
    const selectedRequest = props.selectedRequest;
    const selectedNote = props.selectedNote;
    const denyReason = props.denyReason;
    const setDenyReason = props.setDenyReason;
    const inFlight = props.inFlight;
    const onConfirm = props.onConfirm;
    const onClose = props.onClose;
    const Modal = window.Modal;
    const TypeBadge = window.FreightTypeBadge;

    if (!Modal) {
      console.error('[ZNX v224.71] Modal ausente');
      if(window.Sentry) window.Sentry.captureMessage('[v224.71] Modal undefined em widget', 'warning');
      if(typeof window.ZNX_toast === 'function') window.ZNX_toast('⚠ Modal indisponível · pressione Ctrl+Shift+R', 'warning');
      return null;
    }
    if (!selectedRequest) return null;

    function Field(p) {
      return (
        <label style={{ display: 'flex', flexDirection: 'column', gap: 4 }}>
          <span style={{ fontSize: 12, fontWeight: 600, color: '#374151' }}>{p.label}</span>
          {p.children}
        </label>
      );
    }

    return (
      <Modal title="Negar solicitação" onClose={onClose}>
        <div style={{ display: 'flex', flexDirection: 'column', gap: 12 }}>
          <div style={{ padding: 12, background: '#FEE2E2', borderRadius: 8, fontSize: 12, color: '#7F1D1D' }}>
            Negar {TypeBadge ? <TypeBadge type={selectedRequest.type}/> : <strong>{selectedRequest.type}</strong>} da nota
            <strong> {(selectedNote && selectedNote.number) || '—'}</strong>.
            {selectedNote && selectedNote.status === 'EM_DISPUTA' && ' Nota volta pra EM_RECEBIMENTO automaticamente.'}
          </div>
          <Field label="Razão da negação*">
            <textarea value={denyReason} onChange={e => setDenyReason(e.target.value)}
                      rows={3} placeholder="Explique por que está negando — Abbes vê esta razão"
                      style={{ width: '100%', resize: 'vertical' }} autoFocus />
          </Field>
          <div style={{ display: 'flex', gap: 10, justifyContent: 'flex-end', marginTop: 4 }}>
            <button className="btn-outline" onClick={onClose} disabled={inFlight}>Cancelar</button>
            <button className="btn-danger" onClick={onConfirm} disabled={inFlight}>
              {inFlight ? '⏳ Negando...' : '✗ Confirmar negação'}
            </button>
          </div>
        </div>
      </Modal>
    );
  }

  window.ZNX = window.ZNX || {};
  window.ZNX.components = window.ZNX.components || {};
  window.ZNX.components.DenyCloseRequestModal = DenyCloseRequestModal;
  window.DenyCloseRequestModal = DenyCloseRequestModal;
})();
