// js/components/widgets/freteiros/CarrierCrudModal.jsx
// [Wave 31 v224.12 NUCLEAR EXTRACT 2026-05-24] Modal CRUD novo/editar freteiro
// Extract LITERAL de Freteiros.jsx L361-413
//
// Props:
//   - modalCRUD ('new' | 'edit' | null) — tipo do modal aberto
//   - crudForm, setCrudForm — controla form state (vem do main)
//   - crudSaving — inflight flag (disabled buttons)
//   - onSubmit — handler que executa handleSubmitCrud RPC (vem do main)
//   - onClose — handler que fecha modal (vem do main)
// Deps lazy: window.Modal (global), ZNX.widgets.freteiros.SharedComponents.Field
(function(){
  'use strict';
  function CarrierCrudModal(props){
    const Modal = window.Modal;
    const SC = window.ZNX && window.ZNX.widgets && window.ZNX.widgets.freteiros && window.ZNX.widgets.freteiros.SharedComponents;
    const Field = SC && SC.Field;
    const { modalCRUD, crudForm, setCrudForm, crudSaving, onSubmit, onClose } = props;
    if(!modalCRUD || !Modal || !Field) return null;

    return (
      <Modal title={modalCRUD === 'new' ? 'Novo Freteiro' : 'Editar Freteiro'} onClose={onClose}>
        <div style={{ display: 'flex', flexDirection: 'column', gap: 12 }}>
          <Field label="Nome*">
            <input type="text" value={crudForm.name} onChange={e => setCrudForm(f => ({ ...f, name: e.target.value }))}
                   placeholder="Ex: Gabriel Barreto" autoFocus />
          </Field>
          <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 12 }}>
            <Field label="CPF/CI Paraguay">
              <input type="text" value={crudForm.document} onChange={e => setCrudForm(f => ({ ...f, document: e.target.value }))}
                     placeholder="Ex: 123456789" />
            </Field>
            <Field label="WhatsApp">
              <input type="text" value={crudForm.phone} onChange={e => setCrudForm(f => ({ ...f, phone: e.target.value }))}
                     placeholder="+595 ..." />
            </Field>
          </div>
          <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 12 }}>
            <Field label={modalCRUD === 'new' ? 'Garantia USD*' : 'Garantia USD (read-only — use Saque)'}>
              <input
                type="number" min="0" step="0.01"
                value={crudForm.guarantee_usd}
                onChange={e => setCrudForm(f => ({ ...f, guarantee_usd: e.target.value }))}
                disabled={modalCRUD === 'edit'}
                style={modalCRUD === 'edit' ? { background: '#F3F4F6', color: '#9CA3AF' } : {}}
              />
            </Field>
            <Field label="Comissão %*">
              <input type="number" min="0" max="100" step="0.1"
                     value={crudForm.commission_pct}
                     onChange={e => setCrudForm(f => ({ ...f, commission_pct: e.target.value }))} />
            </Field>
          </div>
          <Field label="Notas">
            <textarea value={crudForm.notes} onChange={e => setCrudForm(f => ({ ...f, notes: e.target.value }))}
                      rows={3} placeholder="Observações internas (opcional)" style={{ width: '100%', resize: 'vertical' }} />
          </Field>
          {modalCRUD === 'edit' && (
            <label style={{ display: 'flex', alignItems: 'center', gap: 8, cursor: 'pointer' }}>
              <input type="checkbox" checked={crudForm.active}
                     onChange={e => setCrudForm(f => ({ ...f, active: e.target.checked }))} />
              <span style={{ fontSize: 13 }}>Freteiro ativo</span>
            </label>
          )}
          <div style={{ display: 'flex', gap: 10, justifyContent: 'flex-end', marginTop: 8 }}>
            <button className="btn-outline" onClick={onClose} disabled={crudSaving}>Cancelar</button>
            <button className="btn-gold" onClick={onSubmit} disabled={crudSaving}>
              {crudSaving ? '⏳ Salvando...' : (modalCRUD === 'new' ? '✓ Cadastrar' : '✓ Salvar')}
            </button>
          </div>
        </div>
      </Modal>
    );
  }
  window.ZNX = window.ZNX || {};
  window.ZNX.widgets = window.ZNX.widgets || {};
  window.ZNX.widgets.freteiros = window.ZNX.widgets.freteiros || {};
  window.ZNX.widgets.freteiros.CarrierCrudModal = CarrierCrudModal;
  // [Wave 31 marker v224.12] confirma CarrierCrudModal executado
  window.FreteirosCarrierCrudModal_v224_12_wave31 = true;
})();
