
import 'dart:convert' as convert;

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:liftmanager/internal/maintenance/maintenance_router.dart';
import 'package:liftmanager/internal/maintenance/model/maintenance_last_entity.dart';
import 'package:liftmanager/internal/maintenance/model/maintenance_list_entity.dart';
import 'package:liftmanager/net/api_service.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 'package:liftmanager/widgets/my_button.dart';

class MaintenanceRecordInfo extends StatefulWidget{

  MaintenanceRecordInfo(this.item);
  final MaintenanceListItem item;
  @override
  State<StatefulWidget> createState() {
    return MaintenanceRecordInfoState();
  }
}

class MaintenanceRecordInfoState extends State<MaintenanceRecordInfo>{

  MaintenanceLastEntity item = MaintenanceLastEntity();

  var _mainlist = [
    "半月保","半月保","半月保","半月保","半月保", "季度保",
    "半月保","半月保","半月保","半月保","半月保", "半年保",
    "半月保","半月保","半月保","半月保","半月保", "季度保",
    "半月保","半月保","半月保","半月保","半月保", "全年保"];

  String _currentTypeName = "";
  int _currentType = -1;

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

  ///获取上次保养信息
  getLastMainRecord(){
    ApiService(context: context).maintenanceRecordInfo(widget.item.liftId,onSuccess: (data){
      if(data != null){
        setState(() {
          item = data;
        });
      }

    },onError: (code,msg){
      toasts(msg);
    });
  }

  _startSignAndSafe(){
    if(_currentType==-1){
      toasts("请选择保养类型");
      return;
    }
    widget.item.maintenanceType = "${_currentType+1}";
    String jsonString = convert.jsonEncode(widget.item);
    NavigatorUtils.pushResult(context, "${MaintenanceRouter.maintenanceSign}?item=${Uri.encodeComponent(jsonString)}",(res){
      print("MaintenanceRecordInfoState");
      NavigatorUtils.goBackWithParams(context, true);
    });
  }

  _showBottomSheet(){
    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("${_mainlist[index]}${index+1}"),
                ),
                onTap: (){
                  setState(() {
                    _currentTypeName = "${_mainlist[index]}${index+1}";
                    _currentType = index;
                  });
                  NavigatorUtils.goBack(context);
                },
              );
            },
            itemCount: _mainlist.length,
          ),
        );
      },
    );
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: MyAppBar(
          centerTitle: "日常保养",
        ),
        body: ListView(padding: EdgeInsets.all(0.0), children: <Widget>[
          ClickItem(title: "上次保养日期", content: "${item?.workDate}"),
          ClickItem(title: "上次保养类型", content: "${item?.typeName}${item.periods}"),
          ClickItem(title: "项目名称", content: "${item?.projectName}"),
          ClickItem(title: "电梯位置", content: "${item?.devicePosition}"),
          ClickItem(title: "本次保养类型",hintText: '请选择', content: "${_currentTypeName}",onTap: (){
            _showBottomSheet();
          },),
          Padding(
            padding: const EdgeInsets.all(16),
            child: MyButton(
              onPressed: (){
                _startSignAndSafe();
              },
              text: "开始保养",
            ),
          )
        ])
    );
  }
}