import 'package:equipment_manage/model/car_status_model.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:amos_flutter_utils/amos_flutter_utils.dart';

import '../../consts/color.dart';
import '../../utils/GetConfig.dart';
import '../../utils/request.dart';

// 区域抽屉
class StatusDrawer extends StatefulWidget {
  final String type;
  final ValueChanged<CarStatusModel> confirm;

  // 重置
  final Function reset;

  StatusDrawer({this.type, this.confirm, this.reset});

  @override
  _RegionDrawerState createState() => _RegionDrawerState();
}

class _RegionDrawerState extends State<StatusDrawer>
    with AutomaticKeepAliveClientMixin {
  List<CarStatusModel> data = [];
  bool isEmpty = false;

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

    HttpRequest.getStatusList(widget.type).then((res) {
      if (res["status"] == 200) {
        List result = res["result"];
        List<CarStatusModel> items = [];
        for (var json in result) {
          items.add(CarStatusModel.fromJson(json));
        }
        setState(() {
          data = items;
        });
        if (items.length == 0) {
          setState(() {
            isEmpty = true;
          });
        }
      }
    }).catchError((err) {});
    initThemeConfig();
  }

  String theme = '';

  initThemeConfig() async {
    SharedPreferences.getInstance().then((preferences) {
      setState(() {
        this.theme =
            preferences.getString("theme") ?? KColorConstant.DEFAULT_COLOR;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    if (isEmpty) {
      return Container(
        color: Colors.white,
        width: 300.px,
        child: SafeArea(
            child: Center(
          child: Text("请求数据为空"),
        )),
      );
    } else if (data.length == 0) {
      return Container(
        color: Colors.white,
        width: 300.px,
        child: SafeArea(
            child: Center(
          child: CupertinoActivityIndicator(),
        )),
      );
    } else {
      return Container(
        color: Colors.white,
        width: 300.px,
        child: SafeArea(
          child: Column(
            children: <Widget>[
              Padding(
                padding: EdgeInsets.only(top: 12.px),
                child: Text(
                  "状态",
                  style: Theme.of(context)
                      .textTheme
                      .headline4
                      .copyWith(color: Color(0xFF333333)),
                ),
              ),
              Expanded(
                child: ListView.builder(
                    itemCount: data.length,
                    itemBuilder: (ctx, index) {
                      final item = data[index];
                      return InkWell(
                          onTap: () {
                            Navigator.of(context).pop();
                            if (widget.confirm != null) {
                              widget.confirm(item);
                            }
                          },
                          child: ListTile(
                            title: Text("${item.name}"),
                          ));
                    }),
              ),
              Container(
                height: 40.px,
                width: double.infinity,
                child: RaisedButton(
                  shape: BeveledRectangleBorder(),
                  color: GetConfig.getColor(theme),
                  child: Text(
                    "重置",
                    style: Theme.of(context).textTheme.headline2,
                  ),
                  onPressed: () {
                    Navigator.of(context).pop();
                    if (widget.reset != null) {
                      widget.reset();
                    }
                  },
                ),
              )
            ],
          ),
        ),
      );
    }
  }

  @override
  bool get wantKeepAlive => true;
}
