import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:get/get.dart'; import 'package:skripsi/config/theme.dart'; import 'package:skripsi/controller/cTambahKasusTraining.dart'; import 'package:skripsi/controller/cUser.dart'; import 'package:skripsi/view/home_page.dart'; import 'package:skripsi/widget/dialog.dart'; class TmbhDataTraining extends StatefulWidget { const TmbhDataTraining({super.key}); @override State createState() => _TmbhDataTrainingState(); } class _TmbhDataTrainingState extends State { final formKey = GlobalKey(); final tkpController = TextEditingController(); final jumlahKejahatanController = TextEditingController(); final jarakTkpPolresController = TextEditingController(); final statusController = TextEditingController(); final cTambahKasusTraining = Get.put(CTambahDataTraining()); final cUser = Get.put(CUser()); postData() async { if (tkpController.text.isEmpty) { Get.snackbar("Error", "Masukkan TKP", backgroundColor: Colors.red); } else if (jumlahKejahatanController.text.isEmpty) { Get.snackbar("Error", "Masukkan Jumlah Kejahatan", backgroundColor: Colors.red); } else if (jarakTkpPolresController.text.isEmpty) { Get.snackbar("Error", "Masukkan Jarak TKP - Polres Terdekat", backgroundColor: Colors.red); } else if (statusController.text.isEmpty) { Get.snackbar("Error", "Masukkan Status", backgroundColor: Colors.red); } else { await cTambahKasusTraining.postDataTraining( cUser.data.idUser ?? "", tkpController.text, jumlahKejahatanController.text, jarakTkpPolresController.text, statusController.text); if (cTambahKasusTraining.success) { Get.snackbar("Success", 'Data Berhasil Ditambahkan', backgroundColor: Colors.green); Get.offAll(const HomePage()); } else { Get.snackbar("Error", 'Data Gagal Ditambahkan', backgroundColor: Colors.red); } } } @override Widget build(BuildContext context) { return Scaffold( resizeToAvoidBottomInset: false, backgroundColor: whiteColor, appBar: AppBar( elevation: 0, backgroundColor: whiteColor, leadingWidth: 70.w, leading: TextButton( onPressed: () { Navigator.pop(context); }, child: Text('Batal', style: robotoSedangHitam), ), centerTitle: true, title: Text('Tambah Kasus Training', style: robotoSedangHitam), ), body: Stack( children: [ Padding( padding: EdgeInsets.all(8.w), child: Column( children: [ TextFormField( keyboardType: TextInputType.emailAddress, controller: tkpController, autofocus: true, decoration: InputDecoration( labelText: 'TKP', labelStyle: robotoSedangHitam.copyWith( fontWeight: FontWeight.bold, ), hintText: 'Masukkan TKP', enabledBorder: const UnderlineInputBorder( borderSide: BorderSide( color: Color(0xffC6C6C6), ), ), focusedBorder: const UnderlineInputBorder( borderSide: BorderSide( color: Color(0xffC6C6C6), ), ), ), ), SizedBox(height: 20.h), TextFormField( controller: jumlahKejahatanController, keyboardType: TextInputType.number, autofocus: true, decoration: InputDecoration( labelText: 'Jumlah Kejahatan', labelStyle: robotoSedangHitam.copyWith(fontWeight: FontWeight.bold), hintText: 'Masukkan Jumlah Kejahatan', enabledBorder: const UnderlineInputBorder( borderSide: BorderSide( color: Color(0xffC6C6C6), ), ), focusedBorder: const UnderlineInputBorder( borderSide: BorderSide( color: Color(0xffC6C6C6), ), ), ), ), SizedBox(height: 20.h), TextFormField( keyboardType: TextInputType.number, controller: jarakTkpPolresController, autofocus: true, decoration: InputDecoration( labelText: 'Jarak TKP - Polres', labelStyle: robotoSedangHitam.copyWith(fontWeight: FontWeight.bold), hintText: 'Masukkan Jarak TKP - Polres', enabledBorder: const UnderlineInputBorder( borderSide: BorderSide( color: Color(0xffC6C6C6), ), ), focusedBorder: const UnderlineInputBorder( borderSide: BorderSide( color: Color(0xffC6C6C6), ), ), ), ), SizedBox(height: 20.h), TextFormField( keyboardType: TextInputType.text, controller: statusController, autofocus: true, decoration: InputDecoration( labelText: 'Status', labelStyle: robotoSedangHitam.copyWith(fontWeight: FontWeight.bold), hintText: 'Masukkan Status', enabledBorder: const UnderlineInputBorder( borderSide: BorderSide( color: Color(0xffC6C6C6), ), ), focusedBorder: const UnderlineInputBorder( borderSide: BorderSide( color: Color(0xffC6C6C6), ), ), ), ), SizedBox(height: 12.h), ElevatedButton( onPressed: () { postData(); }, child: Text( 'Simpan', style: robotoSedangHitam.copyWith( color: primaryColor, fontWeight: FontWeight.bold), ), ), ], ), ), Obx(() { if (cTambahKasusTraining.loading) { return showCustomLoadingDialog(context); } return const SizedBox.shrink(); }), ], ), ); } }