import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:rasadyar_chicken/presentation/pages/steward/root/logic.dart';
import 'package:rasadyar_core/core.dart';

Widget inventoryWidget(StewardRootLogic rootLogic) {
  return Container(
    width: Get.width,
    child: SingleChildScrollView(
      scrollDirection: Axis.horizontal,
      padding: EdgeInsets.symmetric(horizontal: 8.w),
      child: Row(
        spacing: 8.w,
        children: [
          ObxValue((data) {
            // بررسی مقدار null بودن و نشان دادن ActivityIndicator
            final value = data.value?.totalRemainWeight?.separatedByCommaFa;
            return _itemList(
              title: 'موجودی انبار',
              value: value ?? '', // در صورت نال بودن، رشته خالی نمایش داده شود
            );
          }, rootLogic.inventoryModel),
          ObxValue((data) {
            final value = data.value?.totalGovernmentalRemainWeight?.separatedByCommaFa;
            return _itemList(title: 'مانده دولتی', value: value ?? '');
          }, rootLogic.stewardSalesInfoDashboard),
          ObxValue((data) {
            final value = data.value?.totalFreeRemainWeight?.separatedByCommaFa;
            return _itemList(title: 'مانده آزاد', value: value ?? '');
          }, rootLogic.stewardSalesInfoDashboard),
        ],
      ),
    ),
  );
}

Widget _itemList({required String title, required String? value, String? unit}) {
  return Container(
    width: 125.w,
    height: 50.h,
    clipBehavior: Clip.antiAlias,
    decoration: ShapeDecoration(
      color: Colors.white.withValues(alpha: 0.40),
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
    ),
    child: Column(
      children: [
        SizedBox(height: 4),
        Text(
          title,
          textAlign: TextAlign.center,
          style: AppFonts.yekan12.copyWith(color: const Color(0xFF5B5B5B)),
        ),
        SizedBox(height: 4),
        value == null
            ? Center(child: CupertinoActivityIndicator())
            : Padding(
                padding: EdgeInsets.symmetric(horizontal: 4.w),
                child: Row(
                  spacing: 8,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(
                      value!,
                      textAlign: TextAlign.right,
                      style: AppFonts.yekan16.copyWith(color: const Color(0xFF5B5B5B)),
                    ),
                    Text(
                      unit ?? 'کیلوگرم',
                      textAlign: TextAlign.center,
                      style: AppFonts.yekan10.copyWith(color: const Color(0xFF5B5B5B)),
                    ),
                  ],
                ),
              ),
      ],
    ),
  );
}
