import 'dart:ui';

import 'package:cs_resources/generated/assets.dart';
import 'package:domain/entity/response/t_h_pre_selected_table_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_button.dart';
import 'package:widgets/my_load_image.dart';
import 'package:widgets/my_text_view.dart';
import 'package:widgets/widget_export.dart';

import 'pre_selected_staff_controller.dart';

/*
 * 已经预选员工的弹窗
 */
class PreSelectedStaff extends StatefulWidget {
  String? jobId;
  String? requestId;
  int jobStatus = 0;

  PreSelectedStaff({ this.jobId, this.requestId,required this.jobStatus});

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

class _AppliedAddStaffState extends State<PreSelectedStaff> {
  @override
  void initState() {
    super.initState();
    PreSelectedStaffController controller = Get.put(PreSelectedStaffController());
    controller.jobId = widget.jobId;
    controller.requestId = widget.requestId;
  }

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

  @override
  Widget build(BuildContext context) {
    return GetBuilder<PreSelectedStaffController>(
      assignId: true,
      builder: (controller) {
        return Container(
          width: 300,
          height: 555,
          color: const Color(0XFFF7F7F7),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Stack(
                children: [
                  MyTextView(
                    "Pre-selected Staff".tr,
                    fontSize: 19,
                    isFontMedium: true,
                    textColor: ColorConstants.black,
                    marginTop: 22,
                    marginLeft: 22,
                    marginRight: 22,
                  ).alignCenter(),
                  const MyAssetImage(Assets.baseServiceRedDeleteIcon, width: 20, height: 20).marginOnly(top: 15, right: 15).onTap(() {
                    onCancel();
                  }).alignRight(),
                ],
              ),

              //只有在工作状态 = pending 的时候才可以展示
              Visibility(
                visible: widget.jobStatus == 1,
                child: MyButton(
                  type: ClickType.throttle,
                  milliseconds: 500,
                  onPressed: () {
                    FocusScope.of(context).unfocus();
                    controller.showAddStaffDialog();
                  },
                  text: "Add Staff".tr,
                  textColor: ColorConstants.white,
                  fontSize: 16,
                  radius: 20,
                  minHeight: 30,
                  backgroundColor: hexToColor("#FFBB1B"),
                  fontWeight: FontWeight.w500,
                ).marginOnly(left: 17, right: 17),
              ),

              //工作标题与时间
              MyTextView(
                "${controller.indexOptions?.jobTitle} ${controller.indexOptions?.jobDate}",
                fontSize: 14,
                marginLeft: 17,
                marginRight: 17,
                textColor: Colors.black,
                isFontRegular: true,
                marginBottom: 10,
              ),

              //列表容器
              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], controller);
                        },
                        childCount: controller.datas.length,
                      ))
                    ],
                  ),
                ).paddingZero,
              ).expanded(),
            ],
          ),
        ).clipRRect(bottomLeft: 15, bottomRight: 15, topLeft: 15, topRight: 15);
      },
    );
  }

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

  Widget _buildStaffItem(THPreSelectedTableRows item, PreSelectedStaffController controller) {
    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(
                  "${"ID Card No.".tr}:",
                  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(
                  "${"Status".tr}:",
                  marginRight: 3,
                  textColor: ColorConstants.black66,
                  fontSize: 13,
                  isFontRegular: true,
                ),
                MyTextView(
                  item.statusShow ?? "-",
                  textColor: ColorConstants.black66,
                  fontSize: 13,
                  isFontRegular: true,
                ),
              ],
            ).marginOnly(top: 5),

            //消息
            Row(
              children: [
                MyTextView(
                  "${"Message".tr}:",
                  marginRight: 3,
                  textColor: ColorConstants.black66,
                  fontSize: 13,
                  isFontRegular: true,
                ),
                MyTextView(
                  item.message ?? "",
                  textColor: ColorConstants.black66,
                  fontSize: 13,
                  isFontRegular: true,
                ),
              ],
            ).marginOnly(top: 5),

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

        //删除按钮
        MyButton(
          onPressed: () {
            FocusScope.of(context).unfocus();
            controller.doDeleteAction(item);
          },
          text: "Delete".tr,
          textColor: ColorConstants.white,
          fontSize: 13,
          backgroundColor: hexToColor("#DC4462"),
          radius: 17.25,
          padding: const EdgeInsets.only(left: 12, right: 12, top: 5, bottom: 5),
          minWidth: 50,
          minHeight: 35,
        ).marginOnly(right: 17, top: 18).alignRight()
      ],
    );
  }
}
