import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:project_telephony/model/hive/phone_model.dart';
import 'package:project_telephony/ui/widget/scaffold_theme_widget.dart';
import 'package:velocity_x/velocity_x.dart';

import '../../constants/api.dart';
import '../../model/exclude_phone_model.dart';
import '../../model/network/api_client.dart';
import '../../model/network/base_model.dart';
import '../../utils/hive_store.dart';

class ExcludeSinglePage extends StatefulWidget {
  final bool qf;
  final int index;
  const ExcludeSinglePage({Key? key,this.qf=false,this.index=0}) : super(key: key);

  @override
  _ExcludeSinglePageState createState() => _ExcludeSinglePageState();
}

class _ExcludeSinglePageState extends State<ExcludeSinglePage> {
  late TextEditingController _phoneController;
  late TextEditingController _nameController;

  @override
  void initState() {
    super.initState();
    _phoneController = TextEditingController();
    _nameController = TextEditingController();
  }

  @override
  void dispose() {
    super.dispose();
    _phoneController.dispose();
    _nameController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return ScaffoldThemeWidget(
      wid: 110,
      title: '添加单个或批量号码',
      bottom: "添加",
      onTap: () async {
        if (_phoneController.text.isEmpty) {
          BotToast.showText(text: "未输入手机号");
        } else {
          if(widget.qf){
            List<PhoneModel> messList=[];
            messList = await HiveStore.dataBox?.get("ml").cast<PhoneModel>();
            // numList = HiveStore.dataBox?.get("pl").cast<PhoneNum>();
            // print(item[index]);
            // messList.removeWhere((element) => (element.phoneList)!.contains(item[index]));
            // numList.remove(item[index]);
            messList[widget.index].phoneList?.add(
              PhoneNum(
                name: _nameController.text,
                phone: _phoneController.text,
                state: false,
              )
            );
            // print(messList);
            // messList.map((e) => e.phoneList!.add(PhoneNum(
            //     name: _nameController.text,
            //     phone:_phoneController.text ,
            //     state: false)));
            HiveStore.dataBox?.put("ml", messList);
            print(HiveStore.dataBox?.get("ml"));
            // Get.back();
            Get.back();
          }else{
          BaseModel res = await apiClient.request(
            API.exclude.add,
            data: {
              'exclude': [Exclude(
                phone: _phoneController.text,
                remark: _nameController.text,
              )],
            },
          );
          BotToast.showText(text: res.msg);
          if (res.code == 0) {
            // Get.back();
            Get.back();
          }
        }}
      },
      isOpacity:
          _phoneController.text.isNotEmpty || _nameController.text.isNotEmpty,
      child: Container(
        padding: EdgeInsets.only(top: 25.h, left: 50.w, right: 50.w),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            addNumber("号码", "请输入号码", _phoneController),
            15.heightBox,
            addNumber("名称", "请输入名称", _nameController),
            30.heightBox,
            // RichText(
            //   text: const TextSpan(
            //     children: [
            //       TextSpan(
            //         text: "使用说明:\n",
            //         style: TextStyle(
            //           fontWeight: FontWeight.w600,
            //           color: Colors.black38,
            //         ),
            //       ),
            //       TextSpan(
            //         text: "如输入000*,可屏蔽所有000开头的号码",
            //         style: TextStyle(
            //           color: Colors.black38,
            //         ),
            //       ),
            //     ],
            //   ),
            // ),
          ],
        ),
      ),
    );
  }

  addNumber(String title, content, TextEditingController controller) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          title,
          style: TextStyle(
            fontSize: 28.sp,
            color: const Color(0xFF999999),
            fontWeight: FontWeight.w500,
          ),
        ),
        15.heightBox,
        TextField(
          controller: controller,
          onChanged: (value) {
            setState(() {});
          },
          decoration: InputDecoration(
            contentPadding: EdgeInsets.all(30.w),
            hintText: content,
            hintStyle: TextStyle(
              fontSize: 28.sp,
              // fontWeight: FontWeight.w600,
              color: const Color(0xFF999999),
            ),
            fillColor: const Color(0xFFF9F9F9),
            filled: true,
            enabledBorder: const OutlineInputBorder(
              /*边角*/
              borderRadius: BorderRadius.all(
                Radius.circular(5), //边角为5
              ),
              borderSide: BorderSide(
                color: Colors.white, //边线颜色为白色
                width: 1, //边线宽度为2
              ),
            ),
            focusedBorder: const OutlineInputBorder(
              borderSide: BorderSide(
                color: Colors.white, //边框颜色为白色
                width: 1, //宽度为5
              ),
              borderRadius: BorderRadius.all(
                Radius.circular(5), //边角为30
              ),
            ),
          ),
        ),
      ],
    );
  }
}