import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import AntiClipSettings

Popup {
    id: root
    parent: Overlay.overlay
    anchors.centerIn: Overlay.overlay
    modal: true
    property int inputHeight: 50
    background: Rectangle {
        radius: 8
    }
    contentItem: ColumnLayout {
        anchors.centerIn: parent

        Row {
            spacing: 10

            Label {
                text: "模式"
                width: 100
                anchors.verticalCenter: parent.verticalCenter
            }

            RadioButton {
                id: dhcpMode
                text: "DHCP"
                checked: App.currentNetworkInfomation.dhcp
            }

            RadioButton {
                id: staticMode
                text: "静态IP"
                checked: true
                // checked: !App.currentNetworkInfomation.dhcp
            }
        }

        Row {
            spacing: 5
            visible: staticMode.checked
            Label {
                anchors.verticalCenter: parent.verticalCenter
                anchors.verticalCenterOffset: -10
                text: "设备IP"
                width: 100
            }

            IpTextField {
                id: ipInput
                height: inputHeight
                width: 350
                text: App.currentNetworkInfomation.ip
            }
        }

        Row {
            spacing: 5
            visible: staticMode.checked
            Label {
                anchors.verticalCenter: parent.verticalCenter
                anchors.verticalCenterOffset: -10
                width: 100
                text: "子网掩码"
            }

            IpTextField {
                id: netmaskInput
                width: 350
                height: inputHeight
                text: App.currentNetworkInfomation.netmask
            }
        }

        Row {
            spacing: 5
            visible: staticMode.checked
            Label {
                anchors.verticalCenter: parent.verticalCenter
                text: "设备网关"
                anchors.verticalCenterOffset: -10
                width: 100
            }

            IpTextField {
                id: gatewayInput
                width: 350
                height: inputHeight
                text: App.currentNetworkInfomation.gateway
            }
        }

        Row {
            spacing: 5
            visible: staticMode.checked
            Label {
                anchors.verticalCenter: parent.verticalCenter
                text: "DNS服务器"
                anchors.verticalCenterOffset: -10
                width: 100
            }

            IpTextField {
                id: dnsInput
                width: 350
                height: inputHeight
                text: App.currentNetworkInfomation.gateway
            }
        }

        Row {
            Layout.rightMargin: 20
            Layout.alignment: Qt.AlignRight
            spacing: 20

            Button {
                text: "保存"
                onClicked: {
                    if (dhcpMode.checked || (staticMode.checked && ipInput.valid
                                             && netmaskInput.valid
                                             && gatewayInput.valid
                                             && dnsInput.valid)) {
                        App.updateNetworkInfomation(dhcpMode.checked,
                                                    ipInput.text,
                                                    netmaskInput.text,
                                                    gatewayInput.text)
                        networkPopup.close()
                    } else {
                        showMessageDialog(2, "网络设置", "请输入合法参数地址!")
                    }
                }
            }

            Button {
                text: "取消"
                onClicked: root.close()
            }
        }
    }
}