import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:rkm/config/const_variable.dart';
import 'package:rkm/widgets/appbar_default.dart';
import 'package:rkm/widgets/input_text.dart';
import 'package:rkm/widgets/onbackbutton.dart';
import 'package:rkm/widgets/text_default.dart';
import '../config/color_palette.dart';
import '../widgets/button_default.dart';
import '../widgets/dropdown_button_default.dart';
import '../widgets/subtitle_containers.dart';
import '../widgets/title_containers.dart';

class EditProceduresScreen extends StatefulWidget {
  const EditProceduresScreen({Key? key}) : super(key: key);

  @override
  State<EditProceduresScreen> createState() => _EditProceduresScreenState();
}

class _EditProceduresScreenState extends State<EditProceduresScreen> {

  var controllerProfessional = TextEditingController();
  var controllerUnit = TextEditingController();
  String? selectCBO;
  String? selectTeam;
  String? date;

  escolherprazoInicio(BuildContext context) async {
    var pickDate = await showDatePicker(
        context: context,
        initialDate: DateTime.now(),
        firstDate: DateTime(2022),
        lastDate: DateTime(2025));
    if (pickDate != null) {
      setState(() {
        date=null;
        date = formatData(year: pickDate.year, month: pickDate.month, day: pickDate.day);
      });
    }
  }

  String formatData({int? year, int? month, int? day}) {
    DateTime data = DateTime(year!, month!, day!);
    String formattedDate = DateFormat('dd/MM/yyyy').format(data);
    return formattedDate;
  }
  
  @override
  Widget build(BuildContext context) {

    double width = MediaQuery.of(context).size.width;
    double height = MediaQuery.of(context).size.height;

    return WillPopScope(
      onWillPop: ()=> OnBackButton().onBackButton(context),
      child: Scaffold(
      appBar: AppBarDefault().appbar(context, 'RKM AB - Procedimentos Consolidados'),
      body: Container(
        decoration: ConstVariable().backgroundGradient,
        width: width,
        height: height,
        padding: EdgeInsets.all(10),
        child: ListView(
          children: [
            Card(
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  TitleContainers(title: 'Procedimentos',),
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Container(
                          alignment: Alignment.center,
                          height: height*0.32,
                          width: width*0.2,
                          child: Icon(Icons.info_outlined,size: 100,color: ColorPalette.black54,)
                      ),
                      Container(
                        height: height*0.32,
                        width: width*0.75,
                        child: ListView.separated(
                          itemCount: ConstVariable().listProcedures.length,
                          separatorBuilder: (context,index)=>Divider(color: ColorPalette.black54,height: 5,endIndent: 10,),
                          itemBuilder: (context,index){
                            return  Padding(
                              padding: const EdgeInsets.all(8.0),
                              child: TextDefault(
                                  text: ConstVariable().listProcedures[index],
                                  color: ColorPalette.black54,
                                  fontSize: 18
                              ),
                            );
                          },
                        ),
                      )
                    ],
                  ),
                ],
              ),
            ),
            Card(
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  TitleContainers(title: 'Outros',),
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Container(
                          alignment: Alignment.center,
                          height: height*0.24,
                          width: width*0.2,
                          child: Icon(Icons.tag,size: 100,color: ColorPalette.black54,)
                      ),
                      Container(
                        height: height*0.25,
                        width: width*0.75,
                        child: ListView(
                            children: [
                              Row(
                                children: [
                                  Padding(
                                    padding: const EdgeInsets.all(8.0),
                                    child: TextDefault(
                                      text: 'Profissional',
                                      color: ColorPalette.black54,
                                      fontSize: 18,
                                    ),
                                  ),
                                  Spacer(),
                                  Container(
                                    margin: EdgeInsets.symmetric(horizontal: 5,vertical: 10),
                                    padding: EdgeInsets.all(5),
                                    width: width*0.5,
                                    color: ColorPalette.titleContainer,
                                    child: TextDefault(text: 'ADMINISTRADOR',color: ColorPalette.black54,fontSize: 15,),
                                  )
                                ],
                              ),
                              Divider(color: ColorPalette.black54,height: 5,endIndent: 10,),
                              Row(
                                children: [
                                  Padding(
                                    padding: const EdgeInsets.all(8.0),
                                    child: TextDefault(
                                      text: 'Unidade',
                                      color: ColorPalette.black54,
                                      fontSize: 18,
                                    ),
                                  ),
                                  Spacer(),
                                  Container(
                                    margin: EdgeInsets.symmetric(horizontal: 5),
                                    padding: EdgeInsets.all(5),
                                    width: width*0.5,
                                    color: ColorPalette.titleContainer,
                                    child: TextDefault(
                                      text: 'USF PINHAL - EMILIA STEPHANI SIMIONATO',
                                      color: ColorPalette.black54,
                                      fontSize: 15,
                                    ),
                                  )
                                ],
                              ),
                              Divider(color: ColorPalette.black54,height: 5,endIndent: 10,),
                              Row(
                                children: [
                                  Padding(
                                    padding: const EdgeInsets.all(8.0),
                                    child: TextDefault(
                                      text: 'CBO',
                                      color: ColorPalette.black54,
                                      fontWeigth: FontWeight.bold,
                                      fontSize: 18,
                                    ),
                                  ),
                                  Spacer(),
                                  Container(
                                    margin: EdgeInsets.symmetric(horizontal: 0),
                                    padding: EdgeInsets.all(0),
                                    child: DropDownButtonDefault(
                                      title: '',
                                      fontSize: 15,
                                      width: width*0.50,
                                      widthContainer: width*0.45,
                                      select: selectCBO,
                                      list: ConstVariable().itensCBO,
                                      onChanged: (value){
                                        setState(() {
                                          selectCBO = value!;
                                        });
                                      },
                                    ),
                                  )
                                ],
                              ),
                              Divider(color: ColorPalette.black54,height: 5,endIndent: 10,),
                              Row(
                                children: [
                                  Padding(
                                    padding: const EdgeInsets.all(8.0),
                                    child: TextDefault(
                                      text: 'Código Equipe',
                                      color: ColorPalette.black54,
                                      fontSize: 18,
                                    ),
                                  ),
                                  Spacer(),
                                  Container(
                                    margin: EdgeInsets.symmetric(horizontal: 0),
                                    padding: EdgeInsets.all(0),
                                    child: DropDownButtonDefault(
                                      title: '',
                                      fontSize: 15,
                                      width: width*0.5,
                                      widthContainer: width*0.4,
                                      select: selectTeam,
                                      list: [],
                                      onChanged: (value){
                                        setState(() {
                                          selectTeam = value!;
                                        });
                                      },
                                    ),
                                  )
                                ],
                              ),
                              Divider(color: ColorPalette.black54,height: 5,endIndent: 10,),
                              date!=null
                                  ?GestureDetector(
                                onTap: ()=>escolherprazoInicio(context),
                                child: Row(
                                  children: [
                                    Padding(
                                      padding: const EdgeInsets.all(8.0),
                                      child: TextDefault(
                                        text: 'Data',
                                        color: ColorPalette.black54,
                                        fontWeigth: FontWeight.bold,
                                        fontSize: 18,
                                      ),
                                    ),
                                    Spacer(),
                                    Container(
                                      margin: EdgeInsets.symmetric(horizontal: 5),
                                      padding: EdgeInsets.all(5),
                                      width: width*0.5,
                                      child: TextDefault(
                                        text: date!,
                                        color: ColorPalette.black54,
                                        fontSize: 15,
                                      ),
                                    )
                                  ],
                                ),
                              ):GestureDetector(
                                onTap: ()=>escolherprazoInicio(context),
                                child: Padding(
                                  padding: const EdgeInsets.all(8.0),
                                  child: TextDefault(
                                    text: 'Data',
                                    color: ColorPalette.black54,
                                    fontWeigth: FontWeight.bold,
                                    fontSize: 18,
                                  ),
                                ),
                              ),
                              Divider(color: ColorPalette.black54,height: 5,endIndent: 10,),
                            ]
                        ),
                      )
                    ],
                  ),
                ],
              ),
            ),
            Card(
              child: Padding(
                padding: const EdgeInsets.all(8.0),
                child: Column(
                  children: [
                    ButtonDefault(
                      onPressed: (){},
                      title: 'Salvar',
                      borderColor: ColorPalette.appBar,
                      background: ColorPalette.appBar,
                      textColor: ColorPalette.white,
                      width: width,
                    ),
                    ButtonDefault(
                      onPressed: ()=>Navigator.pop(context),
                      title: 'Cancelar',
                      background: ColorPalette.blackButton,
                      borderColor: ColorPalette.blackButton,
                      textColor: ColorPalette.white,
                      width: width,
                    ),
                  ],
                ),
              ),
            )
          ],
        ),
      ),
    ),
    );
  }
}
