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

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link href="../../css/style.css" rel="stylesheet" type="text/css" />
    <link href="../../css/bill.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="../../Scripts/json2.js" type="text/javascript"></script>
    <script src="../../js/myLib/helper.js" type="text/javascript"></script>
    <script src="../../js/myLib/jsHelper_Form.js" type="text/javascript"></script>
</head>
<body>
    <div class="pt-bill">
        <form id="billForm" name="billForm">
            <table class="pt-bill-frm" >
                <tr>
                    <td colspan="6" class="pt-bill-frm-title">盘点单</td>
                </tr>
                <tr>
                    <th>盘点单号[*]:</th>
                    <td><input type="text" id="cNum" name="cNum" runat="server"/></td>
                    <th>盘点仓库:</th>
                    <td><select type="text" id="cWarehouse" name="cWarehouse" runat="server" ></select></td>
                    <th>盘点日期[*]:</th>
                    <td><input type="text" id="cDate" name="cDate" runat="server" onclick="WdatePicker()" error="盘点日期" checkexpression="NotNull"/></td>
                </tr>
                <tr>
                    <th>经手人[*]:</th>
                    <td><input type="text" id="cMan" name="cMan" runat="server" limit="1"/></td>
                    <th>备注:</th>
                    <td><input type="text" id="cRemark" name="cRemark" runat="server" /></td>
                    <th>金额合计[*]:</th>
                    <td><span id="totalMoney" runat="server"></span>元</td>
                </tr>
            </table>
        </form>
        <table id="CheckGrid" class="pt-bill-grid" >
            <thead>
                <tr>
                    <td style="width: 40px">行号</td>
                    <td style="display:none">盘点记录ID</td>
                    <td style="width: 70px">药品编码</td>
                    <td style="width: 100px">药品名称</td>                       
                    <td style="width: 40px">规格</td>
                    <td style="width: 40px">单位</td>
                    <td style="width: 40px">剂型</td>
                    <td style="width: 40px">材质</td>
                    <td style="width: 80px">生产企业</td>
                    <td style="width: 70px">批号</td>
                    <td style="width: 60px">采购价</td>
                    <td style="width: 70px">单价</td>
                    <td style="width: 70px">库存数</td>
                    <td style="width: 70px">实盘数</td>
                    <td style="width: 60px">金额</td>
                    <td style="width: 80px">备注</td>
                    <td style="width: 13px"></td>
                </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
    </div>
    <div class="pt-line"></div>
    <div class="pt-padding" style="text-align:center">
        <input type="button" id="btnImport" class="pt-button active" value="导入盘点记录" />
        <input type="button" id="btnSave" class="pt-button active" value="保存" />
        <input type="button" id="btnClose" class="pt-button active" value="关闭" />
    </div>
    
    
    <script>
    (function() {
            var dataSplit = 'Θ ☺ ☻'.split(' '), //单元格、行、表分隔符
            rowCount = 50, //单据行数
            realRow = 1, //实际行数
            url = "CheckForm.aspx",
            key = "<%=key %>",
            type = "<%=type %>",
            emptyRow = '<tr><td></td><td style="display:none"></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>',
            widths = [],
            isImport = false;


            function init() {
                limit();
                $("#CheckGrid thead tr td").each(function() {
                    widths.push(getStyle($(this).attr("style"), "width"));
                });
                FixedTableHeader("CheckGrid", 300);
                setPage();
                bindEvents();
            }
            function save() {
                if (!JudgeValidate1()) return;
                if (realRow == 1) {
                    top.showTipsMsg("没有盘点明细记录!", 1, 4000);
                    return;
                }
                var result = false;
                $.ajax({
                    url: url,
                    data: $.parseJSON(form2JsonString(billForm, { action: "save", details: getDetails(), key: key })),
                    async: false,
                    success: function(data) {
                        result = parseInt(data, 10) > 0;
                    }
                });
                if (result) {
                    top.main.location.reload();
                    top.showTipsMsg('操作成功!', 0, 3000);
                    top.OpenClose('Check_Form'); //如果写在ajax的success里面报错
                } else {
                    top.showTipsMsg("操作失败，请稍后重试", 1, 4000);
                }
            }
            function getDetails() {
                //单元格、行、表
                var data = [];
                $("#CheckGrid tbody").find("tr").each(function() {
                    if ($(this).find("td:eq(1)").html()) {
                        var row = [], tds = $(this).find("td");
                        row.push(tds[1].innerText); //盘点记录
                        data.push(row.join(dataSplit[0]));
                    } else {
                        return true;
                    }
                });
                return data.join(dataSplit[1]);
            }
            function setPage() {
                var row = 1, data, html = [];
                if (key !== "") {
                    $.ajax({
                        url: url,
                        data: { action: 'query', key: key },
                        async: false,
                        success: function(rs) {
                            data = eval('(' + rs + ')');
                        }
                    });
                    for (var i = 0, k = data.Rows.length; i < k; i++) {
                        var list = data.Rows[i];
                        html.push('<tr><td></td>');
                        html.push('<td style="display:none">' + list.crID + '</td>');
                        html.push('<td>' + list.cCode + '</td>');
                        html.push('<td>' + list.dName + '</td>');
                        html.push('<td>' + list.dSpec + '</td>');
                        html.push('<td>' + list.dSubsection + '</td>');
                        html.push('<td>' + list.dType + '</td>');
                        html.push('<td>' + list.dMaterial + '</td>');
                        html.push('<td>' + list.dFactory + '</td>');
                        html.push('<td>' + list.cBatch + '</td>');
                        html.push('<td>' + list.cPurchasePrice + '</td>');
                        html.push('<td>' + list.cPrice + '</td>');
                        html.push('<td>' + list.cAmount + '</td>');
                        html.push('<td>' + list.cCheckAmount + '</td>');
                        html.push('<td>' + list.cMoney + '</td>');
                        html.push('<td>' + list.cRemark + '</td></tr>');

                        row++;
                        realRow++;
                    }
                }
                for (var i = row; i <= rowCount; i++) {
                    html.push(emptyRow);
                }
                $("#CheckGrid tbody").append(html.join(""));
                $("#CheckGrid tbody tr td").each(function() {
                    $(this).width(widths[$(this).index()]);
                });
                setTrNo();
                if (type == "view") { //查看
                    $("#btnSave").hide();
                    $("#btnImport").hide();
                    $(".pt-bill").children().find("input").each(function() {
                        $(this).parent().html($(this).val());
                    });
                    $(".pt-bill").children().find("select").each(function() {
                        $(this).parent().html($(this).find("option:selected").text());
                    });
                }
            }
            function setTrNo() {//生成序号
                var rowTotal = rowCount >= realRow ? rowCount : realRow;
                for (var i = 0; i < rowTotal; i++) {
                    $("#CheckGrid tbody tr:eq(" + i + ") td:eq(0)").html((i + 1)).attr("title", i < realRow - 1 ? "双击删除该行数据" : "");
                }
            }
            function importCheckRecord() {
                if (isImport) {
                    top.showTipsMsg("已经导入盘点记录!", 1, 4000);
                    return;
                }
                var row = 1, data, html = [];
                $.ajax({
                    url: url,
                    data: { action: "import", date: billForm.cDate.value, warehouse: billForm.cWarehouse.value },
                    async: false,
                    success: function(rs) {
                        data = rs;
                    }
                });
                if (data.length == 0) {
                    top.showTipsMsg("选择的[盘点日期]与[盘点仓库]无盘点记录,请重新选择!", 1, 4000);
                    return;
                } else {
                    data = eval('(' + data + ')');
                    for (var i = 0, k = data.Rows.length; i < k; i++) {
                        var list = data.Rows[i];
                        html.push('<tr><td></td>');
                        html.push('<td style="display:none">' + list.cID + '</td>');
                        html.push('<td>' + list.cCode + '</td>');
                        html.push('<td>' + list.dName + '</td>');
                        html.push('<td>' + list.dSpec + '</td>');
                        html.push('<td>' + list.dSubsection + '</td>');
                        html.push('<td>' + list.dType + '</td>');
                        html.push('<td>' + list.dMaterial + '</td>');
                        html.push('<td>' + list.dFactory + '</td>');
                        html.push('<td>' + list.cBatch + '</td>');
                        html.push('<td>' + list.cPurchasePrice + '</td>');
                        html.push('<td>' + list.cPrice + '</td>');
                        html.push('<td>' + list.cAmount + '</td>');
                        html.push('<td>' + list.cCheckAmount + '</td>');
                        html.push('<td>' + list.cMoney + '</td>');
                        html.push('<td>' + list.cRemark + '</td></tr>');
                        row++;
                        realRow++;
                    }
                }
                for (var i = row; i <= rowCount; i++) {
                    html.push(emptyRow);
                }
                $("#CheckGrid tbody").html(html.join(""));
                $("#CheckGrid tbody tr td").each(function() {
                    $(this).width(widths[$(this).index()]);
                });
                setTrNo();
                top.showTipsMsg("导入成功!", 0, 4000);
                isImport = true;
            }
            function bindEvents() {
                showDetail($("#CheckGrid tbody"));
                $("#CheckGrid tbody").click(function(e) {
                    var target = $(e.target);
                    if (target.is("td") && target.index() == 0 && target.parent().index() < realRow - 1) {
                        top.showConfirmMsg("确认删除该行数据吗?", function(r) {
                            if (r) {
                                target.parent().remove();
                                //实际行数-1
                                realRow--;
                                $("#CheckGrid tbody").append(emptyRow);
                                setTrNo();
                            }
                        });
                    }
                });
                $("#btnClose").click(function() {
                    top.OpenClose('Check_Form');
                });
                $("#btnSave").click(function() {
                    save();
                });
                $("#btnImport").click(function() {
                    importCheckRecord();
                });
            }
            init();
        })();
//        
//        (function() {
//            var dataSplit = 'Θ ☺ ☻'.split(' '), //单元格、行、表分隔符
//            rowCount = 50, //单据行数
//            realRow = 1, //实际行数
//            url = "CheckForm.aspx",
//            key = "<%=key %>",
//            type = "<%=type %>",
//            emptyRow = '<tr><td></td><td style="display:none"></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>',
//            widths = [],
//            isImport = false;


//            function init() {
//                limit();
//                $("#CheckGrid thead tr td").each(function() {
//                    widths.push(getStyle($(this).attr("style"), "width"));
//                });
//                FixedTableHeader("CheckGrid", 300);
//                setPage();
//                bindEvents();
//            }
//            function save() {
//                if (!JudgeValidate1()) return;
//                if (realRow == 1) {
//                    top.showTipsMsg("没有盘点明细记录!", 1, 4000);
//                    return;
//                }
//                var result = false;
//                $.ajax({
//                    url: url,
//                    data: $.parseJSON(form2JsonString(billForm, { action: "save", details: getDetails(), key: key })),
//                    async: false,
//                    success: function(data) {
//                        result = parseInt(data, 10) > 0;
//                    }
//                });
//                if (result) {
//                    top.main.location.reload();
//                    top.showTipsMsg('操作成功!', 0, 3000);
//                    top.OpenClose('Check_Form'); //如果写在ajax的success里面报错
//                } else {
//                    top.showTipsMsg("操作失败，请稍后重试", 1, 4000);
//                }
//            }
//            function getDetails() {
//                //单元格、行、表
//                var data = [];
//                $("#CheckGrid tbody").find("tr").each(function() {
//                    if ($(this).find("td:eq(1)").html()) {
//                        var row = [], tds = $(this).find("td");
//                        row.push(tds[1].innerText); //盘点记录
//                        data.push(row.join(dataSplit[0]));
//                    } else {
//                        return true;
//                    }
//                });
//                return data.join(dataSplit[1]);
//            }
//            function setPage() {
//                var row = 1, data, html = [];
//                if (key !== "") {
//                    $.ajax({
//                        url: url,
//                        data: { action: 'query', key: key },
//                        async: false,
//                        success: function(rs) {
//                            data = eval('(' + rs + ')');
//                        }
//                    });
//                    for (var i = 0, k = data.Rows.length; i < k; i++) {
//                        var list = data.Rows[i];
//                        html.push('<tr><td></td>');
//                        html.push('<td style="display:none">' + list.crID + '</td>');
//                        html.push('<td>' + list.cCode + '</td>');
//                        html.push('<td>' + list.dName + '</td>');
//                        html.push('<td>' + list.dSpec + '</td>');
//                        html.push('<td>' + list.dSubsection + '</td>');
//                        html.push('<td>' + list.dType + '</td>');
//                        html.push('<td>' + list.dMaterial + '</td>');
//                        html.push('<td>' + list.dFactory + '</td>');
//                        html.push('<td>' + list.cBatch + '</td>');
//                        html.push('<td>' + list.cPurchasePrice + '</td>');
//                        html.push('<td>' + list.cPrice + '</td>');
//                        html.push('<td>' + list.cAmount + '</td>');
//                        html.push('<td>' + list.cCheckAmount + '</td>');
//                        html.push('<td>' + list.cMoney + '</td>');
//                        html.push('<td>' + list.cRemark + '</td></tr>');

//                        row++;
//                        realRow++;
//                    }
//                }
//                for (var i = row; i <= rowCount; i++) {
//                    html.push(emptyRow);
//                }
//                $("#CheckGrid tbody").append(html.join(""));
//                $("#CheckGrid tbody tr td").each(function() {
//                    $(this).width(widths[$(this).index()]);
//                });
//                setTrNo();
//                if (type == "view") { //查看
//                    $("#btnSave").hide();
//                    $("#btnImport").hide();
//                    $(".pt-bill").children().find("input").each(function() {
//                        $(this).parent().html($(this).val());
//                    });
//                    $(".pt-bill").children().find("select").each(function() {
//                        $(this).parent().html($(this).find("option:selected").text());
//                    });
//                }
//            }
//            function setTrNo() {//生成序号
//                var rowTotal = rowCount >= realRow ? rowCount : realRow;
//                for (var i = 0; i < rowTotal; i++) {
//                    $("#CheckGrid tbody tr:eq(" + i + ") td:eq(0)").html((i + 1)).attr("title", i < realRow - 1 ? "双击删除该行数据" : "");
//                }
//            }
//            function importCheckRecord() {
//                if (isImport) {
//                    top.showTipsMsg("已经导入盘点记录!", 1, 4000);
//                    return;
//                }
//                var row = 1, data, html = [];
//                $.ajax({
//                    url: url,
//                    data: { action: "import", date: billForm.cDate.value, warehouse: billForm.cWarehouse.value },
//                    async: false,
//                    success: function(rs) {
//                        data = rs;
//                    }
//                });
//                if (data.length == 0) {
//                    top.showTipsMsg("选择的[盘点日期]与[盘点仓库]无盘点记录,请重新选择!", 1, 4000);
//                    return;
//                } else {
//                    data = eval('(' + data + ')');
//                    for (var i = 0, k = data.Rows.length; i < k; i++) {
//                        var list = data.Rows[i];
//                        html.push('<tr><td></td>');
//                        html.push('<td style="display:none">' + list.cID + '</td>');
//                        html.push('<td>' + list.cCode + '</td>');
//                        html.push('<td>' + list.dName + '</td>');
//                        html.push('<td>' + list.dSpec + '</td>');
//                        html.push('<td>' + list.dSubsection + '</td>');
//                        html.push('<td>' + list.dType + '</td>');
//                        html.push('<td>' + list.dMaterial + '</td>');
//                        html.push('<td>' + list.dFactory + '</td>');
//                        html.push('<td>' + list.cBatch + '</td>');
//                        html.push('<td>' + list.cPurchasePrice + '</td>');
//                        html.push('<td>' + list.cPrice + '</td>');
//                        html.push('<td>' + list.cAmount + '</td>');
//                        html.push('<td>' + list.cCheckAmount + '</td>');
//                        html.push('<td>' + list.cMoney + '</td>');
//                        html.push('<td>' + list.cRemark + '</td></tr>');
//                        row++;
//                        realRow++;
//                    }
//                }
//                for (var i = row; i <= rowCount; i++) {
//                    html.push(emptyRow);
//                }
//                $("#CheckGrid tbody").html(html.join(""));
//                $("#CheckGrid tbody tr td").each(function() {
//                    $(this).width(widths[$(this).index()]);
//                });
//                setTrNo();
//                top.showTipsMsg("导入成功!", 0, 4000);
//                isImport = true;
//            }
//            function bindEvents() {
//                showDetail($("#CheckGrid tbody"));
//                $("#CheckGrid tbody").click(function(e) {
//                    var target = $(e.target);
//                    if (target.is("td") && target.index() == 0 && target.parent().index() < realRow - 1) {
//                        top.showConfirmMsg("确认删除该行数据吗?", function(r) {
//                            if (r) {
//                                target.parent().remove();
//                                //实际行数-1
//                                realRow--;
//                                $("#CheckGrid tbody").append(emptyRow);
//                                setTrNo();
//                            }
//                        });
//                    }
//                });
//                $("#btnClose").click(function() {
//                    top.OpenClose('Check_Form');
//                });
//                $("#btnSave").click(function() {
//                    save();
//                });
//                $("#btnImport").click(function() {
//                    importCheckRecord();
//                });
//            }
//            init();
//        })();
    </script>
</body>
</html>