import 'package:fis_jsonrpc/rpc.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:image_picker/image_picker.dart';
import 'package:intl/intl.dart';
import 'package:vitalapp/architecture/storage/storage.dart';
import 'package:vitalapp/components/cell.dart';
import 'package:vitalapp/components/dialog_date.dart';
import 'package:vitalapp/components/dialog_select.dart';
import 'package:vitalapp/consts/rpc_enum_labels.dart';
import 'package:vitalapp/consts/styles.dart';
import 'package:vitalapp/pages/check/follow_up/controller.dart';
import 'package:vitalapp/rpc.dart';
import 'package:vitalapp/store/store.dart';

import '../models/select_model.dart';

class FollowUpFrom extends GetView<FollowUpController> {
  const FollowUpFrom({super.key});

  @override
  Widget build(BuildContext context) {
    return _buildFollowUp();
  }

  Widget _buildFollowUp() {
    return Card(
      elevation: 6,
      shape: RoundedRectangleBorder(
        borderRadius: GlobalStyles.borderRadius,
      ),
      margin: const EdgeInsets.all(16),
      child: Material(
        color: Colors.white,
        borderRadius: const BorderRadius.all(
          GlobalStyles.borderRadiusValue,
        ),
        child: Container(
            padding: const EdgeInsets.symmetric(horizontal: 16),
            child: Column(
              children: [
                _buildFrom(),
              ],
            )),
      ),
    );
  }

  Widget _buildFrom() {
    return Obx(
      () => VListFormCellGroup(
        // mainAxisAlignment: MainAxisAlignment.center,
        children: [
          VListFormCell(
            label: '居民姓名',
            labelWidth: 130,
            content: controller.patientName,
          ),
          VListFormCell(
            label: '随访医生',
            labelWidth: 130,
            content: Store.user.displayName,
          ),
          VListFormCell(
            label: '本次随访日期',
            labelWidth: 130,
            content: getFollowUpTime(controller.state.followUpTime),
            onTap: () async {
              final result = await VDialogDate(
                title: '本次随访日期',
                initialValue: controller.state.followUpTime,
              ).show();
              controller.state.followUpTime = result;
            },
          ),
          VListFormCell(
            label: '下次随访日期',
            labelWidth: 130,
            content: getFollowUpTime(controller.state.nextFollowUpTime),
            onTap: () async {
              final result = await VDialogDate(
                title: '下次随访日期',
                initialValue: controller.state.nextFollowUpTime,
                minimumDate: controller.state.followUpTime,
              ).show();
              controller.state.nextFollowUpTime = result;
            },
          ),
          VListFormCell(
            label: '随访方式',
            labelWidth: 130,
            content: RpcEnumLabels.followUpMode[controller.state.followUpMode],
            onTap: () async {
              final result = await VDialogSelect<SelectModel, FollowUpModeEnum>(
                source: controller.state.followUpModeEnumSelectList,
                valueGetter: (data) => data.code,
                labelGetter: (data) => data.name,
                initialValue: controller.state.followUpMode,
              ).show();
              controller.state.followUpMode = result;
            },
          ),
          VListFormCell(
            label: '拍照',
            labelWidth: 130,
            contentWidget: _buildFollowImage(controller.state.followUpPhoto),
            onTap: () async {
              XFile? image =
                  await ImagePicker().pickImage(source: ImageSource.camera);
              final url = await rpc.storage.upload(image!);
              controller.state.followUpPhoto = url;
            },
          ),
        ],
      ),
    );
  }

  Widget _buildFollowImage(String? followUpPhoto) {
    if (followUpPhoto?.isNotEmpty ?? false) {
      return Image.network(followUpPhoto!);
    } else {
      return const SizedBox();
    }
  }

  String getFollowUpTime(DateTime? time) {
    // print(time);
    if (time != null) {
      return DateFormat('yyyy-MM-dd').format(time);
    }
    return '';
  }
}
