<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ChargeList.aspx.cs" Inherits="Medical.MBusiness.SysCharge.ChargeList" %>

<%@ Register Src="../../Controls/Date.ascx" TagName="Date" TagPrefix="ucDate" %>
<%@ Register Src="../../Controls/Pager.ascx" TagName="Pager" TagPrefix="ucPager" %>
<%@ Register Src="../../Controls/LoadButton.ascx" TagName="LoadButton" TagPrefix="ucLoadButton" %>
<!DOCTYPE html>
<html>
<head>
    <title><%=ModuleName%></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="../../Scripts/My97DatePicker/My97DatePicker/WdatePicker.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>
    <script src="../../js/myLib/jsHelper_Base.js" type="text/javascript"></script>
</head>
<body>
    <div class="pt-panel">
        <strong><%=ModuleName%></strong>
        <form name="fCondition" class="pt-padding">
            <ucLoadButton:LoadButton ID="LoadButton1" runat="server"/>
            <dl>
                <dt>处方编号</dt>
                <dd><input type="text" name="num" /></dd>
            </dl>
            <ucDate:Date ID="ucDate" runat="server" />
            <dl>
                <dt><input type="checkbox" value="1" name="isNoCharge" checked="checked" /></dt>
                <dd>只显示未收费</dd>
            </dl>
        </form>
    </div>
    <div class="pt-panel pt-below">
        <strong>查询一览</strong>
        <div class="pt-grid_frame">
            <table class="pt-grid" id="grid">
                <thead>
                    <tr>
                        <th style="width:50px"><label id="checkAllOff" onclick="CheckAllLine()" title="全选">&nbsp;</label></th>
                        <th style="width:195px">操作</th>
                        <th>姓名</th>
                        <th>开具日期</th>
                        <th>处方编号</th>
                        <th>诊断</th>
                        <th>医生</th>
                        <th>是否收费</th>
                    </tr>
                </thead>
                <tbody></tbody>
            </table>
        </div>
        <ucPager:Pager ID="gridPager" runat="server" />
    </div>
    <script>
        (function() {
            var url = "ChargeList.aspx",
            dialogUrl = "/MBusiness/SysCharge/ChargeForm.aspx",
            dialogFormName = "Charge_Form",
            listbutton = '<%=Medical.MCommon.PageHelper.GetListButton() %>',
            button = $("<div>").append(listbutton),
            grid = new Grid({ id: "grid", url: url, pager: new Pager("gridPager"), bindFun: bindData }),
            title = '<%=ModuleName %>';

            function init() {
                DateValidate(document.getElementById("startDate"), document.getElementById("endDate"));
                grid.search();
                bindEvents();
            }
            
            function bindEvents() {
                $("#btnQuery").click(function() {
                    grid.search(true);
                    CheckAllLinestatus = 0;
                    $("#checkAllOn").attr('title', '全选');
                    $("#checkAllOn").attr('id', 'checkAllOff');
                });
                $("#btnCheck").click(function() {
                    var _tbs = document.getElementById("grid");
                    var flag = false;
                    for (var j = 0, m = _tbs.rows.length - 1; j <= m; j++) {
                        var _row = _tbs.rows[j];
                        if (_row.cells[0].firstChild.checked == true) {
                            flag = true;
                            break;
                        }
                    }
                    if (!flag) {
                        top.showTipsMsg('请选择需要处理的数据 !', 1, 3000);
                        return;
                    }
                    if (confirm("确认收费?")) {
                        var _listData = [];
                        for (var i = 1; i <= _tbs.rows.length - 1; i++) {
                            var _row = _tbs.rows[i];
                            if (_row.cells[0].firstChild.checked == true) {
                                _listData.push(_row.cells[4].innerHTML);
                            }
                        }
                        var numData = _listData.join(",");
                        $.get(url, { action: "confirmAll", nums: numData }, function(data) {
                            if (parseInt(data, 10) > 0) {
                                top.showTipsMsg('批量收费确认操作成功!', 0, 3000);
                                grid.search();
                                CheckAllLinestatus = 0;
                                $("#checkAllOn").attr('title', '全选');
                                $("#checkAllOn").attr('id', 'checkAllOff');
                            } else {
                                top.showTipsMsg("操作失败!", 1, 4000);
                            }
                        });
                    }
                });
                grid.control.click(function(e) {
                    var target = $(e.target);
                    if (target.is("input") && !target.is(":disabled")) {
                        var data = target.parent().parent().data("data");
                        switch (target.val()) {
                            case "查看":
                                top.openDialog(dialogUrl + "?type=view&key=" + data.wNum, dialogFormName, title + ' - 查看', 1000, 400, 50, 50);
                                break;
                            case "收费":
                                top.openDialog(dialogUrl + "?key=" + data.wNum, dialogFormName, title + ' - 收费', 1000, 400, 50, 50, callback);
                                break;
                            case "取消收费":
                                cancleCharge(data.wNum);
                                break;
                        }
                    }
                });
            }
            function cancleCharge(num) {
                $.get(url, { action: "cancle", num: num }, function(data) {
                    if (parseInt(data, 10) > 0) {
                        top.showTipsMsg('操作成功!', 0, 5000);
                        grid.search();
                    } else {
                        top.showTipsMsg('操作失败!', 1, 5000);
                    }
                });
            }
            function callback() {
                window.location.reload();
            }
            function bindData(tr, data) {
                var state = "", buttonTemp = button.clone(),CheckBox="";
                if (data.wChargeState == "未收费") {
                    state = data.wChargeState;
                    CheckBox ='<input type="checkbox" onclick="chkSingle(this)"/>';
                    buttonTemp.find('[value="取消收费"]').removeClass("pt-button active").addClass("pt-button-disabled").attr("disabled", true);
                } else {
                    state = data.wChargeState + "/" + data.wDispensingState + "/" + data.wCheckState;
                    CheckBox ='<input type="checkbox" class="pt-button-disabled" onclick="chkSingle(this)"  disabled="true"/>';
                    if (data.wDispensingState == "未发药") {
                        buttonTemp.find('[value="收费"]').removeClass("pt-button active").addClass("pt-button-disabled").attr("disabled", true);
                    } else {
                        buttonTemp.find('[value="收费"]').removeClass("pt-button active").addClass("pt-button-disabled").attr("disabled", true);
                        buttonTemp.find('[value="取消收费"]').removeClass("pt-button active").addClass("pt-button-disabled").attr("disabled", true);
                    }
                }
                tr.append($("<td style='width:50px'>").html(CheckBox))
                .append($("<td>").html(buttonTemp.html()))
                .append($("<td>").html(data.wName))
                .append($("<td>").html(data.wDate))
                .append($("<td>").html(data.wNum))
                .append($("<td>").html(data.wDiagnosis))
                .append($("<td>").html(data.wDoctor))
                .append($("<td>").html(state));
            }
            init();
        })();
        var CheckAllLinestatus = 0;
        function CheckAllLine() {
                if (CheckAllLinestatus == 0) {
                    CheckAllLinestatus = 1;
                    $("#checkAllOff").attr('title', '反选');
                    $("#checkAllOff").attr('id', 'checkAllOn');
                    var _tb = document.getElementById("grid");                    
                    for (var j = 0, m = _tb.rows.length - 1; j <= m; j++) {
                        var _row = _tb.rows[j];
                        if (_row.cells[0].firstChild.disabled == false) {
                            _row.cells[0].firstChild.checked = true;
                        }
                    }
//                    if(($("#grid tbody tr").find('td :checkbox').disabled=true|false)==1){
//                        $("#grid tbody tr").find('td :checkbox').attr("checked", true);
//                    }
                } else {
                    CheckAllLinestatus = 0;
                    $("#checkAllOn").attr('title', '全选');
                    $("#checkAllOn").attr('id', 'checkAllOff');
//                    $("#grid tbody tr").find('td :checkbox').attr("checked", false);
                    var _tb = document.getElementById("grid");                    
                    for (var j = 0, m = _tb.rows.length - 1; j <= m; j++) {
                        var _row = _tb.rows[j];
                        if (_row.cells[0].firstChild.disabled == false) {
                            _row.cells[0].firstChild.checked = false;
                        }
                    }
                }
            }
            function chkSingle(obj) {
            if (obj.checked == true) {
                //获取其它的CHECKBOX的状态
                var flag = true;
                var _tb = document.getElementById("grid");
                for (var j = 0, m = _tb.rows.length - 1; j <= m; j++) {
                    var _row = _tb.rows[j];
                    if (_row.cells[0].firstChild.checked == false) {
                        flag = false;
                        break;
                    }
                }
                if (flag) {                    
                    CheckAllLinestatus = 1;
                    $("#checkAllOff").attr('title', '反选');
                    $("#checkAllOff").attr('id', 'checkAllOn');
                }
                else {
                    CheckAllLinestatus = 0;
                    $("#checkAllOn").attr('title', '全选');
                    $("#checkAllOn").attr('id', 'checkAllOff');
                }
            }
            else {
                CheckAllLinestatus = 0;
                $("#checkAllOn").attr('title', '全选');
                $("#checkAllOn").attr('id', 'checkAllOff');
            }
        }
    </script>
</body>
</html>
