diff --git a/catavento/lib/bloc/demanda_bloc.dart b/catavento/lib/bloc/demanda_bloc.dart index f71b427..be0f2fa 100644 --- a/catavento/lib/bloc/demanda_bloc.dart +++ b/catavento/lib/bloc/demanda_bloc.dart @@ -1,8 +1,9 @@ import 'dart:io'; +import 'package:catavento/constants.dart'; import 'package:catavento/typedefs.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; -// import 'package:image_picker/image_picker.dart'; +import 'package:intl/intl.dart'; import 'package:supabase_flutter/supabase_flutter.dart'; part 'demanda_event.dart'; @@ -15,7 +16,7 @@ class DemandaBloc extends Bloc { DemandaEvent get initialState => DemandaLoading(); - DemandaBloc() : super(LoadingState([], {})) { + DemandaBloc() : super(DemandaLoadingState([], {})) { on(_onFilter); on(_onLoading); @@ -48,9 +49,9 @@ class DemandaBloc extends Bloc { final metaData = _countDemandas(); if (newData.isEmpty) { - emit(FilterState(currentData, metaData)); + emit(DemandaFilterState(currentData, metaData)); } else { - emit(FilterState(newData, metaData)); + emit(DemandaFilterState(newData, metaData)); } } @@ -60,7 +61,7 @@ class DemandaBloc extends Bloc { final metaData = _countDemandas(); - emit(LoadingState(currentData, metaData)); + emit(DemandaLoadingState(currentData, metaData)); } void _onCreate(DemandaCreate event, Emitter emit) async { @@ -75,15 +76,23 @@ class DemandaBloc extends Bloc { } } + final dataAdicao = DateFormat(timeFormat).format(DateTime.now()); + + print('Teste'); + print(event.descricao); + final demanda = { - 'nomeDemanda': event.nomeDemanda, - 'codigo': event.codigo, - 'descricao': event.descricao, - 'status': event.status, + 'nome_demanda': event.nomeDemanda, + 'descricao': event.descricao == '' ? 'Bolo normal' : event.descricao, + 'status': 'Pendente', + 'status_cobertura': 0, + 'status_aplique': 0, + 'data_adicao': dataAdicao, + 'prioridade': 'media', }; if (fotoUrl != null) { - demanda['imagemUrl'] = fotoUrl; + demanda['image_url'] = fotoUrl; } try { @@ -99,7 +108,7 @@ class DemandaBloc extends Bloc { final metaData = _countDemandas(); - emit(CreateState(currentData, metaData)); + emit(DemandaCreateState(currentData, metaData)); } void _onDelete(DemandaDelete event, Emitter emit) async { @@ -115,29 +124,26 @@ class DemandaBloc extends Bloc { final metaData = _countDemandas(); - emit(DeleteState(currentData, metaData)); + emit(DemandaDeleteState(currentData, metaData)); } void _onUpdate(DemandaUpdate event, Emitter emit) async { try { final nomeDemanda = event.nomeDemanda; - final codigo = event.codigo; final descricao = event.descricao; final order = event.order; await supabase.from('demandas').update({ - 'nomeDemanda': nomeDemanda, - 'codigo': codigo, + 'nome_demanda': nomeDemanda, 'descricao': descricao, }).eq('id', event.id); - currentData[order]['nomeDemanda'] = nomeDemanda; - currentData[order]['codigo'] = codigo; + currentData[order]['nome_demanda'] = nomeDemanda; currentData[order]['descricao'] = descricao; final metaData = _countDemandas(); - emit(UpdateState(currentData, metaData)); + emit(DemandaUpdateState(currentData, metaData)); } catch (e) { print("Erro ao atualizar demanda: $e"); } @@ -152,9 +158,9 @@ class DemandaBloc extends Bloc { switch (data['status']) { case '0' || 'Pendente': espera++; - case '1': + case '1' || 'Em fabricação': fabricacao++; - case '2': + case '2' || 'Finalizado': completo++; } } diff --git a/catavento/lib/bloc/demanda_event.dart b/catavento/lib/bloc/demanda_event.dart index 08f35a2..850da5f 100644 --- a/catavento/lib/bloc/demanda_event.dart +++ b/catavento/lib/bloc/demanda_event.dart @@ -31,7 +31,7 @@ class DemandaDelete extends DemandaEvent { final int id; final int order; - DemandaDelete(this.id, this.order); + const DemandaDelete(this.id, this.order); } class DemandaUpdate extends DemandaEvent { @@ -41,7 +41,7 @@ class DemandaUpdate extends DemandaEvent { final String codigo; final String descricao; - DemandaUpdate( + const DemandaUpdate( this.id, this.order, this.nomeDemanda, diff --git a/catavento/lib/bloc/demanda_state.dart b/catavento/lib/bloc/demanda_state.dart index b4868fa..e03a5be 100644 --- a/catavento/lib/bloc/demanda_state.dart +++ b/catavento/lib/bloc/demanda_state.dart @@ -7,72 +7,22 @@ sealed class DemandaState { const DemandaState(this.databaseResponse, this.metaData); } -class CreateState extends DemandaState { - CreateState(super.databaseResponse, super.metaData); +class DemandaCreateState extends DemandaState { + DemandaCreateState(super.databaseResponse, super.metaData); } -class DeleteState extends DemandaState { - DeleteState(super.databaseResponse, super.metaData); +class DemandaDeleteState extends DemandaState { + DemandaDeleteState(super.databaseResponse, super.metaData); } -class UpdateState extends DemandaState { - UpdateState(super.databaseResponse, super.metaData); +class DemandaUpdateState extends DemandaState { + DemandaUpdateState(super.databaseResponse, super.metaData); } -class LoadingState extends DemandaState { - LoadingState(super.databaseResponse, super.metaData); +class DemandaLoadingState extends DemandaState { + DemandaLoadingState(super.databaseResponse, super.metaData); } -class FilterState extends DemandaState { - FilterState(super.databaseResponse, super.metaData); -} - -class PhotoSelectedState extends DemandaState { - final File? fotoSelecionada; - - PhotoSelectedState( - super.databaseResponse, - super.metaData, - this.fotoSelecionada, - ); -} - -class PhotoUploadedState extends DemandaState { - final String fotoUrl; - - PhotoUploadedState( - super.databaseResponse, - super.metaData, - this.fotoUrl, - ); -} - -class PhotoSelectionErrorState extends DemandaState { - final String message; - - PhotoSelectionErrorState( - super.databaseResponse, - super.metaData, - this.message, - ); -} - -class PhotoUploadErrorState extends DemandaState { - final String error; - - PhotoUploadErrorState( - super.databaseResponse, - super.metaData, - this.error, - ); -} - -class PhotoUploadSuccessState extends DemandaState { - final String publicUrl; - - PhotoUploadSuccessState( - super.databaseResponse, - super.metaData, - this.publicUrl, - ); +class DemandaFilterState extends DemandaState { + DemandaFilterState(super.databaseResponse, super.metaData); } diff --git a/catavento/lib/bloc/usuario_bloc.dart b/catavento/lib/bloc/usuario_bloc.dart new file mode 100644 index 0000000..d4780b7 --- /dev/null +++ b/catavento/lib/bloc/usuario_bloc.dart @@ -0,0 +1,122 @@ +// import 'dart:io'; + +// import 'package:catavento/bloc/demanda_bloc.dart'; +// import 'package:catavento/constants.dart'; +// import 'package:catavento/bloc/demanda_bloc.dart'; +import 'package:catavento/typedefs.dart'; +// import 'package:flutter/widgets.dart'; +// import 'package:flutter/material.dart'; +// import 'package:flutter/widgets.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; +// import 'package:http/http.dart'; +// import 'package:intl/intl.dart'; +import 'package:supabase_flutter/supabase_flutter.dart'; + +part 'usuario_event.dart'; +part 'usuario_state.dart'; + +class UsuarioBloc extends Bloc { + final supabase = Supabase.instance.client; + DatabaseResponse currentData = []; + UsuarioEvent get initialState => UsuarioLoading(); + + UsuarioBloc() : super(UsuarioLoadingState([], {})) { + on(_onLoading); + + on(_onCreate); + + on(_onDelete); + + on(_onUpdate); + } + + void _onCreate(UsuarioCreate event, Emitter emit) async { + final usuario = { + 'usuario_nome': event.nome, + 'email': event.email, + 'setor': event.setor, + 'tipo': event.tipo + }; + + try { + final response = await supabase.from('usuarios').insert(usuario).select(); + if (response.isNotEmpty) { + currentData.add(response[0]); + } else { + throw Exception("Erro ao adiciona usuario"); + } + } catch (_) { + throw Exception('Erro ao adicionar usuario'); + } + + final metaData = _countUsuarios(); + + emit(UsuarioCreateState(currentData, metaData)); + } + + void _onDelete(UsuarioDelete event, Emitter emit) async { + try { + final response = + await supabase.from('usuarios').delete().eq('id', event.id).select(); + if (response.isNotEmpty) { + currentData.removeAt(event.order); + } + } catch (e) { + print('Erro ao buscar dados: $e'); + } + + final metaData = _countUsuarios(); + + emit(UsuarioDeleteState(currentData, metaData)); + } + + void _onUpdate(UsuarioUpdate event, Emitter emit) async { + try { + final nome = event.nome; + final setor = event.setor; + final email = event.email; + final tipo = event.tipo; + final id = event.id; + + await supabase.from('usuarios').update({ + 'nome': nome, + 'setor': setor, + 'email': email, + 'tipo': tipo, + }).eq('id', event.id); + + // verificar se pode usar o id + currentData[id]['nome'] = nome; + currentData[id]['setor'] = setor; + currentData[id]['email'] = email; + currentData[id]['tipo'] = tipo; + + final metaData = _countUsuarios(); + + emit(UsuarioUpdateState(currentData, metaData)); + } catch (e) { + print("Erro ao atualizar usuario: $e"); + } + } + + void _onLoading(UsuarioLoading event, Emitter emit) async { + final response = await supabase.from('usuarios').select(); + currentData = response; + + final metaData = _countUsuarios(); + + emit(UsuarioLoadingState(currentData, metaData)); + } + + Map _countUsuarios() { + int numUsuarios = 0; + + for (var usuario in currentData) { + if (usuario["tipo"] == "funcionario") { + numUsuarios++; + } + } + final metaData = {"total": numUsuarios}; + return metaData; + } +} diff --git a/catavento/lib/bloc/usuario_event.dart b/catavento/lib/bloc/usuario_event.dart new file mode 100644 index 0000000..99162aa --- /dev/null +++ b/catavento/lib/bloc/usuario_event.dart @@ -0,0 +1,47 @@ +part of 'usuario_bloc.dart'; + +sealed class UsuarioEvent { + const UsuarioEvent(); +} + +class UsuarioCreate extends UsuarioEvent { + final String nome; + final String setor; + final String email; + final String tipo; + + const UsuarioCreate( + this.nome, + this.setor, + this.email, + this.tipo, + ); +} + +class UsuarioDelete extends UsuarioEvent { + final int id; + final int order; + + const UsuarioDelete( + this.id, + this.order, + ); +} + +class UsuarioUpdate extends UsuarioEvent { + final int id; + final String nome; + final String setor; + final String email; + final String tipo; + + const UsuarioUpdate( + this.nome, + this.setor, + this.email, + this.tipo, + this.id, + ); +} + +class UsuarioLoading extends UsuarioEvent {} diff --git a/catavento/lib/bloc/usuario_state.dart b/catavento/lib/bloc/usuario_state.dart new file mode 100644 index 0000000..36f4ef6 --- /dev/null +++ b/catavento/lib/bloc/usuario_state.dart @@ -0,0 +1,24 @@ +part of 'usuario_bloc.dart'; + +sealed class UsuarioState { + final DatabaseResponse databaseResponse; + final Map metaData; + + const UsuarioState(this.databaseResponse, this.metaData); +} + +class UsuarioCreateState extends UsuarioState { + UsuarioCreateState(super.databaseResponse, super.metaData); +} + +class UsuarioDeleteState extends UsuarioState { + UsuarioDeleteState(super.databaseResponse, super.metaData); +} + +class UsuarioUpdateState extends UsuarioState { + UsuarioUpdateState(super.databaseResponse, super.metaData); +} + +class UsuarioLoadingState extends UsuarioState { + UsuarioLoadingState(super.databaseResponse, super.metaData); +} diff --git a/catavento/lib/constants.dart b/catavento/lib/constants.dart index d1d11bb..7971c98 100644 --- a/catavento/lib/constants.dart +++ b/catavento/lib/constants.dart @@ -1,4 +1,6 @@ const loginRoute = "/login/"; const homeRoute = "/home/"; +const crudFuncionariosRoute = "/dashboard-admin-crud-funcionarios"; const supabaseUrl = 'https://gfxmrqhwjqekfzrlurqr.supabase.co'; const supabaseKey = String.fromEnvironment('ANON_KEY'); +const timeFormat = 'yyyy-MM-dd HH:mm:ss'; diff --git a/catavento/lib/main.dart b/catavento/lib/main.dart index 3ec2960..1f30a43 100644 --- a/catavento/lib/main.dart +++ b/catavento/lib/main.dart @@ -1,6 +1,7 @@ import 'package:catavento/bloc/demanda_bloc.dart'; import 'package:catavento/constants.dart'; -import 'package:catavento/screens/dashboard_admin.dart'; +import 'package:catavento/screens/dashboardAdmin/dashboard_admin.dart'; +import 'package:catavento/screens/dashboardFuncionarios/dashboard_funcionarios.dart'; import 'package:catavento/views/login_view.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; @@ -9,7 +10,8 @@ import 'package:supabase_flutter/supabase_flutter.dart'; void main() { runApp( BlocProvider( - create: (_) => DemandaBloc()..add(DemandaLoading()), // Providing the bloc here + create: (_) => + DemandaBloc()..add(DemandaLoading()), // Providing the bloc here child: MaterialApp( title: "Gestão Catavento", theme: ThemeData( @@ -20,11 +22,13 @@ void main() { routes: { loginRoute: (context) => const LoginView(), homeRoute: (context) => const DashBoardAdmin(), + crudFuncionariosRoute: (context) => const DashBoardFuncionarios(), }, ), ), ); } + class LoadView extends StatelessWidget { const LoadView({super.key}); @@ -51,4 +55,4 @@ class LoadView extends StatelessWidget { ), ); } -} \ No newline at end of file +} diff --git a/catavento/lib/screens/components/confirmDialog.dart b/catavento/lib/screens/dashboardAdmin/components/confirmDialog.dart similarity index 100% rename from catavento/lib/screens/components/confirmDialog.dart rename to catavento/lib/screens/dashboardAdmin/components/confirmDialog.dart diff --git a/catavento/lib/screens/components/demandCard.dart b/catavento/lib/screens/dashboardAdmin/components/demandCard.dart similarity index 100% rename from catavento/lib/screens/components/demandCard.dart rename to catavento/lib/screens/dashboardAdmin/components/demandCard.dart diff --git a/catavento/lib/screens/components/graph.dart b/catavento/lib/screens/dashboardAdmin/components/graph.dart similarity index 100% rename from catavento/lib/screens/components/graph.dart rename to catavento/lib/screens/dashboardAdmin/components/graph.dart diff --git a/catavento/lib/screens/components/header.dart b/catavento/lib/screens/dashboardAdmin/components/header.dart similarity index 98% rename from catavento/lib/screens/components/header.dart rename to catavento/lib/screens/dashboardAdmin/components/header.dart index 11bdf59..b931fb2 100644 --- a/catavento/lib/screens/components/header.dart +++ b/catavento/lib/screens/dashboardAdmin/components/header.dart @@ -1,5 +1,4 @@ import 'package:flutter/material.dart'; -import 'package:intl/intl.dart'; class Header extends StatelessWidget { final String title; diff --git a/catavento/lib/screens/dashboardAdmin/components/menu.dart b/catavento/lib/screens/dashboardAdmin/components/menu.dart new file mode 100644 index 0000000..b81755b --- /dev/null +++ b/catavento/lib/screens/dashboardAdmin/components/menu.dart @@ -0,0 +1,51 @@ +import 'package:flutter/material.dart'; + +class Navbar extends StatelessWidget { + @override + Widget build(BuildContext context) { + return Drawer( + child: Column( + children: [ + UserAccountsDrawerHeader( + accountName: const Text( + 'João das Couves', // depois alterar aqui pra pegar o nome no banco de dados + style: TextStyle(color: Color.fromARGB(255, 0, 0, 0)), + ), + accountEmail: const Text( + 'Administrador', // depois alterar aqui pra pegar o setor no banco de dados + style: TextStyle(color: Color.fromARGB(255, 0, 0, 0)), + ), + currentAccountPicture: CircleAvatar( + backgroundColor: Colors.grey.shade200, + child: const Icon(Icons.person), + ), + decoration: BoxDecoration( + color: Color(0xFFF4D5D7), + ), + ), + ListTile( + title: const Text('Demandas'), + onTap: () { + Navigator.pushReplacementNamed(context, '/home/'); + }, + ), + ListTile( + title: const Text('Funcionários'), + onTap: () { + Navigator.pushReplacementNamed( + context, '/dashboard-admin-crud-funcionarios'); + }, + ), + ListTile( + title: const Text('Produtos'), + onTap: () {}, + ), + ListTile( + title: const Text('Sair da Conta'), + onTap: () {}, + ), + ], + ), + ); + } +} diff --git a/catavento/lib/screens/components/showCustomDialog.dart b/catavento/lib/screens/dashboardAdmin/components/showCustomDialog.dart similarity index 98% rename from catavento/lib/screens/components/showCustomDialog.dart rename to catavento/lib/screens/dashboardAdmin/components/showCustomDialog.dart index 1ed3edb..06cd2e7 100644 --- a/catavento/lib/screens/components/showCustomDialog.dart +++ b/catavento/lib/screens/dashboardAdmin/components/showCustomDialog.dart @@ -1,6 +1,6 @@ import 'package:flutter/material.dart'; import 'package:intl/intl.dart'; -import 'package:catavento/screens/components/stage_demand.dart'; +import 'package:catavento/screens/dashboardAdmin/components/stage_demand.dart'; class ShowCustomDialog extends StatelessWidget { final String name; diff --git a/catavento/lib/screens/components/stage_demand.dart b/catavento/lib/screens/dashboardAdmin/components/stage_demand.dart similarity index 100% rename from catavento/lib/screens/components/stage_demand.dart rename to catavento/lib/screens/dashboardAdmin/components/stage_demand.dart diff --git a/catavento/lib/screens/dashboard_admin.dart b/catavento/lib/screens/dashboardAdmin/dashboard_admin.dart similarity index 96% rename from catavento/lib/screens/dashboard_admin.dart rename to catavento/lib/screens/dashboardAdmin/dashboard_admin.dart index 1ea20d9..ae6ff47 100644 --- a/catavento/lib/screens/dashboard_admin.dart +++ b/catavento/lib/screens/dashboardAdmin/dashboard_admin.dart @@ -5,10 +5,11 @@ import 'package:catavento/bloc/demanda_controller.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:intl/intl.dart'; -import 'package:catavento/screens/components/stage_demand.dart'; -import '../services/table_import/table_import.dart'; -import '../services/table_import/table_picker.dart'; -import 'components/confirmDialog.dart'; +import 'package:catavento/screens/dashboardAdmin/components/stage_demand.dart'; +import 'package:catavento/screens/dashboardAdmin/components/menu.dart'; +import '../../services/table_import/table_import.dart'; +import '../../services/table_import/table_picker.dart'; +import '../dashboardAdmin/components/confirmDialog.dart'; import 'package:image_picker/image_picker.dart'; @@ -18,9 +19,57 @@ class DashBoardAdmin extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( + drawer: Navbar(), + appBar: AppBar( + backgroundColor: Colors.transparent, + elevation: 0, + iconTheme: const IconThemeData( + color: Color(0xFF015C98), + ), + leading: Builder( + builder: (BuildContext context) { + return Padding( + padding: const EdgeInsets.only(left: 20), // Padding aqui + child: Material( + color: Colors.transparent, // Mantém o fundo transparente + child: InkWell( + onTap: () { + Scaffold.of(context).openDrawer(); // Abre o Drawer + }, + child: const Icon( + Icons.menu, + size: 40.0, + color: Color(0xFF015C98), + ), + ), + ), + ); + }, + ), + actions: [ + Padding( + padding: const EdgeInsets.only( + right: 20), // Ajuste o valor conforme necessário + child: TextButton.icon( + icon: const Icon(Icons.history, size: 18.0), + label: const Text("Ver Histórico"), + onPressed: () {}, + style: TextButton.styleFrom( + foregroundColor: Colors.white, + backgroundColor: const Color(0xFF015C98), + padding: + const EdgeInsets.symmetric(horizontal: 15, vertical: 10), + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(30), + ), + ), + ), + ), + ], + ), + extendBodyBehindAppBar: true, body: Stack( children: [ - // Tela de fundo Container( width: double.infinity, height: double.infinity, @@ -33,9 +82,6 @@ class DashBoardAdmin extends StatelessWidget { ), child: Center(child: AddDemandPageAdmin()), ), - - // Icon menu - IconMenu(), ], ), floatingActionButton: FloatingActionButton( @@ -176,7 +222,7 @@ class QuadroGraficoState extends State { @override Widget build(BuildContext context) { return BlocBuilder( - buildWhen: (previous, current) => current is! FilterState, + buildWhen: (previous, current) => current is! DemandaFilterState, builder: (context, response) { final metaData = response.metaData; diff --git a/catavento/lib/screens/dashboardFuncionarios/dashboard_funcionarios.dart b/catavento/lib/screens/dashboardFuncionarios/dashboard_funcionarios.dart new file mode 100644 index 0000000..6daf9bf --- /dev/null +++ b/catavento/lib/screens/dashboardFuncionarios/dashboard_funcionarios.dart @@ -0,0 +1,90 @@ +import 'package:flutter/material.dart'; + +import 'package:catavento/screens/dashboardAdmin/components/menu.dart'; +import '../../services/table_import/table_import.dart'; +import '../../services/table_import/table_picker.dart'; + +class DashBoardFuncionarios extends StatelessWidget { + const DashBoardFuncionarios({super.key}); + + @override + Widget build(BuildContext context) { + return Scaffold( + drawer: Navbar(), + appBar: AppBar( + backgroundColor: Colors.transparent, + elevation: 0, + iconTheme: const IconThemeData( + color: Color(0xFF015C98), + ), + leading: Builder( + builder: (BuildContext context) { + return Padding( + padding: const EdgeInsets.only(left: 20), // Padding aqui + child: Material( + color: Colors.transparent, // Mantém o fundo transparente + child: InkWell( + onTap: () { + Scaffold.of(context).openDrawer(); // Abre o Drawer + }, + child: const Icon( + Icons.menu, + size: 40.0, + color: Color(0xFF015C98), + ), + ), + ), + ); + }, + ), + actions: [ + Padding( + padding: const EdgeInsets.only( + right: 20), // Ajuste o valor conforme necessário + child: TextButton.icon( + icon: const Icon(Icons.history, size: 18.0), + label: const Text("Ver Histórico"), + onPressed: () {}, + style: TextButton.styleFrom( + foregroundColor: Colors.white, + backgroundColor: const Color(0xFF015C98), + padding: + const EdgeInsets.symmetric(horizontal: 15, vertical: 10), + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(30), + ), + ), + ), + ), + ], + ), + extendBodyBehindAppBar: true, + body: Stack( + children: [ + Container( + width: double.infinity, + height: double.infinity, + decoration: BoxDecoration( + gradient: LinearGradient( + colors: [Color(0xFF75CDF3), Color(0xFFB2E8FF)], + begin: Alignment.topCenter, + end: Alignment.bottomCenter, + ), + ), + child: Center(), + ), + ], + ), + floatingActionButton: FloatingActionButton( + backgroundColor: Color(0xFF015C98), + onPressed: () async { + final filePath = await tablePicker(); + if (filePath != null) { + await importExcelToSupabase(filePath); + } + }, + child: Icon(Icons.upload_file, color: Colors.white), + ), + ); + } +} diff --git a/catavento/lib/services/table_import/table_import.dart b/catavento/lib/services/table_import/table_import.dart index 35968a8..2987a7a 100644 --- a/catavento/lib/services/table_import/table_import.dart +++ b/catavento/lib/services/table_import/table_import.dart @@ -1,7 +1,17 @@ import 'dart:io'; +import 'package:catavento/constants.dart'; +import 'package:intl/intl.dart'; import 'package:supabase_flutter/supabase_flutter.dart'; import 'package:excel/excel.dart'; +const lojasPrioridade = { + 'MAGALU': 'alta', + 'MERCADO LIVRE': 'baixa', + 'SITE': 'media', + 'ELO 7': 'alta', + 'SHOPEE': 'baixa', +}; + Future importExcelToSupabase(String filePath) async { final supabase = Supabase.instance.client; @@ -13,13 +23,9 @@ Future importExcelToSupabase(String filePath) async { final sheet = excel.tables[sheetName]; if (sheet != null) { - excel.link(sheetName, sheet); - - if (sheet.maxColumns == 3) { - sheet.insertColumn(3); - } - var rowId = 0; + var loja = ""; + final dataAdicao = DateFormat(timeFormat).format(DateTime.now()); for (var row in sheet.rows) { final rowData = row.map((cell) => cell?.value.toString() ?? '').toList(); @@ -27,30 +33,29 @@ Future importExcelToSupabase(String filePath) async { try { // Evita linhas inválidas if ((rowData[0] != "" && rowData[0] != "null") && - rowData[3] != "1") { - final Map parsedData = { - 'nomeDemanda': rowData[0], - 'funcionario': rowData[1] != "null" ? rowData[1] : "0", - 'status': rowData[3].isNotEmpty == true ? rowData[3] : '0', + (rowData[1] != "" && rowData[1] != "null")) { + final Map demanda = { + 'nome_demanda': rowData[0], + 'data_adicao': dataAdicao, + 'descricao': 'Bolo normal', + 'status': 'Pendente', }; - // Envia os dados ao Supabase - final response = - await supabase.from('demandas').insert(parsedData); + final codigo = int.tryParse(rowData[1]); - if (response.error != null) { - print( - 'Erro ao inserir no Supabase: ${response.error!.message}'); + if (codigo != null) { + demanda['produto_id'] = codigo; + } + + if (lojasPrioridade.containsKey(loja)) { + demanda['prioridade'] = lojasPrioridade[loja]; } else { - print('Linha ${rowId + 1} enviada com sucesso: $parsedData'); + demanda['prioridade'] = 'media'; } - // Marca a linha como processada - var cell = sheet.cell(CellIndex.indexByColumnRow( - columnIndex: 3, - rowIndex: rowId, - )); - cell.value = TextCellValue('X'); + await supabase.from('demandas').insert(demanda); + } else if (rowData[0] != "" && rowData[0] != "null") { + loja = rowData[0].toUpperCase(); } } catch (e) { print('Erro ao processar a linha ${rowId + 1}: $e'); @@ -59,9 +64,6 @@ Future importExcelToSupabase(String filePath) async { } } } - - // Salva o arquivo Excel atualizado - await file.writeAsBytes(excel.encode()!); print('Planilha importada e atualizada com sucesso!'); } catch (e) { print('Erro ao importar a planilha: $e'); diff --git a/catavento/pubspec.lock b/catavento/pubspec.lock index 9d9b40c..bdb3413 100644 --- a/catavento/pubspec.lock +++ b/catavento/pubspec.lock @@ -348,10 +348,10 @@ packages: dependency: "direct main" description: name: intl - sha256: d6f56758b7d3014a48af9701c085700aac781a92a87a62b1333b46d8879661cf + sha256: "00f33b908655e606b86d2ade4710a231b802eec6f11e87e4ea3783fd72077a50" url: "https://pub.dev" source: hosted - version: "0.19.0" + version: "0.20.1" jwt_decode: dependency: transitive description: @@ -468,10 +468,10 @@ packages: dependency: transitive description: name: path_provider_foundation - sha256: f234384a3fdd67f989b4d54a5d73ca2a6c422fa55ae694381ae0f4375cd1ea16 + sha256: "4843174df4d288f5e29185bd6e72a6fbdf5a4a4602717eed565497429f179942" url: "https://pub.dev" source: hosted - version: "2.4.0" + version: "2.4.1" path_provider_linux: dependency: transitive description: @@ -572,10 +572,10 @@ packages: dependency: transitive description: name: shared_preferences_android - sha256: "3b9febd815c9ca29c9e3520d50ec32f49157711e143b7a4ca039eb87e8ade5ab" + sha256: "7f172d1b06de5da47b6264c2692ee2ead20bbbc246690427cdb4fc301cd0c549" url: "https://pub.dev" source: hosted - version: "2.3.3" + version: "2.3.4" shared_preferences_foundation: dependency: transitive description: @@ -721,10 +721,10 @@ packages: dependency: transitive description: name: url_launcher_ios - sha256: e43b677296fadce447e987a2f519dcf5f6d1e527dc35d01ffab4fff5b8a7063e + sha256: "16a513b6c12bb419304e72ea0ae2ab4fed569920d1c7cb850263fe3acc824626" url: "https://pub.dev" source: hosted - version: "6.3.1" + version: "6.3.2" url_launcher_linux: dependency: transitive description: @@ -737,10 +737,10 @@ packages: dependency: transitive description: name: url_launcher_macos - sha256: "769549c999acdb42b8bcfa7c43d72bf79a382ca7441ab18a808e101149daf672" + sha256: "17ba2000b847f334f16626a574c702b196723af2a289e7a93ffcb79acff855c2" url: "https://pub.dev" source: hosted - version: "3.2.1" + version: "3.2.2" url_launcher_platform_interface: dependency: transitive description: @@ -809,10 +809,10 @@ packages: dependency: transitive description: name: win32 - sha256: "84ba388638ed7a8cb3445a320c8273136ab2631cd5f2c57888335504ddab1bc2" + sha256: "8b338d4486ab3fbc0ba0db9f9b4f5239b6697fcee427939a40e720cbb9ee0a69" url: "https://pub.dev" source: hosted - version: "5.8.0" + version: "5.9.0" xdg_directories: dependency: transitive description: diff --git a/catavento/pubspec.yaml b/catavento/pubspec.yaml index 7976d89..25c2fd0 100644 --- a/catavento/pubspec.yaml +++ b/catavento/pubspec.yaml @@ -37,7 +37,7 @@ dependencies: # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.8 supabase_flutter: ^2.8.1 - intl: ^0.19.0 + intl: ^0.20.1 fl_chart: ^0.69.2 file_picker: ^8.1.4 path_provider: ^2.1.5