import QtQuick 2.15
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.14
import QtQuick.Controls.Styles 1.2
import TService 1.0

Item{
    id: root
    //anchors.fill: parent
    //width:1024
    //height: 768
    //anchors.centerIn: parent
    property var pagetype:rootitem.Page_Type_Testing
    property  string title_str:"手动测试"
    property double pressvalue: 1.22
    property bool bSelfTest_Started: false
    property  string selfTest_State_str:""
    property  string selfTest_Result_str:""
    property  string selfTest_Detail_str:""


    function show_pressure(pressure_str){
        lcd_pressure.text = pressure_str;
    }

    /*
    function show_selftestState(str){
        selfTest_State_str = str
    }

    function show_selftestResult(str){
        selfTest_Result_str = str
    }

    function show_selftestDetail(str){
        selfTest_Detail_str = str
    }

    function selftestStop(){
        var ret1 = testService.tstop(0);
        if(ret1){
            bSelfTest_Started = false;
            //selftest_result.text = ""
            selftest_button.text = "开始"

            readbutton.enabled = true;
            adjustbutton.enabled = true;
            //timer.stop()
        }
    }

    */


   // Component{
    //    id:componet
        DynamicGroupBox{
            id:groupbox
            //x:width
            //x:0

            anchors.centerIn: parent
            anchors.fill: parent

            title: title_str

            Column{
                spacing: 10
                anchors.centerIn: parent

            DynamicGroupBox{
                id:relays_control
                title: "电磁阀控制"

                width: 800
                height: 100

                Row{
                    anchors.centerIn: parent
                    //columns: 4
                    //rows:1
                    //columnSpacing:  80
                    //rowSpacing: 30
                    spacing: 60


                    UserSwitch{
                        id:switch1
                        text: "进气阀关"
                        onCheckedChanged: {
                            //console.log("switch 1 onCheckedChanged")
                            switch1.text = switch1.checked ? "进气阀开":"进气阀关"

                            if(switch1.checked){
                                testService.valve_open(TestService.Valve_ID_INTAKE)
                            }else{
                                testService.valve_close(TestService.Valve_ID_INTAKE)
                            }


                        }
                        onClicked: {
                            //console.log("switch 1 clicked")

                        }
                    }

                    UserSwitch{
                        id:switch2
                        text: "真空阀关"
                        onCheckedChanged: {
                            switch2.text = switch2.checked ? "真空阀开":"真空阀关"

                            if(switch2.checked){
                                testService.valve_open(TestService.Valve_ID_VACUUM)
                            }else{
                                testService.valve_close(TestService.Valve_ID_VACUUM)
                            }

                        }
                        onClicked: {

                        }
                    }
                    UserSwitch{
                        id:switch3
                        text: "泄气阀关"
                        onCheckedChanged: {
                            switch3.text = switch3.checked ? "泄气阀开":"泄气阀关"

                            if(switch3.checked){
                                testService.valve_open(TestService.Valve_ID_VENT)
                            }else{
                                testService.valve_close(TestService.Valve_ID_VENT)
                            }
                        }

                        onClicked: {

                        }
                    }


                    UserSwitch{
                        id:switch4
                        text: "全部关"
                        onCheckedChanged: {
                            switch4.text = switch4.checked ? "全部开":"全部关"
                            if(switch4.checked){
                                testService.valve_open(TestService.Valve_ID_All)
                            }else{
                                testService.valve_close(TestService.Valve_ID_All)
                            }
                        }

                        onClicked: {

                        }
                    }


                }


            }

            DynamicGroupBox{
                id:adjust_control
                title: "电动球阀控制"

                width: 800
                height: 100


                Row{
                        anchors.verticalCenter: parent.verticalCenter
                        anchors.left: parent.left

                        spacing: 20

                        Text {
                            id: text1
                            //anchors.top: parent.top
                            //anchors.bottom: parent.bottom
                            //anchors.centerIn: parent
                            anchors.verticalCenter: parent.verticalCenter
                            text: qsTr("比例球阀：")
                            font.pixelSize: 20

                        }

                        UserButton {
                            id: adjustbutton;
                            text: "设置开度";
                            font.pixelSize: 20

                            anchors.verticalCenter: parent.verticalCenter

                            width: 120
                            height: 45

                            onClicked: {
                                var num = Number(position.text)
                                if((num >= 0) && (num <= 1000)){
                                    if(false === testService.ballvalve_control(position.text)){
                                        log.show("比例调节失败！！！")
                                    }
                                }else{
                                    console.log("input position invalid")
                                    log.show("开度值非法， 请重新输入")
                                }

                            }
                        }


                        InputLine{
                            id:position
                            paraName: "开度值(0--1000):"
                            paraLength: 60

                            anchors.verticalCenter: parent.verticalCenter

                            validator:RegExpValidator {
                                regExp: /[0-9]*/
                            }
                            onTextChanged: {
                                //root.manufacture = text
                            }
                        }

                        UserButton {
                            id: status_button;
                            text: "获取状态";
                            font.pixelSize: 20

                            anchors.verticalCenter: parent.verticalCenter

                            width: 120
                            height: 45

                            onClicked: {

                                var jsontmp = testService.ballvalve_status()
                                status.text = jsontmp.status
                                value.text = jsontmp.position
                            }
                        }

                        Text {
                            id: status
                            //anchors.top: parent.top
                            //anchors.bottom: parent.bottom
                            //anchors.centerIn: parent
                            anchors.verticalCenter: parent.verticalCenter
                            text: ""
                            font.pixelSize: 16

                        }

                        Text {
                            id: value
                            //anchors.top: parent.top
                            //anchors.bottom: parent.bottom
                            //anchors.centerIn: parent
                            anchors.verticalCenter: parent.verticalCenter
                            text: ""
                            font.pixelSize: 16

                        }


                    }

            }

            DynamicGroupBox{
                id:pressure
                title: "实时压力"

                width: 800
                height: 100


                Row{
                    //anchors.fill: parent
                    //anchors.verticalCenter: parent.verticalCenter

                    anchors.verticalCenter: parent.verticalCenter
                    //anchors.left: parent.left
                    layoutDirection: Qt.LeftToRight

                    spacing: 20


                    Timer {
                        id: timer;
                        interval: 1000;//设置定时器定时时间为500ms,默认1000ms
                        repeat: true //是否重复定时,默认为false
                        running: false //是否开启定时，默认是false，当为true的时候，进入此界面就开始定时
                        triggeredOnStart: false // 是否开启定时就触发onTriggered，一些特殊用户可以用来设置初始值。
                        onTriggered: {
                            lcd_pressure.text = testService.read_PressureValue()
                        }
                        //restart ,start,stop,定时器的调用方式，顾名思义

                    }

                    UserButton {
                    id: readbutton;
                    text: "开始";
                    font.pixelSize: 20
                    //anchors.verticalCenter: parent.verticalCenter

                    implicitWidth: 120
                    implicitHeight: 45

                    onEnabledChanged: {
                        if(timer.running){
                            timer.stop()
                            text= "开始"
                        }else{
                            //timer.start()
                            //text= "停止"
                         }
                    }

                    onClicked: {

                        if(timer.running){
                            timer.stop()
                            text= "开始"
                        }else{
                            timer.start()
                            text= "停止"
                         }

                        //lcd_pressure.text = pressvalue.toFixed(3).toString()
                    }
                }

                    Rectangle{
                        id:pressureWindow
                        //anchors.fill: parent
                        //anchors.verticalCenter: parent.verticalCenter
                        width: 120
                        height: 40
                        //color: "white"
                        radius: 4

                        LCDNumber{
                             id:lcd_pressure
                             anchors.centerIn: parent
                             //anchors.verticalCenter: parent.verticalCenter
                             text: "5.00"
                             fontsize: 40
                        }

                    }

                    Text {
                        id: kpa
                        //anchors.left: pressureWindow.right
                        anchors.verticalCenter: parent.verticalCenter
                        text: qsTr("kPa")
                        //font: 40
                        font.bold: true
                        font.pixelSize: 25
                    }

                    UserButton {
                    id: zerobutton;
                    text: "传感器标零";
                    font.pixelSize: 20
                    //anchors.verticalCenter: parent.verticalCenter

                    implicitWidth: 120
                    implicitHeight: 45

                    onClicked: {
                         testService.pressure_zero()
                    }
                }

                }

            }

            /*
            DynamicGroupBox{
                id:selftest
                title: "系统自测"

                width: 800
                height: 100

                Row{
                    anchors.verticalCenter: parent.verticalCenter
                    anchors.left: parent.left

                    spacing: 30

                    UserButton {
                        id: selftest_button;
                        text: "开始";
                        font.pixelSize: 20

                        //height: 40
                        //width: 160
                        implicitWidth: 120
                        implicitHeight: 45
                        onClicked: {
                            //var str_number = voltage.text
                            if(false == bSelfTest_Started){
                                //bSelfTest_Started = true;
                                //selftest_result.text= "测试通过"
                                if(timer.running){
                                    timer.stop()
                                }

                                var ret = testService.tselfstart(0, 0)
                                if(ret){
                                    bSelfTest_Started = true;
                                    readbutton.enabled = false;
                                    adjustbutton.enabled = false;
                                    text = "停止"
                                    selfTest_State_str=""
                                    selfTest_Result_str=""
                                    //timer.start()
                                }else{

                                }

                            }else{
                                var ret1 = testService.tstop(0);
                                if(ret1){
                                    bSelfTest_Started = false;
                                    //selftest_result.text = ""
                                    text = "开始"

                                    readbutton.enabled = true;
                                    adjustbutton.enabled = true;
                                    //timer.stop()
                                }


                            }

                        }
                    }



                    Text {
                        id: selftest_state
                        anchors.verticalCenter: parent.verticalCenter
                        text: qsTr("自测状态：")+selfTest_State_str
                        font.pixelSize: 20

                    }

                    Text {
                        id: selftest_result
                        anchors.verticalCenter: parent.verticalCenter
                        text: qsTr("自测结果：")+selfTest_Result_str
                        font.pixelSize: 20

                    }

                    Text {
                        id: selftest_Detail
                        anchors.verticalCenter: parent.verticalCenter
                        text: selfTest_Detail_str
                        font.pixelSize: 16

                        //lineHeight: Text.ProportionalHeight //设置行间距
                        lineHeight: 0.5 //行间距比例 最大 1
                        wrapMode: Text.WordWrap //换行

                        fontSizeMode: Text.Fit //固定 Text 显示大小->字体自动变化的模式选中还有几种看文档
                        minimumPixelSize: 10 //设置自动变化最小字体大小

                    }


                }

            }

            */

            }



            }
      //  }
    }
