
import 'dart:async';

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/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 SignAddPage extends StatefulWidget{
  SignAddPage(this.address,this.coordinate,this.toptime,this.downtime,this.toptimepm,this.downtimepm,this.toptimeout,this.downtimeot);
  final String address;
  final String coordinate;
  final String toptime;
  final String downtime;
  final String toptimepm;
  final String downtimepm;
  final String toptimeout;
  final String downtimeot;
  @override
  State<StatefulWidget> createState() {
    return SignAddPageState();
  }
}

class SignAddPageState extends State<SignAddPage>{
  Timer _timer;
  String _nowTimeStr = "";
  String signTypeName = "";
  int signType = 0;

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

  @override
  void dispose() {
    _timer.cancel();
    super.dispose();

  }
  startTime() async {
    _timer = Timer.periodic(Duration(seconds: 1), (timer) {
      if(!mounted) return;
      setState(() {
        _nowTimeStr = DateTime.now().toString().split(".")[0];
      });
    });
  }

  signAdd(){
    if(signType ==0){
      toasts("请选择签到类型");
      return;
    }
    ApiService(context: context).signAdd("${widget.coordinate}",widget.address,signType,onSuccess: (res){
      showAlert(context, "提示", "打卡成功", "确定", (){
        NavigatorUtils.goBack(context);
        NavigatorUtils.goBackWithParams(context,true);
      });
    },onError: (code,msg){
      toasts(msg);
    });
  }

  _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,
          ),
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //resizeToAvoidBottomPadding: false,
      appBar: MyAppBar(
        centerTitle: "打卡",

      ),
      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: <Widget>[
        ClickItem(
            title: "打卡时间",
            content:
            "${_nowTimeStr}",
            hintText: "",
        ),
        ClickItem(
          title: "打卡地址",
          content:
          "${widget.address}",
          maxLines: 3,
          hintText: "",
        ),
        ClickItem(
          title: "打卡类型",
          content:
          "${signTypeName}",
          hintText: "请选择签到类型",
          onTap: (){
            var liftTypeArr = ["上班时间：${widget.toptime}", "下班时间：${widget.downtime}","上班时间：${widget.toptimepm}","下班时间：${widget.downtimepm}","上班时间：${widget.toptimeout}","下班时间：${widget.downtimeot}"];
            _showBottomSheet(liftTypeArr, (index) {
              signTypeName = liftTypeArr[index];
              switch(index){
                case 0:
                  signType = 11;
                  break;
                case 1:
                  signType = 12;
                  break;
                case 2:
                  signType = 21;
                  break;
                case 3:
                  signType = 22;
                  break;
                case 4:
                  signType = 31;
                  break;
                case 5:
                  signType = 32;
                  break;
              }
              setState(() {

              });
//              item.category = index + 1;
////              item.categoryName = liftTypeArr[index];
//              liftNotif();
            });
          },
        ),
        Padding(
          padding: const EdgeInsets.all(16),
          child: MyButton(
            onPressed: (){
              signAdd();
            },
            text: "提交",
          ),
        )

      ],
    );
  }
}