import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:loading_animation_widget/loading_animation_widget.dart';
import 'package:sk_base_mobile/apis/index.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/constants/constants.dart';
import 'package:sk_base_mobile/models/index.dart';
import 'package:sk_base_mobile/models/inventory.model.dart';
import 'package:sk_base_mobile/util/date.util.dart';
import 'package:sk_base_mobile/util/screen_adaper_util.dart';
import 'package:sk_base_mobile/widgets/empty.dart';
import 'package:sk_base_mobile/widgets/fade_in_cache_image.dart';
import 'package:sk_base_mobile/widgets/image_preview.dart';
import 'package:sk_base_mobile/widgets/loading_indicator.dart';

class InventoryInoutInfo extends StatelessWidget {
  late int inventoryInoutId;
  late InventoryInouInfoController controller;
  InventoryInoutInfo({Key? key, required this.inventoryInoutId})
      : controller = Get.put(InventoryInouInfoController(inventoryInoutId)),
        super(key: key);

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      child: Column(children: [
        Container(
          padding: EdgeInsets.symmetric(
              horizontal: ScreenAdaper.width(10),
              vertical: ScreenAdaper.height(15)),
          decoration: const BoxDecoration(color: AppTheme.primaryColor),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(
                '出入库详情',
                style: TextStyle(
                    fontSize: ScreenAdaper.sp(25), color: AppTheme.nearlyWhite),
              ),
            ],
          ),
        ),
        Expanded(
            child: Obx(() => controller.inventoryInoutInfo.value == null
                ? const Center(child: LoadingIndicator(common: true))
                : SingleChildScrollView(
                    child: Column(
                    children: [
                      buildListItem(
                          leading: '出入库类型',
                          trailing: InventoryInOrOutEnum.In ==
                                  controller.inventoryInoutInfo.value?.inOrOut
                              ? '入库'
                              : '出库'),
                      customDivider(),
                      if (InventoryInOrOutEnum.In ==
                          controller.inventoryInoutInfo.value?.inOrOut) ...[
                        buildListItem(
                            leading: '存放位置',
                            trailing: controller
                                .inventoryInoutInfo.value?.inventory?.position),
                        customDivider(),
                      ],
                      buildListItem(
                          leading: '出入库时间',
                          trailing: DateUtil.format(
                              controller.inventoryInoutInfo.value!.time!)),
                      customDivider(),
                      buildListItem(
                          leading: '所属公司',
                          trailing: controller.inventoryInoutInfo.value?.product
                              ?.company?.name),
                      customDivider(),
                      buildListItem(
                          leading: '产品名称',
                          trailing: controller
                              .inventoryInoutInfo.value?.product?.name),
                      customDivider(),
                      buildListItem(
                          leading: '产品规格',
                          trailing: controller.inventoryInoutInfo.value?.product
                              ?.productSpecification),
                      customDivider(),
                      buildListItem(
                          leading: '数量',
                          trailing:
                              '${controller.inventoryInoutInfo.value?.quantity}${controller.inventoryInoutInfo.value?.product?.unit?.label ?? ''}'),
                      customDivider(),
                      buildListItem(
                          leading: '单价',
                          trailing:
                              '￥${double.parse('${controller.inventoryInoutInfo.value?.unitPrice ?? 0}')}'),
                      customDivider(),
                      buildListItem(
                          leading: '操作人',
                          trailing: controller.inventoryInoutInfo.value?.agent),
                      customDivider(),
                      buildListItem(
                          leading: '备注',
                          trailing:
                              controller.inventoryInoutInfo.value?.remark),
                      customDivider(),
                      buildListItem(
                        leading: '照片',
                      ),
                      buildAttachmentsPreview()
                    ],
                  ))))
      ]),
    );
  }

  Widget buildAttachmentsPreview() {
    return Container(
      padding: EdgeInsets.symmetric(vertical: ScreenAdaper.height(20)),
      child: Wrap(
        spacing: ScreenAdaper.width(10),
        runSpacing: ScreenAdaper.height(10),
        children: (controller.inventoryInoutInfo.value?.files ?? [])
            .map((e) => buildImage(e.path))
            .toList(),
      ),
    );
  }

  Widget buildImage(String? path) {
    return path == null
        ? const SizedBox()
        : InkWell(
            onTap: () => {
              if (controller.inventoryInoutInfo.value!.files.isNotEmpty)
                {
                  Get.dialog(ImagePreivew(
                      galleryItems: controller.inventoryInoutInfo.value!.files,
                      initialIndex: controller.inventoryInoutInfo.value!.files
                          .indexWhere((element) => element.path == path)))
                }
            },
            child: Container(
                margin: EdgeInsets.only(
                    right: ScreenAdaper.width(defaultPadding) / 2),
                child: ClipRRect(
                  borderRadius: BorderRadius.circular(ScreenAdaper.sp(15)),
                  child: FadeInCacheImage(
                      width: ScreenAdaper.width(300),
                      height: ScreenAdaper.width(300),
                      url: '${GloablConfig.OSS_URL}$path'),
                )),
          );
  }

  Widget customDivider() {
    return Divider(
      height: ScreenAdaper.height(1),
      color: AppTheme.dividerColor,
    );
  }

  Widget buildListItem({String? leading, dynamic trailing}) {
    return ListTile(
        leading: Text(
          '${leading ?? ''}: ',
          style: TextStyle(fontSize: ScreenAdaper.sp(25)),
        ),
        trailing: Text('${trailing ?? ''}',
            style: TextStyle(fontSize: ScreenAdaper.sp(25))));
  }
}

class InventoryInouInfoController extends GetxController {
  final inventoryInoutInfo = Rx<InventoryInOutModel?>(null);
  int inventoryInoutId;
  InventoryInouInfoController(this.inventoryInoutId);

  @override
  onReady() {
    getData();
  }

  Future<void> getData() async {
    printInfo(info: '$inventoryInoutId');
    await Future.delayed(const Duration(milliseconds: 500));
    final res = await Api.getInventoryInOutInfo(inventoryInoutId);
    if (res.data != null) {
      inventoryInoutInfo.value = InventoryInOutModel.fromJson(res.data);
    }
  }
}
