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

<!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" />
    <link href="../../css/autocomplete.css" rel="stylesheet" type="text/css" />
    <script src="../../Scripts/jquery-1.7.2.min.js" type="text/javascript"></script>
    <script src="../../Scripts/jquery.ajaxQueue.js" type="text/javascript"></script>
    <script src="../../Scripts/My97DatePicker/My97DatePicker/WdatePicker.js" type="text/javascript"></script>
    <script src="../../js/myLib/autoComplete.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">
        <input type="hidden" id="iID" runat="server" />
        <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="iCode" name="iCode" runat="server" error="医疗证号" checkexpression="NotNull"/></td>
                    <th>户主姓名:</th>
                    <td><input type="text" id="iName" name="iName" runat="server" class="pt-readonly"  /></td>
                    <th>家庭住址:</th>
                    <td><input type="text" id="iAddress" name="iAddress" runat="server" /></td>
                </tr>
            </table>
        </form>
        <table id="InfoGird" class="pt-bill-grid">
            <thead>
                <tr>
                    <td style="width: 40px">行号</td>
                    <td style="width: 150px">姓名[*]</td>
                    <td style="width: 100px">性别[*]</td>
                    <td style="width: 150px">出生年月[*]</td>
                    <td style="width: 140px">身份证号</td>
                    <td style="width: 140px">家庭关系</td>
                    <td style="width: 13px"></td>
                </tr>
            </thead>
            <tbody></tbody>
        </table>
    </div>
    <div class="pt-line"></div>
    <div class="pt-tip">友情提示：点击第1列行号可对相关记录进行删除操作</div>
    <div class="pt-padding" style="text-align: center">
        <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 = "InfoForm.aspx",
            key = "<%=key %>",
            type = "<%=type %>",
            emptyRow = '<tr><td></td><td></td><td></td><td></td><td></td><td></td></tr>',
            householderName = ""; //户主名称

            function init() {
                setPage();
                bindEvents();
            }
            function save() {
                if (!JudgeValidate1()) return;
                if (!validateDetails()) return;
                endEdit($("#InfoGird tbody"));
                $("#iName").val(householderName);
                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('Info_Form'); //如果写在ajax的success里面报错
                } else {
                    top.showTipsMsg("操作失败，请稍后重试", 1, 4000);
                }
            }
            function getDetails() {
                //单元格、行、表
                var data = [];
                $("#InfoGird tbody").find("tr").each(function() {
                    if ($(this).find("td:eq(1)").html()) {
                        var row = [], tds = $(this).find("td");
                        row.push(tds[1].innerText); //姓名
                        row.push(tds[2].innerText); //性别
                        row.push(tds[3].innerText); //出生年月
                        row.push(tds[4].innerText); //身份证号
                        row.push(tds[5].innerText); //家庭关系
                        data.push(row.join(dataSplit[0]));
                    } else {
                        return true;
                    }
                });
                return data.join(dataSplit[1]);
            }
            function validateDetails() {
                //验证是否重复
                var isValidate = true, isHaveHouseholder = false;
                //验证是否有值
                $("#InfoGird tbody").find("input").each(function() {
                    var trIndex = $(this).parent().parent().index();
                    if (!$(this).val() && trIndex != realRow - 1 && $(this).attr("error")) {
                        var error = "第" + ($(this).parent().parent().index() + 1) + "行[" + $(this).attr("error") + "]不能为空!\n";
                        isValidate = false;
                        var self = this;
                        top.showConfirmMsg(error, function(r) {
                            if (r) {
                                self.focus();
                            }
                        });
                        return false;
                    }
                    //验证整个表单有没有"户主"
                    if ($(this).parent().index() == 5 && trIndex != realRow - 1 && $(this).val().indexOf("户主") != -1) {
                        isHaveHouseholder = true;
                        householderName = $(this).parent().prevAll(":eq(3)").children().val();
                    }
                });
                if (!isHaveHouseholder) {
                    top.showTipsMsg('必须有户主才能保存!', 1, 3000);
                    isValidate = false;
                }
                return isValidate;
            }
            function bindSexAutocomplete(jqueryObject) {
                jqueryObject.autocomplete("../../MCommon/QueryAppendProperty.ashx?action=getSex", {
                    title: "sex",
                    formatItem: function(row, i, max) {
                        return "aValue";
                    },
                    formatMatch: function(row, i, max) {
                        return "aValue";
                    },
                    formatResult: function(row) {
                        return row.aValue
                    }
                });
            }
            function addBlankRow(index) {
                if (index + 1 < rowCount) {
                    var td = $("#InfoGird tbody tr:eq(" + (index + 1) + ") td:eq(1)"),
                    tr = td.parent();
                    if (td.children().length == 0) {
                        var input = $('<input type="text" error="性别" />');
                        tr.find("td:eq(1)").append('<input type="text" error="姓名" />');
                        tr.find("td:eq(2)").append(input);
                        tr.find("td:eq(3)").append('<input type="text" onclick="WdatePicker()" error="出生年月" /></td>');
                        tr.find("td:eq(4)").append('<input type="text" error="身份证号" /></td>');
                        tr.find("td:eq(5)").append('<input type="text" />');

                        bindSexAutocomplete(input);
                        bindAutocomplete(tr.find("td:eq(5) input"));
                        realRow++;
                    }
                }
                setTrNo();
            }
            function setPage() {
                $(".pt-readonly").focus(function() {
                    this.blur();
                });
                var widths = [];
                $("#InfoGird thead tr td").each(function() {
                    widths.push(getStyle($(this).attr("style"), "width"));
                });

                FixedTableHeader("InfoGird", 300);
                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><input type="text" error="姓名"  value="' + list.ilName + '" /></td>');
                        html.push('<td><input type="text" error="性别" value="' + list.ilSex + '" /></td>');
                        html.push('<td><input type="text" onclick="WdatePicker()" error="出生年月" value="' + list.ilBirthday + '" /></td>');
                        html.push('<td><input type="text" error="身份证号" value="' + list.ilIDCard + '" /></td>');
                        html.push('<td><input type="text" value="' + list.ilRelation + '" /></td>');

                        row++;
                        realRow++;
                    }
                }
                for (var i = row; i <= rowCount; i++) {
                    html.push(emptyRow);
                }
                $("#InfoGird tbody").append(html.join(""));
                $("#InfoGird tbody tr td").each(function() {
                    $(this).width(widths[$(this).index()]);
                });
                var tr = $("#InfoGird tbody tr:eq(" + (row - 1) + ")");
                tr.find("td:eq(1)").append('<input type="text" error="姓名" />');
                tr.find("td:eq(2)").append('<input type="text" error="性别" />');
                tr.find("td:eq(3)").append('<input type="text" onclick="WdatePicker()" error="出生年月" /></td>');
                tr.find("td:eq(4)").append('<input type="text" error="身份证号" /></td>');
                tr.find("td:eq(5)").append('<input type="text" />');

                bindAutocomplete($("#InfoGird tbody tr").find("td:eq(5) input"));
                bindSexAutocomplete($("#InfoGird tbody tr").find("td:eq(2) input"));

                setTrNo();
                if (type == "view") { //查看
                    $("#btnSave").hide();
                    $(".pt-bill").children().find("input").each(function() {
                        $(this).parent().html($(this).val());
                    });
                }
            }
            function setTrNo() {//生成序号
                for (var i = 0; i < rowCount; i++) {
                    $("#InfoGird tbody tr:eq(" + i + ") td:eq(0)").html((i + 1)).attr("title", i < realRow - 1 ? "点击删除该行数据" : "");
                }
            }

            function bindAutocomplete(jqueryObject) {
                jqueryObject.autocomplete("../../MCommon/QueryInfo.ashx?action=getRelation", {
                    formatItem: function(row, i, max) {
                        return "ilRelation";
                    },
                    formatMatch: function(row, i, max) {
                        return "ilRelation";
                    },
                    formatResult: function(row) {
                        return row.ilRelation;
                    }
                });
            }
            function bindEvents() {
                showDetail($("#InfoGird tbody"));
                $("#InfoGird 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--;
                                $("#InfoGird tbody").append(emptyRow);
                                setTrNo();
                            }
                        });
                    }
                });
                $("#InfoGird tbody tr").find("td:eq(1) input").on("blur", function() {
                    var trIndex = $(this).parent().parent().index();
                    if ($(this).val() && trIndex <= realRow - 1) {
                        addBlankRow(trIndex);
                    }
                });
                $("#btnClose").click(function() {
                    top.OpenClose('Info_Form');
                });
                $("#btnSave").click(function() {
                    save();
                });
            }
            init();
        })();
    </script>
</body>
</html>
