import 'package:fis_i18n/i18n.dart';
import 'package:fis_theme/theme.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/pages/upload/image_upload_card.dart';
import 'package:flyinsono/pages/user/user_device/controller.dart';
import 'package:flyinsono/pages/user/user_device/widgets/department_select.dart';

///新增or编辑设备
class BuildAddOrEditTerminal extends FisView<DeviceController> {
  final bool isEdit;

  BuildAddOrEditTerminal({
    Key? key,
    this.isEdit = false,
  }) : super(key: key);

  @override
  FWidget build(BuildContext context) {
    return FObx(
      () => FContainer(
        child: FColumn(
          children: [
            _buildTerminalInfoRow(
              i18nBook.user.deviceCode.t,
              _buildShordCodeInput(),
              !isEdit,
            ),
            _buildTerminalInfoRow(
              i18nBook.profileComplete.organizationIn.t,
              _buildHospitalInput(),
            ),
            if (controller.state.isOrgDevice)
              _buildTerminalInfoRow(
                i18nBook.user.departmentIn.t,
                DepartmentSelect(),
              ),
            if (controller.state.isOrgDevice ||
                controller.state.deviceInfo.isNewDevice)
              _buildTerminalInfoRow(
                i18nBook.user.deviceDescription.t,
                _buildTerminalNameInput(),
                !isEdit,
              ),
            _buildTerminalInfoRow(
              i18nBook.user.deviceType.t,
              _buildTerminalTypeRow(),
            ),
            FObx(
              () => _buildTerminalInfoRow(
                i18nBook.user.deviceModel.t,
                _buildTerminalModel(),
              ),
            ),
            if (controller.state.isOrgDevice ||
                controller.state.deviceInfo.isNewDevice)
              _buildTerminalInfoRow(
                i18nBook.user.coverImage.t,
                _buildImageUploadBorder(),
              ),
            if (controller.state.isOrgDevice)
              _buildTerminalInfoRow(
                i18nBook.user.deviceAutoShare.t,
                _buildShareCheck(),
              ),
          ],
        ),
      ),
    );
  }

  ///一行设备信息
  FWidget _buildTerminalInfoRow(String label, FWidget value,
      [bool isRequired = false]) {
    // bool? isRequired = false;
    final labelStyle = TextStyle(fontSize: 16);

    FWidget txt = FText.rich(TextSpan(
      children: [
        TextSpan(
          text: label,
          style: labelStyle,
        ),
        isRequired
            ? TextSpan(
                text: ' *',
                style: labelStyle.copyWith(color: Colors.red),
              )
            : TextSpan()
      ],
      // style: TextStyle,
    ));
    return FContainer(
      margin: EdgeInsets.symmetric(
        vertical: 10,
      ),
      child: FRow(
        children: [
          FContainer(
            width: 150,
            child: txt,
          ),
          FExpanded(
            child: FContainer(
              child: FRow(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [value],
              ),
            ),
          ),
        ],
      ),
    );
  }

  ///组织信息输入框
  FWidget _buildHospitalInput() {
    return FBorderInput(
      hintSize: 16,
      contentSize: 16,
      maxLength: 100,
      fillColor: Color.fromRGBO(224, 224, 224, 1),

      readOnly: true,
      borderColor: Color.fromRGBO(234, 234, 234, 1),
      // width: 400,
      height: 38,
      hintText: isEdit
          ? controller.state.deviceInfo.organizationName
          : i18nBook.user.OrganizationIn.t,
      controller: isEdit ? null : controller.orgNameController,
    );
  }

  ///设备名称
  FWidget _buildTerminalNameInput() {
    return FBorderInput(
      hintSize: 16,
      contentSize: 16,
      maxLength: 200,
      borderColor: Color.fromRGBO(234, 234, 234, 1),
      height: 38,
      hintText: i18nBook.user.inputDeviceDescription.t,
      controller: controller.nameEditingController,
      onChanged: (value) {
        controller.state.deviceInfo.description = value;
      },
    );
  }

  ///设备短码
  FWidget _buildShordCodeInput() {
    var deviceInfo = controller.state.deviceInfo;
    return FBorderInput(
      hintSize: 16,
      contentSize: 16,
      controller: controller.shortCodeController,
      maxLength: 6,
      inputFormatters: [
        //仅支持数字或字母
        FilteringTextInputFormatter.allow(RegExp(r'[a-zA-Z0-9]'))
      ],
      borderColor: Color.fromRGBO(234, 234, 234, 1),
      height: 38,
      readOnly: isEdit,
      fillColor: isEdit ? Color.fromRGBO(224, 224, 224, 1) : null,
      hintText: isEdit ? deviceInfo.shortCode : i18nBook.user.inputDeviceCode.t,
      onChanged: (value) {
        if (value.length == 6) {
          deviceInfo.shortCode = value;
          controller.getDeviceInfo(value);
        } else {
          deviceInfo.deviceType = '';
          deviceInfo.deviceModel = '';
          controller.deviceModelController.text = '';
          controller.nameEditingController.text = '';
          controller.deviceTypeController.text = '';
          controller.orgNameController.text = '';
          controller.state.deviceInfo.organizationName = "";
          controller.state.deviceInfo.description = "";
        }
      },
    );
  }

  ///设备类型
  FWidget _buildTerminalTypeRow() {
    var deviceInfo = controller.state.deviceInfo;
    return FBorderInput(
        hintSize: 16,
        contentSize: 16,
        maxLength: 100,
        fillColor: Color.fromRGBO(224, 224, 224, 1),
        borderColor: Color.fromRGBO(234, 234, 234, 1),
        readOnly: true,
        height: 38,
        hintText:
            isEdit ? deviceInfo.deviceType : i18nBook.user.autoFillDeviceType.t,
        controller: isEdit ? null : controller.deviceTypeController);
  }

  ///设备型号
  FWidget _buildTerminalModel() {
    var deviceInfo = controller.state.deviceInfo;
    return FBorderInput(
        hintSize: 16,
        contentSize: 16,
        maxLength: 100,
        borderColor: Color.fromRGBO(234, 234, 234, 1),
        readOnly: true,
        fillColor: Color.fromRGBO(224, 224, 224, 1),
        height: 38,
        hintText: isEdit
            ? deviceInfo.deviceModel
            : i18nBook.user.autoFillDeviceModel.t,
        controller: isEdit ? null : controller.deviceModelController);
  }

  ///图像上传框
  FWidget _buildImageUploadBorder() {
    var deviceInfo = controller.state.deviceInfo;
    return ImageUploadCard(
      originImgUrl: deviceInfo.coverImage,
      height: 101,
      width: 101,
      isShowAddIcon: true,
      onUploaded: (v) {
        deviceInfo.coverImage = v;
      },
      onDeleted: () {
        deviceInfo.coverImage = '';
      },
      hintText: '',
      localImageUrl:
          (isEdit && controller.state.selectedTerminal.headPicUrl == null)
              ? 'assets/images/mock_terminal.png'
              : '',
    );
  }

  ///一键分享开关
  FWidget _buildShareCheck() {
    return FObx(
      () => FSwitch(
        value: controller.state.checked,
        inactiveThumbColor: FTheme.ins.colorScheme.primary,
        inactiveTrackColor: Colors.grey.withAlpha(88),
        activeColor: Colors.white,
        activeTrackColor: FTheme.ins.colorScheme.primary,
        onChanged: (v) {
          controller.state.checked = v;
        },
      ),
    );
  }
}
