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/lift/lift_router.dart';
import 'package:liftmanager/internal/lift/model/lift_brand_entity.dart';
import 'package:liftmanager/internal/lift/model/lift_item.dart';
import 'package:liftmanager/internal/project/project_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/text_field_item.dart';

class LiftCreatePage extends StatefulWidget {
  LiftCreatePage(this.projectId, {this.item});

  final String projectId;
  LiftItem item;

  @override
  State<StatefulWidget> createState() {
    if (item == null) {
      item = LiftItem();
    }
    return LiftCreatePageState(this.item);
  }
}

class LiftCreatePageState extends State<LiftCreatePage> {
  LiftCreatePageState(this.item);
  List<Widget> liftData = [];
  var liftTypeArr = ["曳引梯", "液压梯", "杂物梯", "自动扶梯", "自动人行道"];
  var clampTypeArr = ["瞬时式安全钳", "渐进式安全钳"];
  var liftUsageArr = ["货梯", "客梯", "医梯", "观光梯", "杂物梯", "别墅梯", "扶梯", "人行道"];

  LiftItem item;

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

  liftNotif() {
    liftData.clear();
    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(_ftrxd());
    }
    liftData.addAll(_commontFooter());
    setState(() {});
  }

  _commontHeader() {
    return [
      TextFieldItem(
        title: "设备注册代码",
        isMust: true,
        enabled: item.id.length == 0,
        content: "${item.registrationCode}",
        controller: TextEditingController(),
        hintText: "请填写设备注册代码",
        focusNode: FocusNode(),
        onChanged: (res) {
          print(res);
          item.registrationCode = res;
          setState(() {});
        },
      ),
      ClickItem(
          title: "电梯类别",
          isMust: true,
          content: item.getCategoryName(),
          hintText: "请选择电梯类别",
          onTap: () {
            _showBottomSheet(liftTypeArr, (index) {
              item.category = index + 1;
              if (item.category > 3) {
                item.liftType = 2;
              } else {
                item.liftType = 1;
              }
              liftNotif();
            });
          }),
      ClickItem(
          title: "地图位置",
          content: "${item.coordinate.length > 0 ? "已定位" : "未定位"}",
          hintText: "请选择地图位置",
          onTap: () {
            NavigatorUtils.pushResult(
                context,
                "${LiftRouter.liftMapPage}?type=check&coordinate=" +
                    item.coordinate, (result) {
              print(result);
              if (result != null && "${result}".contains(",")) {
                setState(() {
                  item.coordinate = result;
                  liftNotif();
                });
              }
            });
          }),
      TextFieldItem(
        title: "设备出厂编号",
        content: "${item.factoryCode}",
        controller: TextEditingController(),
        hintText: "请填写设备出厂编号",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.factoryCode = res;
        },
      ),
      TextFieldItem(
        title: "设备型号",
        content: "${item.liftModel}",
        controller: TextEditingController(),
        hintText: "请填写设备型号",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.liftModel = res;
        },
      ),
      TextFieldItem(
        title: "设备安装地址",
        isMust: true,
        content: "${item.devicePosition}",
        controller: TextEditingController(),
        hintText: "请填写设备安装地址",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.devicePosition = res;
        },
      ),
      TextFieldItem(
        title: "设备内部编号",
        isMust: true,
        content: "${item.useCompanyCode}",
        controller: TextEditingController(),
        hintText: "请填写设备注册代码",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.useCompanyCode = res;
        },
      ),
      TextFieldItem(
        title: "设备制造商",
        content: "${item.factory}",
        controller: TextEditingController(),
        hintText: "请填写设备制造商",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.factory = res;
        },
      ),
      TextFieldItem(
        title: "进口设备代理商",
        content: "${item.agency}",
        controller: TextEditingController(),
        hintText: "请填写进口设备代理商",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.agency = res;
        },
      ),
      TextFieldItem(
        title: "自定义编号",
        content: "${item.customNumber}",
        controller: TextEditingController(),
        hintText: "请填写自定义编号",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.customNumber = res;
        },
      ),
      ClickItem(
          title: "设备出厂日期",
          content: "${item.manufactureDate}",
          hintText: "请选择设备出厂日期",
          onTap: () {
            _selectTime((String time) {
              setState(() {
                item.manufactureDate = time.split(" ")[0];
                liftNotif();
              });
            });
          }),
      TextFieldItem(
        title: "设备改造单位",
        content: "${item.reformCompany}",
        controller: TextEditingController(),
        hintText: "请填写设备改造单位",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.reformCompany = res;
        },
      ),
      ClickItem(
          title: "设备改造日期",
          content: "${item.reformDate}",
          hintText: "请选择设备改造日期",
          onTap: () {
            _selectTime((String time) {
              setState(() {
                item.reformDate = time.split(" ")[0];
                liftNotif();
              });
            });
          }),
      TextFieldItem(
        title: "设备安装单位",
        content: "${item.installCompany}",
        controller: TextEditingController(),
        hintText: "请填写设备安装单位",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.installCompany = res;
        },
      ),
      ClickItem(
          title: "设备安装日期",
          isMust: true,
          content: "${item.installDate}",
          hintText: "请选择安装日期",
          onTap: () {
            _selectTime((String time) {
              setState(() {
                item.installDate = time.split(" ")[0];
                liftNotif();
              });
            });
          }),
      ClickItem(
          title: "电梯维保负责人",
          isMust: true,
          content: "${item.workerName}",
          hintText: "请选择电梯维保负责人",
          onTap: () {
            NavigatorUtils.pushResult(context,
                "${ProjectRouter.projectUsersPage}?id=${widget.projectId}&type=1",
                (res) {
              Map map = res;
              print(item.workerName);
              item.workerName = map["name"];
              item.workerId = map["id"];
              liftNotif();
            });
          }),
      ClickItem(
          title: "设备用途",
          isMust: true,
          content: item.getDeviceUsageName(),
          hintText: "请选择设备用途",
          onTap: () {
            _showBottomSheet(liftUsageArr, (index) {
              int key = 0;
//              11:货梯;12:客梯;13:医梯;14:观光梯;15:杂物梯;16:别墅梯;21:扶梯;22:人行道
              switch (index) {
                case 0:
                  key = 11;
                  break;
                case 1:
                  key = 12;
                  break;
                case 2:
                  key = 13;
                  break;
                case 3:
                  key = 14;
                  break;
                case 4:
                  key = 15;
                  break;
                case 5:
                  key = 16;
                  break;
                case 6:
                  key = 21;
                  break;
                case 7:
                  key = 22;
                  break;
              }
              item.deviceUsage = key;
              print("deviceUsage");
              print(item.deviceUsage);
//              item.deviceUsageName = liftUsageArr[index];
              liftNotif();
            });
          }),
      ClickItem(
          title: "电梯品牌",
          isMust: true,
          content: "${item.liftBrandName}",
          hintText: "请选择电梯品牌",
          onTap: () {
            NavigatorUtils.pushResult(context, LiftRouter.liftBrandPage,
                (result) {
              setState(() {
                LiftBrandEntity model = result;
                item.liftBrand = model.code;
                item.liftBrandName = model.name;
                liftNotif();
              });
            });
          }),
      ClickItem(
          title: "年检日期",
          isMust: true,
          content: "${item.annualInspectionDate}",
          hintText: "请选择年检日期",
          onTap: () {
            _selectTime((String time) {
              setState(() {
                item.annualInspectionDate = time.split(" ")[0];
                liftNotif();
              });
            });
          }),
    ];
  }

  _yeyintiList() {
    return [
      TextFieldItem(
        title: "显示楼层",
        content: "${item.innerFloor}",
        keyboardType: TextInputType.number,
        controller: TextEditingController(),
        hintText: "请填写显示楼层",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.innerFloor = res;
        },
      ),
      TextFieldItem(
        title: "曳引轮直径（mm)",
        content: "${item.pulleyDiameter}",
        controller: TextEditingController(),
        hintText: "请填写曳引轮直径",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.pulleyDiameter = res;
        },
      ),
      TextFieldItem(
        title: "曳引绳根数",
        content: "${item.ropeNum}",
        keyboardType: TextInputType.number,
        controller: TextEditingController(),
        hintText: "请填写曳引绳根数",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.ropeNum = res;
        },
      ),
      TextFieldItem(
        title: "门锁型号",
        content: "${item.lockModel}",
        controller: TextEditingController(),
        hintText: "请填写门锁型号",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.lockModel = res;
        },
      ),
      TextFieldItem(
        title: "层站门",
        isMust: true,
        content: "${item.layerStationDoor}",
        controller: TextEditingController(),
        hintText: "层/站/门",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.layerStationDoor = res;
        },
      ),
      ClickItem(
          title: "安全钳类型",
          isMust: true,
          content:
              "${item.clampType == 1 ? '瞬时式安全钳' : item.clampType == 2 ? '渐进式安全钳' : ''}",
          hintText: "请选择安全钳类型",
          onTap: () {
            _showBottomSheet(clampTypeArr, (index) {
              item.clampType = index + 1;
              liftNotif();
            });
          }),
      TextFieldItem(
        title: "电梯载重",
        isMust: true,
        keyboardType: TextInputType.numberWithOptions(decimal: true),
        content: "${item.ratedLoad}",
        controller: TextEditingController(),
        hintText: "电梯载重(kg)",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.ratedLoad = res;
        },
      ),
      TextFieldItem(
        title: "提升高度",
        isMust: true,
        keyboardType: TextInputType.numberWithOptions(decimal: true),
        content: "${item.promoteHeight}",
        controller: TextEditingController(),
        hintText: "请输入提升高度(m)",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.promoteHeight = res;
        },
      ),
      TextFieldItem(
        title: "电动机功率",
        content: "${item.motorPower}",
        keyboardType: TextInputType.numberWithOptions(decimal: true),
        controller: TextEditingController(),
        hintText: "请输入电动机功率(KW)",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.motorPower = res;
        },
      ),
      TextFieldItem(
        title: "钢带",
        content: "${item.steelBelt}",
        controller: TextEditingController(),
        hintText: "请输入",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.steelBelt = res;
        },
      ),
      TextFieldItem(
        title: "控制方式",
        content: "${item.controlType}",
        controller: TextEditingController(),
        hintText: "请输入控制方式",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.controlType = res;
        },
      ),
      TextFieldItem(
        title: "额定速度",
        isMust: true,
        keyboardType: TextInputType.numberWithOptions(decimal: true),
        content: "${item.ratedSpeed}",
        controller: TextEditingController(),
        hintText: "请输入额定速度",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.ratedSpeed = res;
        },
      ),
    ];
  }

  _yeyatiList() {
    return [
      TextFieldItem(
        title: "显示楼层",
        content: "${item.innerFloor}",
        keyboardType: TextInputType.number,
        controller: TextEditingController(),
        hintText: "请填写显示楼层",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.innerFloor = res;
        },
      ),
      TextFieldItem(
        title: "门锁型号",
        content: "${item.lockModel}",
        controller: TextEditingController(),
        hintText: "请输入",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.lockModel = res;
        },
      ),
      TextFieldItem(
        title: "电梯载重",
        isMust: true,
        keyboardType: TextInputType.numberWithOptions(decimal: true),
        content: "${item.ratedLoad}",
        controller: TextEditingController(),
        hintText: "请输入电梯载重(kg)",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.ratedLoad = res;
        },
      ),
      TextFieldItem(
        title: "提升高度",
        isMust: true,
        keyboardType: TextInputType.numberWithOptions(decimal: true),
        content: "${item.promoteHeight}",
        controller: TextEditingController(),
        hintText: "请输入提升高度(m)",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.promoteHeight = res;
        },
      ),
      TextFieldItem(
        title: "电动机功率",
        isMust: true,
        keyboardType: TextInputType.numberWithOptions(decimal: true),
        content: "${item.motorPower}",
        controller: TextEditingController(),
        hintText: "请输入电动机功率(KW)",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.motorPower = res;
        },
      ),
      TextFieldItem(
        title: "额定速度",
        isMust: true,
        keyboardType: TextInputType.numberWithOptions(decimal: true),
        content: "${item.ratedSpeed}",
        controller: TextEditingController(),
        hintText: "请输入额定速度",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.ratedSpeed = res;
        },
      ),
      TextFieldItem(
        title: "层站门",
        isMust: true,
        content: "${item.layerStationDoor}",
        controller: TextEditingController(),
        hintText: "层/站/门",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.layerStationDoor = res;
        },
      ),
      ClickItem(
          title: "安全钳类型",
          isMust: true,
          content:
              "${item.clampType == 1 ? '瞬时式安全钳' : item.clampType == 2 ? '渐进式安全钳' : ''}",
          hintText: "请选择安全钳类型",
          onTap: () {
            _showBottomSheet(clampTypeArr, (index) {
              item.clampType = index + 1;
              liftNotif();
            });
          }),
      TextFieldItem(
        title: "控制方式",
        isMust: true,
        content: "${item.controlType}",
        controller: TextEditingController(),
        hintText: "请输入控制方式",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.controlType = res;
        },
      ),
      TextFieldItem(
        title: "油缸数量",
        isMust: true,
        keyboardType: TextInputType.number,
        content: "${item.cylinderNum}",
        controller: TextEditingController(),
        hintText: "请输入油缸数量",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.cylinderNum = res;
        },
      ),
      TextFieldItem(
        title: "油缸型式",
        isMust: true,
        content: "${item.cylinderType}",
        controller: TextEditingController(),
        hintText: "请输入油缸型式",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.cylinderType = res;
        },
      ),
      TextFieldItem(
        title: "顶升型式",
        isMust: true,
        content: "${item.topType}",
        controller: TextEditingController(),
        hintText: "请输入顶升型式",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.topType = res;
        },
      ),
      TextFieldItem(
        title: "液压系统满负荷值",
        content: "${item.mpa}",
        controller: TextEditingController(),
        hintText: "请输入液压系统满负荷值(MPa)",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.mpa = res;
        },
      ),
    ];
  }

  _zawutiList() {
    return [
      TextFieldItem(
        title: "显示楼层",
        keyboardType: TextInputType.numberWithOptions(decimal: true),
        content: "${item.innerFloor}",
        controller: TextEditingController(),
        hintText: "请填写显示楼层",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.innerFloor = res;
        },
      ),
      TextFieldItem(
        title: "门锁型号",
        content: "${item.lockModel}",
        controller: TextEditingController(),
        hintText: "请输入",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.lockModel = res;
        },
      ),
      TextFieldItem(
        title: "电梯载重",
        isMust: true,
        keyboardType: TextInputType.numberWithOptions(decimal: true),
        content: "${item.ratedLoad}",
        controller: TextEditingController(),
        hintText: "请输入电梯载重(kg)",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.ratedLoad = res;
        },
      ),
      TextFieldItem(
        title: "提升高度",
        isMust: true,
        keyboardType: TextInputType.numberWithOptions(decimal: true),
        content: "${item.promoteHeight}",
        controller: TextEditingController(),
        hintText: "请输入提升高度(m)",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.promoteHeight = res;
        },
      ),
      TextFieldItem(
        title: "电动机功率",
        isMust: true,
        keyboardType: TextInputType.numberWithOptions(decimal: true),
        content: "${item.motorPower}",
        controller: TextEditingController(),
        hintText: "请输入电动机功率(KW)",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.motorPower = res;
        },
      ),
      TextFieldItem(
        title: "额定速度",
        isMust: true,
        keyboardType: TextInputType.numberWithOptions(decimal: true),
        content: "${item.ratedSpeed}",
        controller: TextEditingController(),
        hintText: "请输入额定速度",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.ratedSpeed = res;
        },
      ),
      TextFieldItem(
        title: "层站门",
        isMust: true,
        content: "${item.layerStationDoor}",
        controller: TextEditingController(),
        hintText: "层/站/门",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.layerStationDoor = res;
        },
      ),
      ClickItem(
          title: "安全钳类型",
          isMust: true,
          content:
              "${item.clampType == 1 ? '瞬时式安全钳' : item.clampType == 2 ? '渐进式安全钳' : ''}",
          hintText: "请选择安全钳类型",
          onTap: () {
            _showBottomSheet(clampTypeArr, (index) {
              item.clampType = index + 1;
              liftNotif();
            });
          }),
      TextFieldItem(
        title: "控制方式",
        content: "${item.controlType}",
        controller: TextEditingController(),
        hintText: "请输入控制方式",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.controlType = res;
        },
      ),
      TextFieldItem(
        title: "钢带",
        content: "${item.steelBelt}",
        controller: TextEditingController(),
        hintText: "请输入",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.steelBelt = res;
        },
      )
    ];
  }

  _ftList() {
    return [
      TextFieldItem(
        title: "电梯载重",
        isMust: true,
        keyboardType: TextInputType.numberWithOptions(decimal: true),
        content: "${item.ratedLoad}",
        controller: TextEditingController(),
        hintText: "电梯载重(kg)",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.ratedLoad = res;
        },
      ),
      TextFieldItem(
        title: "提升高度",
        isMust: true,
        keyboardType: TextInputType.numberWithOptions(decimal: true),
        content: "${item.promoteHeight}",
        controller: TextEditingController(),
        hintText: "请输入提升高度(m)",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.promoteHeight = res;
        },
      ),
      TextFieldItem(
        title: "梯级宽度",
        isMust: true,
        content: "${item.stepWidth}",
        keyboardType: TextInputType.numberWithOptions(decimal: true),
        controller: TextEditingController(),
        hintText: "请输入梯级宽度(mm)",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.stepWidth = res;
        },
      ),
      TextFieldItem(
        title: "倾斜角度",
        isMust: true,
        content: "${item.tiltAngle}",
        keyboardType: TextInputType.numberWithOptions(decimal: true),
        controller: TextEditingController(),
        hintText: "请输入倾斜角度(°)",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.tiltAngle = res;
        },
      ),
      TextFieldItem(
        title: "电动机功率",
        keyboardType: TextInputType.numberWithOptions(decimal: true),
        content: "${item.motorPower}",
        controller: TextEditingController(),
        hintText: "请输入电动机功率(kw)",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.motorPower = res;
        },
      ),
      TextFieldItem(
        title: "额定速度",
        keyboardType: TextInputType.numberWithOptions(decimal: true),
        content: "${item.ratedSpeed}",
        controller: TextEditingController(),
        hintText: "请输入额定速度(m/s)",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.ratedSpeed = res;
        },
      ),
    ];
  }

  _ftrxd() {
    return [
      TextFieldItem(
        title: "人行道长度",
        isMust: true,
        keyboardType: TextInputType.numberWithOptions(decimal: true),
        content: "${item.sidewalkLength}",
        controller: TextEditingController(),
        hintText: "请输入人行道长度(m)",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.sidewalkLength = res;
        },
      ),
    ];
  }

  _commontFooter() {
    return [
      TextFieldItem(
        title: "备注",
        content: "${item.remarks}",
        controller: TextEditingController(),
        hintText: "请填写备注",
        focusNode: FocusNode(),
        onChanged: (res) {
          item.remarks = res;
        },
      ),
    ];
  }

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

  _showBottomSheet(list, Function callback) {
    showModalBottomSheet(
      context: context,
      builder: (BuildContext context) {
        return SizedBox(
          height: 360.0,
          child: ListView.builder(
            itemExtent: 48.0,
            itemBuilder: (_, index) {
              return InkWell(
                child: Container(
                  padding: const EdgeInsets.symmetric(horizontal: 16.0),
                  alignment: Alignment.centerLeft,
                  child: Text(list[index]),
                ),
                onTap: () {
                  setState(() {
                    callback(index);
                  });
                  NavigatorUtils.goBack(context);
                },
              );
            },
            itemCount: list.length,
          ),
        );
      },
    );
  }

  ///保存电梯
  _saveLift() {
    if (!_checkData()) {
      return;
    }
    showLoading(context, "正在保存");
    if (item.id.length > 0) {
      ApiService(context: context).liftEdit(
          widget.projectId, item.workerId, item.toJson(), onSuccess: (data) {
        dismissLoading(context);
        showAlert(context, "提示", "保存成功", "确定", () {
          NavigatorUtils.goBack(context);
//          NavigatorUtils.goBack(context);
          NavigatorUtils.goBackWithParams(context, true);
        });
      }, onError: (code, msg) {
        dismissLoading(context);
        toasts(msg);
      });
    } else {
      ApiService(context: context).liftAdd(
          widget.projectId, item.workerId, item.toJson(), onSuccess: (data) {
        dismissLoading(context);
        showAlert(context, "提示", "保存成功", "确定", () {
          NavigatorUtils.goBack(context);
//          NavigatorUtils.goBack(context);
          NavigatorUtils.goBackWithParams(context, true);
        });
      }, onError: (code, msg) {
        dismissLoading(context);
        toasts(msg);
      });
    }
  }

  ///检查数据
  _checkData() {
    if (item.registrationCode.length == 0) {
      toasts("请填写设备注册代码");
      return false;
    }
    if (item.category == 0) {
      toasts("请选择电梯类别");
      return false;
    }
    if (item.devicePosition.length == 0) {
      toasts("请填写设备安装地址");
      return false;
    }
    if (item.useCompanyCode.length == 0) {
      toasts("请填写设备内部编号");
      return false;
    }
    if (item.installDate.length == 0) {
      toasts("请选择设备安装日期");
      return false;
    }
    if (item.workerId.length == 0) {
      toasts("请选择维保负责人");
      return false;
    }
    if (item.deviceUsage < 0) {
      toasts("请选择设备用途");
      return false;
    }
    if (item.liftBrand.length == 0) {
      toasts("请选择电梯品牌");
      return false;
    }
    if (item.annualInspectionDate.length == 0) {
      toasts("请选择年检日期");
      return false;
    }
    switch (item.category) {
      case 1:
        if (item.layerStationDoor.length == 0) {
          toasts("请填写层站门");
          return false;
        }
        if (item.clampType == 0) {
          toasts("请选择安全钳类型");
          return false;
        }
        if (item.ratedLoad.length == 0) {
          toasts("请填写电梯载重");
          return false;
        }
        if (item.promoteHeight.length == 0) {
          toasts("请填写提升高度");
          return false;
        }
        if (item.ratedSpeed.length == 0) {
          toasts("请填写额定速度");
          return false;
        }
        break;
      case 2:
        if (item.ratedLoad.length == 0) {
          toasts("请填写电梯载重");
          return false;
        }
        if (item.promoteHeight.length == 0) {
          toasts("请填写提升高度");
          return false;
        }
        if (item.motorPower.length == 0) {
          toasts("请填写电动机功率");
          return false;
        }
        if (item.ratedSpeed.length == 0) {
          toasts("请填写额定速度");
          return false;
        }
        if (item.layerStationDoor.length == 0) {
          toasts("请填写层站门");
          return false;
        }
        if (item.clampType == 0) {
          toasts("请选择安全钳类型");
          return false;
        }
        if (item.controlType.length == 0) {
          toasts("请选择控制方式");
          return false;
        }
        if (item.cylinderNum.length == 0) {
          toasts("请填写油缸数量");
          return false;
        }
        if (item.cylinderType.length == 0) {
          toasts("请填写油缸型式");
          return false;
        }
        if (item.topType.length == 0) {
          toasts("请填写顶升型式");
          return false;
        }
        break;
      case 3:
        if (item.ratedLoad.length == 0) {
          toasts("请填写电梯载重");
          return false;
        }
        if (item.promoteHeight.length == 0) {
          toasts("请填写提升高度");
          return false;
        }
        if (item.motorPower.length == 0) {
          toasts("请填写电动机功率");
          return false;
        }
        if (item.ratedSpeed.length == 0) {
          toasts("请填写额定速度");
          return false;
        }
        if (item.layerStationDoor.length == 0) {
          toasts("请填写层站门");
          return false;
        }
        if (item.clampType == 0) {
          toasts("请选择安全钳类型");
          return false;
        }
        break;
      case 4:
        if (item.ratedLoad.length == 0) {
          toasts("请填写电梯载重");
          return false;
        }
        if (item.promoteHeight.length == 0) {
          toasts("请填写提升高度");
          return false;
        }
        if (item.stepWidth.length == 0) {
          toasts("请填写梯级宽度");
          return false;
        }
        if (item.tiltAngle.length == 0) {
          toasts("请填写倾斜角度");
          return false;
        }
        break;
      case 5:
        if (item.ratedLoad.length == 0) {
          toasts("请填写电梯载重");
          return false;
        }
        if (item.promoteHeight.length == 0) {
          toasts("请填写提升高度");
          return false;
        }
        if (item.stepWidth.length == 0) {
          toasts("请填写梯级宽度");
          return false;
        }
        if (item.sidewalkLength.length == 0) {
          toasts("请填写人行道长度");
          return false;
        }
        if (item.tiltAngle.length == 0) {
          toasts("请填写倾斜角度");
          return false;
        }
        break;
      default:
        break;
    }
    return true;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: ThemeUtils.getTabsBg(context),
      //resizeToAvoidBottomPadding: false,
      appBar: MyAppBar(
        centerTitle: item.id.length > 0 ? "编辑电梯" : "新建电梯",
        actions: <Widget>[
          FlatButton(
              onPressed: () {
                _saveLift();
              },
              child: Text(
                "保存",
                style: TextStyle(color: Colours.blue_app_main),
              ),
              textColor: Colours.text,
              highlightColor: Colors.transparent)
        ],
      ),
      body: SafeArea(
        child: Column(
          children: <Widget>[
            Expanded(
              flex: 1,
              child: defaultTargetPlatform == TargetPlatform.iOS
                  ? FormKeyboardActions(child: _buildBody())
                  : SingleChildScrollView(child: _buildBody()),
            )
          ],
        ),
      ),
    );
  }

  _buildBody() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: liftData.map((item) {
        return item;
//        return item["controller"] != null
//            ? TextFieldItem(
////                key: Key(item['key']),
//                isMust: item["isMust"],
//                content: item["content"],
//                controller: item["controller"],
//                hintText: "${item["hintText"] ?? "请填写"}",
//                title: "${item["title"]}",
//                onChanged: item["onChanged"],
//              )
//            : ClickItem(
////                key: Key(item['key']),
//                title: item["title"],
//                hintText: "请选择",
//                content: item["content"],
//                isMust: item["isMust"],
//                onTap: item["onTap"],
//              );
      }).toList(),
    );
  }
}
