+page.svelte 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. <script>
  2. import { onMount } from 'svelte';
  3. import { get } from 'svelte/store';
  4. import Header from '$lib/layout/Header.svelte';
  5. import Tabs from '$lib/components/Tabs.svelte';
  6. import RegisterCpr from '$lib/components/commodities/cpr/RegisterCpr.svelte';
  7. import ContractCpr from '$lib/components/commodities/cpr/ContractCpr.svelte';
  8. import EmissionCpr from '$lib/components/commodities/cpr/EmissionCpr.svelte';
  9. import CprDetailModal from '$lib/components/commodities/cpr/CprDetailModal.svelte';
  10. import { authToken, companyId as companyIdStore } from '$lib/utils/stores';
  11. const apiUrl = import.meta.env.VITE_API_URL;
  12. const fieldKeys = [
  13. 'cpr_contract_code',
  14. 'cpr_contract_number',
  15. 'cpr_number',
  16. 'cpr_self_number',
  17. 'cpr_ipoc_code',
  18. 'cpr_calculation_type_code',
  19. 'cpr_initial_exchange_value',
  20. 'cpr_fixing_type_code',
  21. 'cpr_data_source_type_code',
  22. 'cpr_adjustment_frequency_type_code',
  23. 'cpr_adjustment_pro_rata_type_code',
  24. 'cpr_adjustment_type_code',
  25. 'cpr_issue_date',
  26. 'cpr_maturity_date',
  27. 'cpr_reference_date',
  28. 'cpr_profitability_start_date',
  29. 'cpr_payment_start_date',
  30. 'cpr_amortization_start_date',
  31. 'cpr_interest_payment_date',
  32. 'cpr_issue_quantity',
  33. 'cpr_issue_value',
  34. 'cpr_issue_financial_value',
  35. 'cpr_unit_value',
  36. 'cpr_unit_price_value',
  37. 'cpr_interest_unit_price_value',
  38. 'cpr_residual_value',
  39. 'cpr_amortization_percentage',
  40. 'cpr_event_quantity',
  41. 'cpr_creditor_name',
  42. 'cpr_creditor_document_number',
  43. 'cpr_payment_method_code',
  44. 'cpr_index_code',
  45. 'cpr_index_short_name',
  46. 'cpr_vcp_indicator_type_code',
  47. 'cpr_indexador_percentage_value',
  48. 'cpr_interest_rate_spread_percentage',
  49. 'cpr_interest_rate_criteria_type_code',
  50. 'cpr_interest_payment_value',
  51. 'cpr_interest_payment_frequency_code',
  52. 'cpr_interest_months_quantity',
  53. 'cpr_interestPaymentFlow_time_unit_type_code',
  54. 'cpr_interestPaymentFlow_deadline_type_code',
  55. 'cpr_amortization_type_code',
  56. 'cpr_amortization_months_quantity',
  57. 'cpr_amortizationPaymentFlow_time_unit_type_code',
  58. 'cpr_amortizationPaymentFlow_deadline_type_code',
  59. 'cpr_additional_text',
  60. 'cpr_type_code',
  61. 'cpr_internal_control_number',
  62. 'cpr_isin_code',
  63. 'cpr_electronic_emission_indicator',
  64. 'cpr_automatic_expiration_indicator',
  65. 'cpr_otc_register_account_code',
  66. 'cpr_otc_payment_agent_account_code',
  67. 'cpr_otc_custodian_account_code',
  68. 'cpr_otc_favored_account_code',
  69. 'cpr_settlement_modality_type_code',
  70. 'cpr_otc_settlement_bank_account_code',
  71. 'cpr_ballast_type_code',
  72. 'cpr_lot_number',
  73. 'cpr_ballast_quantity',
  74. 'cpr_currency_code',
  75. 'cpr_transaction_identification',
  76. 'cpr_guarantee_limit_type_code',
  77. 'cpr_mother_code',
  78. 'cpr_deposit_quantity',
  79. 'cpr_deposit_unit_price_value',
  80. 'cpr_deposit_person_type_acronym',
  81. 'cpr_deposit_document_number',
  82. 'cpr_event_type_code',
  83. 'cpr_event_original_date',
  84. 'cpr_operation_modality_type_code',
  85. 'cpr_bacen_reference_code',
  86. 'cpr_children_codes',
  87. 'cpr_scr_type_code',
  88. 'cpr_finality_code',
  89. 'cpr_scr_customer_detail',
  90. 'cpr_scr_person_type_acronym',
  91. 'cpr_scr_document_number',
  92. 'cpr_issuer_name',
  93. 'cpr_place_name',
  94. 'cpr_document_deadline_days_number',
  95. 'cpr_deliveryPlace_state_acronym',
  96. 'cpr_deliveryPlace_city_name',
  97. 'cpr_deliveryPlace_ibge_code',
  98. 'cpr_issuer_legal_nature_code',
  99. 'cpr_issuers_person_type_acronym',
  100. 'cpr_issuers_document_number',
  101. 'cpr_issuers_state_acronym',
  102. 'cpr_issuers_city_name',
  103. 'cpr_issuers_ibge_code',
  104. 'cpr_collateral_type_code',
  105. 'cpr_collateral_type_name',
  106. 'cpr_constitution_process_indicator',
  107. 'cpr_otc_bondsman_account_code',
  108. 'cpr_collaterals_document_number',
  109. 'cpr_product_name',
  110. 'cpr_product_class_name',
  111. 'cpr_product_harvest',
  112. 'cpr_product_quantity',
  113. 'cpr_measure_unit_name',
  114. 'cpr_packaging_way_name',
  115. 'cpr_product_status_code',
  116. 'cpr_production_type_code',
  117. 'cpr_product_description',
  118. 'cpr_production_place_name',
  119. 'cpr_property_registration_number',
  120. 'cpr_notary_name',
  121. 'cpr_total_production_area_in_hectares_number',
  122. 'cpr_total_area_in_hectares_number',
  123. 'cpr_car_code',
  124. 'cpr_latitude_code',
  125. 'cpr_longitude_code',
  126. 'cpr_zip_code',
  127. 'cpr_green_cpr_indicator',
  128. 'cpr_green_cpr_certificate_name',
  129. 'cpr_green_cpr_certificate_cnpj_number',
  130. 'cpr_green_cpr_declaration_indicator',
  131. 'cpr_green_cpr_georeferencing_description'
  132. ];
  133. const allFieldKeys = Array.from(new Set(fieldKeys));
  134. const requiredFields = new Set([
  135. 'cpr_type_code',
  136. 'cpr_otc_register_account_code',
  137. 'cpr_otc_custodian_account_code',
  138. 'cpr_electronic_emission_indicator',
  139. 'cpr_issue_date',
  140. 'cpr_maturity_date',
  141. 'cpr_issue_quantity',
  142. 'cpr_issue_value',
  143. 'cpr_issue_financial_value',
  144. 'cpr_profitability_start_date',
  145. 'cpr_automatic_expiration_indicator',
  146. 'cpr_collateral_type_code',
  147. 'cpr_collateral_type_name',
  148. 'cpr_constitution_process_indicator',
  149. 'cpr_otc_bondsman_account_code',
  150. 'cpr_product_name',
  151. 'cpr_product_class_name',
  152. 'cpr_product_harvest',
  153. 'cpr_product_description',
  154. 'cpr_product_quantity',
  155. 'cpr_measure_unit_name',
  156. 'cpr_packaging_way_name',
  157. 'cpr_product_status_code',
  158. 'cpr_production_type_code',
  159. 'cpr_document_deadline_days_number',
  160. 'cpr_place_name',
  161. 'cpr_deliveryPlace_state_acronym',
  162. 'cpr_deliveryPlace_city_name',
  163. 'cpr_deliveryPlace_ibge_code',
  164. 'cpr_issuer_legal_nature_code',
  165. 'cpr_issuers_person_type_acronym',
  166. 'cpr_issuers_document_number',
  167. 'cpr_issuers_state_acronym',
  168. 'cpr_issuers_city_name',
  169. 'cpr_scr_type_code',
  170. 'cpr_finality_code',
  171. 'cpr_contract_code',
  172. 'cpr_creditor_name',
  173. 'cpr_creditor_document_number',
  174. 'cpr_production_place_name',
  175. 'cpr_zip_code'
  176. ]);
  177. const repeatingGroupDefinitions = [
  178. {
  179. key: 'issuers',
  180. title: 'Dados dos emissores',
  181. description: 'Adicione quantos emissores forem necessários.',
  182. itemLabel: 'Emissor',
  183. addLabel: 'Adicionar emissor',
  184. columns: 2,
  185. fields: [
  186. { key: 'cpr_issuer_name', label: 'Razão Social do Emissor' },
  187. { key: 'cpr_issuers_document_number', label: 'Documento' },
  188. { key: 'cpr_issuers_person_type_acronym', label: 'Tipo de pessoa' },
  189. { key: 'cpr_issuer_legal_nature_code', label: 'Natureza jurídica' },
  190. { key: 'cpr_issuers_state_acronym', label: 'Estado' },
  191. { key: 'cpr_issuers_city_name', label: 'Cidade' }
  192. ]
  193. },
  194. {
  195. key: 'collaterals',
  196. title: 'Garantias e colaterais',
  197. description: 'Informe todos os colaterais vinculados a esta CPR.',
  198. itemLabel: 'Colateral',
  199. addLabel: 'Adicionar colateral',
  200. columns: 2,
  201. fields: [
  202. { key: 'cpr_collateral_type_code', label: 'Código do colateral' },
  203. { key: 'cpr_collateral_type_name', label: 'Descrição do colateral' },
  204. { key: 'cpr_constitution_process_indicator', label: 'Processo constituído', type: 'select', options: [
  205. { label: 'Selecione...', value: '' },
  206. { label: 'Sim', value: 'S' },
  207. { label: 'Não', value: 'N' }
  208. ] },
  209. { key: 'cpr_otc_bondsman_account_code', label: 'Conta OTC do fiador' }
  210. ]
  211. },
  212. {
  213. key: 'productionPlaces',
  214. title: 'Locais de produção',
  215. description: 'Cadastre cada propriedade vinculada ao lastro.',
  216. itemLabel: 'Propriedade',
  217. addLabel: 'Adicionar propriedade',
  218. columns: 3,
  219. fields: [
  220. { key: 'cpr_production_place_name', label: 'Nome da propriedade' },
  221. { key: 'cpr_property_registration_number', label: 'Registro da propriedade' },
  222. { key: 'cpr_notary_name', label: 'Cartório' },
  223. { key: 'cpr_total_production_area_in_hectares_number', label: 'Área de produção (ha)' },
  224. { key: 'cpr_total_area_in_hectares_number', label: 'Área total (ha)' },
  225. { key: 'cpr_car_code', label: 'Código CAR' },
  226. { key: 'cpr_latitude_code', label: 'Latitude' },
  227. { key: 'cpr_longitude_code', label: 'Longitude' },
  228. { key: 'cpr_zip_code', label: 'CEP' }
  229. ]
  230. }
  231. ];
  232. const repeatingFieldToGroup = repeatingGroupDefinitions.reduce((acc, config) => {
  233. config.fields.forEach((field) => {
  234. acc[field.key] = config.key;
  235. });
  236. return acc;
  237. }, {});
  238. function createEmptyRepeatingEntry(config) {
  239. return config.fields.reduce((entry, field) => {
  240. entry[field.key] = '';
  241. return entry;
  242. }, {});
  243. }
  244. const createInitialRepeatingGroups = () => {
  245. const groups = {};
  246. repeatingGroupDefinitions.forEach((config) => {
  247. groups[config.key] = [createEmptyRepeatingEntry(config)];
  248. });
  249. return groups;
  250. };
  251. const createInitialForm = () =>
  252. allFieldKeys.reduce((acc, key) => {
  253. acc[key] = '';
  254. return acc;
  255. }, {});
  256. let cprForm = createInitialForm();
  257. let repeatingGroups = createInitialRepeatingGroups();
  258. let submitError = '';
  259. let submitSuccess = '';
  260. let isSubmitting = false;
  261. const breadcrumb = [{ label: 'Início' }, { label: 'CPR', active: true }];
  262. let activeTab = 4;
  263. const tabs = ['Contrato', 'Registro', 'Emissão'];
  264. const historyEndpoint = `${apiUrl}/cpr/history`;
  265. const historyColumns = [
  266. { key: 'cpr_id', label: 'CPR ID' },
  267. { key: 'cpr_product_class_name', label: 'Produto' },
  268. { key: 'cpr_issue_date', label: 'Data de emissão' },
  269. { key: 'cpr_issuer_name', label: 'Emitente' },
  270. { key: 'cpr_issue_financial_value', label: 'Valor' }
  271. ];
  272. let historyRows = [];
  273. let historyLoading = false;
  274. let historyError = '';
  275. let detailLoading = false;
  276. let detailError = '';
  277. let selectedDetail = null;
  278. let selectedDetailId = null;
  279. let showDetailModal = false;
  280. let historyInitialized = false;
  281. function handleFieldChange(key, value) {
  282. submitError = '';
  283. submitSuccess = '';
  284. cprForm = { ...cprForm, [key]: value ?? '' };
  285. }
  286. function cloneRepeatingEntries(groupKey) {
  287. const entries = repeatingGroups[groupKey] ?? [];
  288. return entries.map((entry) => ({ ...entry }));
  289. }
  290. function handleRepeatingFieldChange(groupKey, index, fieldKey, value) {
  291. submitError = '';
  292. submitSuccess = '';
  293. const nextEntries = cloneRepeatingEntries(groupKey);
  294. if (!nextEntries[index]) return;
  295. nextEntries[index] = {
  296. ...nextEntries[index],
  297. [fieldKey]: value ?? ''
  298. };
  299. repeatingGroups = {
  300. ...repeatingGroups,
  301. [groupKey]: nextEntries
  302. };
  303. }
  304. function handleAddRepeatingEntry(groupKey) {
  305. const config = repeatingGroupDefinitions.find((c) => c.key === groupKey);
  306. if (!config) return;
  307. const nextEntries = cloneRepeatingEntries(groupKey);
  308. nextEntries.push(createEmptyRepeatingEntry(config));
  309. repeatingGroups = {
  310. ...repeatingGroups,
  311. [groupKey]: nextEntries
  312. };
  313. }
  314. function handleRemoveRepeatingEntry(groupKey, index) {
  315. const nextEntries = cloneRepeatingEntries(groupKey);
  316. if (nextEntries.length <= 1) return;
  317. repeatingGroups = {
  318. ...repeatingGroups,
  319. [groupKey]: nextEntries.filter((_, i) => i !== index)
  320. };
  321. }
  322. function handleAddTop() {
  323. cprForm = createInitialForm();
  324. repeatingGroups = createInitialRepeatingGroups();
  325. submitError = '';
  326. submitSuccess = '';
  327. activeTab = 0;
  328. }
  329. function handleCancel() {
  330. activeTab = 4;
  331. submitError = '';
  332. }
  333. function ensureAuthContext() {
  334. const token = get(authToken);
  335. if (!token) {
  336. throw new Error('Sessão expirada. Faça login novamente.');
  337. }
  338. const company = Number(get(companyIdStore));
  339. if (!company || Number.isNaN(company) || company <= 0) {
  340. throw new Error('company_id inválido. Refaça o login.');
  341. }
  342. return { token, company_id: company };
  343. }
  344. async function parseJsonResponse(res) {
  345. const raw = await res.text();
  346. if (!raw) return null;
  347. const trimmed = raw.trim();
  348. if (trimmed.startsWith('<')) {
  349. console.error('Resposta HTML inesperada do endpoint de histórico/detalhe de CPR:', trimmed.slice(0, 300));
  350. const statusMsg =
  351. res.status === 404
  352. ? 'Endpoint /cpr/history não encontrado (404). Verifique a URL da API.'
  353. : `Resposta inesperada do servidor (status ${res.status}).`;
  354. throw new Error(statusMsg);
  355. }
  356. try {
  357. return JSON.parse(trimmed);
  358. } catch (err) {
  359. console.error('Resposta inválida do histórico de CPRs:', err, trimmed);
  360. throw new Error('Resposta inválida do servidor.');
  361. }
  362. }
  363. function normalizeHistoryList(payload) {
  364. if (Array.isArray(payload)) return payload;
  365. if (Array.isArray(payload?.data)) return payload.data;
  366. return [];
  367. }
  368. function sanitizeDetail(detail) {
  369. if (!detail || typeof detail !== 'object') return null;
  370. const cleaned = {};
  371. Object.entries(detail).forEach(([key, value]) => {
  372. if (value === null || value === undefined) return;
  373. if (typeof value === 'string' && value.trim().toUpperCase() === 'NA') return;
  374. cleaned[key] = value;
  375. });
  376. return Object.keys(cleaned).length ? cleaned : null;
  377. }
  378. function formatDetailValue(value) {
  379. if (value == null) return '—';
  380. if (typeof value === 'number') {
  381. return value.toLocaleString('pt-BR', { minimumFractionDigits: 0, maximumFractionDigits: 4 });
  382. }
  383. return String(value);
  384. }
  385. function humanizeKey(key = '') {
  386. if (!key) return '';
  387. return key
  388. .replace(/^cpr_/i, '')
  389. .replace(/_/g, ' ')
  390. .replace(/\b\w/g, (match) => match.toUpperCase());
  391. }
  392. function formatCurrency(value) {
  393. if (value == null || value === '') return '—';
  394. const numeric = Number(value);
  395. if (Number.isNaN(numeric)) return value;
  396. return numeric.toLocaleString('pt-BR', { style: 'currency', currency: 'BRL' });
  397. }
  398. async function fetchHistory() {
  399. historyLoading = true;
  400. historyError = '';
  401. detailError = '';
  402. detailLoading = false;
  403. try {
  404. const { token, company_id } = ensureAuthContext();
  405. const res = await fetch(historyEndpoint, {
  406. method: 'POST',
  407. headers: {
  408. 'content-type': 'application/json',
  409. Authorization: `Bearer ${token}`
  410. },
  411. body: JSON.stringify({ company_id })
  412. });
  413. const body = await parseJsonResponse(res);
  414. if (!res.ok) {
  415. throw new Error(body?.message ?? 'Falha ao carregar histórico.');
  416. }
  417. historyRows = normalizeHistoryList(body);
  418. selectedDetail = null;
  419. selectedDetailId = null;
  420. historyInitialized = true;
  421. } catch (err) {
  422. historyRows = [];
  423. historyError = err?.message ?? 'Falha ao carregar histórico.';
  424. } finally {
  425. historyLoading = false;
  426. }
  427. }
  428. async function fetchDetail(cprId) {
  429. if (!cprId) return;
  430. detailLoading = true;
  431. detailError = '';
  432. try {
  433. const { token, company_id } = ensureAuthContext();
  434. const res = await fetch(historyEndpoint, {
  435. method: 'POST',
  436. headers: {
  437. 'content-type': 'application/json',
  438. Authorization: `Bearer ${token}`
  439. },
  440. body: JSON.stringify({ company_id, cpr_id: cprId })
  441. });
  442. const body = await parseJsonResponse(res);
  443. if (!res.ok) {
  444. throw new Error(body?.message ?? 'Falha ao carregar detalhes da CPR.');
  445. }
  446. const detail = Array.isArray(body) ? body[0] : body?.data ?? body;
  447. selectedDetail = sanitizeDetail(detail) ?? detail ?? {};
  448. } catch (err) {
  449. detailError = err?.message ?? 'Falha ao carregar detalhes da CPR.';
  450. selectedDetail = null;
  451. } finally {
  452. detailLoading = false;
  453. }
  454. }
  455. function handleViewDetails(cprId) {
  456. if (!cprId) return;
  457. selectedDetailId = cprId;
  458. selectedDetail = null;
  459. detailError = '';
  460. showDetailModal = true;
  461. void fetchDetail(cprId);
  462. }
  463. function handleCloseDetail() {
  464. selectedDetail = null;
  465. selectedDetailId = null;
  466. detailError = '';
  467. showDetailModal = false;
  468. detailLoading = false;
  469. }
  470. function handleRetryDetail() {
  471. if (selectedDetailId) {
  472. detailError = '';
  473. detailLoading = true;
  474. void fetchDetail(selectedDetailId);
  475. }
  476. }
  477. onMount(() => {
  478. void fetchHistory();
  479. });
  480. function getMissingRequiredFields() {
  481. const missing = [];
  482. requiredFields.forEach((key) => {
  483. const groupKey = repeatingFieldToGroup[key];
  484. if (groupKey) {
  485. const config = repeatingGroupDefinitions.find((c) => c.key === groupKey);
  486. const entries = repeatingGroups[groupKey] ?? [];
  487. if (!entries.length) {
  488. missing.push(`${key} (${config?.itemLabel ?? groupKey})`);
  489. return;
  490. }
  491. entries.forEach((entry, index) => {
  492. const raw = entry?.[key];
  493. if (!raw || String(raw).trim() === '') {
  494. missing.push(`${key} (${config?.itemLabel ?? groupKey} ${index + 1})`);
  495. }
  496. });
  497. } else {
  498. const raw = cprForm[key];
  499. if (!raw || String(raw).trim() === '') {
  500. missing.push(key);
  501. }
  502. }
  503. });
  504. return missing;
  505. }
  506. function buildPayload() {
  507. const payload = {};
  508. for (const key of allFieldKeys) {
  509. const groupKey = repeatingFieldToGroup[key];
  510. if (groupKey) {
  511. const entries = repeatingGroups[groupKey] ?? [];
  512. const values = entries
  513. .map((entry) => entry?.[key])
  514. .map((value) => (typeof value === 'string' ? value.trim() : value))
  515. .filter((value) => value && value !== '');
  516. payload[key] = values.length
  517. ? values.join('; ')
  518. : requiredFields.has(key)
  519. ? ''
  520. : 'NA';
  521. continue;
  522. }
  523. const raw = cprForm[key];
  524. const trimmed = typeof raw === 'string' ? raw.trim() : raw;
  525. if (trimmed === '' || trimmed === undefined || trimmed === null) {
  526. payload[key] = requiredFields.has(key) ? '' : 'NA';
  527. } else {
  528. payload[key] = raw;
  529. }
  530. }
  531. return payload;
  532. }
  533. async function handleFinalize(event) {
  534. event?.preventDefault();
  535. submitError = '';
  536. submitSuccess = '';
  537. const missing = getMissingRequiredFields();
  538. if (missing.length) {
  539. submitError = `Preencha os campos obrigatórios: ${missing.join(', ')}`;
  540. return;
  541. }
  542. const token = $authToken;
  543. if (!token) {
  544. submitError = 'Sessão expirada. Faça login novamente.';
  545. return;
  546. }
  547. const payload = buildPayload();
  548. isSubmitting = true;
  549. try {
  550. const res = await fetch(`${apiUrl}/b3/cpr/register`, {
  551. method: 'POST',
  552. headers: {
  553. 'content-type': 'application/json',
  554. Authorization: `Bearer ${token}`
  555. },
  556. body: JSON.stringify(payload)
  557. });
  558. const raw = await res.text();
  559. let response = null;
  560. if (raw) {
  561. try {
  562. response = JSON.parse(raw);
  563. } catch (err) {
  564. console.error('Resposta inválida ao registrar CPR:', err, raw);
  565. throw new Error('Resposta inválida do servidor ao registrar CPR.');
  566. }
  567. }
  568. const serverStatus = response?.status?.toLowerCase?.() ?? '';
  569. if (!res.ok || (response?.status && serverStatus !== 'ok')) {
  570. throw new Error(response?.msg ?? 'Falha ao registrar CPR.');
  571. }
  572. submitSuccess = response?.msg ?? 'CPR registrada com sucesso.';
  573. cprForm = createInitialForm();
  574. repeatingGroups = createInitialRepeatingGroups();
  575. activeTab = 4;
  576. } catch (err) {
  577. submitError = err?.message ?? 'Falha ao registrar CPR.';
  578. } finally {
  579. isSubmitting = false;
  580. }
  581. }
  582. </script>
  583. <div>
  584. <Header title="CPR - Cédula de Produto Rural" subtitle="Gestão de contratos, emissão e registro de CPRs" breadcrumb={breadcrumb} />
  585. <div class="p-4">
  586. <div class="max-w-6xl mx-auto mt-4">
  587. {#if submitError}
  588. <div class="mb-4 rounded border border-red-300 bg-red-50 text-red-700 px-3 py-2 text-sm">{submitError}</div>
  589. {/if}
  590. {#if submitSuccess}
  591. <div class="mb-4 rounded border border-green-300 bg-green-50 text-green-700 px-3 py-2 text-sm">{submitSuccess}</div>
  592. {/if}
  593. {#if activeTab === 4}
  594. <section class="space-y-4">
  595. <div class="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
  596. <div>
  597. <h2 class="text-lg font-semibold text-gray-900 dark:text-gray-50">Histórico de CPRs</h2>
  598. <p class="text-sm text-gray-500 dark:text-gray-400">
  599. Consulte as últimas CPRs emitidas e visualize os detalhes completos.
  600. </p>
  601. </div>
  602. <div class="flex gap-2">
  603. <button
  604. type="button"
  605. 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"
  606. on:click={handleAddTop}
  607. >
  608. Nova CPR
  609. </button>
  610. <button
  611. type="button"
  612. 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"
  613. on:click={() => fetchHistory()}
  614. disabled={historyLoading}
  615. >
  616. {historyLoading ? 'Atualizando...' : 'Atualizar'}
  617. </button>
  618. </div>
  619. </div>
  620. {#if historyError}
  621. <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">
  622. {historyError}
  623. </div>
  624. {/if}
  625. <div class="overflow-x-auto rounded-lg border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800">
  626. <table class="min-w-full divide-y divide-gray-200 dark:divide-gray-700 text-sm">
  627. <thead class="bg-gray-50 dark:bg-gray-900/40 text-gray-600 dark:text-gray-300 uppercase text-xs">
  628. <tr>
  629. {#each historyColumns as column}
  630. <th class="px-4 py-3 text-left font-semibold">{column.label}</th>
  631. {/each}
  632. <th class="px-4 py-3 text-left font-semibold">Ação</th>
  633. </tr>
  634. </thead>
  635. <tbody class="divide-y divide-gray-200 dark:divide-gray-700 text-gray-800 dark:text-gray-100">
  636. {#if historyLoading && !historyInitialized}
  637. <tr>
  638. <td class="px-4 py-6 text-center" colspan={historyColumns.length + 1}>
  639. Carregando histórico...
  640. </td>
  641. </tr>
  642. {:else if !historyRows.length}
  643. <tr>
  644. <td class="px-4 py-6 text-center text-gray-500 dark:text-gray-400" colspan={historyColumns.length + 1}>
  645. Nenhuma CPR encontrada.
  646. </td>
  647. </tr>
  648. {:else}
  649. {#each historyRows as row}
  650. <tr class="hover:bg-gray-50 dark:hover:bg-gray-700/60">
  651. <td class="px-4 py-3 font-medium">{row.cpr_id ?? '—'}</td>
  652. <td class="px-4 py-3">{row.cpr_product_class_name ?? '—'}</td>
  653. <td class="px-4 py-3">{row.cpr_issue_date ?? '—'}</td>
  654. <td class="px-4 py-3">{row.cpr_issuer_name ?? '—'}</td>
  655. <td class="px-4 py-3">{formatCurrency(row.cpr_issue_financial_value)}</td>
  656. <td class="px-4 py-3">
  657. <button
  658. type="button"
  659. class="inline-flex items-center justify-center rounded-full p-1.5 text-blue-600 hover:text-blue-500 disabled:opacity-60"
  660. on:click={() => handleViewDetails(row.cpr_id)}
  661. disabled={!row.cpr_id || detailLoading && selectedDetailId === row.cpr_id}
  662. aria-label={detailLoading && selectedDetailId === row.cpr_id ? 'Carregando detalhes' : 'Mais informações'}
  663. >
  664. <svg
  665. class="h-4 w-4"
  666. viewBox="0 0 20 20"
  667. fill="none"
  668. stroke="currentColor"
  669. stroke-width="1.8"
  670. >
  671. <path d="M7 5l5 5-5 5" stroke-linecap="round" stroke-linejoin="round" />
  672. </svg>
  673. </button>
  674. </td>
  675. </tr>
  676. {/each}
  677. {/if}
  678. </tbody>
  679. </table>
  680. </div>
  681. </section>
  682. <CprDetailModal
  683. visible={showDetailModal}
  684. title="Detalhes da CPR"
  685. detailId={selectedDetailId}
  686. loading={detailLoading}
  687. error={detailError}
  688. detail={selectedDetail}
  689. formatKey={humanizeKey}
  690. formatValue={formatDetailValue}
  691. on:close={handleCloseDetail}
  692. on:retry={handleRetryDetail}
  693. />
  694. {:else if activeTab === 0}
  695. <Tabs {tabs} bind:active={activeTab} showCloseIcon={true} on:close={handleCancel} />
  696. <div class="mt-4">
  697. <ContractCpr formData={cprForm} onFieldChange={handleFieldChange} {requiredFields} />
  698. </div>
  699. <!-- Navigation Controls -->
  700. <div class="flex justify-between mt-6">
  701. <button
  702. type="button"
  703. class="px-4 py-2 bg-gray-300 text-gray-700 rounded-lg cursor-not-allowed"
  704. disabled
  705. >
  706. Anterior
  707. </button>
  708. <button
  709. type="button"
  710. class="px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700"
  711. on:click={() => activeTab = 1}
  712. >
  713. Próximo
  714. </button>
  715. </div>
  716. {:else if activeTab === 1}
  717. <Tabs {tabs} bind:active={activeTab} showCloseIcon={true} on:close={handleCancel} />
  718. <div class="mt-4">
  719. <RegisterCpr formData={cprForm} onFieldChange={handleFieldChange} {requiredFields} />
  720. </div>
  721. <!-- Navigation Controls -->
  722. <div class="flex justify-between mt-6">
  723. <button
  724. type="button"
  725. class="px-4 py-2 bg-gray-500 text-white rounded-lg hover:bg-gray-600"
  726. on:click={() => activeTab = 0}
  727. >
  728. Anterior
  729. </button>
  730. <button
  731. type="button"
  732. class="px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700"
  733. on:click={() => activeTab = 2}
  734. >
  735. Próximo
  736. </button>
  737. </div>
  738. {:else if activeTab === 2}
  739. <Tabs {tabs} bind:active={activeTab} showCloseIcon={true} on:close={handleCancel} />
  740. <div class="mt-4">
  741. <EmissionCpr
  742. formData={cprForm}
  743. onFieldChange={handleFieldChange}
  744. {requiredFields}
  745. repeatingConfigs={repeatingGroupDefinitions}
  746. {repeatingGroups}
  747. onRepeatingFieldChange={handleRepeatingFieldChange}
  748. onAddRepeatingEntry={handleAddRepeatingEntry}
  749. onRemoveRepeatingEntry={handleRemoveRepeatingEntry}
  750. />
  751. </div>
  752. <!-- Navigation Controls -->
  753. <div class="flex justify-between mt-6">
  754. <button
  755. type="button"
  756. class="px-4 py-2 bg-gray-500 text-white rounded-lg hover:bg-gray-600"
  757. on:click={() => activeTab = 1}
  758. >
  759. Anterior
  760. </button>
  761. <button
  762. type="button"
  763. class="px-6 py-2 bg-green-600 text-white rounded-lg hover:bg-green-700 font-semibold"
  764. on:click|preventDefault={handleFinalize}
  765. disabled={isSubmitting}
  766. >
  767. {isSubmitting ? 'Enviando...' : 'Finalizar CPR'}
  768. </button>
  769. </div>
  770. {/if}
  771. </div>
  772. </div>
  773. </div>