import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:image_picker/image_picker.dart';
import 'package:vitalapp/architecture/storage/storage.dart';
import 'package:vitalapp/architecture/utils/prompt_box.dart';
import 'package:vitalapp/pages/contract/package_list/controller.dart';
import 'package:vitalapp/rpc.dart';

import '../../../../consts/styles.dart';

class CapturePortraitPage extends GetView<ServicePackageContractController> {
  const CapturePortraitPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Obx(
      () => InkWell(
          onTap: () async {
            XFile? image =
                await ImagePicker().pickImage(source: ImageSource.camera);
            if (image != null) {
              PromptBox.loading('加载图片中');
              String? url = await rpc.storage.upload(image!);

              controller.state.userImage = url;
              PromptBox.dismiss();
            }
          },
          child: _buildImage(controller.state.userImage)),
    );
  }

  Widget _buildImage(String? userImage) {
    Widget image = Center(
      child: Container(
        width: 300,
        height: 300,
        decoration: BoxDecoration(
          border: Border.all(
            color: Colors.grey,
          ),
          borderRadius: GlobalStyles.borderRadius,
        ),
        child: const Icon(
          Icons.add,
          size: 100,
          color: Colors.grey,
        ),
      ),
    );
    if (userImage != null) {
      image = Container(
        padding: const EdgeInsets.all(100),
        child: Image.network(
          userImage,
        ),
      );
    }
    return image;
  }
}
