+page.svelte 47 KB

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