import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/utils/prompt_box.dart';
import 'package:flyinsono/lab/color/lab_colors.dart';
import 'package:flyinsono/lab/pages/lab_record_info/widgets/input_text_field.dart';
import 'package:flyinsono/lab/pages/lab_record_info/widgets/value_selector_field.dart';
import 'package:get/get.dart';
import 'package:fis_jsonrpc/rpc.dart';

import '../index.dart';

/// 创建项目
class CreateProject extends GetView<LabCreateProjectController> {
  const CreateProject({
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return _buildView();
  }

  Widget _buildView() {
    return Center(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          InputTextField(
            label: "项目名称",
            intialValue: controller.state.projectName,
            onChanged: (value) {
              controller.state.projectName = value;
            },
            bgColor: LabColors.base200,
            bgColorOnHover: LabColors.base300,
          ),
          _buildInterval(),
          Obx(
            () => ValueSelectorField(
              label: "项目类型",
              bgColor: LabColors.base200,
              bgColorOnHover: LabColors.base300,
              intialValue: controller.state.projectType ==
                      OrganizationPatientTypeEnum.Person
                  ? "人用科研"
                  : "小动物科研",
              dataSource: ["人用科研", "小动物科研"],
              onChanged: (value) {
                if (value == "人用科研") {
                  controller.state.projectType =
                      OrganizationPatientTypeEnum.Person;
                } else if (value == "小动物科研") {
                  controller.state.projectType =
                      OrganizationPatientTypeEnum.Animals;
                }
                if (controller.projectModel != null) {
                  PromptBox.toast("修改项目类型后会导致已上传的数据异常");
                }
              },
            ),
          ),
          _buildInterval(),
          InputTextField(
            label: "项目简介",
            intialValue: controller.state.projectIntroduction,
            onChanged: (value) {
              controller.state.projectIntroduction = value;
            },
            maxLines: 5,
            singleLine: true,
            maxLength: 100,
            bgColor: LabColors.base200,
            bgColorOnHover: LabColors.base300,
          ),
          // _buildInterval(),
          _buildInterval(),
          if (controller.projectModel == null)
            Container(
              width: 100,
              height: 50,
              child: ElevatedButton(
                style: ElevatedButton.styleFrom(
                  elevation: 0.0,
                  backgroundColor: LabColors.dialogHeaderColor,
                ),
                child: const Text("创建"),
                onPressed: () {
                  controller.createProject();
                },
              ),
            ),
          if (controller.projectModel != null)
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Container(
                  width: 100,
                  height: 50,
                  child: ElevatedButton(
                    style: ElevatedButton.styleFrom(
                      elevation: 0.0,
                      backgroundColor: LabColors.buttonColor,
                    ),
                    child: const Text("确定"),
                    onPressed: () {
                      controller.updateProject();
                    },
                  ),
                ),
                SizedBox(width: 20),
                Container(
                  width: 100,
                  height: 50,
                  child: ElevatedButton(
                    style: ElevatedButton.styleFrom(
                      elevation: 0.0,
                      backgroundColor: LabColors.buttonColor,
                    ),
                    child: const Text("取消"),
                    onPressed: () {
                      Get.back();
                    },
                  ),
                ),
              ],
            )
        ],
      ),
    );
  }

  Widget _buildInterval() {
    return SizedBox(
      height: 20,
    );
  }
}
