<%@ page contentType="text/html;charset=UTF-8" %>
<script type="text/javascript">
		$(document).ready(function() {
		//zTree初始化
			$.getJSON("${ctx}/sg/managementCenter/constructionProject/bootstrapTreeTable",function(data){
                jstree(data);
                dateTimeValue('1');
			});
            $('#selectId').click(function () {
                var object = $('#jstree').treeview('getChecked');
                if (object.length<1){
                    jp.error("请选择项目！")
                    return false;
                }
                var str = "";
                for (var  i = 0; i < object.length; i++) {
                    str+=object[i].id+",";
                }
                var sureStr  = str.substring(0,str.length-1);
                $("#parentIds").val(sureStr);
                add(sureStr);
            });
            //搜索
            var findSearchableNodes = function() {
                return $('#jstree').treeview('search', [ $.trim($('#input-search').val()), { ignoreCase: false, exactMatch: false } ]);
            };
            var search = function(e) {
                var pattern = $.trim($('#input-search').val());
                var options = {
                    ignoreCase: $('#chk-ignore-case').is(':checked'),
                    exactMatch: $('#chk-exact-match').is(':checked'),
                    revealResults: $('#chk-reveal-results').is(':checked')
                };
                $('#jstree').treeview('search', [ $.trim($('#input-search').val()), { ignoreCase: false, exactMatch: false } ]);
                var results = $('#jstree').treeview('search', [ pattern, options ]);
            }
            $('#btn-search').on('click', search);
            $('#btn-clear-search').on('click', function (e) {
                $('#jstree').treeview('clearSearch');
                $('#input-search').val('');
                $('#jstree').treeview('collapseAll', {
                    silent : false//设置初始化节点关闭
                });
            });
            $.getJSON("${ctx}/sg/managementCenter/constructionProject/treeData",function(data){
                jstreeTime(data)
            });
            //搜索
            var search1 = function(e) {
                var pattern = $.trim($('#input-search1').val());
                var options = {
                    ignoreCase: $('#chk-ignore-case').is(':checked'),
                    exactMatch: $('#chk-exact-match').is(':checked'),
                    revealResults: $('#chk-reveal-results').is(':checked')
                };
                $('#jstreeTime').treeview('search', [ $.trim($('#input-search1').val()), { ignoreCase: false, exactMatch: false } ]);
                var results = $('#jstreeTime').treeview('search', [ pattern, options ]);
            }
            $('#btn-search1').on('click', search1);
            $('#btn-clear-search1').on('click', function (e) {
                $('#jstreeTime').treeview('clearSearch');
                $('#input-search1').val('');
                $('#jstreeTime').treeview('collapseAll', {
                    silent : false//设置初始化节点关闭
                });
            });
            $('#selectId1').click(function () {
                var object = $('#jstreeTime').treeview('getChecked');
                if (object.length<1){
                    jp.error("请选择项目！");
                    return false;
                }
                var str = "";
                for (var  i = 0; i < object.length; i++) {
                    str+=object[i].id+",";
                }
                var sureStr  = str.substring(0,str.length-1);
                minus(sureStr);
            })
        })
    //安排计划时间
    function add(ids){
        var dateTimeId = $("#dateTimeId").val();
        if (null!=dateTimeId && dateTimeId !=""){
            jp.post("${ctx}/sg/managementCenter/constructionProject/unplanned",{'ids':ids,'time':dateTimeId},function(data){
                if(data.success){
                    jstreeTime(data.mapList);
                    refreshjstree();
                }else{
                    jp.error(data.message);
                }
            })
        }
    }
    //取消计划时间
    function minus(ids) {
        var dateTimeId = $("#dateTimeId").val();
        if (null!=dateTimeId && dateTimeId !=""){
            jp.post("${ctx}/sg/managementCenter/constructionProject/unplannedMinus",{'ids':ids,'time':dateTimeId},function(data){
                if(data.success){
                    refreshjstree();
                    jstreeTime(data.mapList);
                }else{
                    jp.error(data.message);
                }
            })
        }
    }
    function getIds() {
        return $("#parentIds").val();
    }
    function refreshjstreeTime() {
        $.getJSON("${ctx}/sg/managementCenter/constructionProject/treeData",function(data){
            $('#jstreeTime').treeview({
                data: data,
                levels: 1,
                highlightSelected:false,
                // multiSelect:true,
                // showCheckbox:true,
                color:'blue',
                onNodeSelected: function(event, treeNode) {
                    var id = treeNode.id;
                    jp.openSaveDialog('编辑安排计划项目', "${ctx}/sg/managementCenter/constructionProject/formPlan?id=" + id, '800px', '500px');
                },
            });
        });
    }
    function refreshjstree() {
        $.getJSON("${ctx}/sg/managementCenter/constructionProject/bootstrapTreeTable",function(data){
            $('#jstree').treeview({
                data: data,
                levels: 1,
                highlightSelected:false,
                // multiSelect:true,
                showCheckbox:true
            });
        });
    }
    function jstree(date) {
        $('#jstree').treeview({
            data: date,
            levels: 1,
            highlightSelected:false,
            // multiSelect:true,
            showCheckbox:true,
            // onNodeChecked : function(event, data) {
            //     selected(data);
            // },
            // onNodeUnchecked : function(event, node){
            //     unSelected(node);
            // },
        });
    }
    function jstreeTime(date) {
        $('#jstreeTime').treeview({
            data: date,
            levels: 1,
            highlightSelected:false,
            showCheckbox:true,
            onNodeSelected: function(event, treeNode) {
                var id = treeNode.id;
                jp.openSaveDialog('编辑安排计划项目', "${ctx}/sg/managementCenter/constructionProject/formPlan?id=" + id, '800px', '500px');
            },

        });
    }
    function dateTimeValue(value) {
        if (value != '1'){
            $("#dateTimeId").val(value);
        }else {
            var date = new Date();
            var str = date.toISOString();
            $("#dateTimeId").val( str.substring(0,7));
        }
    }

    function selected(data){
        if(data.nodeId == undefined){
            for (var i = 0; i < data.nodes.length; i++) {
                $("#jstree").treeview('checkNode', [ data.nodes[i], {silent: true}]);
            }
        }else{
            var parentNode = $('#jstree').treeview('getParent', data.nodeId);
            $("#jstree").treeview('checkNode', [ parentNode, {silent: true}]);
        }
    }
    function unSelected(node){
        if(node.id == undefined){
            for (var i = 0; i < node.nodes.length; i++) {
                $("#jstree").treeview('uncheckNode', [ node.nodes[i], {silent: true}]);
            }
        }else{
            var parentNode = $('#jstree').treeview('getParent', node.nodeId);
            var num;
            for (var i = 0; i < parentNode.nodes.length; i++) {
                if(parentNode.nodes[i].state.checked == true){
                    num=1;
                }
            }
            if(num!=1){
                $("#jstree").treeview('uncheckNode', [ parentNode, {silent: true}]);
            }
            console.log(parentNode);
        }
    }
</script>
