import 'package:flutter/material.dart';
import 'package:flyinsono/lab/color/lab_colors.dart';
import 'package:flyinsono/lab/components/classify_data_selector/classify_data_selector.dart';
import 'package:flyinsono/lab/components/operate_button/mini_operate_button.dart';
import 'package:flyinsono/lab/components/rect_icon_button.dart';
import 'package:flyinsono/lab/mock_data/mock_rpc.dart';
import 'package:flyinsono/lab/pages/lab_data/controller.dart';
import 'package:flyinsono/lab/style/lab_box_decoration.dart';
import 'package:get/get.dart';
import 'package:fis_theme/theme.dart';
import 'package:flyinsono/lab/manager/page_manager.dart';
import 'package:flyinsono/lab/pages/lab_export/entitys/export_init_arguments.dart';
import 'package:flyinsono/lab/router/lab_route_names.dart';

class DataSourcePanel extends GetView<LabDataController> {
  const DataSourcePanel({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: [
        // _buildTitle(),
        // SizedBox(
        //   height: 5,
        // ),
        _buildDataSourceSelector(),
        SizedBox(
          height: 10,
        ),
        _buildSearchBar(),
        SizedBox(
          height: 10,
        ),
        Expanded(
          child: _buildDataSourceList(),
        ),
      ],
    );
  }

  // Widget _buildTitle() {
  //   return Container(
  //     padding: EdgeInsets.symmetric(horizontal: 5),
  //     child: Text(
  //       '数据源：',
  //       style: TextStyle(
  //         fontSize: 20,
  //         fontWeight: FontWeight.bold,
  //         color: LabColors.text900,
  //       ),
  //     ),
  //   );
  // }

  // 数据源下拉选择器
  Widget _buildDataSourceSelector() {
    return GetBuilder<LabDataController>(
      id: LabDataController.dataSourceSelectorId,
      builder: (context) {
        return Container(
          height: 40,
          child: Container(
            padding: const EdgeInsets.symmetric(horizontal: 10),
            decoration: LabBoxDecoration.base,
            child: DropdownButtonHideUnderline(
              child: DropdownButton<String>(
                value: controller.selectedDataSourceId,
                dropdownColor: LabColors.base200,
                itemHeight: 50,
                icon: Icon(
                  Icons.keyboard_arrow_down_rounded,
                  color: LabColors.base800,
                ),
                style: TextStyle(
                  fontSize: 14,
                  color: LabColors.text800,
                  fontFamily: FTheme.ins.localeSetting.fontFamily,
                ),
                isExpanded: true,
                elevation: 2,
                items: controller.dataSourceList.map((ProjectModel dataSource) {
                  return DropdownMenuItem<String>(
                    value: dataSource.id,
                    child: Text(dataSource.name),
                  );
                }).toList(),
                onChanged: (String? value) {
                  controller.selectedClassifyIndex = 0;
                  controller.selectedPatientIndex = 0;
                  controller.selectDataSource(value);
                },
              ),
            ),
          ),
        );
      },
    );
  }

  Widget _buildSearchBar() {
    return Container(
      constraints: BoxConstraints(maxWidth: 200, minWidth: 20),
      height: 40,
      decoration: LabBoxDecoration.base,
      child: Row(
        children: [
          SizedBox(
            width: 10,
          ),
          Expanded(
            child: Container(
              height: 28,
              child: Center(
                child: TextField(
                  scrollPadding: EdgeInsets.all(0),
                  controller: controller.dataSourceSearchInputController,
                  style: TextStyle(
                    fontSize: 14,
                    color: LabColors.text800,
                  ),
                  textAlignVertical: TextAlignVertical.bottom,
                  decoration: InputDecoration(
                    hintStyle: TextStyle(
                      fontSize: 14,
                      color: LabColors.text600,
                    ),
                    isCollapsed: true,
                    contentPadding: EdgeInsets.all(0),
                    hintText: "搜索",
                    border: OutlineInputBorder(
                      borderSide: BorderSide.none,
                    ),
                  ),
                ),
              ),
            ),
          ),
          Padding(
            padding: const EdgeInsets.all(2.0),
            child: RectIconButton(
              iconSize: 20,
              icon: Icons.search,
              onPressed: () {
                controller.selectedPatientIndex = 0;
                controller.getResearchProjectData(
                  controller.selectedDataSourceId,
                );
              },
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildDataSourceList() {
    return GetBuilder<LabDataController>(
      id: LabDataController.patientListId,
      builder: (context) {
        return ClassifyDataSelector(
          loading: controller.isPatientListLoading,
          selectedClassifyIndex: controller.selectedClassifyIndex,
          selectedItemIndex: controller.selectedPatientIndex,
          classifyData: controller.patientClassifyData,
          onItemClick: (classifyIndex, itemIndex) {
            controller.clickPatientItem(classifyIndex, itemIndex);
          },
          itemActionButton: () {
            return MiniOperateButton(
              operateName: '删除',
              iconData: Icons.delete_outline_rounded,
              onClick: () {
                controller.deleteCurrentPatient();
              },
            );
          },
          actionButton: () {
            return MiniOperateButton(
              iconData: Icons.file_upload_outlined,
              operateName: '批量导出',
              onClick: () {
                PageManager.ins.openNewTab(
                  LabRouteNames.Export,
                  "批量导出",
                  args: LabExportInitArguments(
                    labExportPatientList: controller
                        .patientClassifyData[controller.selectedClassifyIndex]
                        .data,
                    projectCode: controller.selectedDataSourceId,
                  ),
                );
              },
            );
          },
        );
      },
    );
  }
}
