import 'dart:convert' as convert;

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:liftmanager/common/common.dart';
import 'package:liftmanager/common/user_db.dart';
import 'package:liftmanager/internal/lift/model/lift_item.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/toast.dart';
import 'package:liftmanager/widgets/app_bar.dart';
import 'package:liftmanager/widgets/click_item.dart';

import '../lift_router.dart';

class LiftDetailPage extends StatefulWidget {
  LiftDetailPage(this.projectId, this.id);

  final String projectId;
  final String id;

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

class LiftDetailPageState extends State<LiftDetailPage> {
  LiftItem item = LiftItem();

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

  bool noRole = true;

  getHasRole() async {
    var role = await User().getCompanyRole();
    if (role == Constant.RoleAdmin ||
        role == Constant.RoleRegion ||
        role == Constant.RoleWork) {
      noRole = false;
      setState(() {});
    }
  }

  getLiftDetail() {
    ApiService(context: context).liftDetail(widget.projectId, widget.id,
        onSuccess: (res) {
      item = res;
      setState(() {});
    }, onError: (code, msg) {
      toasts(msg);
    });
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        onWillPop: () {
          NavigatorUtils.goBackWithParams(context, true);
          return Future.value(false);
        },
        child: Scaffold(
          appBar: MyAppBar(
            centerTitle: "电梯详情",
            actions: <Widget>[
              Offstage(
                  offstage: noRole || item.liftLocked,
                  child: FlatButton(
                    child: Text("编辑"),
                    textColor: Colours.text,
                    highlightColor: Colors.transparent,
                    onPressed: () {
                      String jsonString = convert.jsonEncode(item);
                      NavigatorUtils.pushResult(context,
                          "${LiftRouter.liftCreatePage}?id=${widget.projectId}&item=${Uri.encodeComponent(jsonString)}",
                          (res) {
                        if (res != null) {
                          getLiftDetail();
                        }
                      });
                    },
                  ))
            ],
          ),
          body: ListView(
            children: allWidget(),
          ),
        ));
  }

  List<Widget> allWidget() {
    if (item.category == 0) return [];
    List<Widget> liftData = _commontHeader();
    if (item.category == 1) {
      liftData.addAll(_yeyintiList());
    } else if (item.category == 2) {
      liftData.addAll(_yeyatiList());
    } else if (item.category == 3) {
      liftData.addAll(_zawutiList());
    } else if (item.category == 4) {
      liftData.addAll(_ftList());
    } else if (item.category == 5) {
      liftData.addAll(_ftList());
    }
    liftData.addAll(_commontFooter());
    return liftData;
  }

  List<Widget> _commontHeader() {
    return [
      ClickItem(
        title: "设备注册代码",
        content: "${item.registrationCode}",
      ),
      ClickItem(
        title: "电梯类别",
        content: item.getCategoryName(),
      ),
      ClickItem(
        title: "地图位置",
        content: "${item.coordinate.length > 0 ? "已定位" : "未定位"}",
        onTap: () {
          if (item.coordinate.length == 0) return;
          print(item.coordinate);
          NavigatorUtils.push(
              context,
              "${LiftRouter.liftMapPage}?type=look&coordinate=" +
                  item.coordinate);
        },
      ),
      ClickItem(
        title: "设备出厂编号",
        content: "${item.factoryCode}",
      ),
      ClickItem(
        title: "设备型号",
        content: "${item.liftModel}",
      ),
      ClickItem(
        title: "设备安装地址",
        content: "${item.devicePosition}",
      ),
      ClickItem(
        title: "设备内部编号",
        content: "${item.useCompanyCode}",
      ),
      ClickItem(
        title: "设备制造商",
        content: "${item.factory}",
      ),
      ClickItem(
        title: "进口设备代理商",
        content: "${item.agency}",
      ),
      ClickItem(
        title: "自定义编号",
        content: "${item.customNumber}",
      ),
      ClickItem(
        title: "设备出厂日期",
        content: "${item.manufactureDate}",
      ),
      ClickItem(
        title: "设备改造单位",
        content: "${item.reformCompany}",
      ),
      ClickItem(
        title: "设备改造日期",
        content: "${item.reformDate}",
      ),
      ClickItem(
        title: "设备安装单位",
        content: "${item.installCompany}",
      ),
      ClickItem(
        title: "设备安装日期",
        content: "${item.installDate}",
      ),
      ClickItem(
        title: "电梯维保负责人",
        content: "${item.workerName}",
      ),
      ClickItem(
        title: "设备用途",
        content: "${item.getDeviceUsageName()}",
      ),
      ClickItem(
        title: "品牌",
        content: "${item.liftBrandName}",
      ),
      ClickItem(
        title: "年检日期",
        content: "${item.annualInspectionDate}",
      )
    ];
  }

  _yeyintiList() {
    return [
      ClickItem(
        title: "显示楼层",
        content: "${item.innerFloor}",
      ),
      ClickItem(
        title: "曳引轮直径（mm)",
        content: "${item.pulleyDiameter}",
      ),
      ClickItem(
        title: "曳引绳根数",
        content: "${item.ropeNum}",
      ),
      ClickItem(
        title: "门锁型号",
        content: "${item.lockModel}",
      ),
      ClickItem(
        title: "层站门",
        content: "${item.layerStationDoor}",
      ),
      ClickItem(
        title: "安全钳类型",
        content:
            "${item.clampType == "1" ? '瞬时式安全钳' : item.clampType == "2" ? '渐进式安全钳' : ''}",
      ),
      ClickItem(
        title: "电梯载重",
        content: "${item.ratedLoad}",
      ),
      ClickItem(
        title: "提升高度",
        content: "${item.promoteHeight}",
      ),
      ClickItem(
        title: "电动机功率",
        content: "${item.motorPower}",
      ),
      ClickItem(
        title: "钢带",
        content: "${item.steelBelt}",
      ),
      ClickItem(
        title: "控制方式",
        content: "${item.controlType}",
      ),
      ClickItem(
        title: "额定速度",
        content: "${item.ratedSpeed}",
      ),
    ];
  }

  _yeyatiList() {
    return [
      ClickItem(
        title: "显示楼层",
        content: "${item.innerFloor}",
      ),
      ClickItem(
        title: "门锁型号",
        content: "${item.lockModel}",
      ),
      ClickItem(
        title: "电梯载重",
        content: "${item.ratedLoad}",
      ),
      ClickItem(
        title: "提升高度",
        content: "${item.promoteHeight}",
      ),
      ClickItem(
        title: "电动机功率",
        content: "${item.motorPower}",
      ),
      ClickItem(
        title: "额定速度",
        content: "${item.ratedSpeed}",
      ),
      ClickItem(
        title: "层站门",
        content: "${item.layerStationDoor}",
      ),
      ClickItem(
        title: "安全钳类型",
        content:
            "${item.clampType == 1 ? '瞬时式安全钳' : item.clampType == 2 ? '渐进式安全钳' : ''}",
      ),
      ClickItem(
        title: "控制方式",
        content: "${item.controlType}",
      ),
      ClickItem(
        title: "油缸数量",
        content: "${item.cylinderNum}",
      ),
      ClickItem(
        title: "油缸型式",
        content: "${item.cylinderType}",
      ),
      ClickItem(
        title: "顶升型式",
        content: "${item.topType}",
      ),
      ClickItem(
        title: "液压系统满负荷值",
        content: "${item.mpa}",
      ),
    ];
  }

  _zawutiList() {
    return [
      ClickItem(
        title: "显示楼层",
        content: "${item.innerFloor}",
      ),
      ClickItem(
        title: "门锁型号",
        content: "${item.lockModel}",
      ),
      ClickItem(
        title: "电梯载重",
        content: "${item.ratedLoad}",
      ),
      ClickItem(
        title: "提升高度",
        content: "${item.promoteHeight}",
      ),
      ClickItem(
        title: "电动机功率",
        content: "${item.motorPower}",
      ),
      ClickItem(
        title: "额定速度",
        content: "${item.ratedSpeed}",
      ),
      ClickItem(
        title: "层站门",
        content: "${item.layerStationDoor}",
      ),
      ClickItem(
        title: "安全钳类型",
        content:
            "${item.clampType == 1 ? '瞬时式安全钳' : item.clampType == 2 ? '渐进式安全钳' : ''}",
      ),
      ClickItem(
        title: "控制方式",
        content: "${item.controlType}",
      ),
      ClickItem(
        title: "钢带",
        content: "${item.steelBelt}",
      )
    ];
  }

  _ftList() {
    return [
      ClickItem(
        title: "电梯载重",
        content: "${item.ratedLoad}",
      ),
      ClickItem(
        title: "提升高度",
        content: "${item.promoteHeight}",
      ),
      ClickItem(
        title: "梯级宽度",
        content: "${item.stepWidth}",
      ),
      ClickItem(
        title: "人行道长度",
        content: "${item.sidewalkLength}",
      ),
      ClickItem(
        title: "倾斜角度",
        content: "${item.tiltAngle}",
      ),
      ClickItem(
        title: "电动机功率",
        content: "${item.motorPower}",
      ),
      ClickItem(
        title: "额定速度",
        content: "${item.ratedSpeed}",
      ),
    ];
  }

  _commontFooter() {
    return [
      ClickItem(
        title: "备注",
        content: "${item.remarks}",
      ),
    ];
  }
}
