import 'dart:ui';

import 'package:cs_resources/generated/assets.dart';
import 'package:domain/entity/response/job_list_applied_staff_search_entity.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:widgets/ext/ex_widget.dart';
import 'package:cs_resources/constants/color_constants.dart';
import 'package:widgets/load_state_layout.dart';
import 'package:widgets/my_load_image.dart';
import 'package:widgets/my_text_view.dart';
import 'package:widgets/search_app_bar.dart';
import 'package:widgets/widget_export.dart';

import 'applied_add_staff_controller.dart';

/**
 * 添加员工的弹窗
 */
class AppliedAddStaff extends StatefulWidget {
  String jobId;
  void Function(String selectIds)? confirmAction;

  AppliedAddStaff({required this.jobId, this.confirmAction});

  @override
  State<AppliedAddStaff> createState() => _AppliedAddStaffState();
}

class _AppliedAddStaffState extends State<AppliedAddStaff> {
  @override
  void initState() {
    super.initState();
    Get.put(AppliedAddStaffController());
  }

  @override
  void dispose() {
    super.dispose();
    Get.delete<AppliedAddStaffController>();
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<AppliedAddStaffController>(
      assignId: true,
      builder: (controller) {
        return Container(
          width: 300,
          height: 555,
          decoration: BoxDecoration(
            color: Color(0XFFF7F7F7),
            borderRadius: const BorderRadius.all(Radius.circular(15)),
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Center(
                child: MyTextView(
                  "Choose Staff".tr,
                  fontSize: 19,
                  isFontMedium: true,
                  textColor: ColorConstants.black,
                  marginTop: 22,
                  marginBottom: 15,
                  marginLeft: 22,
                  marginRight: 22,
                ),
              ),

              SearchAppBar(
                value: controller.keyword,
                searchBarHeight: 38,
                searchBarBgColor: Colors.white,
                searchBarBorderRadius: 15,
                searchBarBorder: Border.all(
                  color: Color(0XFFC3C3C3), // 设置边框颜色为灰色
                  width: 0.5, // 设置边框宽度
                ),
                textHintColor: Color(0XFFAFB3B7),
                textColor: ColorConstants.black33,
                onSearch: (keyword) {
                  controller.doSearch(keyword);
                },
                hintText: "Staff Name/ID/Phone".tr,
                controller: controller.searchController,
              ).marginOnly(left: 16.5, right: 1.5, bottom: 15.5),

              Container(
                color: Colors.white,
                child: EasyRefresh(
                  header: ClassicHeader(
                    dragText: 'Pull to refresh'.tr,
                    armedText: 'Release ready'.tr,
                    readyText: 'Refreshing...'.tr,
                    processingText: 'Refreshing...'.tr,
                    processedText: 'Succeeded'.tr,
                    noMoreText: 'No more'.tr,
                    failedText: 'Failed'.tr,
                    messageText: 'Last updated at %T'.tr,
                    textStyle: const TextStyle(color: ColorConstants.black66, fontSize: 14),
                    messageStyle: const TextStyle(color: ColorConstants.black66, fontSize: 12),
                    iconTheme: const IconThemeData(color: ColorConstants.black66),
                    backgroundColor: Colors.transparent,
                  ),
                  footer: ClassicFooter(
                    dragText: 'Pull to load'.tr,
                    armedText: 'Release ready'.tr,
                    readyText: 'Loading...'.tr,
                    processingText: 'Loading...'.tr,
                    processedText: 'Succeeded'.tr,
                    noMoreText: 'No more'.tr,
                    failedText: 'Failed'.tr,
                    showMessage: false,
                    triggerOffset: 50,
                    iconDimension: 22,
                    textStyle: const TextStyle(color: ColorConstants.black66, fontSize: 14),
                    messageStyle: const TextStyle(color: ColorConstants.black66, fontSize: 12),
                    iconTheme: const IconThemeData(color: ColorConstants.black66),
                    backgroundColor: Colors.transparent,
                  ),
                  controller: controller.refreshController,
                  onRefresh: controller.onRefresh,
                  onLoad: controller.loadMore,
                  child: LoadStateLayout(
                    themeColor: ColorConstants.black66,
                    state: controller.loadingState,
                    errorMessage: controller.errorMessage,
                    errorRetry: () {
                      controller.retryRequest();
                    },
                    successSliverWidget: [
                      SliverList(
                          delegate: SliverChildBuilderDelegate(
                        (context, index) {
                          return _buildStaffItem(controller.datas[index], () {
                            /// Item选中与未选中设置
                            controller.datas[index].isSelected = !controller.datas[index].isSelected;
                            controller.update();
                          });
                        },
                        childCount: controller.datas.length,
                      ))
                    ],
                  ),
                ),
              ).expanded(),

              //按钮组
              Row(
                children: [
                  //取消按钮
                  Expanded(
                      flex: 1,
                      child: InkWell(
                        onTap: () {
                          onCancel();
                        },
                        child: MyTextView(
                          "Cancel".tr,
                          fontSize: 17.5,
                          isFontMedium: true,
                          textAlign: TextAlign.center,
                          textColor: Color(0XFF0085C4),
                          cornerRadius: 3,
                          borderWidth: 1,
                        ),
                      )),

                  //垂直分割线
                  Container(
                    color: Color(0xff09141F).withOpacity(0.13),
                    width: 0.5,
                  ),

                  //同意按钮
                  Expanded(
                      flex: 1,
                      child: InkWell(
                        onTap: () {
                          doCallbackAction(controller);
                        },
                        child: MyTextView(
                          "Submit".tr,
                          marginLeft: 10,
                          fontSize: 17.5,
                          isFontMedium: true,
                          textAlign: TextAlign.center,
                          textColor: Color(0XFF0085C4),
                          cornerRadius: 3,
                        ),
                      )),
                ],
              ).constrained(height: 46),
            ],
          ),
        );
      },
    );
  }

  //取消弹框
  void onCancel() async {
    SmartDialog.dismiss();
  }

  //执行回调
  void doCallbackAction(AppliedAddStaffController controller) {
    onCancel();

    //找到当前选中的
    var selectedList = controller.datas.where((element) => element.isSelected).toList(growable: false);
    if (selectedList.isNotEmpty) {
      var ids = selectedList.map((e) => e.id.toString()).toList(growable: false);
      String separatedIds = ids.join(',');

      widget.confirmAction?.call(separatedIds);
    }

  }

  Widget _buildStaffItem(JobListAppliedStaffSearchRows item, VoidCallback callback) {
    return Stack(
      children: [
        Column(
          children: [
            //姓名
            Row(
              children: [
                MyTextView(
                  "Staff:",
                  textColor: ColorConstants.black66,
                  fontSize: 13,
                  marginRight: 3,
                  isFontRegular: true,
                ),
                MyTextView(
                  item.name ?? "-",
                  textColor: ColorConstants.black66,
                  fontSize: 13,
                  isFontRegular: true,
                ),
              ],
            ),

            //头像
            Row(
              children: [
                MyTextView(
                  "Avatar:",
                  textColor: ColorConstants.black66,
                  fontSize: 13,
                  marginRight: 3,
                  isFontRegular: true,
                ),
                MyLoadImage(
                  item.avatar ?? "",
                  width: 25,
                  height: 25,
                ),
              ],
            ).marginOnly(top: 5),

            //性别
            Row(
              children: [
                MyTextView(
                  "Gender:",
                  marginRight: 3,
                  textColor: ColorConstants.black66,
                  fontSize: 13,
                  isFontRegular: true,
                ),
                MyTextView(
                  item.sex ?? "-",
                  textColor: ColorConstants.black66,
                  fontSize: 13,
                  isFontRegular: true,
                ),
              ],
            ).marginOnly(top: 5),

            //身份证
            Row(
              children: [
                MyTextView(
                  "NRIC:",
                  marginRight: 3,
                  textColor: ColorConstants.black66,
                  fontSize: 13,
                  isFontRegular: true,
                ),
                MyTextView(
                  item.nric ?? "-",
                  textColor: ColorConstants.black66,
                  fontSize: 13,
                  isFontRegular: true,
                ),
              ],
            ).marginOnly(top: 5),

            //电话
            Row(
              children: [
                MyTextView(
                  "Phone:",
                  marginRight: 3,
                  textColor: ColorConstants.black66,
                  fontSize: 13,
                  isFontRegular: true,
                ),
                MyTextView(
                  item.phone ?? "-",
                  textColor: ColorConstants.black66,
                  fontSize: 13,
                  isFontRegular: true,
                ),
              ],
            ).marginOnly(top: 5),

            Container(
              margin: EdgeInsets.only(top: 19),
              width: double.infinity,
              height: 1,
              color: Color(0XFFF7F7F7),
            )
          ],
        ).paddingOnly(left: 19, right: 20, top: 17),

        //是否勾选
        MyAssetImage(
          item.isSelected ? Assets.baseServiceItemSelectedIcon : Assets.baseServiceItemUnselectedGrayIcon,
          width: 20.5,
          height: 20.5,
        ).alignRight().marginOnly(right: 20, top: 17.5),
      ],
    ).onTap(callback);
  }
}
