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

public partial class A_Record_Record_Income : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!this.Page.IsPostBack)
        {
            OfficeManage.BindDdlIncomeType(ddlIncomeType);//绑定收入类型
            OfficeManage.BindDdlSeller(ddlSeller);//绑定付款方
            OfficeManage.BindDdlProject(ddlProject);//绑定项目
        }
    }
    #region 功能栏事件（支出、收入、转账、借贷按钮）
    protected void btnExpenditure_Click(object sender, EventArgs e)//支出记账
    {
        Response.Redirect("Record_Expenditure.aspx");
    }
    protected void btnIncome_Click(object sender, EventArgs e)//收入记账
    {
        Response.Redirect("Record_Income.aspx");
    }
    protected void btnTransfer_Click(object sender, EventArgs e)//转账记账
    {
        Response.Redirect("Record_transfer.aspx");
    }
    protected void btnLoan_Click(object sender, EventArgs e)//借贷记账
    {
        Response.Redirect("Record_ Loan.aspx");
    }
    #endregion
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        int accountId = 0;//账户ID
        if (uclAccount.AccountTypeID == 1)//现金账户
        {
            accountId = uclAccount.CashAccountID;
        }
        else if (uclAccount.AccountTypeID == 2)//信用卡
        {
            accountId = uclAccount.CreditCardID;
        }
        else if (uclAccount.AccountTypeID == 3)//储蓄卡
        {
            accountId = uclAccount.SavingsCardID;
        }
        else if (uclAccount.AccountTypeID == 4)//网上支付
        {
            accountId = uclAccount.OnlinePayID;
        }
        int flag = SqlHelper.Operation("INSERT INTO dbo.tabA_Record_Income(RIncomeMoney,RIncomeTypeId,RIncomeSellerId,RIAccountTypeId,RIAccountId,RIncomeDateTime,RIncomeProjectId,RIncomeRemarks,CreateTime) VALUES("+ConvertHelper.GetDecimal(txtIncomeMoney.Text.Trim())+","+ConvertHelper.GetInteger(ddlIncomeType.SelectedValue)+","+ConvertHelper.GetInteger(ddlSeller.SelectedValue)+","+uclAccount.AccountTypeID+","+accountId+",'"+ConvertHelper.GetDateTime(txtIncomeDateTime.Text.Trim())+"',"+ConvertHelper.GetInteger(ddlProject.SelectedValue)+",'"+txtIncomeRemarks.Text.Trim()+"','"+System.DateTime.Now+"')", "constring");
        if (flag > 0)
        {
            int flags = 0;//修改收入账户的id
            if (uclAccount.AccountTypeID == 1)//现金账户
            {
                flags = SqlHelper.Operation("UPDATE dbo.tabA_Account_Cash SET CashRemainder=CashRemainder+" + ConvertHelper.GetDecimal(txtIncomeMoney.Text.Trim()) + " WHERE CashAccountId=" + uclAccount.CashAccountID + "", "constring");//修改现金用户余额
            }
            else if (uclAccount.AccountTypeID == 2)//信用卡账户(cardArrears:欠款金额)
            {
                flags = SqlHelper.Operation("UPDATE dbo.tabA_Account_CreditCard SET CardArrears=CardArrears-" + ConvertHelper.GetDecimal(txtIncomeMoney.Text.Trim()) + " WHERE CreditCardId=" + uclAccount.CreditCardID + "", "constring");//修改信用卡账户欠款金额
            }
            else if (uclAccount.AccountTypeID == 3)//储蓄卡账户
            {
                flags = SqlHelper.Operation("UPDATE dbo.tabA_Account_SavingsCard SET SavingsCardRemainder=SavingsCardRemainder+" + ConvertHelper.GetDecimal(txtIncomeMoney.Text.Trim()) + " WHERE SavingsCardId=" + uclAccount.SavingsCardID + "", "constring");//修改储蓄卡账户余额
            }
            else if (uclAccount.AccountTypeID == 4)//网上支付账户
            {
                flags = SqlHelper.Operation("UPDATE dbo.tabA_Account_OnlinePay SET OnlinePayRemainder=OnlinePayRemainder+" + ConvertHelper.GetDecimal(txtIncomeMoney.Text.Trim()) + " WHERE OnlinePayId=" + uclAccount.OnlinePayID + "", "constring");//修改网上支付账户余额
            }
            if (flags > 0)
            {
                ScriptHelper.AlertMessage(this.Page, "添加成功！");
                IncomeClear();
            }
            else//账户余额修改失败，删除刚添加的支出记录
            {
                SqlHelper.Operation("DELETE FROM dbo.tabA_Record_Income WHERE RecordIncomeId=" + flag + "", "constring");//删除刚添加的支出记录
                ScriptHelper.AlertMessage(this.Page, "添加失败，请联系管理员！");
            }
        }
        else
        {
            ScriptHelper.AlertMessage(this.Page,"添加失败，请联系管理员！");
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)//取消事件（即清空）
    {
        IncomeClear();
    }
    public void IncomeClear()
    {
        txtIncomeMoney.Text = "";//收入金额
        OfficeManage.BindDdlIncomeType(ddlIncomeType);//绑定收入类型
        OfficeManage.BindDdlSeller(ddlSeller);//绑定付款方
        uclAccount.AccountTypeID = 1;//账户类型为现金类型
        uclAccount.CashAccountID = 0;//账户为空
        txtIncomeDateTime.Text = "";//收入时间
        OfficeManage.BindDdlProject(ddlProject);//绑定项目
        txtIncomeRemarks.Text = "";//备注
    }
}