| """Página para criar novo checklist""" |
|
|
| import streamlit as st |
| from utils.database import save_checklist as save_checklist_db |
|
|
| st.set_page_config( |
| page_title="Criar Novo Checklist", |
| page_icon="✅", |
| layout="centered" |
| ) |
|
|
| def init_form_state(): |
| if 'form_items' not in st.session_state: |
| st.session_state.form_items = [] |
| if 'item_to_remove' not in st.session_state: |
| st.session_state.item_to_remove = None |
|
|
| def add_item(): |
| st.session_state.form_items.append("") |
|
|
| def remove_item(index): |
| st.session_state.form_items.pop(index) |
|
|
| def save_checklist(name, items, numero_processo): |
| if not name: |
| st.error("Por favor, dê um nome ao seu checklist!") |
| return False |
| |
| if not numero_processo: |
| st.error("Por favor, informe o número do processo!") |
| return False |
| |
| if not items or all(item == "" for item in items): |
| st.error("Por favor, adicione pelo menos um item ao checklist!") |
| return False |
| |
| try: |
| |
| checklist_id = save_checklist_db(name, items, numero_processo) |
| st.session_state.current_checklist_id = checklist_id |
| |
| |
| st.session_state.form_items = [] |
| if 'should_clear' not in st.session_state: |
| st.session_state.should_clear = False |
| st.session_state.should_clear = True |
| |
| return True |
| except Exception as e: |
| st.error(f"Erro ao salvar checklist: {str(e)}") |
| return False |
|
|
| def main(): |
| init_form_state() |
| |
| st.title("📝 Criar Novo Checklist") |
| |
| col1, col2 = st.columns([5, 1]) |
| with col1: |
| st.markdown("---") |
| with col2: |
| if st.button("← Voltar"): |
| st.switch_page("app.py") |
| |
| |
| if 'should_clear' in st.session_state and st.session_state.should_clear: |
| st.session_state.should_clear = False |
| st.switch_page("pages/dashboard.py") |
| |
| if st.session_state.item_to_remove is not None: |
| remove_item(st.session_state.item_to_remove) |
| st.session_state.item_to_remove = None |
| st.rerun() |
| |
| with st.container(): |
| col1, col2 = st.columns(2) |
| |
| with col1: |
| checklist_name = st.text_input( |
| "Nome do Checklist", |
| key="checklist_name_input", |
| placeholder="Ex: Tarefas do Projeto X" |
| ) |
| |
| with col2: |
| numero_processo = st.text_input( |
| "Número do Processo", |
| key="numero_processo_input", |
| placeholder="Ex: PROC-2024-001" |
| ) |
| |
| st.markdown("### Items do Checklist") |
| |
| if not st.session_state.form_items: |
| st.info("Clique em 'Adicionar Item' para começar a criar seu checklist") |
| |
| current_items = [] |
| for i, item in enumerate(st.session_state.form_items): |
| col1, col2 = st.columns([5, 1]) |
| with col1: |
| item_value = st.text_input( |
| f"Item {i+1}", |
| value=item, |
| key=f"item_{i}", |
| placeholder="Digite o item do checklist" |
| ) |
| current_items.append(item_value) |
| with col2: |
| st.markdown("<br>", unsafe_allow_html=True) |
| if st.button("🗑️", key=f"remove_{i}", help="Remover item"): |
| st.session_state.item_to_remove = i |
| st.rerun() |
| |
| st.markdown("") |
| |
| col1, col2, col3 = st.columns([2, 2, 2]) |
| |
| with col1: |
| if st.button("➕ Adicionar Item", use_container_width=True): |
| add_item() |
| st.rerun() |
| |
| with col3: |
| if st.button("💾 Salvar Checklist", type="primary", use_container_width=True): |
| if save_checklist(checklist_name, current_items, numero_processo): |
| st.success("Checklist criado com sucesso!") |
| st.balloons() |
|
|
| if __name__ == "__main__": |
| main() |