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: Stack(
        children: [
          Container(
            margin: EdgeInsets.symmetric(
                vertical: ScreenAdaper.height(10),
                horizontal: ScreenAdaper.width(10)),
            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.start,
              children: [
                buildImage(),
                Expanded(child: buildContent()),
              ],
            ),
          ),
          Positioned(
            child: buildInOrOut(),
            right: ScreenAdaper.width(5),
            top: ScreenAdaper.height(5),
          )
        ],
      ),
    );
  }

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

  Widget buildImage() {
    return Container(
        margin: EdgeInsets.only(right: ScreenAdaper.width(defaultPadding)),
        child: ClipRRect(
          borderRadius: BorderRadius.circular(ScreenAdaper.sp(15)),
          child: FadeInCacheImage(
              width: ScreenAdaper.height(150),
              height: ScreenAdaper.height(150),
              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.height(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: TextStyle(
            fontSize: ScreenAdaper.sp(25),
            color: AppTheme.nearlyWhite,
            fontWeight: FontWeight.w600),
      ),
    );
  }
}
