
<div class="row-fluid">
    <div class="span3">
        <div class="well well-small">
            <ul id="treeDemo" name="treeDemo" class="ztree" style="height:938px;overflow-y:scroll;overflow-x:auto;"></ul>
            <ul class="nav nav-list">
                <li class="nav-header"><a href="<?php echo site_url('infoshare/clear'); ?>" ><i class="icon-trash"></i></a></li>
            </ul>
        </div>
    </div>

    <div class="span9">





        <?php if (!isset($new_infoshate)) { ?>

            <div class="page-header">
                <h3><?php echo $infoshare->isd_title; ?>
                    <a href="<?php echo site_url('infoshare/edit/' . $infoshare->isd_is_id); ?>"><i class="icon-pencil"></i></a>
                    <small><?php echo $infoshare->isd_date; ?></small>
                    <?php if (!empty($infoshare->isd_memo)) { ?>
                        <br>
                        <small class="text-error">提醒：<?php echo $infoshare->isd_memo; ?></small>
                    <?php } ?>
                    <br>
                    <small>
                        附件：
                        <?php
                        $content = $infoshare->isd_html;
                        if ($attachments_list) {
                            foreach ($attachments_list as $item) {
                                $find = explode('~@~', $item->im_value);
                                $content = str_ireplace('cid:' . $find[0], $find[2], $content);
                                ?>
                                <a href="<?php echo $find[2] ?>"><?php echo $find[1]; ?></a> , 
                                <?php
                            }
                        }
                        ?>			  
                    </small>
                </h3>
            </div>
            <h4><?php echo $infoshare->isd_fromname ?>:</h4>

            <!-- 显示内容 -->
            <?php if (substr($infoshare->isd_title, -3) == '.md') { ?>
                <script src="/min?f=/js/editor.md/editormd.min.js,/js/editor.md/lib/marked.min.js,/js/editor.md/lib/prettify.min.js,/js/editor.md/lib/raphael.min.js,/js/editor.md/lib/flowchart.min.js,/js/editor.md/lib/jquery.flowchart.min.js,/js/editor.md/lib/underscore.min.js,/js/editor.md/lib/sequence-diagram.min.js"></script>
                <link rel="stylesheet" href="/min?f=/js/editor.md/css/editormd.preview.css" />

                <SCRIPT LANGUAGE="JavaScript">
                    $(function () {
                        editormd_markdown_preview = editormd.markdownToHTML("editormd-markdown-preview", {
                            htmlDecode: "style,script,iframe", // you can filter tags decode
                            emoji: true,
                            taskList: true,
                            tex: true, // 默认不解析
                            flowChart: true, // 默认不解析
                            sequenceDiagram: true // 默认不解析
                        });
                    });
                </script>
                <div id="editormd-markdown-preview">
                    <textarea style="display:none;" name="editormd-markdown-doc"><?php echo $content; ?></textarea>               
                </div>

            <?php } else { ?>
                <?php echo $content; ?>
            <?php } ?>

        <?php } else { ?>
            <legend>最近分享</legend>
            <table class="table table-striped">
                <thead>
                    <tr>
                        <th class="span1">#</th>
                        <th class="span7">标题</th>
                        <th class="span2">发布者</th>
                        <th class="span2">时间</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    foreach ($new_infoshate as $key => $item) {
                        ?>
                        <tr>
                            <td><?php echo $key + 1; ?></td>
                            <td><a href="<?php echo site_url('infoshare/detail/' . $item->isd_is_id); ?>" ><?php echo $item->isd_title; ?></a></td>
                            <td><?php echo $item->isd_fromname; ?></td>
                            <td><?php echo date('m/d/Y', strtotime($item->isd_datetime)); ?></td>
                        </tr>
                    <?php } ?> 
                </tbody>
            </table>  
        <?php } ?>
    </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($structures_list); ?>;

    function onClickzTree(e, treeId, treeNode, clickFlag) {
        var urlReQuery = "<?php echo site_url('infoshare/detail/'); ?>";
        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('infoshare/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), pId: treeNode.id, name: "New"});
                            } 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('infoshare/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 $infoshare->isd_is_id; ?>);
        zTree.selectNode(node);

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

    //删除结构和信息
    function deleteInfo() {
        if (confirm('是否删除?')) {
            var urlReQuery = "<?php echo site_url('infoshare/delete'); ?>";
            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;
                }
            });
        }
    }

</script>
