<%@ page language="C#" masterpagefile="~/MasterPage/UserMasterPage.master" autoeventwireup="true" inherits="Rest_Gift_IntegralOperat, DoodiiShop" title="Untitled Page" enableEventValidation="false" viewStateEncryptionMode="Never" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder2" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">

    <script type="text/javascript">
       
      
        var gTargetUserInfo = new Object();
        gTargetUserInfo.UserId = 0;
        gTargetUserInfo.Integral = 0;
         gTargetUserInfo.ExpendIntegral = 0;
          gTargetUserInfo.SpreadIntegral = 0;
        var change="";
        $(document).ready( function(){ init() });
        
        function init()
        {
            bindEvents();
        }
        
        function bindEvents()
        {
           
            $("#btnGetTargetUserInfo").bind("click", function(){ getTargetUserInfo() });
            $("#btnPost").bind("click", function(){ post() });
//            $("#btnAdd").bind("click", function(){ add() });
//            $("#btnDeduct").bind("click", function(){ deduct() });
        }
     
        function getTargetUserInfo()
        {
            gTargetUserInfo.UserId = 0;
            gTargetUserInfo.Integral = 0;
            $("#TargetUserInfo").html("");
            var Email = $("#tbTargetUserEmail").val().trim();
            if (Email == "")
            {
                alert("请填写用户Email！");
                return;
            }
            var ret = Rest_Gift_IntegralOperat.GetUserInfo(Email).value;
            if (ret != null && typeof(ret) == "object")
            {
                gTargetUserInfo.UserId = parseInt(ret.Id);
                gTargetUserInfo.Integral = parseInt(ret.Integral);
                
                 gTargetUserInfo.ExpendIntegral = parseInt(ret.ExpendIntegral);
                 gTargetUserInfo.SpreadIntegral = parseInt(ret.SpreadIntegral);
                
                var str = "用户邮箱：" + ret.Email
                    + "<br />用户昵称：" + HtmlEncode(ret.NickName)
                    // + "<br />用户总积分：" + ret.Integral + "分"
                    //+ "<br />用现有户金豆：" + ret.SpreadIntegral + "个"
                    + "<br />剩余积分：" + ret.ExpendIntegral + "分"+change;

                $("#TargetUserInfo").html(str);
            }
            else
            {
                alert("未找到用户信息！");
                
            }
        }
       
       
        function add()
        {
            change="";
//             var rdS = document.getElementById("rdSpread");
//             var rdE = document.getElementById("rdExpend");
              var note1 = document.getElementById("taNote");
              var integral1 = document.getElementById("tbIntegral");
             var source = 0 ;
             var classID = 0;
             var note = document.getElementById("taNote").value.trim();
             
          
            var Email = $("#tbTargetUserEmail").val();
            if (Email == "")
            {
                alert("请填写用户Email！");
                return;
            }
            if(gTargetUserInfo.UserId == 0)
             {
               alert("请输入有效的用户信息！");
                return;
             }  

            if(note =='')
            {
                 window.alert("请输入原因！");
                return;
            }
            else if(note.length > 100)
            {
                window.alert("增加积分原因字符串长度不能大于100");
                return;
            }
            var integral = document.getElementById("tbIntegral").value;
             if(isNaN(integral))
             {
                window.alert("积分值为正整数，请输入正确的数值！");
                return;
             }
             if(integral <1)
             {
                window.alert("请输入一个大于1的积分值");
                return;
             }
             if(integral > 1000000)
             {
                window.alert("一次性增加积分值不能超过100万");
                return;
             }
             if(2100000000-(gTargetUserInfo.SpreadIntegral+gTargetUserInfo.ExpendIntegral)<integral)
             {
                 window.alert("您的总积分增加后已经达到总的积分极限，增加积分失败，请与数据库管理员联系！");
                 return;
             }
//             if(rdS.checked)//推广
//             {
//                source = 11;
//                classID = 2;
//                
//             }else if(rdE.checked)//消费
//             {
                source = 10;
                classID = 1;
//             }
//             else
//             {
//                window.alert("请选择要增加积分/金豆的类型！");
//                return;
//             }
             
             
        if(window.confirm("确实要增加此用户的积分吗？"))
        {
             if( Rest_Gift_IntegralOperat.IntegralOperat(gTargetUserInfo.UserId,source,integral,classID,"手动增加积分,原因："+note,"手动增加积分").value)
                {
                    alert("手动增加积分成功！");
                     note1.value="";
                    integral1.value="";
//                    rdS.checked = false;
//                    rdE.checked = false;
                    if(classID==1)
                        change= "<br /><span style='color:red'>增加用户积分：" + integral + "分</span>";
                    else
                        change= "<br /><span style='color:red'>增加用户金豆：" + integral + "个</span>";
                    getTargetUserInfo();
                    change="";
                    return;
                }
                else
                {
                    alert("手动增加积分失败！");
                    return;
                } 
           } 
           else
           {
                 note1.value="";
                    integral1.value="";
//                    rdS.checked = false;
//                    rdE.checked = false;
           } 
        }
        
         function deduct()
        {
            change="";
//             var rdS = document.getElementById("rdSpread");
//             var rdE = document.getElementById("rdExpend");
             var source = 0 ;
             var classID = 0;
             var note1 = document.getElementById("taNote");
              var integral1 = document.getElementById("tbIntegral");
              
               var note = document.getElementById("taNote").value.trim();
              var integral = document.getElementById("tbIntegral").value;
              
               if(integral < 1)
             {
                window.alert("增减积分请输入一个大于1的积分值");
                return;
             }
             
           
           var Email = $("#tbTargetUserEmail").val();
            if (Email == "")
            {
                alert("请填写用户Email！");
                return;
            }
            else
            {
            if(gTargetUserInfo.UserId == 0)
             {
                alert("请输入有效的用户信息！");
                return;
             }
                 
            }
              if(note =='')
            {
                 window.alert("请输入减少积分的原因！");
                return;
            }
            else if(note.length > 100)
            {
                window.alert("减少积分原因字符串长度不能大于100");
                return;
            }
//             if(rdS.checked)//推广
//             {
//                source = -11;
//                classID = 2;
//                if(gTargetUserInfo.SpreadIntegral < integral)
//                {
//                    alert("用户的金豆不足，请重新确认减少的金豆值！");
//                    return;
//                }
//                 
//                
//             }else if(rdE.checked)//消费
//             {
                source = -10;
                classID = 1;
                 if(gTargetUserInfo.ExpendIntegral < integral)
                {
                    alert("用户的剩余积分不足，请重新确认减少的剩余积分值！");
                    return;
                }
//             }
//             else if (!(rdS.checked || rdE.checked))
//             {
//                window.alert("请选择要减少积分/金豆的类型！");
//                return;
//             }
                
             if(!isNumber(integral))
             {
                window.alert("积分值为正整数，请输入正确的数值！");
                return;
             }
             if(integral < 1)
             {
                window.alert("增减积分请输入大于1的积分值");
                return;
             }
             if(integral > 1000000)
             {
                window.alert("一次性减少的积分值不能超过100万");
                return;
             }
             if(window.confirm("确实要将用户的积分减少吗？"))
             {
                if( Rest_Gift_IntegralOperat.IntegralOperat(gTargetUserInfo.UserId,source, -integral,classID,"手动减少积分,原因："+note,"手动减少积分").value)
                {
                    alert("手动减少积分成功！");
                    note1.value="";
                    integral1.value="";
//                    rdS.checked = false;
//                    rdE.checked = false;
                    if(classID==1)
                        change= "<br /><span style='color:red'>减少用户积分：" + integral + "分</span>";
                    else
                        change= "<br /><span style='color:red'>减少用户金豆：" + integral + "个</span>";
                     getTargetUserInfo();
                    change="";
                    return;
                }
                else
                {
                    alert("手动减少积分失败！");
                    getTargetUserInfo();
                    return;
                } 
            }
            else
            {
                     note1.value="";
                    integral1.value="";
//                    rdS.checked = false;
//                    rdE.checked = false;
            }  
        }
        function post()
        {
             var rdS = document.getElementById("rdAdd");
             var rdE = document.getElementById("rdDeduct");
             if(rdS.checked)
                add();
             else if(rdE.checked)
                deduct();
             else
             {alert("请选择是增减积分！");return;}
        }
        var checkLen = function()
        {
            $("#tbIntegral").val();
        }
      
        function hrefIntegreOperate()
        {
             location.href="IntegreOperate.aspx?Email="+$("#tbTargetUserEmail").val().trim();
        }
    </script>

    <style type="text/css">
        td
        {
            vertical-align:top;
        }
        .UserInfo
        { 
            margin:6px 6px 6px 6px;
            float:left;
            background-color:#ffffff;
            height:80px;
        }
        .si{ width:48px; height:25px; background-color:green; color:#FFFFFF;
 line-height:24px; text-align:center; margin-right:236px;}
 .hi{ width:48px; height:25px; background-color:green; color:#FFFFFF; 
 line-height:24px; text-align:center;}
.am{margin-right:356px;}
 .mt{margin-right:35px; line-height:23px;}

    </style>
    <div id="divDemo" runat="server" style="color: red; font-size: 24px; margin: 0px;
        padding: 0px;">
        <strong>如果您需要申请试用或购买本模块请<a href='http://www.doulaiding.com/buy/' target="_blank">联系我们</a>！</strong><br />
    </div>
    <div id="divContent" runat="server">
        <table border="1" class="NewTable" style=" border-right: thin solid;
            border-top: thin solid; border-left: thin solid; border-bottom: thin solid; border-collapse: collapse;
            width:649px; margin-left:3px;">
            <tr>
                <td style="background:#f0f0f0;">
                    用户邮箱：
                </td>
                <td style="width: 485px; height:auto;">
                    <input id="tbTargetUserEmail" style="width: 180px;" type="text" />
                    <input class="si" id="btnGetTargetUserInfo" type="button" value="查询" />
                </td>
            </tr>
            <tr>
                <td style=" background:#f0f0f0; height: 100px;vertical-align:middle;"> 
                    查询结果：
                </td>
                <td style=" vertical-align:middle;">
                    <div class="UserInfo" id="TargetUserInfo">
                    </div>
                </td>
            </tr>
            <tr>
                <td style=" height: 90px; background:#f0f0f0; vertical-align:middle;">
                    增减积分：</td>
         
                <td style="width: 470px; height: 90px;">
                <div class="am" style="vertical-align:middle;">
               <br /><input id="rdAdd" type="radio" value="1" name="rd" style="width: 14px" />增加 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input
                        id="rdDeduct" type="radio" name="rd" value="2" style="width: 16px" />
                    减少
                    <div class="mt">
                    <input id="tbIntegral" style="width: 61px" type="text" maxlength="7" onkeyup="checkLen();" />分
                </div></div></td >
            </tr>
            <tr>
                <td style="width: 161px; height: 60px; background:#f0f0f0;vertical-align:middle;">
                    增减原因：
                </td>
                <td style="width: 485px; height: 60px">
                    <textarea id="taNote" style="width: 468px; height: 80px" cols="5" rows="5"></textarea>
                </td>
            </tr>
            <tr align="center">
                <%--<td style="width: 161px; height: 26px; background:#f0f0f0;">
                    <input id="btnAdd" type="button" value="增加" style="width: 63px; background-color: green;
                        width: 60px; height: 20px; border: 1px solid #144d00; text-align: center; line-height: 22px;
                        color: #fff; display: block; text-decoration: none; font-size: 12px; height: 23px;" /></td>
                <td style="width: 485px; height: 26px">
                     &nbsp;<input id="btnDeduct" type="button" value="减少" style="width: 65px; background-color: red;
                        width: 60px; height: 20px; border: 1px solid #144d00; text-align: center; line-height: 22px;
                        color: #fff; text-decoration: none; font-size: 12px; height: 23px;" /></td>--%>
                <td colspan="2">
                    <input class="hi" id="btnPost" type="button" value="提交" /></td>
            </tr>
        </table>
    </div>
</asp:Content>
