import 'dart:convert' as convert;

import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:keyboard_actions/keyboard_actions.dart';
import 'package:liftmanager/internal/repair/model/repair_list_entity.dart';
import 'package:liftmanager/internal/repair/repair_router.dart';
import 'package:liftmanager/net/api_service.dart';
import 'package:liftmanager/res/resources.dart';
import 'package:liftmanager/routers/fluro_navigator.dart';
import 'package:liftmanager/utils/theme_utils.dart';
import 'package:liftmanager/utils/toast.dart';
import 'package:liftmanager/widgets/app_bar.dart';
import 'package:liftmanager/widgets/click_item.dart';
import 'package:liftmanager/widgets/my_button.dart';
import 'package:liftmanager/widgets/radio_item.dart';

class RepairSafePage extends StatefulWidget {
  RepairSafePage(this.repairItem);

  RepairItem repairItem;

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

class RepairSafePageState extends State<RepairSafePage> {
  List<String> groupValue = ["", "", "", "", "", ""];
  String _stopTime = "";

  ///全选
  _allSelected() {
    groupValue = ["1", "1", "1", "1", "1", "1"];
    setState(() {});
  }

  /// 选择时间
  Future<void> _selectTime() async {
    DatePicker.showDateTimePicker(context,
        showTitleActions: true, onChanged: (date) {}, onConfirm: (date) {
      _stopTime = "${date.toString().split(".")[0]}";
      setState(() {});
    }, currentTime: DateTime.now(), locale: LocaleType.zh);
  }

  ///确认
  _stopAndSafe() {
    if(_stopTime.length == 0){
      toasts("请选择停梯时间");
      return;
    }
    showLoading(context, "正在执行...");
    ApiService(context: context)
        .repairStop(widget.repairItem.id, _stopTime, groupValue.toString(),
            onSuccess: (data) {
      dismissLoading(context);
      if (data != null && data) {
        String jsonString = convert.jsonEncode(widget.repairItem);
        NavigatorUtils.pushResult(context,"${RepairRouter.repairSubmitPage}?item=${Uri.encodeComponent(jsonString)}",(res){
          if(res != null && res){
            NavigatorUtils.goBackWithParams(context,res);
          }
        });
      }
    }, onError: (code, msg) {
      dismissLoading(context);
      toasts(msg);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        //resizeToAvoidBottomPadding: false,
        appBar: const MyAppBar(
          centerTitle: "安全确认",
        ),
        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()),
                ),
                Container(
//                    color: Colours.bg_color,
                    child: Padding(
                  padding: const EdgeInsets.all(16),
                  child: MyButton(
                    fontSize: 14,
                    onPressed: () {
                      _stopAndSafe();
                    },
                    text: "确定",
                  ),
                ))
              ],
            ),
          ),
        ));
  }

  _buildBody() {
    return Padding(
      padding: EdgeInsets.only(bottom: 30),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          ClickItem(
            title: "停梯时间",
            hintText: "请选择",
            content: "${_stopTime}",
            onTap: () {
              _selectTime();
            },
          ),
          Container(
              color: ThemeUtils.getTabsBg(context),
              padding: EdgeInsets.only(left: 15, right: 15),
              height: 32.5,
              child: Row(
                children: <Widget>[
                  Expanded(
                      flex: 1,
                      child: Text(
                        "安全防护确认",
                        style: TextStyle(
                            fontSize: 12, color: Colours.dark_text_gray),
                      )),
                  GestureDetector(
                      onTap: _allSelected,
                      child: Text(
                        "全选",
                        style: TextStyle(fontSize: 12, color: Colours.app_main),
                      )),
                ],
              )),
          RadioItem(
              title: "用电安全",
              value: "1",
              groupValue: groupValue[0],
              onTap: () {
                groupValue[0] = groupValue[0] == "1" ? "0" : "1";
                setState(() {});
              }),
          RadioItem(
              title: "护栏",
              value: "1",
              groupValue: groupValue[1],
              onTap: () {
                groupValue[1] = groupValue[1] == "1" ? "0" : "1";
                setState(() {});
              }),
          RadioItem(
              title: "手套",
              value: "1",
              groupValue: groupValue[2],
              onTap: () {
                groupValue[2] = groupValue[2] == "1" ? "0" : "1";
                setState(() {});
              }),
          RadioItem(
              title: "安全帽",
              value: "1",
              groupValue: groupValue[3],
              onTap: () {
                groupValue[3] = groupValue[3] == "1" ? "0" : "1";
                setState(() {});
              }),
          RadioItem(
              title: "电器防护",
              value: "1",
              groupValue: groupValue[4],
              onTap: () {
                groupValue[4] = groupValue[4] == "1" ? "0" : "1";
                setState(() {});
              }),
          RadioItem(
              title: "照明",
              value: "1",
              groupValue: groupValue[5],
              onTap: () {
                groupValue[5] = groupValue[5] == "1" ? "0" : "1";
                setState(() {});
              }),
          Container(
            color: ThemeUtils.getTabsBg(context),
              padding: EdgeInsets.only(left: 15, right: 15),
              height: 32.5,
              child: Row(
                children: <Widget>[
                  Expanded(
                      flex: 1,
                      child: Text(
                        "请对已安全防护选项进行勾选，确保已做好安",
                        style: TextStyle(fontSize: 12, color: Colours.app_main),
                      ))
                ],
              )),
        ],
      ),
    );
  }
}
