using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using xabaraCOM;

public partial class webXabara_money_moneyExe : System.Web.UI.Page
{
    sqlServer sql = new sqlServer();
    admin a = new admin();
    ipAddress ip = new ipAddress();
    javasrcipt js = new javasrcipt();

    DataTable dt;

    protected void Page_Load(object sender, EventArgs e)
    {
        a.check("|moneyExe|");

        if (!IsPostBack)
        {
            regExpression reg = new regExpression();
            payRegularExpressionValidator.ValidationExpression = reg.Double;
            payRegularExpressionValidator.ErrorMessage = reg.DoubleError;
        }

        orderID.Text = DateTime.Now.ToString("yyyyMMddHHmmss") + xabara.rnd("123", 5);
    }

    protected void uID_TextChanged(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(uID.Text as string))
        {
            dt = sql.getDataTable("SELECT top 1 xabaraUser.userCardID,moneyDetail.nowMoney,moneyDetail.buyMoney FROM xabaraUser LEFT JOIN moneyDetail ON xabaraUser.userCardID=moneyDetail.uID WHERE xabaraUser.userCardID='" + uID.Text.Trim() + "' or xabaraUser.userID='" + uID.Text.Trim() + "' ORDER BY moneyDetail.dID DESC");
            if (dt.Rows.Count > 0)
            {
                uID.Text = dt.Rows[0]["userCardID"].ToString().Trim();
                uMoneyInfo.Text = "当前帐户余额：" + rmb.format(dt.Rows[0]["nowMoney"].ToString()) + " 已消费额：" + rmb.format(dt.Rows[0]["buyMoney"].ToString());
            }
            else
            {
                uMoneyInfo.Text = "不存在当前用户名：" + uID.Text.Trim() + "，请核对后重新输入！";
                uID.Text = string.Empty;
            }
            dt.Dispose();
        }
    }

    protected void addMoney_Click(object sender, EventArgs e)
    {
        double myMoney = 0;
        double myBuyMoney = 0;
        dt = sql.getDataTable("select top 1 * from moneyDetail where uID='" + uID.Text.Trim() + "' order by dID DESC");  //取原余额及已消费额
        if (dt.Rows.Count > 0)
        {
            myMoney = Convert.ToDouble(dt.Rows[0]["nowMoney"]);
            myBuyMoney = Convert.ToDouble(dt.Rows[0]["buyMoney"]);
        }
        dt.Dispose();

        string strDim = "@uID,@orderID,@addMoney,@subtractMoney,@nowMoney,@buyMoney,@moneyInfo,@adminID,@postDate,@postIP";
        string[] strValue = new string[10];
        strValue[0] = uID.Text;
        strValue[1] = orderID.Text;
        if (moneyType.Text.Trim().Equals("加"))
        {
            strValue[2] = payTxt.Text.Trim();
            strValue[3] = "0";
            strValue[4] = (myMoney + Convert.ToDouble(payTxt.Text.Trim())).ToString();
            strValue[5] = myBuyMoney.ToString();
        }
        else
        {
            strValue[2] = "0";
            strValue[3] = payTxt.Text.Trim();
            strValue[4] = (myMoney - Convert.ToDouble(payTxt.Text.Trim())).ToString();
            strValue[5] = (myBuyMoney + Convert.ToDouble(payTxt.Text.Trim())).ToString();
        }
        strValue[6] = CHAR.htmSafe(mInfo.Text);
        strValue[7] = a.uid;
        strValue[8] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        strValue[9] = ip.address();

        sql.log(a.uid, "admin", "款项操作：" + uID.Text.Trim());

        if (sql.add("moneyDetail", strDim, strValue).Equals("发布成功"))
        {
            js.div(false, "", "text", "操作成功", 350, 150, "ConsumeDetail.aspx", "goto");
        }
        else
        {
            js.div(false, "", "text", "操作失败", 350, 150, "", "");
        }
    }
}
