import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:sport/application.dart';
import 'package:sport/bean/feed_back.dart';
import 'package:sport/provider/feed_back_model.dart';
import 'package:sport/services/api/inject_api.dart';
import 'package:sport/utils/toast.dart';
import 'package:sport/widgets/appbar.dart';
import 'package:sport/widgets/button_primary.dart';
import 'package:sport/widgets/dialog/request_dialog.dart';
import 'package:sport/widgets/space.dart';

class FeedbackDetailPage extends StatefulWidget {
  final FeedTypeInfoData data;
  FeedbackDetailPage(this.data);
  @override
  State<StatefulWidget> createState() => _PageState();
}

class _PageState extends State<FeedbackDetailPage> with InjectApi {
//  List<String> tags;
//  List<String> tagsId;
  String tag;
  int tagId;
  String _content;

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

  postFeedBackpostFeedBack(String content, {int typeId = 0}) async {
    typeId == null ? typeId = 0 : typeId = typeId;
    await api.postFeedback(typeId, content, extra: await Application.getDeviceInfo());
  }

  @override
  Widget build(BuildContext context) {
//    String tag = "数据不同步";
    return Scaffold(
        backgroundColor: Colors.white,
        body: CustomScrollView(
          slivers: <Widget>[
            buildSliverAppBar(context, "${widget.data.groupName}"),
            SliverToBoxAdapter(
                child: Padding(
              padding: const EdgeInsets.all(12.0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text("请选择问题类型", style: Theme.of(context).textTheme.headline3),
                  Padding(
                    padding: const EdgeInsets.symmetric(vertical: 6.0),
                    child: Wrap(
                        alignment: WrapAlignment.start,
                        spacing: 12,
                        children: widget.data.types.map<Widget>((e) {
                          return ChoiceChip(
                            labelPadding: const EdgeInsets.fromLTRB(12.0, 0, 12.0, 0),
                            pressElevation: 0,
                            //未选定的时候背景
                            selectedColor: Theme.of(context).accentColor,
                            //被禁用得时候背景
                            backgroundColor: Colors.white,
                            disabledColor: Color(0xfff1f1f1),
                            shape: StadiumBorder(
                              side: BorderSide(color: Color(tag == e.typeName ? 0xffffffff : 0xffcecece), width: 0.5),
                            ),
                            selected: tag == e.typeName,
                            label: Text("${e.typeName}"),
                            onSelected: (value) {
//                              if (tags.indexOf(e['typeName']) >= 0) {
//                                tags.remove(e['typeName']);
//                                tagsId.remove(e['typeId']);
//                                setState(() {
//                                  tags = tags;
//                                  tagsId = tagsId;
//                                });
//                              } else {
//                                tags.add(e['typeName']);
//                                tagsId.add(e['typeId']);
//                                setState(() {
//                                  tags = tags;
//                                  tagsId = tagsId;
//                                });
//                              }

                              setState(() {
                                tag = e.typeName;
                                tagId = int.parse(e.typeId);
                              });
                            },
                          );
                        }).toList()),
                  ),
                  Space(
                    height: 10,
                  ),
//                  Text("问题发生时间", style: Theme.of(context).textTheme.subtitle1),
//                  InkWell(
//                    onTap: () async {
//                      var result = await showDatePicker(
//                          context: context,
//                          initialDate: DateTime.now(),
//                          firstDate: DateTime(1900),
//                          lastDate: DateTime.now());
//                      print('$result');
//                      var time = await showTimePicker(
//                          context: context, initialTime: TimeOfDay.now());
//                      print('$time');
//                      if (time != null) {
//                        DateFormat.yM().format(result);
//                      }
//                    },
//                    child: Container(
//                      height: 30,
//                      margin: const EdgeInsets.symmetric(vertical: 6.0),
//                      decoration: BoxDecoration(
//                          border: Border(
//                        bottom: Divider.createBorderSide(context),
//                      )),
//                    ),
//                  ),
                  Space(
                    height: 10,
                  ),
                  Text("问题详述", style: Theme.of(context).textTheme.subtitle1),
                  Space(
                    height: 6,
                  ),
                  TextField(
                    maxLines: 5,
                    decoration: InputDecoration(
                      enabledBorder: OutlineInputBorder(
                          borderSide: BorderSide(width: 0.5,
                              color: Theme.of(context).dividerTheme.color)),
                      hintText: '问题详述...',
                      focusedBorder: OutlineInputBorder(
                          borderSide:
                              BorderSide(width: 0.5,color: Theme.of(context).accentColor)),
                    ),
                    onChanged: (value) {
                      setState(() {
                        _content = value;
                      });
                    },
                  ),
                  Space(
                    height: 24,
                  ),
                  PrimaryButton(
                    content: "提交",
                    callback: _content == null || _content == "" ? null : () async {
                      await request(context, () async {
                        postFeedBackpostFeedBack(_content, typeId: tagId);
                      });
                      ToastUtil.show("感谢你的建议，我们会第一时间解决");
                      Navigator.pop(context);
                    },
                  )
                ],
              ),
            )),
          ],
        ));
  }
}
