import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:liftmanager/net/api_service.dart';
import 'package:liftmanager/res/colors.dart';
import 'package:liftmanager/res/gaps.dart';
import 'package:liftmanager/routers/fluro_navigator.dart';
import 'package:liftmanager/utils/image_utils.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/my_button.dart';

class TeamDetailPage extends StatefulWidget {
  final String id;
  final String name;
  final String logoImg;
  final String corporator;
  final String telephone;
  final int userNum;
  final String createDate;
  final String remarks;
  final int isCertificated;

  TeamDetailPage(
      this.id,
      this.name,
      this.logoImg,
      this.corporator,
      this.telephone,
      this.userNum,
      this.createDate,
      this.remarks,
      this.isCertificated);

  @override
  State<StatefulWidget> createState() {
    return TeamDetailPageState();
  }
}

class TeamDetailPageState extends State<TeamDetailPage> {
  TextEditingController _dialogController = TextEditingController();

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

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

  ///加入团队
  _joinTeam() {
    showLoading(context, "正在申请...");
    ApiService(context: context).applyTeam(
        widget.id, _dialogController.text.toString().trim(), onSuccess: (data) {
      dismissLoading(context);
      showAlert(context, "提示", "申请成功", "确定", () {
        NavigatorUtils.goBack(context);
      });
    }, onError: (code, msg) {
      dismissLoading(context);
      toasts(msg);
    });
  }

  ///填写申请说明
  _joinTeamDialog() {
    showDialog(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return AlertDialog(
            title: Text("确定要加入此团队？"),
            content: Container(
              padding: EdgeInsets.only(left: 10, right: 10),
              decoration: BoxDecoration(
                border: Border.all(color: Colours.text_gray_c, width: 0.5),
                borderRadius: BorderRadius.circular(3),
              ),
              child: TextField(
                textAlign: TextAlign.left,
                controller: _dialogController,
                decoration: InputDecoration(
                  border: InputBorder.none,
                  hintText: "申请加入说明",
                  hintStyle: TextStyle(color: Colours.text_gray_c),
                ),
                maxLines: 4,
              ),
            ),
            actions: <Widget>[
              FlatButton(
                child: Text(
                  "取消",
                  style: TextStyle(color: Colours.text_gray_c),
                ),
                onPressed: () {
                  Navigator.of(context).pop();
                },
              ),
              FlatButton(
                onPressed: () {
                  Navigator.of(context).pop();
                  _joinTeam();
                },
                child: Text(
                  "确定",
//                  style: TextStyle(color: Colors.red),
                ),
              )
            ],
          );
        });
//
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: MyAppBar(
          centerTitle: "团队详情",
        ),
        body: ListView(padding: EdgeInsets.all(0.0), children: <Widget>[
          Container(
            color: ThemeUtils.getTabsBg(context),
            child: Center(
              child: CircleAvatar(
                  radius: 30.0,
                  backgroundColor: Colors.transparent,
                  backgroundImage: ImageUtils.getImageProvider(
                      widget.logoImg)),
            ),
          ),
          Container(
            color: ThemeUtils.getTabsBg(context),
            child: Center(
                child: Container(

              padding: EdgeInsets.fromLTRB(8, 2, 8, 2),
              decoration: BoxDecoration(
                  color: Colours.app_main,
                  borderRadius: BorderRadius.circular(12)),
              child: Text(
                  widget.isCertificated == 1
                      ? "已认证"
                      : widget.isCertificated == 2 ? "审核中" : "未认证",
                  style: TextStyle(
                      fontSize: 10,
                      color: Colors.white,
                      fontWeight: FontWeight.bold)),
            )),
          ),
          Gaps.vGap12,
          ClickItem(title: "团队名", content: "${widget.name}"),
          ClickItem(title: "联系人", content: "${widget.corporator}"),
          ClickItem(title: "联系电话", content: "${widget.telephone}"),
          ClickItem(title: "团队人数", content: "${widget.userNum}"),
          ClickItem(title: "注册时间", content: "${widget.createDate}"),
          Container(
            margin: EdgeInsets.only(top: 8, bottom: 8),
            child: ClickItem(
              title: "团队描述",
              content: "${widget.remarks}",
            ),
          ),
          Padding(
            padding: const EdgeInsets.all(16),
            child: MyButton(
              fontSize: 14,
              onPressed: () {
                _joinTeamDialog();
              },
              text: "加入团队",
            ),
          )
        ]));
  }
}
