<div class="row-fluid">
    <div class="span3">
        <div class="well" style="overflow:hidden;">
            <ul id="treeDemo" name="treeDemo" class="ztree span12" style="height:938px;overflow-y:scroll;overflow-x:auto;"></ul>
        </div>
    </div>

    <div class="span9">
        <div class="span12" style="background:#f5f5f5;padding:15px;margin-bottom:15px;">
            <form action="<?php echo site_url("Keyworlds/edit"); ?>" method="post" id="search-rank-form">
                <div class="input-append">
                    <input class="span5" type="text" id="keyworlds" name="keyworlds" value="<?php echo $current_keyworld->kw_keyworlds; ?>">
                    <input type="hidden" name="kw_id" value="<?php echo $current_keyworld->kw_id; ?>">
                    <input type="hidden" name="kw_is_id" value="<?php echo $current_keyworld->kw_is_id; ?>">
                    <select class="span1" name="status">
                        <option value="1" <?php if($current_keyworld->kw_status==1) echo 'selected'; ?> >发布</option>
                        <option value="0" <?php if($current_keyworld->kw_status==0) echo 'selected'; ?>>不发布</option>
                    </select>
                    <button class="btn" type="button" onclick="javascript:submitForm('search-rank-form');">保存更改</button>
                    <button class="btn" type="button" onclick="javascript:searchkeywords();">搜索关键词</button>
                    <button class="btn" type="button" onclick="javascript:getrank();">查询当前排名</button>
                    <button class="btn" type="button" onclick="javascript:deleteInfo();">删除该关键词</button>
                </div>
            </form>
            <div class="hide" id="currentrank-div">
                关键词 <strong class="text-error">“<?php echo $current_keyworld->kw_keyworlds; ?>”</strong> 的当前排名：<strong id="currentrank"><img src="/css/images/loading.gif"></strong>
            </div>
        </div>
        
        <legend>最新关键词排名变化</legend>
        <div class="span12 compare_rank_box" style="margin-left:0;">
            <table class="table table-striped">
              <thead>
                <tr>
                  <th>#</th>
                  <th>关键词</th>
                  <th>上次排名</th>
                  <th>最新排名</th>
                  <th> <a href="javascript:void(0);" id="compare_rank_order" data-order="<?php echo "$order"; ?>">排名变化</a></th>
                  <th>日期</th>
                </tr>
              </thead>
              <tbody>
                <?php foreach ($compare_rank as $key_a=>$a) { ?>
                <tr>
                  <td><?php echo $key_a+1; ?></td>
                  <td><a target="_blank" href="<?php echo site_url('keyworlds/index/'.$a->kw_is_id); ?>"><?php echo $a->kw_keyworlds; ?></a></td>
                  <td><?php echo $a->m_sender; ?></td>
                  <td><?php echo $a->m_receiver; ?></td>
                  <td><?php if($a->m_content>0) {echo '<span class="text-error">↓  </span>';}elseif ($a->m_content<0) {echo '<span class="text-success">↑  </span>';} echo abs($a->m_content); ?></td>
                  <td><?php echo $a->m_datetime; ?></td>
                </tr>
                <?php } ?>
              </tbody>
            </table>
        </div>

    </div>
</div>
<script type="text/javascript">
    var setting = {
        view: {expandSpeed: "",
            dblClickExpand: true,
            showLine: true,
            showTitle: false,
            addHoverDom: addHoverDom,
            removeHoverDom: removeHoverDom,
            selectedMulti: false,
            fontCss: set_font_css
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        edit: {
            enable: true,
            showRenameBtn: false,
            showRemoveBtn: false,
            drag: {
                isCopy: false,
                isMove: true,
                prev: true,
                next: true,
                inner: true
            }
        },
        callback: {
            onClick: onClickzTree,
            beforeDrop: beforeDrop,
            onDrop: onDrop
        }
    };

    zNodes =<?PHP echo json_encode($keyworldsList); ?>;

    function onClickzTree(e, treeId, treeNode, clickFlag) {
        var urlReQuery = "<?php echo site_url('keyworlds/index/'); ?>";
        window.location.href = urlReQuery + '/' + treeNode.id;
        return true;
    }

    function addHoverDom(treeId, treeNode) {
        var sObj = $("#" + treeNode.tId + "_span");
        if ($("#addBtn_" + treeNode.id).length > 0)
            return;
        var addStr = "<span class='button add' id='addBtn_" + treeNode.id + "' title='\u589e\u52a0' onfocus='this.blur();'></span>";
        sObj.append(addStr);
        var btn = $("#addBtn_" + treeNode.id);
        if (btn)
            btn.bind("click", function() {
                var urlReQuery = "<?php echo site_url('keyworlds/add'); ?>";
                $.ajax({
                    type: "get",
                    url: urlReQuery + '/' + treeNode.id,
                    success: function(data, textStatus) {
                        var dataArray = $.parseJSON(data);
                        for (var key in dataArray) {
                            if (dataArray[key].name == 'ok') {
                                var infoStructure = dataArray[key].value;
                                var zTree = $.fn.zTree.getZTreeObj(treeId);
                                zTree.addNodes(treeNode, {id: (infoStructure.kw_is_id), pId: treeNode.id, name: "New Keyworlds"});
                            } else {
                                $.modaldialog.error('发生错误，请联系YCC.' + dataArray[key].value);
                            }
                        }
                    },
                    error: function() {
                        alert('发生错误，请联系YCC');
                    }
                });
                return false;
            });
    }
    ;

    function removeHoverDom(treeId, treeNode) {
        $("#addBtn_" + treeNode.id).unbind().remove();
    }
    ;

    //节点移动规则
    function beforeDrop(treeId, treeNodes, targetNode, moveType) {
        var move_type_title;
        if (moveType == 'inner') {
            move_type_title = ' 之中，成为子节点';
        } else if (moveType == 'prev') {
            move_type_title = ' 之前';
        } else if (moveType == 'next') {
            move_type_title = ' 之后';
        }
        //是否把子节点移到根节点
        if (targetNode.getParentNode() == null) {
            if (moveType == 'inner') { //只允许放到根节点之内
                if (confirm('是否把 ' + treeNodes[0].name + ' 移到 ' + targetNode.name + move_type_title)) {
                    return true;
                }
            }
        } else {
            if (treeNodes[0].getParentNode().tId != targetNode.getParentNode().tId || moveType == 'inner') {
                if (confirm('是否把 ' + treeNodes[0].name + ' 移到 ' + targetNode.name + move_type_title)) {
                    return true;
                }
            } else {
                return true;
            }
        }
        return false;
    }

    function onDrop(event, treeId, treeNodes, targetNode, moveType) {
        var idStr = '';
        if (targetNode != null && treeNodes[0].getParentNode() != null) {
            var childNodes = treeNodes[0].getParentNode().children;
            for (var i = 0; i < childNodes.length; i++) {
                idStr = idStr + childNodes[i].id + ',';
            }
            var urlReQuery = "<?php echo site_url('keyworlds/move'); ?>";
            $.ajax({
                type: "post",
                url: urlReQuery,
                data: {'pid': treeNodes[0].getParentNode().id, 'ids': idStr},
                success: function(data, textStatus) {
                    return true;
                },
                error: function() {
                    $.modaldialog.error('发生错误，请联系YCC');
                    return false;
                }
            });
        }
    }

    //设置样式
    function set_font_css(treeId, treeNode) {
        var nodeColor = '';
        if (treeNode.status !== 1) {
            nodeColor = 'red';
        }
        return treeNode.level == 1 ? {'font-weight': 'bold', 'color': nodeColor} : {'color': nodeColor};
    }

    //初始化树形菜单
    $(document).ready(function() {
        $.fn.zTree.init($("#treeDemo"), setting, zNodes);
        var zTree = $.fn.zTree.getZTreeObj('treeDemo');
        var nodes = zTree.getNodes();
        //是否自动展开节点
        if (nodes.length > 0) {
            zTree.expandNode(nodes[0], true, false, true);
        }
        //选择节点
        var node = zTree.getNodeByParam("id",<?php echo $current_keyworld->kw_is_id; ?>);
        zTree.selectNode(node);   

        $("#startdate,#enddate").datepicker({
            showButtonPanel: true,
            dateFormat:'yy-mm-dd'
        }); 

        $("body").on('click',"#compare_rank_order",function() {
            $("tbody").html('<img src="/css/images/loading.gif">');
            var order=$(this).attr('data-order');
            if (order=='asc') {
                order='desc';
            }else{
                order='asc';
            }
            var url="<?php echo site_url('keyworlds/index'); ?>";
            $(".compare_rank_box").load(url+'/'+order+' table');
        });
    });

    //删除结构和信息
    function deleteInfo() {
        if (confirm('是否删除?')) {
            var urlReQuery = "<?php echo site_url('keyworlds/delete_keyworlds'); ?>";
            var datas=$("#search-rank-form").serialize();
            $.ajax({
                type: "post",
                data: datas,
                url: urlReQuery,
                success: function(data, textStatus) {
                    var dataArray = $.parseJSON(data);
                    for (var key in dataArray) {
                        if (dataArray[key].name == 'ok') {
                            $.modaldialog.success('删除成功!，程序正在刷新...');
                            setTimeout(function() {
                                window.location.href = dataArray[key].value;
                            }, 500);
                            return true;
                        } else if (dataArray[key].name == 'no') {
                            $.modaldialog.error(dataArray[key].value);
                            return false;
                        }
                    }
                },
                error: function() {
                    $.modaldialog.error('发生错误，请联系YCC');
                    return false;
                }
            });
        }
    }

    function getrank(){
        var urlReQuery = "<?php echo site_url('keyworlds/get_google_rank'); ?>";

        /*$("#search-rank-form").attr('action',urlReQuery);
        $("#search-rank-form").submit();
        return false;*/

        var datas=$("#search-rank-form").serialize();
        $("#currentrank-div").removeClass('hide');
        $.ajax({
            type: "post",
            data: datas,
            url: urlReQuery,
            success: function(data, textStatus) {
                var dataArray = $.parseJSON(data);
                for (var key in dataArray) {
                    $("#currentrank").html(dataArray[key].value);
                    return true;
                }
            },
            error: function() {
                $("#currentrank").html("#");
                $.modaldialog.error('发生错误，请联系YCC');
                return false;
            }
        });
    }

    function searchkeywords(){
        var urlReQuery = "<?php echo site_url('keyworlds/search_keywords'); ?>";
        var datas=$("#search-rank-form").serialize();
        $.ajax({
            type: "post",
            data: datas,
            url: urlReQuery,
            success: function(htmls, textStatus) {
                if (htmls==0) {
                    $.modaldialog.error('请输入要搜索的关键词!');
                }else{
                    $(".compare_rank_box").html(htmls);
                }
                return true;
            },
            error: function() {
                $.modaldialog.error('发生错误，请联系YCC');
                return false;
            }
        });
    }
</script>