import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:vitalapp/components/button.dart';
import 'package:vitalapp/components/cell.dart';
import 'package:vitalapp/components/dialog_input.dart';
import 'package:vitalapp/components/dialog_number.dart';
import 'package:vitalapp/pages/settings/server/widgets/protocol_switch.dart';

import 'controller.dart';

class ServerSettingPage extends GetView<ServerSettingController> {
  const ServerSettingPage({super.key});

  @override
  Widget build(BuildContext context) {
    final state = controller.state;
    return Column(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        VListFormCellGroup(
          children: [
            Obx(
              () => VListFormCell(
                label: "协议",
                contentWidget: ProtocolSwitchWidget(
                  useSSL: state.enableSSL,
                  onChanged: (value) {
                    state.enableSSL = value;
                  },
                ),
              ),
            ),
            Obx(
              () => VListFormCell(
                label: "域名/IP",
                content: state.host,
                onTap: () async {
                  final newValue = await VDialogInput(
                    title: "域名/IP",
                    initialValue: state.host.toString(),
                  ).show();
                  if (newValue != null) {
                    state.host = newValue;
                  }
                },
              ),
            ),
            Obx(
              () => VListFormCell(
                label: "端口",
                content: state.port.toString(),
                onTap: () async {
                  final newValue = await VDialogNumber(
                    title: "端口",
                    initialValue: state.port.toString(),
                  ).show();
                  if (newValue != null) {
                    state.port = int.parse(newValue);
                  }
                },
              ),
            ),
          ],
        ),
        Padding(
          padding: const EdgeInsets.only(bottom: 30),
          child: VButton(
            label: "保存",
            onTap: controller.submit,
          ),
        ),
      ],
    );
  }
}
