import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/pages/upload/image_upload_card.dart';
import 'package:flyinsono/pages/upload/upload.dart';
import 'package:flyinsono/pages/upload/upload_sample_card.dart';
import 'package:flyinsono/pages/user/user_id_auth_apply/controller.dart';
import 'package:flyinsono/pages/user/user_id_auth_apply/widgets/image_display_box.dart';
import 'package:flyinsono/pages/user/user_id_auth_apply/widgets/row_label.dart';
import 'package:flyinsono/pages/user/user_id_auth_apply/widgets/user_label_item.dart';

import 'selected_labels.dart';

///上传身份证弹出框内容
class UploadUserIdBody extends FisView<UserIdAuthApplyController> {
  @override
  FWidget build(BuildContext context) {
    return FContainer(
      padding: const EdgeInsets.symmetric(horizontal: 48, vertical: 14),
      child: FObx(
        () => controller.state.loading
            ? FSizedBox(
                height: 300,
                child: FCenter(child: FCircularProgressIndicator()))
            : FListView(
                shrinkWrap: true,
                children: [
                  const FSizedBox(height: 14),
                  UserLabelItem(
                    label: i18nBook.user.platformRole.t,
                    child: FText(
                      controller.roleInfo.name,
                      style: const TextStyle(fontSize: 16),
                    ),
                  ),
                  // 身份证
                  if (controller.roleInfo.isNeedIdCard)
                    UserLabelItem(
                      label: i18nBook.user.idCard.t,
                      child: _buildIdCardArea(),
                    ),
                  // 医师执照
                  if (controller.roleInfo.isNeedDocCard)
                    UserLabelItem(
                      label: i18nBook.user.doctorLicense.t,
                      child: _buildLicenseArea(),
                    ),
                  if (controller.roleInfo.fieldShowType ==
                      RoleShowTypeEnum.ISShow) ...[
                    if (controller.state.applyState ==
                            ApplyStateEnum.NotApply ||
                        controller.state.inEditing) ...[
                      UserLabelItem(
                        label: i18nBook.user.skilledArea.t,
                        child: RoleLableRow(),
                        isCenterLabel: true,
                      ),
                    ] else ...[
                      UserLabelItem(
                        label: i18nBook.user.skilledArea.t,
                        child: SelectedLabels(),
                        isCenterLabel: true,
                      ),
                    ],
                  ],
                  // 审核状态和驳回原因
                  if (!controller.state.inEditing) ...[
                    // 编辑状态下不展示
                    if (controller.state.applyState != ApplyStateEnum.NotApply)
                      UserLabelItem(
                        label: i18nBook.user.verificationStatus.t,
                        child: FText(
                          controller.state.applyStateDescription,
                          style: TextStyle(
                            fontSize: 16,
                            color: controller.state.applyPassed
                                ? Colors.green
                                : Colors.red,
                          ),
                        ),
                      ),
                    if (controller.state.applyState == ApplyStateEnum.Refused)
                      UserLabelItem(
                        label: i18nBook.user.refuseReason.t,
                        child: FText(
                          controller.state.refuseNote,
                          overflow: TextOverflow.ellipsis,
                          maxLines: 10,
                          style: TextStyle(fontSize: 16, color: Colors.red),
                        ),
                      ),
                  ],
                ],
              ),
      ),
    );
  }

  ///身份证编辑或展示区域
  FWidget _buildIdCardArea() {
    return FObx(() {
      if (controller.state.inEditing) {
        return _buildIdCardEidtArea();
      } else {
        return _buildIdCardViewArea();
      }
    });
  }

  ///身份证区域
  FWidget _buildIdCardEidtArea() {
    final idFront = FObx(
      () => ImageUploadCard(
        isShowAddIcon: true,
        originImgUrl: controller.state.idCardFront,
        hintText: i18nBook.user.uploadIdCardFrontSide.t,
        onUploaded: (url) {
          Future.microtask(() {
            if (controller.state.idCardFront != url) {
              controller.state.idCardFront = url;
            }
          });
        },
        onDeleted: () {
          controller.state.idCardFront = '';
        },
        width: 190,
        height: 120,
      ),
    );
    final idBack = FObx(
      () => ImageUploadCard(
        isShowAddIcon: true,
        originImgUrl: controller.state.idCardBack,
        hintText: i18nBook.user.uploadIdCardReverseSide.t,
        onUploaded: (url) {
          Future.microtask(() {
            controller.state.idCardBack = url;
          });
        },
        onDeleted: () {
          controller.state.idCardBack = '';
        },
        width: 190,
        height: 120,
      ),
    );
    return FRow(
      mainAxisSize: MainAxisSize.min,
      children: [
        FColumn(
          mainAxisSize: MainAxisSize.min,
          children: [
            idFront,
            const FSizedBox(height: 8),
            idBack,
          ],
        ),
        const FSizedBox(width: 12),
        FColumn(
          mainAxisSize: MainAxisSize.min,
          children: [
            UploadSampleCard(type: UploadSampleType.idCardFront),
            const FSizedBox(height: 8),
            UploadSampleCard(type: UploadSampleType.idCardBack),
          ],
        ),
      ],
    );
  }

  ///证件展示区域
  FWidget _buildIdCardViewArea() {
    return FColumn(
      children: [
        if (controller.state.idCardFront.isEmpty)
          FSizedBox(height: 120)
        else
          ImageDisplayBox(
            url: controller.state.idCardFront,
            width: 190,
            height: 120,
          ),
        const FSizedBox(height: 8),
        if (controller.state.idCardBack.isEmpty)
          FSizedBox(height: 120)
        else
          ImageDisplayBox(
            url: controller.state.idCardBack,
            width: 190,
            height: 120,
          ),
      ],
    );
  }

  ///医师执照区域
  FWidget _buildLicenseArea() {
    return FObx(() {
      if (controller.state.inEditing) {
        return ImageUploadCard(
          isShowAddIcon: true,
          originImgUrl: controller.state.doctorCard,
          hintText: i18nBook.user.uploaddoctorLicense.t,
          onUploaded: (url) {
            Future.microtask(() {
              controller.state.doctorCard = url;
            });
          },
          onDeleted: () {
            controller.state.doctorCard = '';
          },
          width: 190,
          height: 120,
        );
      } else {
        if (controller.state.doctorCard.isEmpty) return FSizedBox();
        return ImageDisplayBox(
          url: controller.state.doctorCard,
          width: 190,
          height: 120,
        );
      }
    });
  }
}
