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/maintenance/maintenance_router.dart';
import 'package:liftmanager/internal/repair/model/repair_fault_entity.dart';
import 'package:liftmanager/internal/repair/model/repair_list_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/click_item.dart';
import 'package:liftmanager/widgets/load_image.dart';

class RepairOrderPage extends StatefulWidget {
  RepairOrderPage(this.item);

  final RepairItem item;

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

class RepairOrderPageState extends State<RepairOrderPage> {
  List<String> images = [];
  List<String> signImage = [];
  List<RepairFaultEntity> repairList = [];


  TextEditingController _controller = TextEditingController();

  String faultPart = "";
  String faultReason = "";
  String faultHandle = "";
  String faultNature = "";
  String faultDuty = "";

  String erRecordImg = "";
  String recoveryDate = "";

  var mainSignImgByte;
  var mainSignImgByte2;

  @override
  void initState() {
    super.initState();
    initData();
  }

  initData() {
    for (var i = 0; i < widget.item.faultParts.length; ++i) {
      var item = widget.item.faultParts[i];
      if (faultPart.length == 0) {
        faultPart = item.faultDesc;
      } else {
        faultPart = faultPart + item.faultDesc;
      }
    }

    for (var i = 0; i < widget.item.faultReasons.length; ++i) {
      var item = widget.item.faultReasons[i];
      if (faultReason.length == 0) {
        faultReason = item.faultDesc;
      } else {
        faultReason = faultReason + "、" + item.faultDesc;
      }
    }

    for (var i = 0; i < widget.item.faultHandles.length; ++i) {
      var item = widget.item.faultHandles[i];
      if (faultHandle.length == 0) {
        faultHandle = item.faultType;
      } else {
        faultHandle = faultHandle + "、" + item.faultType;
      }
    }

    for (var i = 0; i < widget.item.faultNatures.length; ++i) {
      var item = widget.item.faultNatures[i];
      if (faultNature.length == 0) {
        faultNature = item.faultType;
      } else {
        faultNature = faultNature + "、" + item.faultType;
      }
    }

    for (var i = 0; i < widget.item.faultDuties.length; ++i) {
      var item = widget.item.faultDuties[i];
      if (faultDuty.length == 0) {
        faultDuty = item.faultType;
      } else {
        faultDuty = faultDuty + "、" + item.faultType;
      }
    }
    _controller.text = widget.item.fieldDescription;
    recoveryDate = widget.item.recoveryDate;
    for (var i = 0; i < widget.item.erRecordImgs.length; ++i) {
      ErRecordImgs item = widget.item.erRecordImgs[i];
      images.add(item.imgUrl);
    }
    if (widget.item.mainSign.length > 0) {
      signImage.add(widget.item.mainSign);
    }
    if (widget.item.secondSign.length > 0) {
      signImage.add(widget.item.secondSign);
    }
    print(widget.item.erRecordImgs.length);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        //resizeToAvoidBottomPadding: false,
        appBar: 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()),
                ),
              ],
            ),
          ),
        ));
  }

  _buildBody() {
    return Padding(
        padding: EdgeInsets.only(bottom: 30),
        child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              ClickItem(
                title: "故障部位",
                hintText: "请选择",
                maxLines: 10,
                content: "${faultPart}",
              ),
              ClickItem(
                  title: "故障原因",
                  hintText: "请选择",
                  maxLines: 10,
                  content: "${faultReason}"),
              ClickItem(
                  title: "故障处理",
                  hintText: "请选择",
                  maxLines: 10,
                  content: "${faultHandle}"),
              ClickItem(
                  title: "故障性质",
                  hintText: "请选择",
                  maxLines: 10,
                  content: "${faultNature}"),
              ClickItem(
                  title: "故障责任",
                  hintText: "请选择",
                  maxLines: 10,
                  content: "${faultDuty}"),
              SizedBox(
                height: 8,
              ),
              ClickItem(
                title: "现场情况描述",
                hintText: "",
              ),
              Container(
                color: ThemeUtils.getTabsBg(context),
                child: Padding(
                  padding: const EdgeInsets.only(
                      top: 5, left: 15.0, right: 15.0, bottom: 8.0),
                  child: TextField(
                      maxLength: 30,
                      maxLines: 3,
//                  autofocus: false,
                      controller: _controller,
                      enabled: false,
//                    keyboardType: widget.keyboardType,
                      //style: TextStyles.textDark14,
                      decoration: InputDecoration(
                          hintText: "填写现场情况描述",
                          border: InputBorder.none,
                          hintStyle: TextStyles.textGray14)),
                ),
              ),
              SizedBox(
                height: 8,
              ),
              ClickItem(title: "恢复时间", hintText: "请选择", content: recoveryDate),
              SizedBox(
                height: 8,
              ),
              ClickItem(
                title: "修理图片",
                hintText: "",
              ),
              Container(
                  color: ThemeUtils.getTabsBg(context),
                  child: GridView.builder(
                    shrinkWrap: true,
                    padding: const EdgeInsets.fromLTRB(8.0, 12, 8.0, 12.0),
                    physics: NeverScrollableScrollPhysics(),
                    gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                        crossAxisCount: 3, childAspectRatio: 1.18),
                    itemCount: images.length,
                    itemBuilder: (_, index) {
                      return Stack(
                        children: <Widget>[
                          Center(
                            child: GestureDetector(
                                onTap: () {
                                  NavigatorUtils.push(
                                      context,
                                      "${MaintenanceRouter.viewImage}?edit=0&img=" +
                                          Uri.encodeComponent(
                                              images[index]));
                                },
                                child: LoadImage(
                                  images[index],
                                  width: 80,
                                  height: 80,
                                )),
                          )
                        ],
                      );
                    },
                  )),
              SizedBox(
                height: 8,
              ),
              ClickItem(
                title: "负责人签名",
                hintText: "",
              ),
              Container(
                  color: ThemeUtils.getTabsBg(context),
                  child: GridView.builder(
                    shrinkWrap: true,
                    padding: const EdgeInsets.fromLTRB(8.0, 12, 8.0, 12.0),
                    physics: NeverScrollableScrollPhysics(),
                    gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                        crossAxisCount: 3, childAspectRatio: 1.18),
                    itemCount: signImage.length,
                    itemBuilder: (_, index) {
                      return Stack(
                        children: <Widget>[
                          Center(
                            child: LoadImage(
                              signImage[index],
                              width: 80,
                              height: 80,
                            ),
                          )
                        ],
                      );
                    },
                  )),
            ]));
  }
}
