+page.svelte 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  1. <script>
  2. import { browser } from '$app/environment';
  3. import { onDestroy, onMount, tick } from 'svelte';
  4. import { get } from 'svelte/store';
  5. import Header from '$lib/layout/Header.svelte';
  6. import Tabs from '$lib/components/Tabs.svelte';
  7. import RegisterCpr from '$lib/components/commodities/cpr/RegisterCpr.svelte';
  8. import ContractCpr from '$lib/components/commodities/cpr/ContractCpr.svelte';
  9. import EmissionCpr from '$lib/components/commodities/cpr/EmissionCpr.svelte';
  10. import CprDetailModal from '$lib/components/commodities/cpr/CprDetailModal.svelte';
  11. import { authToken, companyId as companyIdStore } from '$lib/utils/stores';
  12. const apiUrl = import.meta.env.VITE_API_URL;
  13. const fieldKeys = [
  14. 'cpr_contract_code',
  15. 'cpr_contract_number',
  16. 'cpr_number',
  17. 'cpr_self_number',
  18. 'cpr_ipoc_code',
  19. 'cpr_calculation_type_code',
  20. 'cpr_initial_exchange_value',
  21. 'cpr_fixing_type_code',
  22. 'cpr_data_source_type_code',
  23. 'cpr_adjustment_frequency_type_code',
  24. 'cpr_adjustment_pro_rata_type_code',
  25. 'cpr_adjustment_type_code',
  26. 'cpr_maturity_date',
  27. 'cpr_reference_date',
  28. 'cpr_payment_start_date',
  29. 'cpr_amortization_start_date',
  30. 'cpr_interest_payment_date',
  31. 'cpr_issue_value',
  32. 'cpr_unit_value',
  33. 'cpr_unit_price_value',
  34. 'cpr_interest_unit_price_value',
  35. 'cpr_residual_value',
  36. 'cpr_amortization_percentage',
  37. 'cpr_event_quantity',
  38. 'cpr_payment_method_code',
  39. 'cpr_index_code',
  40. 'cpr_index_short_name',
  41. 'cpr_vcp_indicator_type_code',
  42. 'cpr_indexador_percentage_value',
  43. 'cpr_interest_rate_spread_percentage',
  44. 'cpr_interest_rate_criteria_type_code',
  45. 'cpr_interest_payment_value',
  46. 'cpr_interest_payment_frequency_code',
  47. 'cpr_interest_months_quantity',
  48. 'cpr_interestPaymentFlow_time_unit_type_code',
  49. 'cpr_interestPaymentFlow_deadline_type_code',
  50. 'cpr_amortization_type_code',
  51. 'cpr_amortization_months_quantity',
  52. 'cpr_amortizationPaymentFlow_time_unit_type_code',
  53. 'cpr_amortizationPaymentFlow_deadline_type_code',
  54. 'cpr_additional_text',
  55. 'cpr_internal_control_number',
  56. 'cpr_isin_code',
  57. // 'cpr_type_code',
  58. // 'cpr_otc_register_account_code',
  59. // 'cpr_otc_payment_agent_account_code',
  60. // 'cpr_otc_custodian_account_code',
  61. 'cpr_otc_favored_account_code',
  62. 'cpr_settlement_modality_type_code',
  63. 'cpr_otc_settlement_bank_account_code',
  64. 'cpr_ballast_type_code',
  65. 'cpr_lot_number',
  66. 'cpr_ballast_quantity',
  67. 'cpr_currency_code',
  68. 'cpr_transaction_identification',
  69. 'cpr_guarantee_limit_type_code',
  70. 'cpr_mother_code',
  71. 'cpr_deposit_quantity',
  72. 'cpr_deposit_unit_price_value',
  73. 'cpr_deposit_person_type_acronym',
  74. 'cpr_deposit_document_number',
  75. 'cpr_event_type_code',
  76. 'cpr_event_original_date',
  77. 'cpr_operation_modality_type_code',
  78. 'cpr_bacen_reference_code',
  79. 'cpr_children_codes',
  80. // 'cpr_scr_type_code',
  81. // 'cpr_finality_code',
  82. 'cpr_scr_customer_detail',
  83. 'cpr_scr_person_type_acronym',
  84. 'cpr_scr_document_number',
  85. 'cpr_issuer_name',
  86. 'cpr_place_name',
  87. 'cpr_document_deadline_days_number',
  88. 'cpr_deliveryPlace_state_acronym',
  89. 'cpr_deliveryPlace_city_name',
  90. 'cpr_deliveryPlace_ibge_code',
  91. 'cpr_issuer_legal_nature_code',
  92. 'cpr_issuers_person_type_acronym',
  93. 'cpr_issuers_document_number',
  94. 'cpr_issuers_state_acronym',
  95. 'cpr_issuers_city_name',
  96. 'cpr_issuers_ibge_code',
  97. 'cpr_collateral_type_code',
  98. 'cpr_collateral_type_name',
  99. 'cpr_constitution_process_indicator',
  100. 'cpr_otc_bondsman_account_code',
  101. 'cpr_collaterals_document_number',
  102. 'cpr_product_name',
  103. 'cpr_product_class_name',
  104. 'cpr_product_harvest',
  105. 'cpr_product_quantity',
  106. 'cpr_measure_unit_name',
  107. 'cpr_packaging_way_name',
  108. 'cpr_product_status_code',
  109. 'cpr_production_type_code',
  110. 'cpr_product_description',
  111. 'cpr_production_place_name',
  112. 'cpr_property_registration_number',
  113. 'cpr_notary_name',
  114. 'cpr_total_production_area_in_hectares_number',
  115. 'cpr_total_area_in_hectares_number',
  116. 'cpr_car_code',
  117. 'cpr_latitude_code',
  118. 'cpr_longitude_code',
  119. 'cpr_zip_code',
  120. 'cpr_green_cpr_indicator',
  121. 'cpr_green_cpr_certificate_name',
  122. 'cpr_green_cpr_certificate_cnpj_number',
  123. 'cpr_green_cpr_declaration_indicator',
  124. 'cpr_green_cpr_georeferencing_description'
  125. ];
  126. const allFieldKeys = Array.from(new Set(fieldKeys));
  127. const requiredFields = new Set([
  128. 'cpr_maturity_date',
  129. 'cpr_issue_value',
  130. 'cpr_collateral_type_code',
  131. 'cpr_collateral_type_name',
  132. 'cpr_constitution_process_indicator',
  133. 'cpr_product_name',
  134. 'cpr_product_class_name',
  135. 'cpr_product_harvest',
  136. 'cpr_product_description',
  137. 'cpr_product_quantity',
  138. 'cpr_measure_unit_name',
  139. 'cpr_packaging_way_name',
  140. 'cpr_product_status_code',
  141. 'cpr_production_type_code',
  142. 'cpr_place_name',
  143. 'cpr_deliveryPlace_state_acronym',
  144. 'cpr_deliveryPlace_city_name',
  145. 'cpr_issuer_legal_nature_code',
  146. 'cpr_issuers_person_type_acronym',
  147. 'cpr_issuers_document_number',
  148. 'cpr_issuers_state_acronym',
  149. 'cpr_issuers_city_name',
  150. 'cpr_contract_code',
  151. 'cpr_production_place_name',
  152. 'cpr_zip_code'
  153. ]);
  154. const repeatingGroupDefinitions = [
  155. {
  156. key: 'issuers',
  157. title: 'Dados dos emissores',
  158. description: 'Adicione quantos emissores forem necessários.',
  159. itemLabel: 'Emissor',
  160. addLabel: 'Adicionar emissor',
  161. columns: 2,
  162. fields: [
  163. { key: 'cpr_issuer_name', label: 'Nome / (Razão Social do Emissor)' },
  164. { key: 'cpr_issuers_document_number', label: 'Documento (CPF/CNPJ)' },
  165. {
  166. key: 'cpr_issuers_person_type_acronym',
  167. label: 'Tipo de pessoa',
  168. type: 'select',
  169. options: [
  170. { label: 'Selecione...', value: '' },
  171. { label: 'Pessoa Jurídica (PJ)', value: 'PJ' },
  172. { label: 'Pessoa Física (PF)', value: 'PF' }
  173. ]
  174. },
  175. {
  176. key: 'cpr_issuer_legal_nature_code',
  177. label: 'Natureza jurídica',
  178. type: 'select',
  179. options: [
  180. { label: 'Selecione...', value: '' },
  181. { label: 'Produtor Rural', value: '02' }
  182. ]
  183. },
  184. { key: 'cpr_issuers_state_acronym', label: 'Estado' },
  185. { key: 'cpr_issuers_city_name', label: 'Cidade' }
  186. ]
  187. },
  188. {
  189. key: 'collaterals',
  190. title: 'Garantias e colaterais',
  191. description: 'Informe todos os colaterais vinculados a esta CPR.',
  192. itemLabel: 'Colateral',
  193. addLabel: 'Adicionar colateral',
  194. columns: 2,
  195. fields: [
  196. {
  197. key: 'cpr_collateral_type_code',
  198. label: 'Código do colateral',
  199. type: 'select',
  200. options: [
  201. { label: 'Selecione...', value: '' },
  202. { label: 'Penhor', value: '6' },
  203. { label: 'Alienação', value: '7' }
  204. ]
  205. },
  206. { key: 'cpr_collateral_type_name', label: 'Descrição do colateral' },
  207. { key: 'cpr_constitution_process_indicator', label: 'Processo constituído', type: 'select', options: [
  208. { label: 'Selecione...', value: '' },
  209. { label: 'Sim', value: 'S' },
  210. { label: 'Não', value: 'N' }
  211. ] },
  212. { key: 'cpr_otc_bondsman_account_code', label: 'Conta OTC do fiador' }
  213. ]
  214. },
  215. {
  216. key: 'productionPlaces',
  217. title: 'Locais de produção',
  218. description: 'Cadastre cada propriedade vinculada ao lastro.',
  219. itemLabel: 'Propriedade',
  220. addLabel: 'Adicionar propriedade',
  221. columns: 3,
  222. fields: [
  223. { key: 'cpr_production_place_name', label: 'Nome da propriedade' },
  224. { key: 'cpr_property_registration_number', label: 'Registro da propriedade' },
  225. { key: 'cpr_notary_name', label: 'Cartório' },
  226. { key: 'cpr_total_production_area_in_hectares_number', label: 'Área de produção (ha)' },
  227. { key: 'cpr_total_area_in_hectares_number', label: 'Área total (ha)' },
  228. { key: 'cpr_car_code', label: 'Código CAR' },
  229. { key: 'cpr_latitude_code', label: 'Latitude' },
  230. { key: 'cpr_longitude_code', label: 'Longitude' },
  231. { key: 'cpr_zip_code', label: 'CEP' }
  232. ]
  233. }
  234. ];
  235. const repeatingFieldToGroup = repeatingGroupDefinitions.reduce((acc, config) => {
  236. config.fields.forEach((field) => {
  237. acc[field.key] = config.key;
  238. });
  239. return acc;
  240. }, {});
  241. function createEmptyRepeatingEntry(config) {
  242. return config.fields.reduce((entry, field) => {
  243. entry[field.key] = '';
  244. return entry;
  245. }, {});
  246. }
  247. const createInitialRepeatingGroups = () => {
  248. const groups = {};
  249. repeatingGroupDefinitions.forEach((config) => {
  250. groups[config.key] = [createEmptyRepeatingEntry(config)];
  251. });
  252. return groups;
  253. };
  254. const createInitialForm = () =>
  255. allFieldKeys.reduce((acc, key) => {
  256. acc[key] = '';
  257. return acc;
  258. }, {});
  259. let cprForm = createInitialForm();
  260. let repeatingGroups = createInitialRepeatingGroups();
  261. let submitError = '';
  262. let submitSuccess = '';
  263. let isSubmitting = false;
  264. const PAYMENT_STORAGE_KEY = 'tooeasy_cpr_payment';
  265. const PAYMENT_TIMEOUT_MS = 30 * 60 * 1000;
  266. const PAYMENT_POLL_INTERVAL_MS = 10_000;
  267. let paymentModalVisible = false;
  268. let paymentCode = '';
  269. let paymentId = null;
  270. let paymentError = '';
  271. let paymentStatusMessage = '';
  272. let paymentCopyFeedback = '';
  273. let paymentCountdownMs = PAYMENT_TIMEOUT_MS;
  274. let paymentStartedAt = null;
  275. let paymentLoadingVisible = false;
  276. let paymentQrContainer;
  277. let paymentQrInstance = null;
  278. let paymentPollIntervalId = null;
  279. let paymentCountdownIntervalId = null;
  280. let paymentCopyTimeoutId = null;
  281. let isCheckingPayment = false;
  282. let paymentSuccessOverlayVisible = false;
  283. let paymentSuccessMessage = '';
  284. const breadcrumb = [{ label: 'Início' }, { label: 'CPR', active: true }];
  285. let activeTab = 4;
  286. const tabs = ['Contrato', 'Registro', 'Emissão'];
  287. const historyEndpoint = `${apiUrl}/cpr/history`;
  288. const paymentConfirmEndpoint = `${apiUrl}/b3/payment/confirm`;
  289. const historyColumns = [
  290. { key: 'cpr_id', label: 'CPR ID' },
  291. { key: 'cpr_product_class_name', label: 'Produto' },
  292. { key: 'cpr_issue_date', label: 'Data de emissão' },
  293. { key: 'cpr_issuer_name', label: 'Emitente' },
  294. { key: 'cpr_issue_financial_value', label: 'Valor' }
  295. ];
  296. let historyRows = [];
  297. let historyLoading = false;
  298. let historyError = '';
  299. let detailLoading = false;
  300. let detailError = '';
  301. let selectedDetail = null;
  302. let selectedDetailId = null;
  303. let showDetailModal = false;
  304. let historyInitialized = false;
  305. function handleFieldChange(key, value) {
  306. submitError = '';
  307. submitSuccess = '';
  308. cprForm = { ...cprForm, [key]: value ?? '' };
  309. }
  310. function cloneRepeatingEntries(groupKey) {
  311. const entries = repeatingGroups[groupKey] ?? [];
  312. return entries.map((entry) => ({ ...entry }));
  313. }
  314. function handleRepeatingFieldChange(groupKey, index, fieldKey, value) {
  315. submitError = '';
  316. submitSuccess = '';
  317. const nextEntries = cloneRepeatingEntries(groupKey);
  318. if (!nextEntries[index]) return;
  319. nextEntries[index] = {
  320. ...nextEntries[index],
  321. [fieldKey]: value ?? ''
  322. };
  323. repeatingGroups = {
  324. ...repeatingGroups,
  325. [groupKey]: nextEntries
  326. };
  327. }
  328. function handleAddRepeatingEntry(groupKey) {
  329. const config = repeatingGroupDefinitions.find((c) => c.key === groupKey);
  330. if (!config) return;
  331. const nextEntries = cloneRepeatingEntries(groupKey);
  332. nextEntries.push(createEmptyRepeatingEntry(config));
  333. repeatingGroups = {
  334. ...repeatingGroups,
  335. [groupKey]: nextEntries
  336. };
  337. }
  338. function handleRemoveRepeatingEntry(groupKey, index) {
  339. const nextEntries = cloneRepeatingEntries(groupKey);
  340. if (nextEntries.length <= 1) return;
  341. repeatingGroups = {
  342. ...repeatingGroups,
  343. [groupKey]: nextEntries.filter((_, i) => i !== index)
  344. };
  345. }
  346. function handleAddTop() {
  347. cprForm = createInitialForm();
  348. repeatingGroups = createInitialRepeatingGroups();
  349. submitError = '';
  350. submitSuccess = '';
  351. activeTab = 0;
  352. }
  353. function handleCancel() {
  354. activeTab = 4;
  355. submitError = '';
  356. }
  357. function ensureAuthContext() {
  358. const token = get(authToken);
  359. if (!token) {
  360. throw new Error('Sessão expirada. Faça login novamente.');
  361. }
  362. const company = Number(get(companyIdStore));
  363. if (!company || Number.isNaN(company) || company <= 0) {
  364. throw new Error('company_id inválido. Refaça o login.');
  365. }
  366. return { token, company_id: company };
  367. }
  368. async function parseJsonResponse(res) {
  369. const raw = await res.text();
  370. if (!raw) return null;
  371. const trimmed = raw.trim();
  372. if (trimmed.startsWith('<')) {
  373. console.error('Resposta HTML inesperada do endpoint de histórico/detalhe de CPR:', trimmed.slice(0, 300));
  374. const statusMsg =
  375. res.status === 404
  376. ? 'Endpoint /cpr/history não encontrado (404). Verifique a URL da API.'
  377. : `Resposta inesperada do servidor (status ${res.status}).`;
  378. throw new Error(statusMsg);
  379. }
  380. try {
  381. return JSON.parse(trimmed);
  382. } catch (err) {
  383. console.error('Resposta inválida do histórico de CPRs:', err, trimmed);
  384. throw new Error('Resposta inválida do servidor.');
  385. }
  386. }
  387. function normalizeHistoryList(payload) {
  388. if (Array.isArray(payload)) return payload;
  389. if (Array.isArray(payload?.data)) return payload.data;
  390. return [];
  391. }
  392. function sanitizeDetail(detail) {
  393. if (!detail || typeof detail !== 'object') return null;
  394. const cleaned = {};
  395. Object.entries(detail).forEach(([key, value]) => {
  396. if (value === null || value === undefined) return;
  397. if (typeof value === 'string' && value.trim().toUpperCase() === 'NA') return;
  398. cleaned[key] = value;
  399. });
  400. return Object.keys(cleaned).length ? cleaned : null;
  401. }
  402. function formatDetailValue(value) {
  403. if (value == null) return '—';
  404. if (typeof value === 'number') {
  405. return value.toLocaleString('pt-BR', { minimumFractionDigits: 0, maximumFractionDigits: 4 });
  406. }
  407. return String(value);
  408. }
  409. function humanizeKey(key = '') {
  410. if (!key) return '';
  411. return key
  412. .replace(/^cpr_/i, '')
  413. .replace(/_/g, ' ')
  414. .replace(/\b\w/g, (match) => match.toUpperCase());
  415. }
  416. function formatCurrency(value) {
  417. if (value == null || value === '') return '—';
  418. const numeric = Number(value);
  419. if (Number.isNaN(numeric)) return value;
  420. return numeric.toLocaleString('pt-BR', { style: 'currency', currency: 'BRL' });
  421. }
  422. function formatCountdown(ms) {
  423. if (ms == null || Number.isNaN(ms)) return '--:--';
  424. const totalSeconds = Math.max(0, Math.floor(ms / 1000));
  425. const minutes = String(Math.floor(totalSeconds / 60)).padStart(2, '0');
  426. const seconds = String(totalSeconds % 60).padStart(2, '0');
  427. return `${minutes}:${seconds}`;
  428. }
  429. function persistPaymentState() {
  430. if (!browser || !paymentId || !paymentCode) return;
  431. try {
  432. const payload = {
  433. paymentId,
  434. paymentCode,
  435. startedAt: paymentStartedAt ?? Date.now()
  436. };
  437. localStorage.setItem(PAYMENT_STORAGE_KEY, JSON.stringify(payload));
  438. } catch (err) {
  439. console.warn('[CPR] Não foi possível salvar o estado do pagamento', err);
  440. }
  441. }
  442. function removePaymentStateFromStorage() {
  443. if (!browser) return;
  444. try {
  445. localStorage.removeItem(PAYMENT_STORAGE_KEY);
  446. } catch {}
  447. }
  448. function stopPaymentPolling() {
  449. if (paymentPollIntervalId) {
  450. clearInterval(paymentPollIntervalId);
  451. paymentPollIntervalId = null;
  452. }
  453. }
  454. function stopPaymentCountdown() {
  455. if (paymentCountdownIntervalId) {
  456. clearInterval(paymentCountdownIntervalId);
  457. paymentCountdownIntervalId = null;
  458. }
  459. }
  460. function stopPaymentCopyTimeout() {
  461. if (paymentCopyTimeoutId) {
  462. clearTimeout(paymentCopyTimeoutId);
  463. paymentCopyTimeoutId = null;
  464. }
  465. }
  466. function startPaymentCountdown(startTimestamp = Date.now()) {
  467. if (!browser) return;
  468. paymentStartedAt = startTimestamp;
  469. updatePaymentCountdown();
  470. stopPaymentCountdown();
  471. paymentCountdownIntervalId = window.setInterval(updatePaymentCountdown, 1000);
  472. }
  473. function updatePaymentCountdown() {
  474. if (!paymentStartedAt) {
  475. paymentCountdownMs = PAYMENT_TIMEOUT_MS;
  476. return;
  477. }
  478. const elapsed = Date.now() - paymentStartedAt;
  479. const remaining = PAYMENT_TIMEOUT_MS - elapsed;
  480. paymentCountdownMs = remaining > 0 ? remaining : 0;
  481. if (remaining <= 0) {
  482. handlePaymentExpired();
  483. }
  484. }
  485. function handlePaymentExpired() {
  486. if (!paymentId) return;
  487. stopPaymentPolling();
  488. stopPaymentCountdown();
  489. removePaymentStateFromStorage();
  490. paymentStatusMessage = '';
  491. paymentError = 'O tempo limite para pagamento expirou. Gere uma nova CPR para emitir outro QR Code.';
  492. paymentId = null;
  493. paymentCode = '';
  494. }
  495. function startPaymentPolling(immediate = true) {
  496. if (!browser || !paymentId) return;
  497. stopPaymentPolling();
  498. if (immediate) {
  499. void pollPaymentStatus();
  500. }
  501. paymentPollIntervalId = window.setInterval(() => {
  502. void pollPaymentStatus();
  503. }, PAYMENT_POLL_INTERVAL_MS);
  504. }
  505. async function pollPaymentStatus() {
  506. if (!paymentId || isCheckingPayment) return;
  507. isCheckingPayment = true;
  508. paymentError = '';
  509. try {
  510. const result = await requestPaymentConfirmation();
  511. if (result.state === 'success') {
  512. handlePaymentConfirmationSuccess(result.data);
  513. } else if (result.state === 'pending') {
  514. paymentStatusMessage = result.message ?? 'Pagamento pendente. Continuaremos monitorando.';
  515. } else if (result.state === 'error') {
  516. paymentStatusMessage = '';
  517. paymentError = result.message ?? 'Falha ao confirmar pagamento.';
  518. stopPaymentPolling();
  519. }
  520. } catch (err) {
  521. console.error('[CPR] Erro ao confirmar pagamento', err);
  522. paymentError = err?.message ?? 'Não foi possível verificar o pagamento.';
  523. } finally {
  524. isCheckingPayment = false;
  525. }
  526. }
  527. async function requestPaymentConfirmation() {
  528. if (!paymentId) {
  529. return { state: 'error', message: 'Pagamento inválido. Gere um novo QR Code.' };
  530. }
  531. const { token, company_id } = ensureAuthContext();
  532. const res = await fetch(paymentConfirmEndpoint, {
  533. method: 'POST',
  534. headers: {
  535. 'content-type': 'application/json',
  536. Authorization: `Bearer ${token}`
  537. },
  538. body: JSON.stringify({ payment_id: paymentId, company_id })
  539. });
  540. const raw = await res.text();
  541. let body = null;
  542. if (raw) {
  543. try {
  544. body = JSON.parse(raw);
  545. } catch (err) {
  546. console.error('[CPR] Resposta inválida ao confirmar pagamento:', err, raw);
  547. }
  548. }
  549. const isSuccessResponse = Boolean(body?.success || body?.status === 'ok');
  550. if (res.ok && isSuccessResponse) {
  551. console.log('[CPR] Confirmação de pagamento bem-sucedida:', body);
  552. return { state: 'success', data: body };
  553. }
  554. const code = body?.code ?? body?.error;
  555. const backendMessage = body?.message ?? body?.msg ?? body?.data?.message ?? raw ?? '';
  556. const backendMessageText = typeof backendMessage === 'string' ? backendMessage.trim() : '';
  557. const isKnownPending =
  558. code === 'E_PAYMENT_PENDING' ||
  559. body?.data?.status === 0 ||
  560. res.status === 409 ||
  561. backendMessageText.toLowerCase?.().includes('pagamento ainda não confirmado');
  562. if (isKnownPending) {
  563. return {
  564. state: 'pending',
  565. message: backendMessageText || 'Pagamento pendente. Assim que confirmado, você será avisado.'
  566. };
  567. }
  568. const normalizedBackendMessage = typeof backendMessage === 'string' ? backendMessage.trim() : '';
  569. const normalizedRaw = typeof raw === 'string' ? raw.trim() : '';
  570. const message = normalizedBackendMessage || normalizedRaw || 'Não foi possível confirmar o pagamento.';
  571. return { state: 'error', message };
  572. }
  573. function handlePaymentConfirmationSuccess(payload) {
  574. const message = payload?.data?.message ?? payload?.message ?? 'Pagamento confirmado com sucesso.';
  575. submitSuccess = message;
  576. paymentSuccessMessage = message;
  577. paymentError = '';
  578. paymentStatusMessage = '';
  579. paymentSuccessOverlayVisible = true;
  580. stopPaymentPolling();
  581. stopPaymentCountdown();
  582. stopPaymentCopyTimeout();
  583. removePaymentStateFromStorage();
  584. paymentId = null;
  585. paymentCode = '';
  586. paymentStartedAt = null;
  587. paymentCountdownMs = PAYMENT_TIMEOUT_MS;
  588. paymentCopyFeedback = '';
  589. }
  590. function resetPaymentTracking({ hideModal = true } = {}) {
  591. stopPaymentPolling();
  592. stopPaymentCountdown();
  593. stopPaymentCopyTimeout();
  594. removePaymentStateFromStorage();
  595. paymentId = null;
  596. paymentCode = '';
  597. paymentStartedAt = null;
  598. paymentCountdownMs = PAYMENT_TIMEOUT_MS;
  599. paymentCopyFeedback = '';
  600. paymentSuccessOverlayVisible = false;
  601. paymentSuccessMessage = '';
  602. if (hideModal) {
  603. paymentModalVisible = false;
  604. }
  605. }
  606. function handlePaymentSuccessAcknowledge() {
  607. paymentSuccessOverlayVisible = false;
  608. paymentModalVisible = false;
  609. activeTab = 4;
  610. void fetchHistory();
  611. }
  612. function handleCancelPaymentFlow() {
  613. resetPaymentTracking({ hideModal: true });
  614. paymentStatusMessage = '';
  615. paymentError = '';
  616. }
  617. function handleRetryPaymentCheck() {
  618. if (!paymentId) return;
  619. paymentError = '';
  620. paymentStatusMessage = 'Verificando status do pagamento...';
  621. startPaymentPolling(true);
  622. }
  623. async function renderPaymentQrCode(link) {
  624. if (!browser || !link) return;
  625. try {
  626. if (!paymentQrInstance) {
  627. const module = await import('qr-code-styling');
  628. const QRCodeStyles = module.default ?? module;
  629. paymentQrInstance = new QRCodeStyles({
  630. width: 240,
  631. height: 240,
  632. type: 'svg',
  633. data: link,
  634. dotsOptions: {
  635. color: '#0f172a',
  636. type: 'rounded'
  637. },
  638. backgroundOptions: {
  639. color: '#ffffff'
  640. }
  641. });
  642. } else {
  643. paymentQrInstance.update({ data: link });
  644. }
  645. await tick();
  646. if (paymentQrContainer) {
  647. paymentQrContainer.innerHTML = '';
  648. paymentQrInstance.append(paymentQrContainer);
  649. }
  650. } catch (error) {
  651. console.error('[CPR] Falha ao renderizar QR Code de pagamento', error);
  652. paymentError = 'Não foi possível gerar o QR Code. Use o código Pix copiado para pagar.';
  653. }
  654. }
  655. async function openPaymentModalWithPayment(data, startedAt = Date.now(), immediatePoll = true) {
  656. if (!data?.payment_id || !data?.payment_code) return;
  657. paymentModalVisible = true;
  658. paymentId = Number(data.payment_id);
  659. paymentCode = data.payment_code;
  660. paymentError = '';
  661. paymentStatusMessage = 'Aguardando pagamento via Pix.';
  662. paymentCopyFeedback = '';
  663. paymentStartedAt = startedAt;
  664. const remaining = PAYMENT_TIMEOUT_MS - (Date.now() - startedAt);
  665. paymentCountdownMs = remaining > 0 ? remaining : PAYMENT_TIMEOUT_MS;
  666. persistPaymentState();
  667. await tick();
  668. await renderPaymentQrCode(paymentCode);
  669. startPaymentCountdown(startedAt);
  670. startPaymentPolling(immediatePoll);
  671. }
  672. async function restorePersistedPayment() {
  673. if (!browser) return;
  674. try {
  675. const raw = localStorage.getItem(PAYMENT_STORAGE_KEY);
  676. if (!raw) return;
  677. const stored = JSON.parse(raw);
  678. if (!stored?.paymentId || !stored?.paymentCode) {
  679. removePaymentStateFromStorage();
  680. return;
  681. }
  682. const startedAt = stored.startedAt ?? Date.now();
  683. const remaining = PAYMENT_TIMEOUT_MS - (Date.now() - startedAt);
  684. if (remaining <= 0) {
  685. removePaymentStateFromStorage();
  686. return;
  687. }
  688. await openPaymentModalWithPayment(
  689. { payment_id: stored.paymentId, payment_code: stored.paymentCode },
  690. startedAt,
  691. false
  692. );
  693. paymentStatusMessage = 'Retomamos a verificação do pagamento pendente.';
  694. startPaymentPolling(true);
  695. } catch (err) {
  696. console.error('[CPR] Não foi possível restaurar o pagamento pendente', err);
  697. removePaymentStateFromStorage();
  698. }
  699. }
  700. async function handleCopyPaymentCode() {
  701. if (!browser || !paymentCode) return;
  702. try {
  703. await navigator.clipboard.writeText(paymentCode);
  704. paymentCopyFeedback = 'Código Pix copiado!';
  705. } catch {
  706. paymentCopyFeedback = 'Não foi possível copiar automaticamente. Copie manualmente.';
  707. } finally {
  708. stopPaymentCopyTimeout();
  709. if (browser) {
  710. paymentCopyTimeoutId = window.setTimeout(() => {
  711. paymentCopyFeedback = '';
  712. paymentCopyTimeoutId = null;
  713. }, 2000);
  714. }
  715. }
  716. }
  717. async function fetchHistory() {
  718. historyLoading = true;
  719. historyError = '';
  720. detailError = '';
  721. detailLoading = false;
  722. try {
  723. const { token, company_id } = ensureAuthContext();
  724. const res = await fetch(historyEndpoint, {
  725. method: 'POST',
  726. headers: {
  727. 'content-type': 'application/json',
  728. Authorization: `Bearer ${token}`
  729. },
  730. body: JSON.stringify({ company_id })
  731. });
  732. const body = await parseJsonResponse(res);
  733. if (!res.ok) {
  734. throw new Error(body?.message ?? 'Falha ao carregar histórico.');
  735. }
  736. historyRows = normalizeHistoryList(body);
  737. selectedDetail = null;
  738. selectedDetailId = null;
  739. historyInitialized = true;
  740. } catch (err) {
  741. historyRows = [];
  742. historyError = err?.message ?? 'Falha ao carregar histórico.';
  743. } finally {
  744. historyLoading = false;
  745. }
  746. }
  747. async function fetchDetail(cprId) {
  748. if (!cprId) return;
  749. detailLoading = true;
  750. detailError = '';
  751. try {
  752. const { token, company_id } = ensureAuthContext();
  753. const res = await fetch(historyEndpoint, {
  754. method: 'POST',
  755. headers: {
  756. 'content-type': 'application/json',
  757. Authorization: `Bearer ${token}`
  758. },
  759. body: JSON.stringify({ company_id, cpr_id: cprId })
  760. });
  761. const body = await parseJsonResponse(res);
  762. if (!res.ok) {
  763. throw new Error(body?.message ?? 'Falha ao carregar detalhes da CPR.');
  764. }
  765. const detail = Array.isArray(body) ? body[0] : body?.data ?? body;
  766. selectedDetail = sanitizeDetail(detail) ?? detail ?? {};
  767. } catch (err) {
  768. detailError = err?.message ?? 'Falha ao carregar detalhes da CPR.';
  769. selectedDetail = null;
  770. } finally {
  771. detailLoading = false;
  772. }
  773. }
  774. function handleViewDetails(cprId) {
  775. if (!cprId) return;
  776. selectedDetailId = cprId;
  777. selectedDetail = null;
  778. detailError = '';
  779. showDetailModal = true;
  780. void fetchDetail(cprId);
  781. }
  782. function handleCloseDetail() {
  783. selectedDetail = null;
  784. selectedDetailId = null;
  785. detailError = '';
  786. showDetailModal = false;
  787. detailLoading = false;
  788. }
  789. function handleRetryDetail() {
  790. if (selectedDetailId) {
  791. detailError = '';
  792. detailLoading = true;
  793. void fetchDetail(selectedDetailId);
  794. }
  795. }
  796. onMount(() => {
  797. void fetchHistory();
  798. void restorePersistedPayment();
  799. });
  800. onDestroy(() => {
  801. stopPaymentPolling();
  802. stopPaymentCountdown();
  803. stopPaymentCopyTimeout();
  804. });
  805. function getMissingRequiredFields() {
  806. const missing = [];
  807. requiredFields.forEach((key) => {
  808. const groupKey = repeatingFieldToGroup[key];
  809. if (groupKey) {
  810. const config = repeatingGroupDefinitions.find((c) => c.key === groupKey);
  811. const entries = repeatingGroups[groupKey] ?? [];
  812. if (!entries.length) {
  813. missing.push(`${key} (${config?.itemLabel ?? groupKey})`);
  814. return;
  815. }
  816. entries.forEach((entry, index) => {
  817. const raw = entry?.[key];
  818. if (!raw || String(raw).trim() === '') {
  819. missing.push(`${key} (${config?.itemLabel ?? groupKey} ${index + 1})`);
  820. }
  821. });
  822. } else {
  823. const raw = cprForm[key];
  824. if (!raw || String(raw).trim() === '') {
  825. missing.push(key);
  826. }
  827. }
  828. });
  829. return missing;
  830. }
  831. function buildPayload() {
  832. const payload = {};
  833. for (const key of allFieldKeys) {
  834. const groupKey = repeatingFieldToGroup[key];
  835. if (groupKey) {
  836. const entries = repeatingGroups[groupKey] ?? [];
  837. const values = entries
  838. .map((entry) => entry?.[key])
  839. .map((value) => (typeof value === 'string' ? value.trim() : value))
  840. .filter((value) => value && value !== '');
  841. payload[key] = values.length
  842. ? values.join('; ')
  843. : requiredFields.has(key)
  844. ? ''
  845. : 'NA';
  846. continue;
  847. }
  848. const raw = cprForm[key];
  849. const trimmed = typeof raw === 'string' ? raw.trim() : raw;
  850. if (trimmed === '' || trimmed === undefined || trimmed === null) {
  851. payload[key] = requiredFields.has(key) ? '' : 'NA';
  852. } else {
  853. payload[key] = raw;
  854. }
  855. }
  856. return payload;
  857. }
  858. async function handleFinalize(event) {
  859. event?.preventDefault();
  860. submitError = '';
  861. submitSuccess = '';
  862. const missing = getMissingRequiredFields();
  863. if (missing.length) {
  864. submitError = `Preencha os campos obrigatórios: ${missing.join(', ')}`;
  865. return;
  866. }
  867. const token = $authToken;
  868. if (!token) {
  869. submitError = 'Sessão expirada. Faça login novamente.';
  870. return;
  871. }
  872. const payload = buildPayload();
  873. isSubmitting = true;
  874. paymentLoadingVisible = true;
  875. try {
  876. const res = await fetch(`${apiUrl}/b3/cpr/register`, {
  877. method: 'POST',
  878. headers: {
  879. 'content-type': 'application/json',
  880. Authorization: `Bearer ${token}`
  881. },
  882. body: JSON.stringify(payload)
  883. });
  884. const raw = await res.text();
  885. let response = null;
  886. if (raw) {
  887. try {
  888. response = JSON.parse(raw);
  889. } catch (err) {
  890. console.error('Resposta inválida ao registrar CPR:', err, raw);
  891. throw new Error('Resposta inválida do servidor ao registrar CPR.');
  892. }
  893. }
  894. const serverStatus = response?.status?.toLowerCase?.() ?? '';
  895. if (!res.ok || (response?.status && serverStatus !== 'ok')) {
  896. console.error('[CPR] Falha ao registrar CPR', {
  897. status: res.status,
  898. statusText: res.statusText,
  899. raw,
  900. response
  901. });
  902. throw new Error((response?.msg ?? raw) || 'Falha ao registrar CPR.');
  903. }
  904. submitSuccess = response?.msg ?? 'CPR registrada com sucesso.';
  905. const paymentData = response?.data;
  906. cprForm = createInitialForm();
  907. repeatingGroups = createInitialRepeatingGroups();
  908. if (paymentData?.payment_id && paymentData?.payment_code) {
  909. await openPaymentModalWithPayment(paymentData);
  910. }
  911. } catch (err) {
  912. console.error('[CPR] Erro inesperado ao registrar CPR', err);
  913. submitError = err?.message ?? 'Falha ao registrar CPR.';
  914. } finally {
  915. isSubmitting = false;
  916. paymentLoadingVisible = false;
  917. }
  918. }
  919. </script>
  920. <div>
  921. <Header title="CPR - Cédula de Produto Rural" subtitle="Gestão de contratos, emissão e registro de CPRs" breadcrumb={breadcrumb} />
  922. <div class="p-4">
  923. <div class="max-w-6xl mx-auto mt-4">
  924. {#if submitError}
  925. <div class="mb-4 rounded border border-red-300 bg-red-50 text-red-700 px-3 py-2 text-sm">{submitError}</div>
  926. {/if}
  927. {#if submitSuccess}
  928. <div class="mb-4 rounded border border-green-300 bg-green-50 text-green-700 px-3 py-2 text-sm">{submitSuccess}</div>
  929. {/if}
  930. {#if activeTab === 4}
  931. <section class="space-y-4">
  932. <div class="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
  933. <div>
  934. <h2 class="text-lg font-semibold text-gray-900 dark:text-gray-50">Histórico de CPRs</h2>
  935. <p class="text-sm text-gray-500 dark:text-gray-400">
  936. Consulte as últimas CPRs emitidas e visualize os detalhes completos.
  937. </p>
  938. </div>
  939. <div class="flex gap-2">
  940. <button
  941. type="button"
  942. class="inline-flex items-center gap-2 rounded-md border border-gray-300 dark:border-gray-600 px-4 py-2 text-sm font-medium text-gray-700 dark:text-gray-100 hover:bg-gray-100 dark:hover:bg-gray-700 disabled:opacity-60"
  943. on:click={handleAddTop}
  944. >
  945. Nova CPR
  946. </button>
  947. <button
  948. type="button"
  949. class="inline-flex items-center gap-2 rounded-md border border-gray-300 dark:border-gray-600 px-4 py-2 text-sm font-medium text-gray-700 dark:text-gray-100 hover:bg-gray-100 dark:hover:bg-gray-700 disabled:opacity-60"
  950. on:click={() => fetchHistory()}
  951. disabled={historyLoading}
  952. >
  953. {historyLoading ? 'Atualizando...' : 'Atualizar'}
  954. </button>
  955. </div>
  956. </div>
  957. {#if historyError}
  958. <div class="rounded border border-red-200 bg-red-50 dark:border-red-900/40 dark:bg-red-900/20 px-3 py-2 text-sm text-red-700 dark:text-red-300">
  959. {historyError}
  960. </div>
  961. {/if}
  962. <div class="overflow-x-auto rounded-lg border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800">
  963. <table class="min-w-full divide-y divide-gray-200 dark:divide-gray-700 text-sm">
  964. <thead class="bg-gray-50 dark:bg-gray-900/40 text-gray-600 dark:text-gray-300 uppercase text-xs">
  965. <tr>
  966. {#each historyColumns as column}
  967. <th class="px-4 py-3 text-left font-semibold">{column.label}</th>
  968. {/each}
  969. <th class="px-4 py-3 text-left font-semibold">Ação</th>
  970. </tr>
  971. </thead>
  972. <tbody class="divide-y divide-gray-200 dark:divide-gray-700 text-gray-800 dark:text-gray-100">
  973. {#if historyLoading && !historyInitialized}
  974. <tr>
  975. <td class="px-4 py-6 text-center" colspan={historyColumns.length + 1}>
  976. Carregando histórico...
  977. </td>
  978. </tr>
  979. {:else if !historyRows.length}
  980. <tr>
  981. <td class="px-4 py-6 text-center text-gray-500 dark:text-gray-400" colspan={historyColumns.length + 1}>
  982. Nenhuma CPR encontrada.
  983. </td>
  984. </tr>
  985. {:else}
  986. {#each historyRows as row}
  987. <tr class="hover:bg-gray-50 dark:hover:bg-gray-700/60">
  988. <td class="px-4 py-3 font-medium">{row.cpr_id ?? '—'}</td>
  989. <td class="px-4 py-3">{row.cpr_product_class_name ?? '—'}</td>
  990. <td class="px-4 py-3">{row.cpr_issue_date ?? '—'}</td>
  991. <td class="px-4 py-3">{row.cpr_issuer_name ?? '—'}</td>
  992. <td class="px-4 py-3">{formatCurrency(row.cpr_issue_financial_value)}</td>
  993. <td class="px-4 py-3">
  994. <button
  995. type="button"
  996. class="inline-flex items-center justify-center rounded-full p-1.5 text-blue-600 hover:text-blue-500 disabled:opacity-60"
  997. on:click={() => handleViewDetails(row.cpr_id)}
  998. disabled={!row.cpr_id || detailLoading && selectedDetailId === row.cpr_id}
  999. aria-label={detailLoading && selectedDetailId === row.cpr_id ? 'Carregando detalhes' : 'Mais informações'}
  1000. >
  1001. <svg
  1002. class="h-4 w-4"
  1003. viewBox="0 0 20 20"
  1004. fill="none"
  1005. stroke="currentColor"
  1006. stroke-width="1.8"
  1007. >
  1008. <path d="M7 5l5 5-5 5" stroke-linecap="round" stroke-linejoin="round" />
  1009. </svg>
  1010. </button>
  1011. </td>
  1012. </tr>
  1013. {/each}
  1014. {/if}
  1015. </tbody>
  1016. </table>
  1017. </div>
  1018. </section>
  1019. <CprDetailModal
  1020. visible={showDetailModal}
  1021. title="Detalhes da CPR"
  1022. detailId={selectedDetailId}
  1023. loading={detailLoading}
  1024. error={detailError}
  1025. detail={selectedDetail}
  1026. formatKey={humanizeKey}
  1027. formatValue={formatDetailValue}
  1028. on:close={handleCloseDetail}
  1029. on:retry={handleRetryDetail}
  1030. />
  1031. {:else if activeTab === 0}
  1032. <Tabs {tabs} bind:active={activeTab} showCloseIcon={true} on:close={handleCancel} />
  1033. <div class="mt-4">
  1034. <ContractCpr formData={cprForm} onFieldChange={handleFieldChange} {requiredFields} />
  1035. </div>
  1036. <!-- Navigation Controls -->
  1037. <div class="flex justify-between mt-6">
  1038. <button
  1039. type="button"
  1040. class="px-4 py-2 bg-gray-300 text-gray-700 rounded-lg cursor-not-allowed"
  1041. disabled
  1042. >
  1043. Anterior
  1044. </button>
  1045. <button
  1046. type="button"
  1047. class="px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700"
  1048. on:click={() => activeTab = 1}
  1049. >
  1050. Próximo
  1051. </button>
  1052. </div>
  1053. {:else if activeTab === 1}
  1054. <Tabs {tabs} bind:active={activeTab} showCloseIcon={true} on:close={handleCancel} />
  1055. <div class="mt-4">
  1056. <RegisterCpr formData={cprForm} onFieldChange={handleFieldChange} {requiredFields} />
  1057. </div>
  1058. <!-- Navigation Controls -->
  1059. <div class="flex justify-between mt-6">
  1060. <button
  1061. type="button"
  1062. class="px-4 py-2 bg-gray-500 text-white rounded-lg hover:bg-gray-600"
  1063. on:click={() => activeTab = 0}
  1064. >
  1065. Anterior
  1066. </button>
  1067. <button
  1068. type="button"
  1069. class="px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700"
  1070. on:click={() => activeTab = 2}
  1071. >
  1072. Próximo
  1073. </button>
  1074. </div>
  1075. {:else if activeTab === 2}
  1076. <Tabs {tabs} bind:active={activeTab} showCloseIcon={true} on:close={handleCancel} />
  1077. <div class="mt-4">
  1078. <EmissionCpr
  1079. formData={cprForm}
  1080. onFieldChange={handleFieldChange}
  1081. {requiredFields}
  1082. repeatingConfigs={repeatingGroupDefinitions}
  1083. {repeatingGroups}
  1084. onRepeatingFieldChange={handleRepeatingFieldChange}
  1085. onAddRepeatingEntry={handleAddRepeatingEntry}
  1086. onRemoveRepeatingEntry={handleRemoveRepeatingEntry}
  1087. />
  1088. </div>
  1089. <!-- Navigation Controls -->
  1090. <div class="flex justify-between mt-6">
  1091. <button
  1092. type="button"
  1093. class="px-4 py-2 bg-gray-500 text-white rounded-lg hover:bg-gray-600"
  1094. on:click={() => activeTab = 1}
  1095. >
  1096. Anterior
  1097. </button>
  1098. <button
  1099. type="button"
  1100. class="px-6 py-2 bg-green-600 text-white rounded-lg hover:bg-green-700 font-semibold"
  1101. on:click|preventDefault={handleFinalize}
  1102. disabled={isSubmitting}
  1103. >
  1104. {isSubmitting ? 'Enviando...' : 'Finalizar CPR'}
  1105. </button>
  1106. </div>
  1107. {/if}
  1108. </div>
  1109. </div>
  1110. {#if paymentModalVisible}
  1111. <div class="fixed inset-0 z-40 flex items-center justify-center bg-black/60 px-4">
  1112. <div class="w-full max-w-xl rounded-2xl bg-white dark:bg-gray-900 p-6 shadow-2xl relative">
  1113. <button
  1114. class="absolute top-4 right-4 text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200"
  1115. aria-label="Fechar"
  1116. on:click={handleCancelPaymentFlow}
  1117. >
  1118. </button>
  1119. <div class="space-y-4">
  1120. <div>
  1121. <p class="text-sm uppercase tracking-wider text-blue-500 font-semibold">Pagamento pendente</p>
  1122. <h3 class="text-2xl font-bold text-gray-900 dark:text-white">Finalize a CPR via Pix</h3>
  1123. <p class="text-sm text-gray-600 dark:text-gray-300">
  1124. Utilize o QR Code ou copie o código Pix para concluir o pagamento. O QR Code expira em
  1125. <span class="font-semibold">{formatCountdown(paymentCountdownMs)}</span>.
  1126. </p>
  1127. </div>
  1128. <div class="flex flex-col gap-6 md:flex-row">
  1129. <div class="flex-1 rounded-xl border border-gray-200 dark:border-gray-700 bg-gray-50 dark:bg-gray-800/80 p-4 flex flex-col items-center gap-4">
  1130. <div class="text-center">
  1131. <p class="text-xs uppercase tracking-wider text-gray-500">QR Code Pix</p>
  1132. <p class="text-sm text-gray-600 dark:text-gray-300">Escaneie com o app do banco</p>
  1133. </div>
  1134. <div
  1135. class="w-56 h-56 flex items-center justify-center bg-white rounded-xl shadow-inner"
  1136. bind:this={paymentQrContainer}
  1137. aria-label="QR Code do pagamento"
  1138. ></div>
  1139. <button
  1140. type="button"
  1141. class="w-full rounded-lg bg-blue-600 hover:bg-blue-700 text-white py-2 text-sm font-semibold transition disabled:opacity-60"
  1142. on:click={handleCopyPaymentCode}
  1143. disabled={!paymentCode}
  1144. >
  1145. {paymentCopyFeedback ? paymentCopyFeedback : 'Copiar código Pix'}
  1146. </button>
  1147. </div>
  1148. <div class="flex-1 space-y-4">
  1149. <div class="rounded-lg border border-amber-200 bg-amber-50 text-amber-900 px-3 py-2 text-sm">
  1150. <p class="font-semibold text-amber-900">Tempo restante</p>
  1151. <p class="text-3xl font-mono tracking-widest">{formatCountdown(paymentCountdownMs)}</p>
  1152. </div>
  1153. <div class="space-y-2">
  1154. <label class="text-sm font-medium text-gray-700 dark:text-gray-300" for="payment-code-copy">Código Pix copia e cola</label>
  1155. <textarea
  1156. id="payment-code-copy"
  1157. class="w-full text-sm rounded-lg border border-gray-300 dark:border-gray-700 bg-gray-50 dark:bg-gray-900 text-gray-800 dark:text-gray-100 p-2 resize-none"
  1158. rows="4"
  1159. readonly
  1160. value={paymentCode}
  1161. ></textarea>
  1162. </div>
  1163. {#if paymentStatusMessage}
  1164. <div class="rounded-lg border border-blue-200 bg-blue-50 text-blue-900 px-3 py-2 text-sm">
  1165. {paymentStatusMessage}
  1166. </div>
  1167. {/if}
  1168. {#if paymentError}
  1169. <div class="rounded-lg border border-red-200 bg-red-50 text-red-900 px-3 py-2 text-sm">
  1170. {paymentError}
  1171. </div>
  1172. {/if}
  1173. <div class="flex flex-wrap gap-2">
  1174. <button
  1175. type="button"
  1176. class="flex-1 min-w-[140px] inline-flex items-center justify-center rounded-lg border border-gray-300 dark:border-gray-700 px-3 py-2 text-sm font-semibold text-gray-700 dark:text-gray-200 hover:bg-gray-100 dark:hover:bg-gray-800 disabled:opacity-60"
  1177. on:click={handleRetryPaymentCheck}
  1178. disabled={isCheckingPayment}
  1179. >
  1180. {isCheckingPayment ? 'Verificando...' : 'Reverificar agora'}
  1181. </button>
  1182. <button
  1183. type="button"
  1184. class="flex-1 min-w-[140px] inline-flex items-center justify-center rounded-lg border border-red-300 px-3 py-2 text-sm font-semibold text-red-600 hover:bg-red-50"
  1185. on:click={handleCancelPaymentFlow}
  1186. >
  1187. Cancelar pagamento
  1188. </button>
  1189. </div>
  1190. </div>
  1191. </div>
  1192. </div>
  1193. </div>
  1194. </div>
  1195. {/if}
  1196. {#if paymentLoadingVisible}
  1197. <div class="fixed inset-0 z-50 flex items-center justify-center bg-gray-900/60 backdrop-blur-sm">
  1198. <div class="flex flex-col items-center gap-4 text-white">
  1199. <div class="w-12 h-12 border-4 border-white/30 border-t-white rounded-full animate-spin"></div>
  1200. <p class="text-base font-semibold">Gerando QR Code do Pix...</p>
  1201. </div>
  1202. </div>
  1203. {/if}
  1204. {#if paymentSuccessOverlayVisible}
  1205. <div class="fixed inset-0 z-50 flex items-center justify-center bg-black/70 backdrop-blur-sm px-4">
  1206. <div class="w-full max-w-md rounded-2xl bg-white dark:bg-gray-900 p-6 text-center space-y-4 shadow-2xl">
  1207. <div class="text-green-600 dark:text-green-400 flex justify-center">
  1208. <svg class="w-16 h-16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
  1209. <path d="M20 6L9 17l-5-5" />
  1210. </svg>
  1211. </div>
  1212. <h3 class="text-2xl font-semibold text-gray-900 dark:text-gray-50">Pagamento confirmado!</h3>
  1213. <p class="text-gray-600 dark:text-gray-300">{paymentSuccessMessage || 'A CPR foi emitida com sucesso. Você será redirecionado para o histórico.'}</p>
  1214. <button
  1215. type="button"
  1216. class="w-full rounded-lg bg-green-600 hover:bg-green-700 text-white font-semibold py-2"
  1217. on:click={handlePaymentSuccessAcknowledge}
  1218. >
  1219. Ir para histórico de CPRs
  1220. </button>
  1221. </div>
  1222. </div>
  1223. {/if}
  1224. </div>