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/yearly/model/yearly_list_entity.dart';
import 'package:liftmanager/internal/yearly/yearly_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';

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

  YearlyListItem item;

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

class YearlyDetailState extends State<YearlyDetailPage> {
  YearlyListItem item;

  YearlyDetailState(this.item);

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

  _uploadBaoGao() {
    if(item.stepStatus == 0){
          showLoading(context, "确认中");
    ApiService(context: context).yearlyConfirm(item.liftId,item.isCheckGoverner,item.loadInspectionSetting,item.planDate, onSuccess: (data) {
      dismissLoading(context);
      if (data != null) {
        toasts("确认成功");
        item.stepStatus = 1;
        item.id = data;
        setState(() {});
      } else {
        toasts("失败");
      }
    }, onError: (code, msg) {
      toasts(msg);
      dismissLoading(context);
    });
    }else if(item.stepStatus == 1){
      NavigatorUtils.pushResult(
          context, "${YearlyRouter.yearlyUploadPage}?type=0&id=${item.id}",
              (result) {
            if(result != null){
              var res = result as Map<String, String>;
              item.selfcheckDate = res["time"];
              item.selfcheckReportImg = res["img"];
              item.stepStatus = 4;
              setState(() {});
            }
          });
    }else if(item.stepStatus ==3){
      NavigatorUtils.pushResult(
          context, "${YearlyRouter.yearlyUploadPage}?type=1&id=${item.id}",
              (result) {
                if(result != null) {
                  var res = result as Map<String, String>;
                  item.checkDate = res["time"];
                  item.checkResultImg = res["img"];
                  item.stepStatus = 4;
                  setState(() {});
                }
          });
    }else if(item.stepStatus ==5||item.stepStatus ==6||item.stepStatus ==7){
      NavigatorUtils.pushResult(
          context, "${YearlyRouter.yearlyUploadPage}?type=2&id=${item.id}",
              (result) {
                if(result != null) {
                  var res = result as Map<String, String>;
                  item.annualInspectionImg = res["img1"];
                  item.certificateImgUrl = res["img2"];
                  item.stepStatus = 8;
                  setState(() {});
                }
          });
    }
    
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //resizeToAvoidBottomPadding: false,
      appBar: const MyAppBar(
        centerTitle: "年检详情",
      ),
      body: SafeArea(
        child: Column(
          children: <Widget>[
            Expanded(
              flex: 1,
              child: defaultTargetPlatform == TargetPlatform.iOS
                  ? FormKeyboardActions(child: _buildBody())
                  : SingleChildScrollView(child: _buildBody()),
            ),
            Offstage(
                offstage: isShowBtn(),
                child: Container(
                   
                   
                    child: Padding(
                  padding: const EdgeInsets.all(16),
                  child: MyButton(
                    // textColor: Colors.white,
                    fontSize: 14,
                    onPressed: () {
                      _uploadBaoGao();
                    },
                    text: btnStr(),
                  ),
                )))
          ],
        ),
      ),
    );
  }

  bool isShowBtn() {
    bool isHide = false;
    switch (item.stepStatus) {
      case 0:
      case 1:
        isHide = false;
        break;
      case 2:
        isHide = true;
        break;
      case 3:
        isHide = false;
        break;
      case 4:
        isHide = true;
        break;
      case 5:
      case 6:
      case 7:
        isHide = false;
        break;
      case 8:

      case 9:
      case 10:
        isHide = true;
        break;
    }
    return isHide;
  }

  String btnStr() {
    String str = "";
    switch (item.stepStatus) {
      case 0:
        str = "确认年检计划";
        break;
      case 1:
        str = "上传自检报告";
        break;
      case 2:
        break;
      case 3:
        str = "上传检验报告";
        break;
      case 4:
      case 5:
      case 6:
      case 7:
        str = "上传年检报告";
        break;
      case 8:
      case 9:
      case 10:
        break;
    }
    return str;
  }

  _buildBody() {
    return Padding(
        padding: const EdgeInsets.symmetric(vertical: 0),
        child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              ClickItem(
                title: "区域",
                content: "${item.areaName}",
              ),
              ClickItem(
                title: "项目名称",
                content: "${item.projectName}",
              ),
              ClickItem(
                title: "设备内部编号",
                content: "${item.useCompanyCode}",
              ),
              ClickItem(
                title: "注册代码",
                content: "${item.registrationCode}",
              ),
              ClickItem(
                title: "电梯位置",
                content: "${item.devicePosition}",
              ),
              ClickItem(
                title: "区域主管",
                content: "${item.directorName}",
              ),
              ClickItem(
                title: "维保工",
                content: "${item.workerName}",
              ),
              ClickItem(
                title: "计划时间",
                content: "${item.planDate}",
              ),
              ClickItem(
                  title: "是否检查限速器",
                  content: "${item.isCheckGoverner == 1 ? "是" : "否"}"),
              ClickItem(
                  title: "荷载年检设置",
                  content: "${item.loadInspectionSetting == 1 ? "是" : "否"}"),
              ClickItem(
                title: "政府质检人员",
                content: "${item.inspector}",
              ),
              ClickItem(
                title: "公司年检人员",
                content: "${item.operator}",
              ),
              Column(children: timeWidget()),
              Column(
                children: imgWidget(),
              )
            ]));
  }

  List<Widget> timeWidget() {
    List<Widget> list = [];
    if (item.selfcheckDate.length == 0) return list;
    list.add(ClickItem(
      title: "自检时间",
      content: "${item.selfcheckDate}",
    ));
    if (item.checkDate.length == 0) return list;
    list.add(ClickItem(
      title: "检验时间",
      content: "${item.checkDate}",
    ));
    if (item.finishTime.length == 0) return list;
    list.add(ClickItem(
      title: "完成时间",
      content: "${item.finishTime}",
    ));
    return list;
  }

  List<Widget> imgWidget() {
    List<Widget> list = [];

    if (item.selfcheckReportImg.length == 0) return list;
    print(item.selfcheckReportImg);
    List<String> selfArr = item.selfcheckReportImg.split(",");
    print(selfArr.length);
    for (var i = 0; i < selfArr.length; ++i) {
      String img = selfArr[i];
      list.add(Container(
        color: ThemeUtils.getTabsBg(context),
        padding: EdgeInsets.all(10),
        child: Row(
          children: <Widget>[
            Gaps.hGap15,
            Container(
              width: 80,
              child: Text(i==0?"自检图片":""),
            ),
            Gaps.hGap15,

            Image.network(
              img.trim(),
              width: 150,
              height: 150,
              fit: BoxFit.fill,
            )
          ],
        ),
      ));
    }

    if (item.checkResultImg.length == 0) return list;
    list.add(Container(
        color: ThemeUtils.getTabsBg(context),
        padding: EdgeInsets.all(10),
        child: Row(
          children: <Widget>[
            Gaps.hGap15,
            Container(
              width: 80,
              child: Text("检验图片"),
            ),
            Gaps.hGap15,
            Image.network(
              "${item.checkResultImg}",
              width: 150,
              height: 150,
              fit: BoxFit.fill,
            )
          ],
        )));

    if (item.annualInspectionImg.length == 0) return list;
    list.add(Container(
        color: ThemeUtils.getTabsBg(context),
        padding: EdgeInsets.all(10),
        child: Row(
          children: <Widget>[
            Gaps.hGap15,
            Container(
              width: 80,
              child: Text("年检图片"),
            ),
            Gaps.hGap15,
            Image.network(
              "${item.annualInspectionImg}",
              width: 150,
              height: 150,
              fit: BoxFit.fill,
            )
          ],
        )));

    if (item.certificateImgUrl.length == 0) return list;
    list.add(Container(
        color: ThemeUtils.getTabsBg(context),
        padding: EdgeInsets.all(10),
        child: Row(
          children: <Widget>[
            Gaps.hGap15,
            Container(
              width: 80,
              child: Text("合格证图片"),
            ),
            Gaps.hGap15,
            Image.network(
              "${item.certificateImgUrl}",
              width: 150,
              height: 150,
              fit: BoxFit.fill,
            )
          ],
        )));

    return list;
  }
}
