import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:unit2/bloc/passo/land/land_property_owner_info/land_property_owner_info_bloc.dart';
import 'package:unit2/model/passo/land_property_boundaries.dart';
import 'package:unit2/model/passo/land_property_loc.dart';
import 'package:unit2/screens/passo/Land/add_land.dart';
import 'package:unit2/widgets/passo/custom_button.dart';
import 'package:unit2/widgets/passo/custom_formBuilder_fields.dart';

class LandLocationAndBoundaries extends StatefulWidget {
  Function PrevBtn;
  Function NextBtn;
  LandLocationAndBoundaries(this.PrevBtn, this.NextBtn);
  @override
  _LandLocationAndBoundaries createState() => _LandLocationAndBoundaries();
}

class _LandLocationAndBoundaries extends State<LandLocationAndBoundaries> {
  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Padding(
        padding: const EdgeInsets.all(15.0),
        child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Container(
                margin: const EdgeInsets.only(
                    left: 0, top: 20, right: 0, bottom: 10),
                child: const Text('PROPERTY LOCATION',
                    style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
                    textAlign: TextAlign.left),
              ),
              const SizedBox(height: 15),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: <Widget>[
                  Expanded(
                      // optional flex property if flex is 1 because the default flex is 1
                      flex: 1,
                      child: customTextField("No. / Street", "", "street")),
                  const SizedBox(width: 10.0),
                  Expanded(
                      // optional flex property if flex is 1 because the default flex is 1
                      flex: 1,
                      child: customTextField("Brgy./District", "", "brgy")),
                ],
              ),
              Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: <Widget>[
                    Expanded(
                      flex: 1,
                      child:
                          customTextField("Municipality", "", "municipality"),
                    ),
                    const SizedBox(width: 10.0),
                    Expanded(
                        // optional flex property if flex is 1 because the default flex is 1
                        flex: 1,
                        child: customTextField("Province/City", "", "province"))
                  ]),
              Container(
                margin: const EdgeInsets.only(
                    left: 0, top: 20, right: 0, bottom: 10),
                child: const Text('PROPERTY BOUNDARIES',
                    style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
                    textAlign: TextAlign.left),
              ),
              Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: <Widget>[
                    Expanded(
                      flex: 1,
                      child: customTextField("North", "", "north"),
                    ),
                    const SizedBox(width: 10.0),
                    Expanded(
                        // optional flex property if flex is 1 because the default flex is 1
                        flex: 1,
                        child: customTextField("East", "", "east"))
                  ]),
              Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: <Widget>[
                    Expanded(
                      flex: 1,
                      child: customTextField("South", "", "south"),
                    ),
                    const SizedBox(width: 10.0),
                    Expanded(
                        // optional flex property if flex is 1 because the default flex is 1
                        flex: 1,
                        child: customTextField("West", "", "west"))
                  ]),
              SizedBox(
                height: 50,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  CustomButton(
                      icon: const Icon(Icons.chevron_left, color: Colors.white),
                      onPressed: () {
                        widget.PrevBtn();
                      }),
                  CustomButton(
                      icon:
                          const Icon(Icons.chevron_right, color: Colors.white),
                      onPressed: () async {
                        final tempID = await SharedPreferences.getInstance();
                        print(tempID.getInt('landid'));
                        var boundaries = LandPropertyBoundaries(
                          id: tempID.getInt('landid')! - 1,
                          north: landKey.currentState?.value['north'],
                          east: landKey.currentState?.value['east'],
                          west: landKey.currentState?.value['west'],
                          south: landKey.currentState?.value['south'],
                        );
                        var location = LandPropertyLoc(
                          id: tempID.getInt('landid')! - 1,
                          street: landKey.currentState?.value['street'],
                          barangay: landKey.currentState?.value['brgy'],
                          municipality:
                              landKey.currentState?.value['municipality'],
                          province: landKey.currentState?.value['province'],
                        );

                        context.read<LandPropertyOwnerInfoBloc>()
                          ..add(
                              UpdateLandBoundaries(land_boundaries: boundaries))
                          ..add(UpdateLandLoc(land_loc: location));

                        widget.NextBtn();
                      })
                ],
              )
            ]),
      ),
    );
  }
}
