import 'package:absenkuv3/Bloc/Aktivitas/DetailAktivitasBloc.dart';
import 'package:absenkuv3/Bloc/AuthBloc.dart';
import 'package:absenkuv3/Bloc/NavigatorBloc.dart';
import 'package:absenkuv3/Repository/Model/PageModel/mAktivitasPage.dart';
import 'package:absenkuv3/Repository/Model/mLogin.dart';
import 'package:absenkuv3/Setting/PengaturanBahasa.dart';
import 'package:absenkuv3/Setting/value_image.dart';
import 'package:absenkuv3/UI/Template/halamanDetailMap.dart';
import 'package:absenkuv3/UI/Template/style.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class HalamanDetailAktivitas extends StatefulWidget {
  final String? data;
  HalamanDetailAktivitas(this.data);
  @override
  _HalamanDetailAktivitasState createState() => _HalamanDetailAktivitasState();
}

class _HalamanDetailAktivitasState extends State<HalamanDetailAktivitas> {
  GlobalKey<ScaffoldState> scaffoldKey = GlobalKey();
  @override
  void initState() {
    super.initState();
    BlocProvider.of<DetailAktivitasBloc>(context)
        .add(GetDetailAktivitas(widget.data));
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<PageAktivitasModel>(
        future: PengaturanBahasa().aktivitas(),
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            return Scaffold(
                key: scaffoldKey,
                backgroundColor: Colors.white,
                appBar: AppBar(
                  flexibleSpace: Image(
                    image: AssetImage(ValueImage.headerBiruDashboard),
                    fit: BoxFit.cover,
                  ),
                  backgroundColor: Colors.transparent,
                  centerTitle: true,
                  title: Text(
                    snapshot.data!.menuAktivitas!.titleDetail!,
                    style: TextStyle(fontSize: CustomStyle.fontSizeTitle),
                  ),
                  // actions: [

                  // ],
                ),
                body: BlocBuilder<DetailAktivitasBloc, DetailAktivitasState>(
                    builder:
                        (BuildContext context, DetailAktivitasState state) {
                  if (state is DetailAktivitasSuccess)
                    return Stack(
                      children: [
                        Container(
                          height: double.infinity,
                          child: SingleChildScrollView(
                            child: Padding(
                              padding:
                                  const EdgeInsets.fromLTRB(16, 18, 16, 16),
                              child: Column(
                                mainAxisSize: MainAxisSize.min,
                                children: [
                                  Padding(
                                    padding: const EdgeInsets.only(
                                        bottom: 12, top: 6),
                                    child: SingleChildScrollView(
                                      child: Column(
                                        crossAxisAlignment:
                                            CrossAxisAlignment.start,
                                        children: [
                                          Row(
                                            mainAxisAlignment:
                                                MainAxisAlignment.spaceBetween,
                                            children: [
                                              Row(
                                                children: [
                                                  // Icon(Icons.calendar_today_sharp, size: 30, color: CustomStyle.fontColorBlack,),
                                                  Image.asset(
                                                      'images/kalender_icon.png',
                                                      height: 25),
                                                  SizedBox(
                                                    width: 10,
                                                  ),
                                                  Text(
                                                    state.aktivitasRespon
                                                        .data![0].tanggalRaw!,
                                                    style: TextStyle(
                                                        fontSize: CustomStyle
                                                                .fontSizeSmall +
                                                            1,
                                                        color: CustomStyle
                                                            .fontColorBlack,
                                                        fontWeight:
                                                            FontWeight.w600),
                                                  ),
                                                  SizedBox(
                                                    width: 12,
                                                  ),
                                                ],
                                              ),

                                              BlocBuilder<AuthBloc,
                                                      LoginRespon>(
                                                  builder: (BuildContext
                                                          context,
                                                      LoginRespon loginRespon) {
                                                  return state.aktivitasRespon.data![0].idKaryawan==loginRespon.dataLogin!.idKaryawan? 
                                                  GestureDetector(
                                                    onTap: () => state
                                                                .aktivitasRespon
                                                                .data![0]
                                                                .flagFoto ==
                                                            'kamera'
                                                        ? BlocProvider.of<
                                                                    NavigatorBloc>(
                                                                context)
                                                            .add(
                                                                NavigateToEditAktivitas(
                                                                    context,[widget.data,context]))
                                                        : BlocProvider.of<
                                                                    NavigatorBloc>(
                                                                context)
                                                            .add(
                                                                NavigateToEditAktivitasForm(
                                                                    context, [widget.data,context])),
                                                    child: Container(
                                                        margin: EdgeInsets.only(
                                                            right: 14),
                                                        child: Icon(
                                                          Icons.edit,
                                                          size: 26,
                                                        )),
                                                  ):Container();
                                                }
                                              ),
                                            ],
                                          ),
                                          SizedBox(
                                            height: 6,
                                          ),
                                          Divider(
                                            thickness: 1,
                                          ),
                                          SizedBox(
                                            height: 4,
                                          ),
                                          Column(
                                            crossAxisAlignment:
                                                CrossAxisAlignment.start,
                                            children: [
                                              Text(
                                                snapshot.data!.menuAktivitas!
                                                    .data!.header!,
                                                style: TextStyle(
                                                    fontWeight: FontWeight.w600,
                                                    color: CustomStyle.color1,
                                                    fontSize: CustomStyle
                                                            .fontSizeSubTitle -
                                                        1),
                                              ),
                                              SizedBox(
                                                height: 10,
                                              ),
                                              Card(
                                                elevation: 2,
                                                shape: RoundedRectangleBorder(
                                                    borderRadius:
                                                        BorderRadius.circular(
                                                            13)),
                                                child: Row(
                                                  children: [
                                                    GestureDetector(
                                                      onTap: () => BlocProvider
                                                              .of<NavigatorBloc>(
                                                                  context)
                                                          .add(NavigateToDetailImageClicked(
                                                              context,
                                                              state
                                                                  .aktivitasRespon
                                                                  .data![0]
                                                                  .image!)),
                                                      child: Container(
                                                        width: 100,
                                                        height: 122,
                                                        decoration: BoxDecoration(
                                                            borderRadius: BorderRadius.only(
                                                                topLeft: Radius
                                                                    .circular(
                                                                        13),
                                                                bottomLeft: Radius
                                                                    .circular(
                                                                        13)),
                                                            image: DecorationImage(
                                                                image: NetworkImage(state
                                                                    .aktivitasRespon
                                                                    .data![0]
                                                                    .image!),
                                                                fit: BoxFit
                                                                    .fill)),
                                                      ),
                                                    ),
                                                    SizedBox(width: 27),
                                                    Expanded(
                                                      child: Column(
                                                        crossAxisAlignment:
                                                            CrossAxisAlignment
                                                                .start,
                                                        children: [
                                                          SizedBox(height: 17),
                                                          Row(
                                                            mainAxisAlignment:
                                                                MainAxisAlignment
                                                                    .spaceBetween,
                                                            children: [
                                                              Column(
                                                                crossAxisAlignment:
                                                                    CrossAxisAlignment
                                                                        .start,
                                                                children: [
                                                                  Text(
                                                                      snapshot
                                                                          .data!
                                                                          .menuAktivitas!
                                                                          .data!
                                                                          .jam!,
                                                                      style: TextStyle(
                                                                          fontSize:
                                                                              13,
                                                                          color: Color(
                                                                              0xFF828282),
                                                                          fontWeight:
                                                                              FontWeight.w400)),
                                                                  SizedBox(
                                                                      height:
                                                                          5),
                                                                  Text(
                                                                      state
                                                                          .aktivitasRespon
                                                                          .data![
                                                                              0]
                                                                          .waktu!,
                                                                      style: TextStyle(
                                                                          fontSize:
                                                                              14,
                                                                          fontWeight:
                                                                              FontWeight.w600)),
                                                                ],
                                                              ),
                                                              Container(
                                                                margin: EdgeInsets
                                                                    .only(
                                                                        right:
                                                                            17),
                                                                child:
                                                                    GestureDetector(
                                                                  onTap: () => BlocProvider.of<NavigatorBloc>(context).add(NavigateToDetailMap(
                                                                      context,
                                                                      ModelDetailMap(
                                                                          snapshot
                                                                              .data!
                                                                              .menuAktivitas!
                                                                              .data!
                                                                              .lokasiAktivitas!,
                                                                          double.parse(state
                                                                              .aktivitasRespon
                                                                              .data![
                                                                                  0]
                                                                              .latitude!),
                                                                          double.parse(state
                                                                              .aktivitasRespon
                                                                              .data![0]
                                                                              .longitude!)))),
                                                                  child: Icon(
                                                                    Icons
                                                                        .pin_drop,
                                                                    size: 30,
                                                                    color: Colors
                                                                        .redAccent,
                                                                  ),
                                                                ),
                                                              ),
                                                            ],
                                                          ),
                                                          SizedBox(height: 17),
                                                          Text(
                                                              snapshot
                                                                  .data!
                                                                  .menuAktivitas!
                                                                  .data!
                                                                  .lokasi!,
                                                              style: TextStyle(
                                                                  fontSize: 12,
                                                                  color: Color(
                                                                      0xFF828282),
                                                                  fontWeight:
                                                                      FontWeight
                                                                          .w400)),
                                                          SizedBox(height: 5),
                                                          Text(
                                                              state
                                                                  .aktivitasRespon
                                                                  .data![0]
                                                                  .lokasi!,
                                                              style: TextStyle(
                                                                  fontSize: 14,
                                                                  fontWeight:
                                                                      FontWeight
                                                                          .w600,
                                                                  overflow:
                                                                      TextOverflow
                                                                          .ellipsis)),
                                                          SizedBox(height: 17),
                                                        ],
                                                      ),
                                                    )
                                                  ],
                                                ),
                                              ),
                                              SizedBox(height: 17),
                                              BlocBuilder<AuthBloc,
                                                      LoginRespon>(
                                                  builder: (BuildContext
                                                          context,
                                                      LoginRespon loginRespon) {
                                                return Column(
                                                        crossAxisAlignment:
                                                            CrossAxisAlignment
                                                                .start,
                                                        children: [
                                                          Text(
                                                              snapshot
                                                                  .data!
                                                                  .menuAktivitas!
                                                                  .data!
                                                                  .namaKaryawan!,
                                                              style: TextStyle(
                                                                  fontSize: 13,
                                                                  fontWeight:
                                                                      FontWeight
                                                                          .w400)),
                                                          SizedBox(height: 5),
                                                          Text(
                                                              state
                                                                      .aktivitasRespon
                                                                      .data![0]
                                                                      .namaKaryawan ??
                                                                  '',
                                                              style: TextStyle(
                                                                  fontSize: 13,
                                                                  fontWeight:
                                                                      FontWeight
                                                                          .bold)),
                                                          SizedBox(height: 17),
                                                          Text(
                                                              snapshot
                                                                  .data!
                                                                  .menuAktivitas!
                                                                  .data!
                                                                  .jabatan!,
                                                              style: TextStyle(
                                                                  fontSize: 13,
                                                                  color: Color(
                                                                      0xFF828282),
                                                                  fontWeight:
                                                                      FontWeight
                                                                          .w400)),
                                                          SizedBox(height: 5),
                                                          Text(
                                                              state
                                                                      .aktivitasRespon
                                                                      .data![0]
                                                                      .namajabatan ??
                                                                  '',
                                                              style: TextStyle(
                                                                  fontSize: 13,
                                                                  fontWeight:
                                                                      FontWeight
                                                                          .bold)),
                                                          SizedBox(height: 17),
                                                          Text(
                                                              snapshot
                                                                  .data!
                                                                  .menuAktivitas!
                                                                  .data!
                                                                  .cabang!,
                                                              style: TextStyle(
                                                                  fontSize: 13,
                                                                  color: Color(
                                                                      0xFF828282),
                                                                  fontWeight:
                                                                      FontWeight
                                                                          .w400)),
                                                          SizedBox(height: 5),
                                                          Text(
                                                              state
                                                                      .aktivitasRespon
                                                                      .data![0]
                                                                      .namaCabang ??
                                                                  '',
                                                              style: TextStyle(
                                                                  fontSize: 13,
                                                                  fontWeight:
                                                                      FontWeight
                                                                          .bold)),
                                                          SizedBox(height: 17),
                                                          Text(
                                                              snapshot
                                                                  .data!
                                                                  .menuAktivitas!
                                                                  .data!
                                                                  .departemen!,
                                                              style: TextStyle(
                                                                  fontSize: 13,
                                                                  color: Color(
                                                                      0xFF828282),
                                                                  fontWeight:
                                                                      FontWeight
                                                                          .w400)),
                                                          SizedBox(height: 5),
                                                          Text(
                                                              state
                                                                      .aktivitasRespon
                                                                      .data![0]
                                                                      .namaDepartemen ??
                                                                  '',
                                                              style: TextStyle(
                                                                  fontSize: 13,
                                                                  fontWeight:
                                                                      FontWeight
                                                                          .bold)),
                                                          SizedBox(height: 17),
                                                        ],
                                                      );
                                              }),
                                              Text(
                                                  snapshot.data!.menuAktivitas!
                                                      .data!.keterangan!,
                                                  style: TextStyle(
                                                      fontSize: 13,
                                                      color: Color(0xFF828282),
                                                      fontWeight:
                                                          FontWeight.w400)),
                                              SizedBox(height: 7),
                                              Text(
                                                  state.aktivitasRespon.data![0]
                                                      .keterangan!,
                                                  style: TextStyle(
                                                      fontSize: 15,
                                                      fontWeight:
                                                          FontWeight.bold)),
                                              SizedBox(height: 17),
                                            ],
                                          ),
                                        ],
                                      ),
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ),
                        ),
                      ],
                    );
                  else if (state is DetailAktivitasProgress)
                    return Container(
                        height: MediaQuery.of(context).size.height,
                        width: MediaQuery.of(context).size.width,
                        child: Center(
                            child: CircularProgressIndicator(
                          strokeWidth: 8,
                        )));
                  else
                    return Container();
                }));
          } else {
            return CircularProgressIndicator(strokeWidth: 8);
          }
        });
  }
}
