<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TreatList.aspx.cs" Inherits="Medical.MBase.SysTreat.TreatList" %>

<%@ Register Src="../../Controls/Condition.ascx" TagName="Condition" TagPrefix="ucCondition" %>
<%@ Register Src="../../Controls/QueryButton.ascx" TagName="QueryButton" TagPrefix="ucQueryButton" %>
<%@ Register Src="../../Controls/Pager.ascx" TagName="Pager" TagPrefix="ucPager" %>
<%@ Register Src="../../Controls/Grid.ascx" TagName="Grid" TagPrefix="ucGrid" %>
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link href="../../css/style.css" rel="stylesheet" type="text/css" />
    <script src="../../Scripts/jquery-1.7.2.min.js" type="text/javascript"></script>
    <script src="../../js/myLib/jsHelper_Form.js" type="text/javascript"></script>
    <script src="../../js/myLib/Pager.js" type="text/javascript"></script>
    <script src="../../js/myLib/Grid.js" type="text/javascript"></script>
</head>
<body>
    <table>
        <tr>
            <td style="width:260px">
                <div class="pt-panel" style="height:512px">
                    <strong>诊疗项目分类</strong>
                    <div class="pt-padding" style="text-align:center">
                        <ucQueryButton:QueryButton ID="btnCategory" Text="新增类别" runat="server" />
                        <ucQueryButton:QueryButton ID="btnItem" Text="新增项目" runat="server" />
                    </div>
                    <div class="pt-padding" style="text-align:center">
                        <ucQueryButton:QueryButton ID="btnEdit" Text="编辑" runat="server" />
                        <ucQueryButton:QueryButton ID="btnDelete" Text="删除" runat="server" />
                    </div>
                    <div id="Category" class="pt-tree">
                        <%=GetCategory() %>
                    </div>
                </div>
            </td>
            <td>
                <div style="margin-left:3px">
                    <div class="pt-panel">
                        <strong>诊疗项目档案</strong>
                        <form name="fCondition" class="pt-padding">
                        <div class="pt-padding" >
                            <ucQueryButton:QueryButton ID="btnAdd" Text="新增" runat="server" />
                            <ucQueryButton:QueryButton ID="btnQuery" Text="查询" runat="server" />
                        </div>
                        <ucCondition:Condition ID="Condition2" Text="名称" Name="name" runat="server" />
                        </form>
                    </div>
                    <div class="pt-panel pt-below">
                        <strong>查询一览</strong>
                        <div class="pt-grid_frame">
                            <ucGrid:Grid ID="grid" runat="server" />
                        </div>
                        <ucPager:Pager ID="gridPager" runat="server" />
                    </div>
                </div>
            </td>
        </tr>
    </table>
    <script>
        (function() {
            var url = "TreatList.aspx",
            categoryDialogUrl = "/MBase/SysTreat/CategoryForm.aspx",
            categoryDialogFormName = "Category_Form",
            itemDialogUrl = "/MBase/SysTreat/ItemForm.aspx",
            itemDialogFormName = "Item_Form",
            treatDialogUrl = "/MBase/SysTreat/TreatForm.aspx",
            treatDialogFormName = "Treat_Form",

            grid = new Grid({ id: "grid", url: url, pager: new Pager("gridPager"), bindFun: bindData }),

            isExpand=false,//是否展开树
            currentLevel = -1,
            currentID = "",
            currentName = "";

            grid.getCondition = function() {
                var parms = { action: "query", searchFlag: grid.searchFlag };
                if (grid.pager) {
                    parms = { action: "query", searchFlag: grid.searchFlag, pageSize: grid.pager.pageSize, pageIndex: grid.pager.pageIndex };
                }
                if (currentLevel != -1) {
                    if (grid.pager) {
                        parms = { action: "query", searchFlag: grid.searchFlag, pageSize: grid.pager.pageSize, pageIndex: grid.pager.pageIndex, category: currentID,level:currentLevel };
                    } else {
                        parms = { action: "query", searchFlag: grid.searchFlag, category: currentID,level:currentLevel };
                    }
                }
                if ($("[name='fCondition']").length > 0) {
                    return $.parseJSON(form2JsonString(fCondition, parms));
                } else {
                    return parms;
                }
            };

            function init() {
                setTree();
                grid.search();
                bindEvents();
                categoryEvents();
            }
            function setTree(){
                if(isExpand){
                    $("#Category").find("h4 img").attr("src","../../images/minus.gif");
                    $("#Category").find("ul").show();
                }else{
                    $("#Category").find("h4 img").attr("src","../../images/plus.gif");
                    $("#Category").find("ul").hide();
                }
            }
            function categoryEvents() {
                $("#Category").click(function(e) {
                    var target = $(e.target),
                    currentTarget=null,//触发的菜单
                    menuTarget=null;//子菜单
                    if (target.is("div")) {
                        if (target.parent().is("h4")) {
                            currentLevel = 0;
                            currentTarget = target;
                            menuTarget = target.parent().next();
                        } else if (target.parent().parent().prev().is("h4")) {
                            currentLevel = 1;
                            currentTarget=target;
                        }
                    }
                    if(target.is("img") || (target.is("span") && target.parent().parent().is("h4"))){
                        currentLevel = 0;
                        currentTarget = target.parent();
                        menuTarget = target.parent().parent().next();
                    }
                    if(target.is("span")&&target.parent().parent().parent().prev().is("h4")){
                        currentLevel = 1;
                        currentTarget = target.parent();
                    }
                    
                    if(currentTarget!=null){
                        currentID = currentTarget.attr("data");
                        currentName = currentTarget.find("span").text();
                        removeHighlight();
                        currentTarget.addClass("pt-highlight");
                        grid.search(true);
                        if( currentLevel == 0 && menuTarget!=null ){
                            if(currentTarget.find("img").attr("src").indexOf("minus")!=-1){//展开的
                                if(menuTarget.is("ul")){
                                    menuTarget.hide();
                                }
                               
                                currentTarget.find("img").attr("src","../../images/plus.gif");
                            }else{
                                if(menuTarget.is("ul")){//如果没有子菜单，则menuTarget找到的将是相邻的DIV
                                    menuTarget.show();
                                }
                                currentTarget.find("img").attr("src","../../images/minus.gif");
                            }
                        }
                    }
                });

                $("#btnCategory").click(function() {
                    top.openDialog(categoryDialogUrl, categoryDialogFormName, '类别 - 添加', 700, 100, 50, 50);
                });
                $("#btnItem").click(function() {
                    if ($("#Category").children().length == 0) {
                        top.showTipsMsg('请先添加类别!', 1, 5000);
                        return;
                    }
                    top.openDialog(itemDialogUrl, itemDialogFormName, '项目 - 添加', 700, 100, 50, 50);
                });
                $("#btnEdit").click(function() {
                    if (currentLevel == -1) {
                        top.showTipsMsg('请选择要修改的项目!', 1, 5000);
                        return;
                    }
                    switch (currentLevel) {
                        case 0:
                            top.openDialog(categoryDialogUrl + "?key=" + currentID, categoryDialogFormName, '类别 - 编辑', 700, 100, 50, 50);
                            break;
                        case 1:
                            top.openDialog(itemDialogUrl + "?key=" + currentID, itemDialogFormName, '项目 - 编辑', 700, 100, 50, 50);
                            break;
                    }
                });
                $("#btnDelete").click(function() {
                    if (currentLevel == -1) {
                        top.showTipsMsg('请选择要删除的项目!', 1, 5000);
                        return;
                    }
                    top.showConfirmMsg("要删除名称为[" + currentName + "]的诊疗分类信息吗?", function(r) {
                        if (r) {
                            $.get(url, { action: "removeCategory", id: currentID }, function(data) {
                                if (data == "200") {
                                    top.showTipsMsg('删除成功!', 0, 5000);
                                    location.reload();
                                } else if (data == "100") {
                                    top.showTipsMsg('操作失败:您要删除的诊疗分类信息存在子项目!', 1, 5000);
                                } else {
                                    top.showTipsMsg('删除失败!', 1, 5000);
                                }
                            });
                        }
                    });
                });
            }

            function removeHighlight() {
                $("[class='pt-highlight']").removeClass("pt-highlight");
            }
            function bindEvents() {
                var title = '<%=ModuleName %>';
                $("#btnAdd").click(function() {
                    if (currentLevel != 1) {
                        top.showTipsMsg('请选择[项目分类]最底层节点!', 1, 5000);
                        return;
                    }
                    top.openDialog(treatDialogUrl+"?parentId="+currentID+"&parentName="+currentName, treatDialogFormName, title + ' - 添加', 700, 100, 50, 50);
                });
                $("#btnQuery").click(function() {
                    grid.search(true);
                });
                grid.control.click(function(e) {
                    var target = $(e.target);
                    if (target.is("input")) {
                        var data = target.parent().parent().data("data");
                        switch (target.val()) {
                            case "修改":
                                top.openDialog(treatDialogUrl + "?key=" + data.tID, treatDialogFormName, title + ' - 编辑', 700, 100, 50, 50);
                                break;
                            case "删除":
                                remove(data);
                                break;
                        }
                    }
                });
            }
            function remove(data) {
                top.showConfirmMsg(removeTip(data), function(r) {
                    if (r) {
                        $.get(url, { action: "remove", id: data.tID }, function(data) {
                            if (data == "success") {
                                top.showTipsMsg('删除成功!', 0, 5000);
                                grid.search();
                            } else {
                                top.showTipsMsg('删除失败!', 1, 5000);
                            }
                        });
                    }
                });
            }
            function bindData(tr, data) {
                tr.append($("<td>").html("<input type='button' class='pt-button active' value='修改' /><input type='button' class='pt-button active' value='删除' />")).append($("<td>").html(data.tName))
                .append($("<td>").html(data.tUnit))
                .append($("<td>").html(data.tPrice));
            }
            function removeTip(data) {
                return "确定要删除[名称]为[" + data.tName + "]的诊疗项目吗?";
            }
            init();
        })();
    </script>
</body>
</html>
