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: standard_root
    //property var pagetype:rootitem.Page_Type_Testing
    property  string title_str:"标准管理"
    property int para_len: 80

    property string stdname_str:""
    property double std_PTV: 0.0
    property double std_NTV: 0.0
    property double std_interval: 0

    property var volume: [0,0,0,0]
    property var s_threshold: [0.0, 0.0, 0.0, 0.0]
    property var v_threshold: [0.0, 0.0, 0.0, 0.0]


    function check_volume(){

        for(var i=0; i<4-1; i++){
            var v = volume[i]
            for(var j=i+1; j<4; j++){
                if(v < volume[j]){
                    return false
                }
            }
        }

        return true

    }

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

        lmd.clear()

        for(var i=0; i<jsonObj.num; i++){
            lmd.append({text:jsonObj.names[i]})
        }

    }

    function update_detail(){
        var jsontmp = standard_listView.model.get(standard_listView.currentIndex)
        //console.log("the index = "+index+", standardname = "+ jsontmp.text);
        jsontmp = StandardManager.get_standard(jsontmp.text);
        //console.log("the count = " + jsontmp.count +", json str:"+ JSON.stringify(jsontmp));

        std_name.text = jsontmp.name
        std_positive_target.text = jsontmp.ptv
        std_negative_target.text = jsontmp.ntv
        std_intervaltime.text = jsontmp.intervals

        if(jsontmp.count === 1){
           level1_v.text = jsontmp.item[0].volume
           level1_st.text = jsontmp.item[0].s_threshold
           level1_vt.text = jsontmp.item[0].v_threshold

        }else if(jsontmp.count === 2){
            level1_v.text = jsontmp.item[0].volume
            level1_st.text = jsontmp.item[0].s_threshold
            level1_vt.text = jsontmp.item[0].v_threshold

            level2_v.text = jsontmp.item[1].volume
            level2_st.text = jsontmp.item[1].s_threshold
            level2_vt.text = jsontmp.item[1].v_threshold

        }else if(jsontmp.count === 3){
            level1_v.text = jsontmp.item[0].volume
            level1_st.text = jsontmp.item[0].s_threshold
            level1_vt.text = jsontmp.item[0].v_threshold

            level2_v.text = jsontmp.item[1].volume
            level2_st.text = jsontmp.item[1].s_threshold
            level2_vt.text = jsontmp.item[1].v_threshold

            level3_v.text = jsontmp.item[2].volume
            level3_st.text = jsontmp.item[2].s_threshold
            level3_vt.text = jsontmp.item[2].v_threshold

        }else if(jsontmp.count === 4){
            level1_v.text = jsontmp.item[0].volume
            level1_st.text = jsontmp.item[0].s_threshold
            level1_vt.text = jsontmp.item[0].v_threshold

            level2_v.text = jsontmp.item[1].volume
            level2_st.text = jsontmp.item[1].s_threshold
            level2_vt.text = jsontmp.item[1].v_threshold

            level3_v.text = jsontmp.item[2].volume
            level3_st.text = jsontmp.item[2].s_threshold
            level3_vt.text = jsontmp.item[2].v_threshold

            level4_v.text = jsontmp.item[3].volume
            level4_st.text = jsontmp.item[3].s_threshold
            level4_vt.text = jsontmp.item[3].v_threshold

            //console.log("the index = "+index+", tvolume[0] = "+ tvolume +", volume[0]"+ volume[0]);

        }
    }

    DynamicGroupBox{
        id:groupbox_all
        anchors.centerIn: parent
        anchors.fill: parent

        title: title_str

        Rectangle{
            id:rect_items
            //anchors.centerIn: parent
            //width: 1000
            //height: 700

            width: groupbox_all.width
            height: groupbox_all.height

            x:20
            y: !inputPanel.active ? 0 : Math.min(0, main_window.height - inputPanel.height - activeFocusItemBottom)
            Behavior on y {
                NumberAnimation {
                    duration: 250
                    easing.type: Easing.InOutQuad
                }
            }


            Column {
            id: column

            //width: 600
            //height:600

            spacing: 40
            anchors.centerIn: parent

            Row {
                id: row
                //width: 600
                //height: 500

                 spacing: 30

                 DynamicGroupBox{
                     id:groupbox_list

                     width: 150
                     height: 400
                     //anchors.centerIn: parent
                     //anchors.fill: parent

                     title: "标准列表"

                     //Rectangle{
                     //    id:rect_list
                     //    width: 120
                     //    height: 400
                     //    anchors.margins: 10
                     //    color: "white"
                     //    radius: 6
                     //    border.color: "black"

                         ListView {
                             //anchors.fill: parent
                             anchors.centerIn: parent

                             id: standard_listView
                             width: 120
                             height: 360

                             model: ListModel{
                                id: lmd
                             }
                             delegate: ItemDelegate {
                                 text: modelData
                                 font.bold: true
                                 font.pixelSize: 24

                                 highlighted: ListView.isCurrentItem

                                 Binding {
                                      target: background
                                      property: "color"
                                      //value: highlighted ? "#81A3CF" : "#B3B2B2"
                                      value:highlighted ? "#C9DDF8" : "#B3B2B2"
                                     }

                                 //text: model.index + (highlighted ? " [highlighted]" : "")
                                 //contentItem.color: "black"

                                 Binding {
                                      target: contentItem
                                      property: "color"
                                      value: "black"
                                     }

                                 onClicked:{

                                     standard_listView.currentIndex = index
                                     update_detail()
                                 }
                             }
                         }

                    // }

                 }



                Rectangle{
                    id: rect_ops
                    width: 180
                    height: 400

                    //anchors.margins: 10
                    color: "transparent"
                    //radius: 6
                    //border.color: "black"

                    Column{
                        //anchors.fill: parent
                        anchors.centerIn: parent
                        spacing: 20

                        UserButton {
                            id: addButton
                            text: "添加/修改"
                            width: 180
                            height: 60
                            onClicked: {
                                var standardJson={}
                                var itemArrayJson=[]

                                if(check_volume()){

                                    standardJson.name = std_name.text
                                    standardJson.ptv = std_PTV
                                    standardJson.ntv = std_NTV
                                    standardJson.intervals = std_interval
                                    standardJson.count = 4
                                    for(var i=0; i<4; i++){
                                        var item={}
                                        item.volume = volume[i]
                                        item.s_threshold=s_threshold[i]
                                        item.v_threshold=v_threshold[i]
                                        itemArrayJson[i] = item
                                    }
                                    standardJson.item = itemArrayJson
                                    StandardManager.add_standard(JSON.stringify(standardJson) );

                                    update_listView()


                                }else{
                                    log.show("容积必须从大到小降序排列")
                                }

                            }
                        }

                        UserButton {
                            id: removeButton
                            text: "删除"
                            width: 180
                            height: 60
                            onClicked: {
                                var jsontmp = standard_listView.model.get(standard_listView.currentIndex)
                                StandardManager.remove_standard(jsontmp.text);
                                update_listView()

                            }
                        }

                    }
                }

                DynamicGroupBox{
                    id:groupbox_detail

                    width: 640
                    height: 400
                    //anchors.centerIn: parent
                    //anchors.fill: parent

                    title: "标准详情"
                    Column{
                        spacing:10

                        InputLine{
                            id:std_name
                            paraName: "标准名称："
                            paraLength: 160
                            onTextChanged: {
                                stdname_str=text
                            }
                        }

                        Row{
                            spacing:30

                            InputLine{
                                id:std_positive_target
                                paraName: "正压目标值（kPa）："
                                paraLength: 60
                                validator:RegExpValidator {
                                    regExp: /(-?[0-9]\d*((.\d+)*))*/
                                }
                                onTextChanged: {
                                    std_PTV = Number(text)
                                }
                            }

                            InputLine{
                                id:std_negative_target
                                paraName: "负压目标值(kPa)："
                                paraLength: 60
                                validator:RegExpValidator {
                                    regExp: /(-?[0-9]\d*((.\d+)*))*/
                                }
                                onTextChanged: {
                                    std_NTV = Number(text)
                                }
                            }
                        }

                        InputLine{
                            id:std_intervaltime
                            paraName: "测量间隔（秒）："
                            paraLength: 60
                            validator:RegExpValidator {
                                regExp: /[0-9]*/
                            }
                            onTextChanged: {
                                std_interval = Number(text)
                            }
                        }


                    GridLayout {
                        id: grid
                        columns: 3
                        columnSpacing: 30
                        rows:5
                        rowSpacing: 10

                        Text { text: "单个油仓容积V(L)"; font.bold: true;  font.pointSize: 12; wrapMode: Text.WrapAnywhere; Layout.maximumWidth: 180}
                        Text { text: "油气回收系统压力变动限值（kPa）"; font.bold: true; font.pointSize: 12; wrapMode: Text.WrapAnywhere; Layout.maximumWidth: 180}
                        Text { text: "油气回收阀压力变动限值（kPa）"; font.bold: true; font.pointSize: 12; wrapMode: Text.WrapAnywhere; Layout.maximumWidth: 180}

                        //level 1
                        InputLine{
                            id:level1_v
                            paraName: " V≥ "
                            paraLength: para_len
                            validator:RegExpValidator {
                                regExp: /[0-9]*/
                            }
                            onTextChanged: {
                                volume[0] = Number(text)
                                //console.log("level1 volume[0] == " + volume[0]);
                            }
                        }
                        InputLine{
                            id:level1_st
                            paraName: " ≤ "
                            paraLength: para_len
                            validator:RegExpValidator {
                                regExp: /(-?[0-9]\d*((.\d+)*))*/
                            }
                            onTextChanged: {
                                s_threshold[0] = Number(text)
                                //console.log("level1 s_threshold[0] == " + s_threshold[0]);
                            }
                        }

                        InputLine{
                            id:level1_vt
                            paraName: " ≤ "
                            paraLength: para_len
                            validator:RegExpValidator {
                                regExp: /(-?[0-9]\d*((.\d+)*))*/
                            }
                            onTextChanged: {
                                v_threshold[0] = Number(text)
                            }
                        }

                        //level 2
                        InputLine{
                            id:level2_v
                            paraName: " V≥ "
                            paraLength: para_len
                            validator:RegExpValidator {
                                regExp: /[0-9]*/
                            }
                            onTextChanged: {
                                volume[1] = Number(text)
                            }
                        }
                        InputLine{
                            id:level2_st
                            paraName: " ≤ "
                            paraLength: para_len
                            validator:RegExpValidator {
                                regExp: /(-?[0-9]\d*((.\d+)*))*/
                            }
                            onTextChanged: {
                                s_threshold[1] = Number(text)
                            }
                        }

                        InputLine{
                            id:level2_vt
                            paraName: " ≤ "
                            paraLength: para_len
                            validator:RegExpValidator {
                                regExp: /(-?[0-9]\d*((.\d+)*))*/
                            }
                            onTextChanged: {
                                v_threshold[1] = Number(text)
                            }
                        }

                        //level 3
                        InputLine{
                            id:level3_v
                            paraName: " V≥ "
                            paraLength: para_len
                            validator:RegExpValidator {
                                regExp: /[0-9]*/
                            }
                            onTextChanged: {
                                volume[2] = Number(text)
                            }
                        }
                        InputLine{
                            id:level3_st
                            paraName: " ≤ "
                            paraLength: para_len
                            validator:RegExpValidator {
                                regExp: /(-?[0-9]\d*((.\d+)*))*/
                            }
                            onTextChanged: {
                                s_threshold[2] = Number(text)
                            }
                        }

                        InputLine{
                            id:level3_vt
                            paraName: " ≤ "
                            paraLength: para_len
                            validator:RegExpValidator {
                                regExp: /(-?[0-9]\d*((.\d+)*))*/
                            }
                            onTextChanged: {
                                v_threshold[2] = Number(text)
                            }
                        }

                        //level 4
                        InputLine{
                            id:level4_v
                            paraName: " V≥ "
                            paraLength: para_len
                            validator:RegExpValidator {
                                regExp:/[0-9]*/
                            }
                            text:"0"
                            onTextChanged: {
                                volume[3] = Number(text)
                            }
                        }
                        InputLine{
                            id:level4_st
                            paraName: " ≤ "
                            paraLength: para_len
                            validator:RegExpValidator {
                                regExp: /(-?[0-9]\d*((.\d+)*))*/
                            }
                            onTextChanged: {
                                s_threshold[3] = Number(text)
                            }
                        }

                        InputLine{
                            id:level4_vt
                            paraName: " ≤ "
                            paraLength: para_len
                            validator:RegExpValidator {
                                regExp: /(-?[0-9]\d*((.\d+)*))*/
                            }
                            onTextChanged: {
                                v_threshold[3] = Number(text)
                            }
                        }



                    }

                    }

                }


            }

            UserButton {
                id: exitButton
                text: "退出"
                width: 120
                height: 60
                //anchors.horizontalCenter: parent
                onClicked: {
                    page_set.update_standard()
                    standard_root.destroy()
                }
            }
        }
        }
    }

    Component.onCompleted: {
        update_listView()
        standard_listView.currentIndex = 0;
        update_detail()

    }
}
