<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="gift_donate.aspx.cs" Inherits="VA.Modules.JDingMember.gift.gift_donate" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <!--#include virtual="/include/css/edit.aspx"-->
    <!--#include virtual="/include/js/edit.aspx"-->     
    <script type="text/javascript">

        function save() {
            
            var donateType = $("#ddlDonateType").val();
            if (donateType == "1") { //自由赠送
                if ($("#ckbIsFreeCustomer")[0].checked == false) {
                    var memberID = $("#hfMemberID").val();
                    if ($.trim(memberID) == "") {
                        JTongAlert('提示', '会员信息不能为空！', 'info');
                        return;
                    }
                }
            }
            else if (donateType == "2" || donateType == "3") {
                var bizNo = $("#txtBizNo").val();
                if ($.trim(bizNo) == "") {
                    JTongAlert('提示', '当赠送类型为‘充值赠送’或‘消费赠送’时，业务单号不能为空！', 'info');
                    return;
                }
            }

        if (!formEditListTable_checkTableData()) {
            return;
        }

        JTongAlertConfirm("确定要赠送吗？", add);
    }

        function add() {

            $("#btnSubmit").attr("disabled", "disabled");

            var requestType = "POST";
            var url = pageName + "?act=add";  //pageName在BasePage中注册到的客户端；
            var dataType = "html";
            var data = $("form").eq(0).serialize();
            var successFN = function (data, textStatus) {
                var bizNo = "";
                if (data.substring(0, 2) == "ok") {
                    bizNo = data.split("|")[1];
                    showActResult("赠送成功！", true, doAfterAdd, bizNo);
                } else if (data.indexOf("01|") != -1) {
                    showActResult(data.substring(3), false, doAfterAdd, bizNo);
                }
                else {
                    showActResult("赠送失败：" + data, false, doAfterAdd, bizNo);
                }
            };
            ajaxRequest(requestType,
                url,
                dataType,
                data,
                successFN,
                errorFN
                );
        }

        function doAfterAdd(actResult, bizNo) {
            if (actResult) {

                if ($("#ckbNotify")[0].checked == true) {
                    ajaxExecute("get", "../notification/biztransaction_notify.aspx", { "biztype": "donategift", "bizno": bizNo });
                }

                resetPage();
            }
        }

        //显示操作结果；
        function showActResult(sMsg, actResult, callback, bizNo) {

            $("#btnSubmit").removeAttr("disabled");

            var icon = "info";
            if (!actResult) {
                icon = "error";
            }

            JTongAlert('操作结果', sMsg, icon, function () {
                if (callback != null && callback != undefined) {
                    callback(actResult, bizNo);
                }
            });
        }

    function resetPage() {

        $("#txtBizNo").val("");        
        $("#txtRemark").val("");

        formEditListTable_clearRows();

        clearMemberInfo();

        $("#ddlDonateType").focus();
    }

    function JTongSmartDropDownList_AfterTakeValue(JTongSmartDropDownList_inputObjID, selectedRow) {
        if (selectedRow != null) {

            if (JTongSmartDropDownList_inputObjID == "txtItemName") {
                
                var itemCode = selectedRow.cells[0].innerHTML;
                var itemName = selectedRow.cells[1].innerHTML;
                var unit = selectedRow.cells[2].innerHTML;
                //var price = selectedRow.cells[3].innerHTML;
                var price = 0;
                var quantity = 1;

                formEditListTable_addRow(itemCode, itemName, unit, price,quantity);
                $("#txtItemName").val("");
            }
        }

        //返回true，继续取值；返回false，取消取值；
        return true;
    }
    
    function setControls() {
        
        var donateType = $("#ddlDonateType").val();
        if (donateType == "1") { //自由赠送
            $("#tr_BizNo").hide();
            $("#divMemberInfoContainer").show();
            $("#tbCustomerType").show();
        } 
        else if (donateType == "2") { //充值赠送
            $("#tr_BizNo").show();
            $("#divMemberInfoContainer").hide();
            $("#tbCustomerType").hide();
        }
        else if (donateType == "3") { //消费赠送
            $("#tr_BizNo").show();
            $("#divMemberInfoContainer").hide();
            $("#tbCustomerType").hide();
        }
    }

    function browseBizNoList() {
        var donateType = $("#ddlDonateType").val();
        if (donateType == "2" ){ //充值赠送
            browseWalletChargeList();
        }
        else if (donateType == "3") { //消费赠送
            browseMemberConsumeList();
        }
    }

    //充值记录；
    function browseWalletChargeList() {
        $('#winBizNoList').css("display", "block");
        $('#winBizNoList').window({
            width: 780,
            height: 430,
            modal: true,
            minimizable: false,
            collapsible: false,
            title: '充值记录'
        });    
        $('#winBizNoList').html("<iframe src='../datasrc/get_walletcharge_list.aspx' style='width:100%;height:100%;border:0px;margin:0px;padding:0px;'  ></iframe>");
    }
    //从弹窗窗口中获取充值业务流水号，并关闭弹出窗口
    function takeWalletChargeInfo(bizNo) {
        $("#winBizNoList").window("close", true);
        $("#txtBizNo").val(bizNo);
    }

    //消费记录；
    function browseMemberConsumeList() {
        $('#winBizNoList').css("display", "block");
        $('#winBizNoList').window({
            width: 830,
            height: 419,
            modal: true,
            minimizable: false,
            collapsible: false,
            title: '消费收银记录'
        });
        $('#winBizNoList').html("<iframe src='../datasrc/get_charge_list.aspx' style='width:100%;height:100%;border:0px;margin:0px;padding:0px;'  ></iframe>");
    }
    //从弹窗窗口中获取消费业务流水号，并关闭弹出窗口
    function takeConsumeInfo(bizNo) {
        $("#winBizNoList").window("close", true);
        $("#txtBizNo").val(bizNo);
    }

    //会员信息加载成功时调用的方法；            
    function doWhenMemberInfoLoadSuc(memberInfo) {

        $("#txtItemName").focus();
    }

    //会员信息加载失败时调用的方法；
    function doWhenMemberInfoLoadFail() {

    }

    $(function () {
        $("#ddlDonateType").focus();
        $("#ddlDonateType").change(function () {
            $("#txtBizNo").val("");
            setControls();
        });

        $("#ckbIsFreeCustomer").click(function () {
            var ckbIsFreeCustomer = $("#ckbIsFreeCustomer")[0];
            if (ckbIsFreeCustomer.checked) {

                clearMemberInfo();

                $("#txtKey").attr("disabled", "disabled");
                $("#ddlKeyType").attr("disabled", "disabled");
                $("#btnBrowseMembers").attr("disabled", "disabled");

                $("#ckbNotify").attr("disabled", "disabled");
                $("#ckbNotify")[0].checked = false;
            }
            else {
                $("#txtKey").removeAttr("disabled");
                $("#ddlKeyType").removeAttr("disabled");
                $("#btnBrowseMembers").removeAttr("disabled");

                $("#ckbNotify").removeAttr("disabled");
            }
        });

        setControls();
    });

    </script>

    <script type="text/javascript">

        var formEditListTable_RowCounter = 0;

        function formEditListTable_addRow(itemCode, itemName, unit, price, quantity) {
            
            formEditListTable_RowCounter++;

            var $srcTable = $("#srcTable");
            var srcTableID = $srcTable.attr("id");

            //如果找到相同数据行，并进行了数量的合并，则不再添加新的行；
            if (formEditListTable_dataMerge($srcTable, itemCode, quantity)) {
                return;
            }

            var rowHtml = '';
            var $row = null;
            var rowCnt = $srcTable[0].rows.length;
            var rowID = formEditListTable_RowCounter;

            itemName = itemName.replace(/'/g, "‘");
            unit = unit.replace(/'/g, "‘");
            
            var amount = price * quantity;

            rowHtml += '<tr id="tr_' + rowID + '">';
            rowHtml += '<td style="text-align:center;" class="tdRowNo"></td>';

            rowHtml += '<td style="text-align:center;">';
            rowHtml += '<span>' + itemCode + '</span>';
            rowHtml += '</td>';

            rowHtml += '<td style="text-align:left;padding-left:5px;">';
            rowHtml += '<span>' + itemName + '</span>';
            rowHtml += '<input type="hidden" name="hfItemName" value="' + itemName + '" />';
            rowHtml += '<input type="hidden" name="hfItemCode" value="' + itemCode + '" />';
            rowHtml += '</td>';

            rowHtml += '<td style="text-align:center;">';
            rowHtml += '<span>' + unit + '</span>';
            rowHtml += '<input type="hidden" name="hfUnit" value="' + unit + '" />';
            rowHtml += '</td>';
            
            rowHtml += '<td style="text-align:center;">';
            rowHtml += '<input type="hidden" name="hfPrice" value="' + price + '" id="hfPrice' + rowID + '" />';
            rowHtml += '<input type="text" name="txtQuantity" value="' + quantity + '" id="txtQuantity_' + rowID + '" onkeyup="formEditListTable_caculateAmount(' + rowID + ')" />';
            rowHtml += '<input type="hidden" name="hfAmount" value="' + amount + '" id="hfAmount_' + rowID + '" />';
            rowHtml += '</td>';

            rowHtml += '<td style="text-align:center;"><a href="javascript:formEditListTable_removeRow(' + rowID + ')">删除</a></td>';
            rowHtml += '</tr>';
            
            //调试代码；
            //$("#txtRemark").val(rowHtml);
            
            $row = $(rowHtml);
            $srcTable.append($row);

            formEditListTable_setRowNo();
            formEditListTable_setTotalAmount();
            formEditListTable_hideTbNoRecord();
           
        }

        function formEditListTable_setTotalAmount() {
            var $srcTable = $("#srcTable");
            $srcTable.find("#trTotal").remove();

            var totalRowHtml = '';
            var total = 0.00;
            var $hfAmountList = $srcTable.find("input[name=hfAmount]");

            $hfAmountList.each(function () {
                total += parseFloat($(this).val());
            });

            /*
            if ($hfAmountList.length > 0) {  //如果有数据行，则添加合计行
                totalRowHtml += '<tr id="trTotal">';

                totalRowHtml += '<td colspan="7" style="text-align:right;padding-right:5px;font-weight:bold;">合计</td>';

                totalRowHtml += '<td style="text-align:right;padding-right:5px;">';
                totalRowHtml += '<span style="font-weight:bold;" id="spanTotalAmount">' + total.toFixed(2).toString() + '</span>';
                totalRowHtml += '<input type="hidden" name="hfTotalAmount" value="' + total.toFixed(2).toString() + '" id="hfTotalAmount" />';
                totalRowHtml += '</td>';

                totalRowHtml += '<td></td>';

                totalRowHtml += '</tr>';

                $srcTable.append($(totalRowHtml));
            }
            */
        }

        function formEditListTable_caculateAmount(rowID) {
            var $txtQuantity = $("#txtQuantity_" + rowID);
            var $txtPrice = $("#hfPrice" + rowID);
            var sQuantity = $txtQuantity.val();
            var sPrice = $txtPrice.val();
            var amount = 0.00;

            if (sQuantity == "") {
                sQuantity = "0";
            }
            if (sPrice == "") {
                sPrice = "0";
            }

            ////清除非数字字符；(会影响光标键输入，故不再使用，更改为判断如果为非数字，直接清空)
            //clearNoNum($txtQuantity[0]);
            //clearNoNum($txtPrice[0]);

            if (isNaN(sQuantity)) {
                alert("数量为无效数字！");
                $txtQuantity.val("");
                $txtQuantity.focus();
                return;
            }
        
            if (isNaN(sPrice)) {
                alert("单价为无效数字.");
                $txtPrice.val("");
                $txtPrice.focus();
                return;
            }

            if (parseFloat(sQuantity) > 100000 || parseFloat(sQuantity) < 0) {  //限制数量不能超过100000
                alert("数量不能小于0或超过100000.");
                $txtQuantity.val("");
                $txtQuantity.focus();
                return;
            }
            if (parseFloat(sPrice) > 100000 || parseFloat(sPrice) < 0) {  //单价不能超过100000
                alert("单价不能小于0或超过100000.");
                $txtPrice.val("");
                $txtPrice.focus();
                return;
            }

            if ($txtQuantity.hasClass("error")) {
                $txtQuantity.removeClass("error");
            }
            if ($txtPrice.hasClass("error")) {
                $txtPrice.removeClass("error");
            }

            amount = parseFloat(sQuantity) * parseFloat(sPrice);

            $("#hfAmount_" + rowID).val(amount.toFixed(2).toString());            

            formEditListTable_setTotalAmount();
        }

        function formEditListTable_checkTableData() {

            var result = true;

            var dataRowCount = 0;
            var $srcTable = $("#srcTable");

            $srcTable.find("input[name=txtQuantity]").each(function () {
                dataRowCount++;
                var sQuantity = $(this).val();
                if ($.trim(sQuantity) == "" || $.trim(sQuantity) == "0") {
                    alert("数量不能为空或0！");
                    $(this).focus();
                    $(this).addClass("error");
                    result = false;
                    return;
                }
            });

            /*
            $srcTable.find("input[name=hfPrice]").each(function () {
                var sPrice = $(this).val();
                if ($.trim(sPrice) == "" || $.trim(sPrice) == "0") {
                    alert("单价不能为空或0！");
                    $(this).focus();
                    $(this).addClass("error");
                    result = false;
                    return;
                }
            });
            */

            if (dataRowCount == 0) {
                alert("赠送礼品清单不能为空！");
                result = false;
            }

            return result;
        }

        function formEditListTable_clearNoNum(obj) {
            obj.value = obj.value.replace(/[^\d.]/g, "");  //清除“数字”和“.”以外的字符  
            obj.value = obj.value.replace(/^\./g, "");  //验证第一个字符是数字而不是. 
            obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的.   
            obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
        }
        function clearItems() {
            formEditListTable_clearRows();
        }

    </script>

     <style type="text/css">  
    .editTable .th{
        width:100px;
    }
    </style>

</head>
<body>
<form id="Form1" method="post" runat="server">

<div id="wrapper" style="height:1%;overflow:hidden;">

<div class="editPageWrapper" style="width:700px;float:left;">

<%
    if (VA.Modules.JDingMember.Core.SystemInfo.SoftInfo.SoftLevel == VA.Modules.JDingMember.Core.Enums.SoftLevel.Free ||
        VA.Modules.JDingMember.Core.SystemInfo.SoftInfo.SoftLevel == VA.Modules.JDingMember.Core.Enums.SoftLevel.Basic)
    {
        base.ShowSoftFunctionLimitNote(VA.Modules.JDingMember.Core.SystemInfo.SoftInfo.SoftLevelName);
    }
%>

<div class="centerFormTitle">
   <h1><asp:Label ID="lblFormTitle" runat="server">礼品赠送</asp:Label></h1>
</div>

<table cellspacing="0" cellpadding="3" class="editTable">
<tr style="display:none;">
<td class="th">(<span class="note mustfill">*</span>)赠送类型：</td>
<td>
    <asp:DropDownList ID="ddlDonateType" runat="server" EnableViewState="false">
    </asp:DropDownList>      
</td>
</tr>
<tr id="tr_BizNo">
<td class="th">(<span class="note mustfill">*</span>)业务单号：</td>
<td>
    <VACTL:TextBox ID="txtBizNo" Runat="server"
            EnableViewState="False"
            CssClass="input"
            Width="150px"
            MaxLength="50" />  
    <input type="button" id="Button1" class="simpleBtn" onclick="browseBizNoList()" value="浏览" />    
</td>
</tr>
</table>

<table id="tbCustomerType" cellspacing="0" cellpadding="3" class="editTable" style="margin-top:-1px;">
<tr>
<td style="font-weight:bold;text-align:right;">
    <asp:CheckBox ID="ckbIsFreeCustomer" runat="server"
         Text="散客" />
</td>
</tr>
</table>

<div id="divMemberInfoContainer" style="margin-top:-1px;">
<!--#include file="../include/memberinfo.aspx"--> 
</div>

<table cellspacing="0" cellpadding="3" class="editTable" style="margin-top:-1px;">
<tr>
<td class="th" style="font-weight:bold;text-align:left;">(<span class="note mustfill">*</span>)赠送礼品清单</td>
</tr>
</table>
<div class="formEditListTableWrapper" style="min-height:223px;">
<table cellspacing="0" class="formEditListTable" id="srcTable" style="width:700px;margin-top:-1px;">
<thead>
<tr>
<td style="width:30px;text-align:center;">No.</td>
<td style="width:70px;text-align:center;">代码</td>
<td style="width:390px;text-align:center;">品名</td>
<td style="width:50px;text-align:center;">单位</td>
<td style="width:50px;text-align:center;">数量</td>
<td style="text-align:center;">操作</td>
</tr>    
</thead>    
<tbody>
</tbody>
</table>

<table id="tbNoRecord" cellspacing="0" cellpadding="3" style="margin-top:-1px;width:100%;">
<tr>
<td style="text-align:center;height:190px;font-size:14px;">
    暂无数据
</td>
</tr>
</table>
</div>

<table id="Table1" cellspacing="0" cellpadding="3" class="editTable" style="margin-top:-1px;">
<tr>
<td style="text-align:right;">
    添加礼品：
    <VACTL:TextBox ID="txtItemName" Runat="server"
                EnableViewState="False"
                CssClass="input"                
                Width="310px"
                />
    <asp:HiddenField runat="server" id="txtItemName_hiddenValue" />  
    <input type="button" id="btnClearItems" class="simpleBtn" onclick="clearItems()" value="清除礼品" />    
</td>
</tr>
</table>

<table cellspacing="0" cellpadding="3" class="editTable" style="margin-top:-1px;">
<tr>
<td class="th">备注：</td>
<td>    
    <VACTL:TextBox ID="txtRemark" Runat="server"
        EnableViewState="False"
        CssClass="input"
        Width="561px"
        MaxLength="100"     
        />        
</td>
</tr>
</table>

<table cellspacing="0" cellpadding="3" class="editTable" style="margin-top:-1px;">
<tr>
<td class="footer">
    <asp:CheckBox ID="ckbNotify" runat="server"
             Text="短信通知" />   
    <input type="button" id="btnSubmit" class="btn" style="width:100px" onclick="save()" value="保存" />
    <input type="button" id="btnResetPage" class="btn" style="width:100px" onclick="resetPage()" value="重置" />    
</td>
</tr>
</table>

<!--每个页面上只能有一个该控件实例-->
<VACTL:SmartDropDownList ID="SmartDropDownList1" runat="server"></VACTL:SmartDropDownList>  

</div><!--editPageWrapper.END-->

</div><!--"wrapper.END"-->

<!--业务记录窗口-->
<div id="winBizNoList" style="display:none;"></div> 

</form>
</body>
</html>

