import 'package:fis_measure/define.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/router/mobile/names.dart';
import 'package:flyinsono/architecture/states/states.dart';
import 'package:flyinsono/views/wechat_mini_program/wx_public_widgets/wx_footer.dart';
import 'package:flyinsono/views/wechat_mini_program/wx_public_widgets/wx_header.dart';
import 'package:get/get.dart';

import 'index.dart';

// 账号绑定
class WxAccountLinkingPage extends FisView<WxAccountLinkingController> {
  const WxAccountLinkingPage({Key? key}) : super(key: key);

  @override
  FWidget build(BuildContext context) {
    return FisBuilder<WxAccountLinkingController>(
      builder: (_) {
        return Scaffold(
          backgroundColor: Color.fromARGB(255, 241, 241, 241),
          body: SafeArea(
            child: _buildView(),
          ),
        );
      },
    );
  }

  FWidget _buildButton(String text, Color color, void Function()? onTap) {
    return FContainer(
      height: 50,
      margin: EdgeInsets.all(5),
      decoration: BoxDecoration(
        color: Color.fromARGB(255, 84, 144, 249),
        // border: Border.all(color: Color.fromARGB(255, 170, 170, 170), width: 1),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.2),
            spreadRadius: 0,
            blurRadius: 20,
            offset: Offset(3, 3), // changes position of shadow
          ),
        ],
        borderRadius: BorderRadius.circular(10),
      ),
      clipBehavior: Clip.antiAlias,
      child: FMaterial(
        color: Color.fromARGB(255, 84, 144, 249),
        child: FInkWell(
          highlightColor: Color.fromARGB(255, 49, 125, 255),
          onTap: onTap,
          child: FColumn(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              FCenter(
                child: FText(
                  text,
                  style: TextStyle(
                    color: Color.fromARGB(255, 241, 241, 241),
                    fontSize: 16,
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  // 主视图
  FWidget _buildView() {
    return FColumn(
      children: [
        WxHeader(),
        FContainer(
          margin: EdgeInsets.all(20),
          padding: EdgeInsets.all(20),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(10),
          ),
          child: QuickFWidget(AspectRatio(
            aspectRatio: 1,
            child: FColumn(
              children: [
                FContainer(
                  width: double.infinity,
                  child: FObx(() => FText(
                        controller.state.isShowBindUI
                            ? "请输入账号来进行身份绑定"
                            : "✅当前身份已绑定: \n\n用户：${Store.user.displayName}\n\n医院：${Store.user.rootOrganizationName}",
                        textAlign: TextAlign.left,
                        style: TextStyle(
                            fontSize: 18, fontWeight: FontWeight.bold),
                      )),
                ),
                FExpanded(child: FContainer()),
                FObx(() => controller.state.isShowBindUI
                    ? FContainer(
                        child: FTextField(
                          controller: controller.accountController,
                          decoration: InputDecoration(
                            hintText: "请输入账号",
                            border: OutlineInputBorder(
                              borderRadius: BorderRadius.circular(10),
                            ),
                          ),
                        ),
                      )
                    : FContainer()),
                FObx(() => controller.state.isShowBindUI
                    ? FContainer(
                        margin: EdgeInsets.only(top: 20),
                        child: FTextField(
                          controller: controller.passwordController,
                          decoration: InputDecoration(
                            hintText: "请输入密码",
                            border: OutlineInputBorder(
                              borderRadius: BorderRadius.circular(10),
                            ),
                          ),
                          obscureText: true,
                          obscuringCharacter: '･',
                        ),
                      )
                    : FContainer()),
                FExpanded(child: FContainer()),
                FObx(() => FRow(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        FExpanded(
                          child: _buildButton(
                            controller.state.isShowBindUI ? "绑定" : "重新绑定",
                            Colors.blue,
                            () {
                              if (controller.state.isShowBindUI) {
                                controller.bindAccount();
                              } else {
                                controller.reBindAccount();
                              }
                            },
                          ),
                        ),
                        FSizedBox(width: 20),
                        FExpanded(
                          child: _buildButton(
                            "取消",
                            Colors.grey,
                            () {
                              Get.offNamed(
                                  MobileRouteNames.Wechat.MiniProgramHome);
                            },
                          ),
                        ),
                      ],
                    )),
              ],
            ),
          )),
        ),
        FExpanded(child: FContainer()),
        WxFooter(),
      ],
    );
  }
}
