+page.svelte 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <script>
  2. import Header from '$lib/layout/Header.svelte';
  3. import Tabs from '$lib/components/Tabs.svelte';
  4. import RegisterCpr from '$lib/components/commodities/cpr/RegisterCpr.svelte';
  5. import Tables from '$lib/components/Tables.svelte';
  6. import ContractCpr from '$lib/components/commodities/cpr/ContractCpr.svelte';
  7. import EmissionCpr from '$lib/components/commodities/cpr/EmissionCpr.svelte';
  8. import CprEditModal from '$lib/components/commodities/cpr/CprEditModal.svelte';
  9. import ConfirmModal from '$lib/components/ui/PopUpDelete.svelte';
  10. // EditModal
  11. let showCprEdit = false;
  12. let selectedRow = null;
  13. let editValue = {};
  14. // Delete confirmation
  15. let showDeleteConfirm = false;
  16. let rowToDelete = null;
  17. const breadcrumb = [{ label: 'Início' }, { label: 'CPR', active: true }];
  18. let activeTab = 4;
  19. const tabs = ["Contrato", "Registro", "Emissão"];
  20. let columns = [
  21. { key: "nome", label: "Nome" },
  22. { key: "status", label: "Status" }
  23. ];
  24. let data = [
  25. { nome: "CPR A", status: "Disponível" },
  26. { nome: "CPR B", status: "Indisponível" }
  27. ];
  28. function handleAddTop() {
  29. // ao clicar em "+", mudar para a aba de Registro (index 1) e exibir Tabs + Form
  30. activeTab = 0;
  31. }
  32. function handleDeleteRow(e) {
  33. const { row } = e.detail;
  34. rowToDelete = row;
  35. showDeleteConfirm = true;
  36. }
  37. function handleEditRow(e) {
  38. const { row } = e.detail;
  39. selectedRow = row;
  40. editValue = { ...row };
  41. showCprEdit = true;
  42. }
  43. function handleCprSave(e) {
  44. const { value } = e.detail;
  45. if (selectedRow) {
  46. // Atualiza a linha com os dados editados
  47. data = data.map((r) => (r === selectedRow ? { ...r, ...value } : r));
  48. }
  49. handleCprCancel();
  50. }
  51. function handleCprCancel() {
  52. showCprEdit = false;
  53. selectedRow = null;
  54. editValue = {};
  55. }
  56. function handleFinalize() {
  57. // Aqui você pode adicionar validação dos campos obrigatórios
  58. // Por enquanto, apenas volta para a tabela principal
  59. alert('CPR finalizado com sucesso!');
  60. activeTab = 4;
  61. }
  62. function handleCancel() {
  63. // Volta para a tabela principal cancelando o processo
  64. activeTab = 4;
  65. }
  66. function confirmDelete() {
  67. // Remove pelo objeto (referência) para manter simples no exemplo
  68. data = data.filter((r) => r !== rowToDelete);
  69. showDeleteConfirm = false;
  70. rowToDelete = null;
  71. }
  72. function cancelDelete() {
  73. showDeleteConfirm = false;
  74. rowToDelete = null;
  75. }
  76. </script>
  77. <div>
  78. <Header title="CPR - Cédula de Produto Rural" subtitle="Gestão de contratos, emissão e registro de CPRs" breadcrumb={breadcrumb} />
  79. <div class="p-4">
  80. <div class="max-w-6xl mx-auto mt-4">
  81. {#if activeTab === 4}
  82. <Tables
  83. title="CPRs"
  84. columns={columns}
  85. data={data}
  86. on:addTop={handleAddTop}
  87. on:editRow={handleEditRow}
  88. on:deleteRow={handleDeleteRow}
  89. />
  90. <CprEditModal
  91. visible={showCprEdit}
  92. title="Editar CPR"
  93. value={editValue}
  94. on:save={handleCprSave}
  95. on:cancel={handleCprCancel}
  96. />
  97. {:else if activeTab === 0}
  98. <Tabs {tabs} bind:active={activeTab} showCloseIcon={true} on:close={handleCancel} />
  99. <div class="mt-4">
  100. <ContractCpr />
  101. </div>
  102. <!-- Navigation Controls -->
  103. <div class="flex justify-between mt-6">
  104. <button
  105. class="px-4 py-2 bg-gray-300 text-gray-700 rounded-lg cursor-not-allowed"
  106. disabled
  107. >
  108. Anterior
  109. </button>
  110. <button
  111. class="px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700"
  112. on:click={() => activeTab = 1}
  113. >
  114. Próximo
  115. </button>
  116. </div>
  117. {:else if activeTab === 1}
  118. <Tabs {tabs} bind:active={activeTab} showCloseIcon={true} on:close={handleCancel} />
  119. <div class="mt-4">
  120. <RegisterCpr />
  121. </div>
  122. <!-- Navigation Controls -->
  123. <div class="flex justify-between mt-6">
  124. <button
  125. class="px-4 py-2 bg-gray-500 text-white rounded-lg hover:bg-gray-600"
  126. on:click={() => activeTab = 0}
  127. >
  128. Anterior
  129. </button>
  130. <button
  131. class="px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700"
  132. on:click={() => activeTab = 2}
  133. >
  134. Próximo
  135. </button>
  136. </div>
  137. {:else if activeTab === 2}
  138. <Tabs {tabs} bind:active={activeTab} showCloseIcon={true} on:close={handleCancel} />
  139. <div class="mt-4">
  140. <EmissionCpr />
  141. </div>
  142. <!-- Navigation Controls -->
  143. <div class="flex justify-between mt-6">
  144. <button
  145. class="px-4 py-2 bg-gray-500 text-white rounded-lg hover:bg-gray-600"
  146. on:click={() => activeTab = 1}
  147. >
  148. Anterior
  149. </button>
  150. <button
  151. class="px-6 py-2 bg-green-600 text-white rounded-lg hover:bg-green-700 font-semibold"
  152. on:click={handleFinalize}
  153. >
  154. Finalizar CPR
  155. </button>
  156. </div>
  157. {/if}
  158. </div>
  159. </div>
  160. </div>
  161. <ConfirmModal
  162. visible={showDeleteConfirm}
  163. title="Confirmar exclusão"
  164. confirmText="Excluir"
  165. cancelText="Cancelar"
  166. on:confirm={confirmDelete}
  167. on:cancel={cancelDelete}
  168. >
  169. <p>Tem certeza que deseja excluir a CPR "{rowToDelete?.nome}"?</p>
  170. </ConfirmModal>