import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sk_base_mobile/app_theme.dart';
import 'package:sk_base_mobile/config.dart';
import 'package:sk_base_mobile/constants/bg_color.dart';
import 'package:sk_base_mobile/screens/inventory_inout/inventory_inout_controller.dart';
import 'package:sk_base_mobile/util/util.dart';
import 'package:sk_base_mobile/widgets/fade_in_cache_image.dart';

import '../../../constants/enum.dart';

class InventoryInoutCard extends StatelessWidget {
  InventoryInoutCard({required this.index, required this.ind});
  final int index;
  final int ind;
  final controller = Get.find<InventoryInoutController>();
  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {
        controller
            .showInventoryInoutInfoDialog(controller.list[ind][index].id!);
      },
      child: Container(
        margin: EdgeInsets.symmetric(
            vertical: ScreenAdaper.height(15),
            horizontal: ScreenAdaper.width(15)),
        padding: EdgeInsets.symmetric(
            horizontal: ScreenAdaper.width(defaultPadding),
            vertical: ScreenAdaper.height(defaultPadding)),
        decoration: BoxDecoration(
            boxShadow: [
              BoxShadow(
                  color: AppTheme.barrierColor.withOpacity(0.2),
                  offset: Offset(0, ScreenAdaper.height(5)),
                  blurRadius: ScreenAdaper.sp(10)),
            ],
            color: AppTheme.nearlyWhite,
            borderRadius: BorderRadius.circular(ScreenAdaper.sp(30))),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            buildImage(),
            Expanded(child: buildContent()),

            // const Spacer(
            //   flex: 2,
            // ),
            // controller.list[ind][index].inOrOut ==
            //         InventoryInOrOutEnumValues[InventoryInOrOutEnum.In]
            //     ? Container(
            //         height: ScreenAdaper.height(40),
            //         width: ScreenAdaper.width(40),
            //         decoration: BoxDecoration(
            //             shape: BoxShape.circle,
            //             gradient: const LinearGradient(
            //                 begin: Alignment.topCenter,
            //                 end: Alignment.bottomCenter,
            //                 colors: [
            //                   lightOrange,
            //                   darkOrange,
            //                 ]),
            //             boxShadow: [
            //               BoxShadow(
            //                   color: lightOrange,
            //                   offset: Offset(0, ScreenAdaper.height(10)),
            //                   blurRadius: ScreenAdaper.sp(10))
            //             ]),
            //         child: const Icon(
            //           Icons.done,
            //           color: Colors.white,
            //         ),
            //       )
            //     : Align(
            //         alignment: Alignment.topRight,
            //         child: Padding(
            //             padding: EdgeInsets.only(top: ScreenAdaper.height(20)),
            //             child: PopupMenuButton(
            //               onSelected: (value) => {
            //                 // controller.onTaskComplete(value, index, ind,
            //                 //     controller.list[ind][index].key, context)
            //               },
            //               surfaceTintColor: Colors.white,
            //               padding: EdgeInsets.zero,
            //               icon: Icon(
            //                 Icons.more_vert_rounded,
            //                 color: Colors.grey,
            //                 size: ScreenAdaper.sp(24),
            //               ),
            //               shape: OutlineInputBorder(
            //                 borderRadius:
            //                     BorderRadius.circular(ScreenAdaper.sp(20)),
            //                 borderSide: BorderSide.none,
            //               ),
            //               itemBuilder: (context) {
            //                 return [
            //                   PopupMenuItem(
            //                       height: ScreenAdaper.height(20),
            //                       value: 1,
            //                       child: Row(
            //                         children: [
            //                           Icon(
            //                             Icons.edit_note,
            //                             color: Colors.orange,
            //                             size: ScreenAdaper.sp(14),
            //                           ),
            //                           SizedBox(
            //                             width:
            //                                 ScreenAdaper.width(defaultPadding) /
            //                                     2,
            //                           ),
            //                           const Text('Edit')
            //                         ],
            //                       )),
            //                   PopupMenuItem(
            //                       height: ScreenAdaper.height(25),
            //                       value: 2,
            //                       child: Row(
            //                         children: [
            //                           Icon(
            //                             Icons.delete_outline,
            //                             color: Colors.orange,
            //                             size: ScreenAdaper.sp(14),
            //                           ),
            //                           SizedBox(
            //                             width:
            //                                 ScreenAdaper.width(defaultPadding) /
            //                                     2,
            //                           ),
            //                           Text('Delete')
            //                         ],
            //                       )),
            //                   PopupMenuItem(
            //                       height: ScreenAdaper.height(25),
            //                       value: 3,
            //                       child: Row(
            //                         children: [
            //                           Icon(
            //                             Icons.done_all_outlined,
            //                             color: Colors.orange,
            //                             size: ScreenAdaper.sp(14),
            //                           ),
            //                           SizedBox(
            //                             width:
            //                                 ScreenAdaper.width(defaultPadding) /
            //                                     2,
            //                           ),
            //                           Text('Complete')
            //                         ],
            //                       )),
            //                 ];
            //               },
            //             )),
            //       ),
          ],
        ),
      ),
    );
  }

  Widget buildContent() {
    return Column(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Expanded(
                child: Text(
              controller.list[ind][index].product?.name ?? '-',
              overflow: TextOverflow.ellipsis,
              style: TextStyle(
                  color: Colors.black,
                  fontWeight: FontWeight.bold,
                  fontSize: ScreenAdaper.sp(25)),
            )),
            buildInOrOut()
          ],
        ),
        Text(
          controller.list[ind][index].product?.company?.name ?? '-',
          style: TextStyle(
              color: AppTheme.grey,
              fontWeight: FontWeight.bold,
              fontSize: ScreenAdaper.sp(20)),
        ),
        SizedBox(
          height: ScreenAdaper.height(5),
        ),
        Row(
          children: [
            Expanded(
              child: Text(
                '项目: ${controller.list[ind][index].project?.name}',
                style: TextStyle(
                    fontWeight: FontWeight.w600, fontSize: ScreenAdaper.sp(20)),
              ),
            ),
          ],
        ),
        SizedBox(
          height: ScreenAdaper.height(5),
        ),
        Row(
          children: [
            Expanded(
              child: Text(
                '数量: ${controller.list[ind][index].quantity} ${controller.list[ind][index].product?.unit?.label ?? '-'}',
                style: TextStyle(
                    fontWeight: FontWeight.w600, fontSize: ScreenAdaper.sp(20)),
              ),
            ),
            Text(
              controller.list[ind][index].agent ?? '-',
              style: TextStyle(
                  fontWeight: FontWeight.w600, fontSize: ScreenAdaper.sp(20)),
            ),
          ],
        )
      ],
    );
  }

  Widget buildImage() {
    return Container(
        margin: EdgeInsets.only(right: ScreenAdaper.width(defaultPadding) / 2),
        child: ClipRRect(
          borderRadius: BorderRadius.circular(ScreenAdaper.sp(15)),
          child: FadeInCacheImage(
              width: ScreenAdaper.width(100),
              height: ScreenAdaper.width(100),
              url: controller.list[ind][index].files.isNotEmpty
                  ? '${GloablConfig.OSS_URL}${controller.list[ind][index].files[0]?.path}'
                  : ''),
        ));
  }

  Widget buildInOrOut() {
    final bgColor =
        controller.list[ind][index].inOrOut == InventoryInOrOutEnum.In
            ? AppTheme.snackbarSuccessBackgroudColor
            : AppTheme.snackbarErrorBackgroudColor;
    final textInOut =
        controller.list[ind][index].inOrOut == InventoryInOrOutEnum.In
            ? '入'
            : '出';
    return Container(
      alignment: Alignment.center,
      height: ScreenAdaper.height(40),
      width: ScreenAdaper.width(40),
      decoration: BoxDecoration(
          shape: BoxShape.circle,
          gradient: LinearGradient(
              begin: Alignment.centerLeft,
              end: Alignment.centerRight,
              colors: [
                bgColor.withOpacity(0.6),
                bgColor,
              ]),
          boxShadow: [
            BoxShadow(
                color: lightOrange,
                offset: Offset(0, ScreenAdaper.height(10)),
                blurRadius: ScreenAdaper.sp(10))
          ]),
      child: Text(
        textInOut,
        style: const TextStyle(
            color: AppTheme.nearlyWhite, fontWeight: FontWeight.w600),
      ),
    );
  }
}
