import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sk_base_mobile/models/base_search_more.dart';
import 'package:sk_base_mobile/models/sale_quotation.model.dart';
import 'package:sk_base_mobile/screens/sale_quotation/components/sale_quotation_group_search.dart';
import 'package:sk_base_mobile/widgets/core/sk_single_search_more.dart';
import 'package:sk_base_mobile/util/modal.util.dart';
import 'package:sk_base_mobile/util/screen_adaper_util.dart';
import 'package:pinyin/pinyin.dart';

class SaleQuotationController extends GetxController {
  static SaleQuotationController get to => Get.find();
  final RxList editingcell = RxList([null, null, null]);
  RxList<SaleQuotationItemModel> products = RxList([
    SaleQuotationItemModel(name: '矿用本安型支架控制器', unit: '台', spec: 'ZDYZ-Z'),
    SaleQuotationItemModel(name: '矿用本安型电磁阀驱动器'),
    SaleQuotationItemModel(name: '矿用隔爆兼本安型电源'),
    SaleQuotationItemModel(name: '矿用本安型隔离耦合器'),
    SaleQuotationItemModel(name: '钢丝编织橡胶护套连接器'),
    SaleQuotationItemModel(name: '钢丝编织橡胶护套连接器', remark: '控制器-控制器'),
    SaleQuotationItemModel(name: '钢丝编织橡胶护套连接器', remark: '控制器-驱动器'),
    SaleQuotationItemModel(name: '钢丝编织橡胶护套连接器', remark: '控制器-隔离耦合器'),
    SaleQuotationItemModel(name: '矿用本安型支架无线遥控器'),
    SaleQuotationItemModel(name: '矿用隔爆兼本安型电源'),
    SaleQuotationItemModel(name: '电液换向阀(10功能10接口)', remark: '中间过渡架主阀组'),
    SaleQuotationItemModel(name: '电液换向阀(20功能20接口)', remark: '端头架主阀组'),
    SaleQuotationItemModel(name: '自动反冲洗过滤装置', remark: '流量:900L/min,过滤精度25μm'),
    SaleQuotationItemModel(name: '全自动反冲洗过滤器电缆', remark: '控制器-自动反冲洗'),
    SaleQuotationItemModel(name: '矿用本安型位移传感器'),
    SaleQuotationItemModel(name: '矿用本安型压力传感器'),
    SaleQuotationItemModel(name: '矿用本安型红外发射器'),
    SaleQuotationItemModel(name: '矿用本安型LED信号灯'),
    SaleQuotationItemModel(name: '倾角传感器'),
    SaleQuotationItemModel(name: '各类安装附件'),
  ]);
  RxList<SaleQuotationModel> groups = RxList<SaleQuotationModel>([]);

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

  Future<void> addGroup() async {
    final controller = Get.put(GroupSearchMoreController());
    // 选择组件  选择分组
    ModalUtil.showGeneralDialog(
            content: SkSingleSearchMore<SaleQuotationModel>(
              controller: controller,
              enablePullUp: false,
              enablePullDown: true,
              itemBuilder: (_, index) {
                return InkWell(
                    onTap: () {
                      Get.back();
                      groups.add(controller.list[index]);
                    },
                    child: Container(
                      padding: EdgeInsets.symmetric(
                          horizontal: ScreenAdaper.width(5),
                          vertical: ScreenAdaper.height(10)),
                      child: Row(
                        children: [
                          Text(
                            controller.list[index].name,
                            style: TextStyle(fontSize: ScreenAdaper.height(25)),
                          ),
                        ],
                      ),
                    ));
              },
            ),
            width: Get.width - ScreenAdaper.width(50))
        .then((value) => Get.delete<GroupSearchController>());
  }

  Future<void> removeGroup(int index) async {
    groups.removeAt(index);
  }

  Future<void> addItems(int groupIndex) async {
    final controller = Get.put(ItemSearchMoreController());
    //   选择产品
    ModalUtil.showGeneralDialog(
            content: SkSingleSearchMore<SaleQuotationItemModel>(
              controller: controller,
              enablePullUp: false,
              enablePullDown: true,
              isDialog: true,
              itemBuilder: (_, index) {
                return InkWell(
                    onTap: () {
                      Get.back();
                      groups[groupIndex].items.add(controller.list[index]);
                    },
                    child: Container(
                      padding: EdgeInsets.symmetric(
                          horizontal: ScreenAdaper.width(5),
                          vertical: ScreenAdaper.height(10)),
                      child: Row(
                        children: [
                          Text(
                            controller.list[index].name,
                            style: TextStyle(fontSize: ScreenAdaper.height(25)),
                          ),
                        ],
                      ),
                    ));
              },
              // onSelected: (SaleQuotationItemModel info) {
              //   Get.back();
              //   // groups.add(info);
              // },
            ),
            width: Get.width - ScreenAdaper.width(50))
        .then((value) => Get.delete<ItemSearchMoreController>());
  }
}

class GroupSearchMoreController extends GetxController
    with BaseSearchMore<SaleQuotationModel> {
  @override
  Future<List<SaleQuotationModel>> getData({bool isRefresh = false}) async {
    List<SaleQuotationModel> newList = [
      SaleQuotationModel(name: '中间过渡架电控部分', items: RxList([])),
      SaleQuotationModel(name: '端头架电控部分', items: RxList([])),
      SaleQuotationModel(name: '主阀部分', items: RxList([])),
      SaleQuotationModel(name: '自动反冲洗过滤器部分', items: RxList([])),
      SaleQuotationModel(name: '位移测量部分', items: RxList([])),
      SaleQuotationModel(name: '压力检测部分', items: RxList([])),
      SaleQuotationModel(name: '煤机定位部分', items: RxList([])),
      SaleQuotationModel(name: '姿态检测部分', items: RxList([])),
    ];
    list.assignAll(newList
        .where((element) => PinyinHelper.getPinyin(element.name, separator: '')
            .contains(searchKey.value))
        .toList());
    return newList;
  }
}

class ItemSearchMoreController extends GetxController
    with BaseSearchMore<SaleQuotationItemModel> {
  @override
  Future<List<SaleQuotationItemModel>> getData({bool isRefresh = false}) async {
    List<SaleQuotationItemModel> newList = [
      SaleQuotationItemModel(name: '矿用本安型支架控制器', unit: '台', spec: 'ZDYZ-Z'),
      SaleQuotationItemModel(name: '矿用本安型电磁阀驱动器'),
      SaleQuotationItemModel(name: '矿用隔爆兼本安型电源'),
      SaleQuotationItemModel(name: '矿用本安型隔离耦合器'),
      SaleQuotationItemModel(name: '钢丝编织橡胶护套连接器'),
      SaleQuotationItemModel(name: '钢丝编织橡胶护套连接器', remark: '控制器-控制器'),
      SaleQuotationItemModel(name: '钢丝编织橡胶护套连接器', remark: '控制器-驱动器'),
      SaleQuotationItemModel(name: '钢丝编织橡胶护套连接器', remark: '控制器-隔离耦合器'),
      SaleQuotationItemModel(name: '矿用本安型支架无线遥控器'),
      SaleQuotationItemModel(name: '矿用隔爆兼本安型电源'),
      SaleQuotationItemModel(name: '电液换向阀(10功能10接口)', remark: '中间过渡架主阀组'),
      SaleQuotationItemModel(name: '电液换向阀(20功能20接口)', remark: '端头架主阀组'),
      SaleQuotationItemModel(name: '自动反冲洗过滤装置', remark: '流量:900L/min,过滤精度25μm'),
      SaleQuotationItemModel(name: '全自动反冲洗过滤器电缆', remark: '控制器-自动反冲洗'),
      SaleQuotationItemModel(name: '矿用本安型位移传感器'),
      SaleQuotationItemModel(name: '矿用本安型压力传感器'),
      SaleQuotationItemModel(name: '矿用本安型红外发射器'),
      SaleQuotationItemModel(name: '矿用本安型LED信号灯'),
      SaleQuotationItemModel(name: '倾角传感器'),
      SaleQuotationItemModel(name: '各类安装附件'),
    ];
    list.assignAll(newList
        .where((element) => PinyinHelper.getPinyin(element.name, separator: '')
            .contains(searchKey.value))
        .toList());
    return newList;
  }
}
