import 'package:flutter/material.dart';
import 'package:flyinsono/lab/color/lab_colors.dart';
import 'package:flyinsono/lab/pages/lab_import/widgets/data_source_and_target_panel.dart';
import 'package:flyinsono/lab/pages/lab_import/widgets/data_selector.dart';
import 'package:flyinsono/lab/style/lab_box_decoration.dart';
import 'package:get/get.dart';
import 'index.dart';

class LabImportPage extends GetView<LabImportController> {
  const LabImportPage(this.initPara, {Key? key}) : super(key: key);
  final dynamic initPara;
  // 主视图
  Widget _buildView() {
    return Container(
      padding: EdgeInsets.all(10),
      child: Column(
        children: [
          DataSourceAndTargetPanel(),
          SizedBox(height: 10),
          Expanded(
            child: _buildMainContent(),
          ),
          SizedBox(height: 10),
          _buildBottomPart(),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<LabImportController>(
      init: LabImportController(initPara, tabHashCode: this.hashCode),
      builder: (_) {
        return Scaffold(
          backgroundColor: LabColors.base300,
          body: SafeArea(
            child: _buildView(),
          ),
        );
      },
    );
  }

  Widget _buildBottomPart() {
    return GetBuilder<LabImportController>(
      id: LabImportController.bottomPanelId,
      builder: (_) {
        return Container(
          width: double.maxFinite,
          height: 70,
          decoration: LabBoxDecoration.base,
          child: Row(
            children: [
              SizedBox(width: 15),
              Text(
                "已选择：${controller.selectedIndex.length} 条数据",
                style: TextStyle(
                  fontSize: 16,
                  color: LabColors.text800,
                ),
              ),
              Expanded(child: Container()),
              if (controller.isSelectAll)
                _buildcancelSelectAllButton()
              else
                _buildSelectAllButton(),
              SizedBox(width: 10),
              Container(
                height: 50,
                child: ElevatedButton.icon(
                  icon: const Icon(
                    Icons.file_download_outlined,
                    size: 20,
                  ),
                  style: ElevatedButton.styleFrom(
                    elevation: 0.0,
                    backgroundColor: LabColors.buttonColor,
                    padding: EdgeInsets.symmetric(
                      horizontal: 40,
                      vertical: 10,
                    ),
                  ),
                  label: const Text(
                    "导入",
                    style: TextStyle(
                      fontSize: 16,
                      height: 1,
                      color: LabColors.text100,
                    ),
                  ),
                  onPressed: () {
                    controller.importDataToProject();
                  },
                ),
              ),
              SizedBox(width: 15),
            ],
          ),
        );
      },
    );
  }

  Widget _buildMainContent() {
    return Container(
      width: double.maxFinite,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          Expanded(
            child: DataSelector(),
          ),
        ],
      ),
    );
  }

  Widget _buildSelectAllButton() {
    return Container(
      height: 50,
      width: 120,
      child: OutlinedButton.icon(
        icon: const Icon(
          Icons.library_add_check_outlined,
          size: 20,
        ),
        style: OutlinedButton.styleFrom(
          elevation: 0.0,
          backgroundColor: LabColors.base200,
          foregroundColor: LabColors.base800,
          padding: EdgeInsets.symmetric(
            vertical: 10,
          ),
        ),
        label: const Text(
          "全选",
          style: TextStyle(
            fontSize: 16,
            height: 1,
            color: LabColors.text800,
          ),
        ),
        onPressed: () {
          controller.setectAll();
        },
      ),
    );
  }

  Widget _buildcancelSelectAllButton() {
    return Container(
      height: 50,
      width: 120,
      child: OutlinedButton.icon(
        icon: const Icon(
          Icons.indeterminate_check_box_outlined,
          size: 20,
        ),
        style: OutlinedButton.styleFrom(
          elevation: 0.0,
          backgroundColor: LabColors.base200,
          foregroundColor: LabColors.base800,
          padding: EdgeInsets.symmetric(
            vertical: 10,
          ),
        ),
        label: const Text(
          "取消全选",
          style: TextStyle(
            fontSize: 16,
            height: 1,
            color: LabColors.text800,
          ),
        ),
        onPressed: () {
          controller.cancelSelectAll();
        },
      ),
    );
  }
}
