import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:keyboard_actions/keyboard_actions.dart';
import 'package:liftmanager/internal/repair/model/repair_fault_entity.dart';
import 'package:liftmanager/res/resources.dart';
import 'package:liftmanager/routers/fluro_navigator.dart';
import 'package:liftmanager/utils/theme_utils.dart';
import 'package:liftmanager/widgets/app_bar.dart';
import 'package:liftmanager/widgets/radio_item.dart';

class RepairFaultPage extends StatefulWidget {
  RepairFaultPage(this.title, this.item);

  final String title;
  final RepairFaultEntity item;

  @override
  State<StatefulWidget> createState() {
    return RepairFaultPageState();
  }
}

class RepairFaultPageState extends State<RepairFaultPage> {
  final Map<String,String> selected_map = {};

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: MyAppBar(
          centerTitle: "${widget.title}",
          actions: <Widget>[
            FlatButton(
              child: Text("确定", key: const Key('actionName')),
              textColor: Colours.dark_text,
              highlightColor: Colors.transparent,
              onPressed: (){
                NavigatorUtils.goBackWithParams(context, selected_map);
              },
            )
          ],
        ),
        body: SafeArea(
          child: Container(
            color: ThemeUtils.getBackgroundColor(context),
            child: Column(
              children: <Widget>[
                Expanded(
                  flex: 1,
                  child: defaultTargetPlatform == TargetPlatform.iOS
                      ? FormKeyboardActions(child: _buildBody())
                      : SingleChildScrollView(child: _buildBody()),
                )
              ],
            ),
          ),
        ));
  }

  _buildBody() {
    return Padding(
      padding: EdgeInsets.only(bottom: 30),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[

          Column(
              children: widget.item.faultTypes.map((faultType) {
            return Column(
              children: <Widget>[
                Offstage(
                  offstage: widget.item.faultCategory == "故障处理" ||widget.item.faultCategory == "故障性质"||widget.item.faultCategory == "故障责任",
                  child: Container(
                    padding: EdgeInsets.all(15),
                    child: Text("${faultType.faultType}"),
                  ),
                ),
                Column(
                  children: faultType.liftFaults.map((liftFaults){
                    return RadioItem(
                        title: "${liftFaults.faultDesc.length==0?liftFaults.faultType:liftFaults.faultDesc}",
                        value: "${liftFaults.faultDesc.length==0?liftFaults.faultType:liftFaults.faultDesc}",
                        groupValue: selected_map[liftFaults.id],
                        onTap: () {
                          if(selected_map[liftFaults.id] !=null){

                            selected_map.remove(liftFaults.id);

                          }else{
                            selected_map[liftFaults.id] = "${liftFaults.faultDesc.length==0?liftFaults.faultType:liftFaults.faultDesc}";
                          }
                          setState(() {});
                        });
                  }).toList(),
                )
              ],
            );
          }).toList()),
        ],
      ),
    );
  }
}
