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

Item{
    id: root
    //anchors.fill: parent
    //width:1024
    //height: 768
    //anchors.centerIn: parent

    property var pagetype:rootitem.Page_Type_Settings

    property  string title_str:"信息录入"
    property string standard_name:"国标"
    property string company:""
    property string licenseplate: ""
    property int compartment_num: 1
    property int total_volume: 0
    property var volume_arry: [0,0,0,0,0,0,0,0]


    function update_standard(){
        var jsonObj = StandardManager.get_names();

         lmd.clear()
        for(var i=0; i<jsonObj.num; i++){
            //console.log("name ===="+jsonArry.names[i]);
            lmd.append({text:jsonObj.names[i]})
        }

        standard_name_comboBox.currentIndex = 0
    }

    function caculate_volume(){
        if(root.total_volume > 0 && root.compartment_num >0){
            var vol = root.total_volume/root.compartment_num;
            var volume = parseInt(vol)
            if(1 == root.compartment_num){
                rectvolume_1.text=volume
                rectvolume_2.text=0
                rectvolume_3.text=0
                rectvolume_4.text=0
                rectvolume_5.text=0
                rectvolume_6.text=0
                rectvolume_7.text=0
                rectvolume_8.text=0

            }else if(2 == root.compartment_num){
                rectvolume_1.text=volume
                rectvolume_2.text=root.total_volume-volume
                rectvolume_3.text=0
                rectvolume_4.text=0
                rectvolume_5.text=0
                rectvolume_6.text=0
                rectvolume_7.text=0
                rectvolume_8.text=0
            }else if(3 == root.compartment_num){
                rectvolume_1.text=volume
                rectvolume_2.text=volume
                rectvolume_3.text=root.total_volume-volume*2
                rectvolume_4.text=0
                rectvolume_5.text=0
                rectvolume_6.text=0
                rectvolume_7.text=0
                rectvolume_8.text=0
            }else if(4 == root.compartment_num){
                rectvolume_1.text=volume
                rectvolume_2.text=volume
                rectvolume_3.text=volume
                rectvolume_4.text=root.total_volume-volume*3
                rectvolume_5.text=0
                rectvolume_6.text=0
                rectvolume_7.text=0
                rectvolume_8.text=0
            }else if(5 == root.compartment_num){
                rectvolume_1.text=volume
                rectvolume_2.text=volume
                rectvolume_3.text=volume
                rectvolume_4.text=volume
                rectvolume_5.text=root.total_volume-volume*4
                rectvolume_6.text=0
                rectvolume_7.text=0
                rectvolume_8.text=0
            }else if(6 == root.compartment_num){
                rectvolume_1.text=volume
                rectvolume_2.text=volume
                rectvolume_3.text=volume
                rectvolume_4.text=volume
                rectvolume_5.text=volume
                rectvolume_6.text=root.total_volume-volume*5
                rectvolume_7.text=0
                rectvolume_8.text=0
            }else if(7 == root.compartment_num){
                rectvolume_1.text=volume
                rectvolume_2.text=volume
                rectvolume_3.text=volume
                rectvolume_4.text=volume
                rectvolume_5.text=volume
                rectvolume_6.text=volume
                rectvolume_7.text=root.total_volume-volume*6
                rectvolume_8.text=0
            }else if(8 == root.compartment_num){
                rectvolume_1.text=volume
                rectvolume_2.text=volume
                rectvolume_3.text=volume
                rectvolume_4.text=volume
                rectvolume_5.text=volume
                rectvolume_6.text=volume
                rectvolume_7.text=volume
                rectvolume_8.text=root.total_volume-volume*7
            }
        }
    }

    function check_invalid(){
        var valid = true
        if(0 == licenseplate.length){
            valid = false
            log.show("车牌不能为空")
            return valid
        }

        if(0 == company.length){
            valid = false
            log.show("车辆单位不能为空")
            return valid
        }

        if(0 == total_volume){
            valid = false
            log.show("容积不能为空")
            return valid
        }
        if(0 == compartment_num){
            valid = false
            log.show("仓数不能为空")
            return valid
        }

        var all_volume = 0
        for(var i=0; i<compartment_num; i++){
            all_volume = all_volume + volume_arry[i]
        }

        if(all_volume !== total_volume){
            valid = false
            log.show("体积总容量与每仓数据不匹配")
        }

        return valid
    }



   // Component{
    //    id:componet
        DynamicGroupBox{
            id:groupbox

            //width: 1020
            //height: 760

            anchors.centerIn: parent
            anchors.fill: parent

            title: title_str

/*
            function show(){
                animationShow.running = true
            }

            function exit(){
                //BreatheTestJs.exit = true
                animationExit.running = true
                //startStopButton.running = false
                //stopTimer.start()
            }

            //加载任务动画效果
            NumberAnimation {
                id: animationShow
                target: groupbox
                property: "x"
                from: groupbox.x
                to: 0
                duration: 500
            }
            NumberAnimation {
                id: animationExit
                target: groupbox
                property: "x"
                from: groupbox.x
                to: 1024
                duration: 500
            }

*/
            ColumnLayout{
                spacing:2

                Rectangle{
                    Layout.alignment: Qt.AlignCenter
                    //Layout.fillWidth: true
                    color: "transparent"

                    Layout.preferredHeight: 600
                    Layout.preferredWidth: 1240

                    Row{

                        anchors.centerIn: parent
                        spacing:10

                        Column{
                            spacing:20

                        DynamicGroupBox{
                            id:standard_select
                            title: "标准选择："
                            //anchors.centerIn: parent

                            width: 360
                            height: 160

                            Column{
                                spacing:10
                               // anchors.centerIn: parent

                                Rectangle{
                                    width:320
                                    height: 30
                                    color: "transparent"
                                    Row{
                                        spacing:30
                                        Label{
                                            text: "执行标准"
                                            //font.pixelSize: 24
                                            font.bold: true
                                            font.pixelSize: 20
                                            //color: "red"

                                        }

                                        UserComboBox{
                                           id:standard_name_comboBox
                                           //anchors.fill:parent
                                           implicitWidth: 100
                                           implicitHeight: 30
                                           bgColor:"#D2D5D9"
                                           model: ListModel{
                                                id:lmd
                                           }
                                           onCurrentTextChanged: {
                                               root.standard_name = currentText
                                               console.log("choose standard: "+root.standard_name)
                                           }

                                        }
                                    }

                                }

                                UserButton{
                                       text: "标准管理"
                                       font.pixelSize: 20

                                       implicitHeight: 45
                                       implicitWidth: 120

                                       //Layout.leftMargin: -30
                                       //Layout.topMargin: 10
                                       onClicked: {
                                           var compMainPage = Qt.createComponent("PageStandard.qml")
                                                       .createObject(main_window, {x:0, y:0, width:main_window.width, height:main_window.height});

                                       }
                                }

                            }
/*
                        GridLayout{
                            anchors.centerIn: parent
                            columns: 2
                            rows:2
                            //columnSpacing:  10
                            //rowSpacing: 5
                            //Layout.rowSpan: 10
                            Label{
                                text: "执行标准"
                                //width:120
                                //height: 40
                                //font.pixelSize: 24
                                font.bold: true
                                font.pixelSize: 20
                                //color: "red"

                            }

                            //Rectangle{
                            //    id:rectcom1
                            //    implicitWidth: 100
                            //    implicitHeight:30
                                UserComboBox{
                                   id:standard_name_comboBox
                                   //anchors.fill:parent
                                   implicitWidth: 100
                                   implicitHeight: 30
                                   bgColor:"#D2D5D9"
                                   model: ListModel{
                                        id:lmd
                                   }
                                   onCurrentTextChanged: {
                                       root.standard_name = currentText
                                       console.log("choose standard: "+root.standard_name)
                                   }

                                }

                           // }

                            UserButton{
                                   text: "标准管理"
                                   font.pixelSize: 24

                                   implicitHeight: 120
                                   implicitWidth: 140

                                   //Layout.leftMargin: -30
                                   //Layout.topMargin: 10
                                   onClicked: {
                                       var compMainPage = Qt.createComponent("PageStandard.qml")
                                                   .createObject(main_window, {x:0, y:0, width:main_window.width, height:main_window.height});

                                   }
                            }
                        }
*/
                        }

                        DynamicGroupBox{
                            id:tankinfo
                            title: "罐车信息"

                            width: 360
                            height: 320
                            //anchors.centerIn: parent
                            //Layout.horizontalCenter: parent.horizontalCenter
                            //Layout.verticalCenter: parent.verticalCenter

                        ColumnLayout{

                            anchors.centerIn: parent
                            spacing: 20

                            Column{
                                    spacing: 10
                                    //Layout.alignment: Qt.AlignLeft

                                    InputLine{
                                        id:manufactureInputLine
                                        paraName: "  车辆单位:"
                                        paraLength: 200
                                        onTextChanged: {
                                            root.company = text
                                        }
                                    }

                                    InputLine{
                                        id:carplate
                                        paraName: "    车牌号:"
                                        paraLength: 200
                                        onTextChanged: {
                                            root.licenseplate = text
                                        }
                                    }

                                    InputLine{
                                        id:t_volume
                                        paraName: "总容积 (L):"
                                        text: "0"
                                        paraLength: 200
                                        validator:RegExpValidator {
                                            regExp: /[0-9]*/
                                        }
                                        onTextChanged: {
                                            if(text.length > 0){
                                                root.total_volume = Number(text)
                                                if(0 == root.compartment_num){
                                                    compartment.text = "1"
                                                }
                                                root.caculate_volume()
                                            }else{
                                                root.total_volume=0
                                            }
                                        }
                                    }

                                    InputLine{
                                        id:compartment
                                        paraName: "  仓    数:"
                                        text: "1"
                                        paraLength: 200

                                        validator:IntValidator {
                                            bottom:1;
                                            top:8
                                        }
                                        onTextChanged: {
                                            if(text.length > 0){
                                                var n = Number(text);
                                                if(n >0 && n<5 ){
                                                    root.compartment_num = Number(text)

                                                    root.caculate_volume()
                                                    //console.log("compartment:"+text)
                                                }else{
                                                    if(n >4){
                                                        log.show("仓数不能大于4")
                                                        text="4"
                                                    }
                                                    if(n === 0){
                                                        text="1"
                                                    }
                                                }
                                            }else{
                                                root.compartment_num=0;
                                            }
                                        }
                                    }

                                }

                            GridLayout{
                            //anchors.centerIn: parent
                            columns: 2
                            rows:4
                            columnSpacing:  20
                            rowSpacing: 5

                            //Layout.rowSpan: 5

                            InputLine{
                                id:rectvolume_1
                                paraName: "1仓:"
                                text: "0"
                                paraLength: 80
                                validator:RegExpValidator {
                                    regExp: /[0-9]*/
                                }
                                onTextChanged: {
                                    root.volume_arry[0] = Number(text)
                                    //console.log("1:volume[%d]", root.volume_arry[0])
                                }
                            }

                            InputLine{
                                id:rectvolume_2
                                paraName: "2仓:"
                                text: "0"
                                paraLength: 80
                                validator:RegExpValidator {
                                    regExp: /[0-9]*/
                                }
                                onTextChanged: {
                                    root.volume_arry[1] = Number(text)
                                }
                            }
                            InputLine{
                                id:rectvolume_3
                                paraName: "3仓:"
                                text: "0"
                                paraLength: 80
                                validator:RegExpValidator {
                                    regExp: /[0-9]*/
                                }
                                onTextChanged: {
                                    root.volume_arry[2] = Number(text)
                                }
                            }
                            InputLine{
                                id:rectvolume_4
                                paraName: "4仓:"
                                text: "0"
                                paraLength: 80
                                validator:RegExpValidator {
                                    regExp: /[0-9]*/
                                }
                                onTextChanged: {
                                    root.volume_arry[3] = Number(text)
                                }
                            }

                            InputLine{
                                id:rectvolume_5
                                paraName: "5仓:"
                                text: "0"
                                paraLength: 60
                                visible: false
                                validator:RegExpValidator {
                                    regExp: /[0-9]*/
                                }
                                onTextChanged: {
                                    root.volume_arry[4] = Number(text)
                                }
                            }
                            InputLine{
                                id:rectvolume_6
                                paraName: "6仓:"
                                text: "0"
                                paraLength: 60
                                visible: false
                                validator:RegExpValidator {
                                    regExp: /[0-9]*/
                                }
                                onTextChanged: {
                                    root.volume_arry[5] = Number(text)
                                }
                            }
                            InputLine{
                                id:rectvolume_7
                                paraName: "7仓:"
                                text: "0"
                                paraLength: 60
                                visible: false
                                validator:RegExpValidator {
                                    regExp: /[0-9]*/
                                }
                                onTextChanged: {
                                    root.volume_arry[6] = Number(text)
                                }
                            }
                            InputLine{
                                id:rectvolume_8
                                paraName: "8仓:"
                                text: "0"
                                paraLength: 60
                                visible: false
                                validator:RegExpValidator {
                                    regExp: /[0-9]*/
                                }
                                onTextChanged: {
                                    root.volume_arry[7] = Number(text)
                                }
                            }

                        }

                        }
                        }

                        }
                        DynamicGroupBox{
                            id:visual_inspection
                            title: "外观检查"
                            //anchors.centerIn: parent

                            width: 820
                            height: 500

                            Row{
                                spacing:10
                                DynamicGroupBox{
                                    id:visual_VRS
                                    title: "油气回收系统"
                                    titleFontPixel:18
                                    //anchors.centerIn: parent

                                    width: 390
                                    height: 450

                                    UserTableView{
                                        id:table_vrs
                                        table_name: "vrs"
                                        anchors.fill:parent

                                    }

                                }

                                DynamicGroupBox{
                                    id:visual_BLS
                                    title: "底部装油系统"
                                    titleFontPixel:18
                                    //anchors.centerIn: parent

                                    width: 390
                                    height: 450

                                    UserTableView{
                                        id:table_bls
                                        table_name: "bls"
                                        anchors.fill:parent

                                    }
                                }

                            }
                        }
                    }

                }

                Rectangle{
                    Layout.alignment: Qt.AlignBottom
                    Layout.fillWidth: true
                    color: "transparent"
                    Layout.preferredHeight: 100
                    Layout.preferredWidth: 300

                    UserButton {
                        id: prev_step
                        text: "返回首页"
                        height: 60
                        width: 160
                        font.pixelSize: 24
                        anchors.left: parent.left;
                        anchors.leftMargin: 40;
                        anchors.bottom: parent.bottom;
                        anchors.bottomMargin: 10;

                        onClicked: myLoder.sourceComponent = null // 切换显示主页面
                    }

                    UserButton {
                        id: self_test
                        text: "系统自测"
                        height: 60
                        width: 160
                        font.pixelSize: 24
                        anchors.left: prev_step.right;
                        anchors.leftMargin: 540;
                        anchors.bottom: parent.bottom;
                        anchors.bottomMargin: 10;

                        onClicked:{
                            //var varPage = Qt.createComponent("PageLeakageInput.qml")
                            //            .createObject(main_window, {x:0, y:0, width:main_window.width/2, height:main_window.height/2});

                            var compMainPage = Qt.createComponent("PageSelfTest.qml")
                                        .createObject(main_window, {x:0, y:0, width:main_window.width, height:main_window.height});


                            //popupCenter.raiseItem = com_leakage
                            //popupCenter.open()

                        }
                    }

                    UserButton {

                        id: next_step;
                        text: "进入检测";
                        //visible: false
                        font.pixelSize: 24
                        anchors.right: parent.right;
                        anchors.rightMargin: 60;
                        anchors.bottom: parent.bottom;
                        anchors.bottomMargin: 10;
                        height: 60
                        width: 160

                        onClicked: {
                            //log.show(utr.state)
                            //console.log("state ==== 1111");

                            if(root.check_invalid()){
                                var infoArry = [];
                                standard_name = standard_name_comboBox.currentText;
                                infoArry.push(standard_name)
                                infoArry.push(licenseplate);
                                infoArry.push(company);
                                infoArry.push(total_volume);
                                infoArry.push(compartment_num);
                                for(var i=0; i<4; i++){
                                    infoArry.push(volume_arry[i])
                                }

                                var ret = testService.setTankinfo(infoArry);
                                if(false === ret){
                                    log.show("设置罐车参数不成功")
                                }else{

                                    for(var n=0; n<4; n++){
                                        switch(n){
                                        case 0:
                                            page_recoveryvalve1.volume = volume_arry[n]
                                            page_recoveryvalve1.organize = company
                                            page_recoveryvalve1.carplate = licenseplate
                                            page_recoveryvalve1.total_volume = total_volume
                                            break
                                        case 1:
                                            page_recoveryvalve2.volume = volume_arry[n]
                                            page_recoveryvalve2.organize = company
                                            page_recoveryvalve2.carplate = licenseplate
                                            page_recoveryvalve2.total_volume = total_volume
                                            break
                                        case 2:
                                            page_recoveryvalve3.volume = volume_arry[n]
                                            page_recoveryvalve3.organize = company
                                            page_recoveryvalve3.carplate = licenseplate
                                            page_recoveryvalve3.total_volume = total_volume
                                            break
                                        case 3:
                                            page_recoveryvalve4.volume = volume_arry[n]
                                            page_recoveryvalve4.organize = company
                                            page_recoveryvalve4.carplate = licenseplate
                                            page_recoveryvalve4.total_volume = total_volume
                                            break
                                            /*
                                        case 4:
                                            page_recoveryvalve5.volume = volume_arry[n]
                                            page_recoveryvalve5.organize = company
                                            page_recoveryvalve5.carplate = licenseplate
                                            page_recoveryvalve5.total_volume = total_volume
                                            break
                                        case 5:
                                            page_recoveryvalve6.volume = volume_arry[n]
                                            page_recoveryvalve6.organize = company
                                            page_recoveryvalve6.carplate = licenseplate
                                            page_recoveryvalve6.total_volume = total_volume
                                            break
                                        case 6:
                                            page_recoveryvalve7.volume = volume_arry[n]
                                            page_recoveryvalve7.organize = company
                                            page_recoveryvalve7.carplate = licenseplate
                                            page_recoveryvalve7.total_volume = total_volume
                                            break
                                        case 7:
                                            page_recoveryvalve8.volume = volume_arry[n]
                                            page_recoveryvalve8.organize = company
                                            page_recoveryvalve8.carplate = licenseplate
                                            page_recoveryvalve8.total_volume = total_volume
                                            break
*/
                                        }

                                    }

                                    swipeView.changePages(compartment_num)
                                    swipeView.incrementCurrentIndex();
                                    //swipeView.incrementCurrentIndex();
                                }

                            }else{
                                //log.show("体积或仓数，不正确")
                            }

                        }
                    }


                }
            }


            }
      //  }

        Component.onCompleted: {

            var jsonObj = StandardManager.get_names()

             lmd.clear()
            for(var i=0; i<jsonObj.num; i++){
                //console.log("name ===="+jsonArry.names[i]);
                lmd.append({text:jsonObj.names[i]})
            }

            standard_name_comboBox.currentIndex = 0

            var jsonObj_vrs = testService.get_vrs_items()
            for(var j=0; j<jsonObj_vrs.count; j++){
                table_vrs.add_item(jsonObj_vrs.item[j])
            }

            var jsonObj_bls = testService.get_bls_items()
            for( j=0; j<jsonObj_bls.count; j++){
                table_bls.add_item(jsonObj_bls.item[j])
            }

        }
    }
