import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:line_icons/line_icons.dart';
import 'package:rkm/models/check_model.dart';
import 'package:rkm/config/color_palette.dart';
import 'package:rkm/widgets/alert_dialog_default.dart';
import 'package:rkm/widgets/appbar_default.dart';
import '../../config/const_variable.dart';
import '../../widgets/button_default.dart';
import '../../widgets/check_box_subtitle.dart';
import '../../widgets/dropdown_with_subtitle.dart';
import '../../widgets/input_with_subtitle.dart';
import '../../widgets/onbackbutton.dart';
import '../../widgets/text_default.dart';
import '../../widgets/title_containers.dart';

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

  @override
  State<ServiceHomeScreen> createState() => _ServiceHomeScreenState();
}

class _ServiceHomeScreenState extends State<ServiceHomeScreen> {

  var professionalController = TextEditingController(text: 'ADMINISTRADOR');
  var unitController = TextEditingController(text: 'USF PINHAL - EMILIA STEPHANI SIMIONATO');
  var controllerSearchCondition = TextEditingController();
  var controllerSearchCID = TextEditingController();
  String date='Data';
  String? selectCBO;
  String? selectCodTeam;
  String? selectServicePlace;
  String? selectTypeService;
  String? selectModality;
  String? selectRotation;
  String? selectCIAP;
  String? selectCID;
  String? selectConduct;
  List<CheckModel> serviceHomeModel = [];
  List<CheckModel> proceduresModel = [];

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

  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 heigth = MediaQuery.of(context).size.height;
    double width = MediaQuery.of(context).size.width;

    return WillPopScope(
      onWillPop: ()=> OnBackButton().onBackButton(context),
      child: Scaffold(
        appBar: AppBarDefault().appbar(context, 'RKM AB - Atendimento Domiciliar'),
        body: Container(
          height: double.infinity,
          width: double.infinity,
          decoration: ConstVariable().backgroundGradient,
          child: ListView(
            children: [
              Card(
                margin: EdgeInsets.all(10),
                child: Column(
                  children: [
                    TitleContainers(title: 'Cadastro'),
                    Row(
                      children: [
                        Container(
                          height: heigth*0.5,
                          width: width*0.2,
                          child: Icon(Icons.business_center,size: 80,),
                        ),
                        Column(
                          mainAxisAlignment: MainAxisAlignment.start,
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            InputWithSubtitle(
                              controller: professionalController,
                              subtitle: 'Profissional',
                              errorInput: 'Campo Obrigatório',
                              labelInput: 'Profissional',
                              widthInput: width*0.7,
                              widthSubtitle: width*0.7,
                              hintInput: 'Escreva aqui',
                              enabled: false,
                              onChanged: (v)=>setState((){}),
                            ),
                            InputWithSubtitle(
                              controller: unitController,
                              subtitle: 'Unidade',
                              errorInput: 'Campo Obrigatório',
                              labelInput: 'Unidade',
                              widthInput: width*0.7,
                              widthSubtitle: width*0.7,
                              hintInput: 'Escreva aqui',
                              enabled: false,
                              onChanged: (v)=>setState((){}),
                            ),
                            DropdownWithSubtitle(
                              list: [],
                              widthSubtitle: width*0.7,
                              subtitle: 'CBO',
                              hintDropdown: 'CBO',
                              select: selectCBO,
                              widthDropdown:  width*0.7,
                              widthContainerDropdown: width*0.6,
                              onChanged: (value){
                                setState(() {
                                  selectCBO = value!;
                                });
                              },
                              colorBorder: ColorPalette.white,
                            ),
                            DropdownWithSubtitle(
                              list: ['INE: 0001558536 / Área 4 - Microárea: - '],
                              widthSubtitle: width*0.2,
                              subtitle: 'Código da Equipe',
                              hintDropdown: 'Selecione',
                              select: selectCodTeam,
                              widthDropdown:  width*0.7,
                              widthContainerDropdown:  width*0.4,
                              onChanged: (value){
                                setState(() {
                                  selectCodTeam = value!;
                                });
                              },
                              colorBorder: ColorPalette.white,
                            ),
                            GestureDetector(
                              onTap: ()=>escolherprazoInicio(context),
                              child: Container(
                                height: 50,
                                margin: EdgeInsets.symmetric(horizontal: 10),
                                padding: EdgeInsets.symmetric(horizontal: 8,vertical: 10),
                                decoration: BoxDecoration(
                                  border: Border.all(color: ColorPalette.white),
                                  borderRadius: BorderRadius.all(
                                      Radius.circular(5.0) //                 <--- border radius here
                                  ),
                                ),
                                width: width*0.7,
                                child: TextDefault(
                                  text: date,
                                  color: ColorPalette.black54,
                                  fontSize: 18,
                                ),
                              ),
                            ),
                            DropdownWithSubtitle(
                              list: ConstVariable().listServicePlace,
                              widthSubtitle: width*0.2,
                              subtitle: 'Local de Atendimento',
                              hintDropdown: 'Local de Atendimento',
                              select: selectServicePlace,
                              widthDropdown:  width*0.7,
                              widthContainerDropdown:  width*0.6,
                              onChanged: (value)=>setState(()=>selectServicePlace = value!),
                              colorBorder: ColorPalette.white,
                            ),
                            DropdownWithSubtitle(
                              list: ConstVariable().typeService,
                              widthSubtitle: width*0.2,
                              subtitle: 'Tipo de Atendimento',
                              hintDropdown: 'Tipo de Atendimento',
                              select: selectTypeService,
                              widthDropdown:  width*0.7,
                              widthContainerDropdown:  width*0.6,
                              onChanged: (value){
                                setState(() {
                                  selectTypeService = value!;
                                });
                              },
                              colorBorder: ColorPalette.white,
                            ),
                            DropdownWithSubtitle(
                              list: ['AD1','AD2','AD3'],
                              widthSubtitle: width*0.2,
                              subtitle: 'Modalidade',
                              hintDropdown: 'Modalidade',
                              select: selectModality,
                              widthDropdown:  width*0.7,
                              widthContainerDropdown:  width*0.6,
                              onChanged: (value){
                                setState(() {
                                  selectModality = value!;
                                });
                              },
                              colorBorder: ColorPalette.white,
                            ),
                            DropdownWithSubtitle(
                              list: ['MANHÃ','TARDE','NOITE'],
                              widthSubtitle: width*0.2,
                              subtitle: 'Turno',
                              hintDropdown: 'Turno',
                              select: selectRotation,
                              widthDropdown:  width*0.7,
                              widthContainerDropdown:  width*0.6,
                              onChanged: (value){
                                setState(() {
                                  selectRotation = value!;
                                });
                              },
                              colorBorder: ColorPalette.white,
                            ),
                          ],
                        )
                      ],
                    )
                  ],
                ),
              ),
              Card(
                margin: EdgeInsets.all(10),
                child: Column(
                  children: [
                    TitleContainers(title: 'Condições Avaliadas'),
                    Row(
                        children: [
                          Container(
                            height: heigth*0.4,
                            width: width*0.2,
                            child: Icon(LineIcons.edit,size: 80,),
                          ),
                          Padding(
                            padding: const EdgeInsets.symmetric(horizontal: 10.0),
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.start,
                              mainAxisSize: MainAxisSize.min,
                              children: [
                                SizedBox(height: 10,),
                                TitleContainers(title: 'Condição Avaliada',width: width * 0.73),
                                Container(
                                  width: width*0.7,
                                  height: heigth*1.3,
                                  child: ListView.builder(
                                    physics: const NeverScrollableScrollPhysics(),
                                    itemCount: ConstVariable().serviceHomeCheckConditionlist.length,
                                    itemBuilder: (context,index){
                                      serviceHomeModel.add(
                                          CheckModel(
                                            subtitle: ConstVariable().serviceHomeCheckConditionlist[index],
                                            check: false
                                          )
                                      );
                                      return CheckBoxSubtitle(
                                        subtitle: serviceHomeModel[index].subtitle,
                                        checkbox:serviceHomeModel[index].check,
                                        width: width*0.6,
                                        onChanged: (value)=>setState(()=>serviceHomeModel[index].check=value),
                                      );
                                    }
                                  ),
                                ),
                                GestureDetector(
                                  onTap: ()=>AlertDialogDefault().alert_with_search(
                                      context,
                                      controllerSearchCondition,
                                      ListView.separated(
                                        itemCount: ConstVariable().CIAP.length,
                                        separatorBuilder: (context,index)=>Divider(color: ColorPalette.black54),
                                        itemBuilder: (context,index){
                                          return ListTile(
                                            onTap: ()=>setState(() {
                                              selectCIAP = ConstVariable().CIAP[index];
                                              Navigator.pop(context);
                                            }),
                                            title: TextDefault(text: ConstVariable().CIAP[index],maxLines: 2,)
                                          );
                                        },
                                      )
                                  ),
                                  child: Container(
                                    padding: EdgeInsets.symmetric(vertical: 10),
                                    width: width*0.7,
                                    child: TextDefault(
                                      text: selectCIAP!=null?'CIAP $selectCIAP':'CIAP',
                                      color: ColorPalette.black54,
                                    )
                                  )
                                ),
                                GestureDetector(
                                  onTap: ()=>AlertDialogDefault().alert_with_search(
                                      context,
                                      controllerSearchCID,
                                      ListView.separated(
                                        itemCount: ConstVariable().CID.length,
                                        separatorBuilder: (context,index)=>Divider(color: ColorPalette.black54),
                                        itemBuilder: (context,index){
                                          return ListTile(
                                            onTap: ()=>setState(() {
                                              selectCID = ConstVariable().CIAP[index];
                                              Navigator.pop(context);
                                            }),
                                            title: TextDefault(text: ConstVariable().CID[index],maxLines: 2,)
                                          );
                                        },
                                      )
                                  ),
                                  child: Container(
                                    padding: EdgeInsets.symmetric(vertical: 10),
                                    width: width*0.7,
                                    child: TextDefault(
                                      text: selectCID!=null?'CID $selectCIAP':'CID',
                                      color: ColorPalette.black54,
                                    )
                                  )
                                ),
                              ],
                            ),
                          ),
                        ]
                    ),
                  ],
                ),
              ),
              Card(
                margin: EdgeInsets.all(10),
                child: Column(
                  children: [
                    TitleContainers(title: 'Procedimentos'),
                    Row(
                        children: [
                          Container(
                            height: heigth*0.4,
                            width: width*0.2,
                            child: Icon(Icons.info_outlined,size: 80,),
                          ),
                          Padding(
                            padding: const EdgeInsets.symmetric(horizontal: 10.0),
                            child:  Container(
                              width: width*0.7,
                              height: heigth*1.1,
                              child: ListView.builder(
                                  physics: const NeverScrollableScrollPhysics(),
                                  itemCount: ConstVariable().proceduresModel.length,
                                  itemBuilder: (context,index){
                                    proceduresModel.add(
                                        CheckModel(
                                            subtitle: ConstVariable().proceduresModel[index],
                                            check: false
                                        )
                                    );
                                    return CheckBoxSubtitle(
                                      subtitle: proceduresModel[index].subtitle,
                                      checkbox:proceduresModel[index].check,
                                      width: width*0.6,
                                      onChanged: (value)=>setState(()=>proceduresModel[index].check=value),
                                    );
                                  }
                              ),
                            ),
                          ),
                        ]
                    ),
                  ],
                ),
              ),
              Card(
                margin: EdgeInsets.all(10),
                child: Column(
                  children: [
                    TitleContainers(title: 'Procedimentos'),
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Container(
                        height: heigth*0.2,
                          width: width*0.2,
                          child: Icon(Icons.add,size: 130,),
                        ),
                        Padding(
                            padding: const EdgeInsets.symmetric(horizontal: 10.0),
                            child:  Container(
                              width: width*0.7,
                              child: Row(
                                children: [
                                  TextDefault(text: 'Procedimento'),
                                  Spacer(),
                                  IconButton(onPressed: (){}, icon: Icon(Icons.add_circle,color: ColorPalette.blackButton,))
                                ],
                              ),
                            )
                        )
                      ]
                    )
                  ]
                )
              ),
              Card(
                margin: EdgeInsets.all(10),
                child: Column(
                  children: [
                    TitleContainers(title: 'Outros'),
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Container(
                        height: heigth*0.2,
                          width: width*0.2,
                          child: Icon(Icons.tag,size: 130,),
                        ),
                        Padding(
                            padding: const EdgeInsets.symmetric(horizontal: 5.0),
                            child:  Container(
                              width: width*0.7,
                              child: Row(
                                children: [
                                  TextDefault(text: 'Conduta / Desfecho',fontSize: 15),
                                  Spacer(),
                                  DropdownWithSubtitle(
                                    colorBorder: ColorPalette.white,
                                    list: ConstVariable().listConduct,
                                    subtitle: 'SELECIONE',
                                      select: selectConduct,
                                      widthDropdown: width*0.4,
                                      widthContainerDropdown: width*0.35,
                                      widthSubtitle: width*0.35,
                                      hintDropdown: 'SELECIONE',
                                      onChanged: (value)=>setState(()=>selectConduct=value),
                                  )
                                ],
                              ),
                            )
                        )
                      ]
                    )
                  ]
                )
              ),
              Card(
                margin: EdgeInsets.all(10),
                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: double.infinity,
                      ),
                      ButtonDefault(
                        onPressed: ()=>Navigator.pop(context),
                        title: 'Cancelar',
                        borderColor: ColorPalette.blackButton,
                        background: ColorPalette.blackButton,
                        textColor: ColorPalette.white,
                        width: double.infinity,
                      )
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
