using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using CRM.BLL;
using CRM.Model;
using CRM.Common;
using CRM.DBUtility;
using System.Data.SqlClient;

public partial class Pages_sxt_Moneys3 : System.Web.UI.Page
{
    protected void Bind()
    {
        string sql = "select * from 客户余额表 where 充值状态=0";
        GridView1.DataSource = SqlHelper.ExecuteDataSet(SqlHelper.connstr, CommandType.Text, sql, null);
        GridView1.DataKeyNames = new string[] { "id","eid","充值" };
        GridView1.DataBind();

    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        Bind();
    }
    protected void Button2_Click(object o, EventArgs e)
    {
        string sql = "insert into 客户余额表(eid,充值) values(@eid,@充值)";
        try
        {
            SqlParameter[] paras = new SqlParameter[2];
            paras[0] = new SqlParameter("eid", Session["LoginID"].ToString());
            paras[1] = new SqlParameter("充值", decimal.Parse(TextBox1.Text.Trim()));
            SqlHelper.ExecuteNonQuery(SqlHelper.connstr, CommandType.Text, sql, paras);

            RegisterClientScriptBlock("", "<script language=javascript>alert('充值申请已提交，请等待处理!');</script>");

            Bind();
        }
        catch
        {
            RegisterClientScriptBlock("", "<script language=javascript>alert('只允许输入数值，请检查!');</script>");
        }

    }
    public string GetClientMobile(string eid)
    {
        string sql = "select 手机 from 员工表 where eid=" + eid;
        return SqlHelper.ExecuteScalar(SqlHelper.connstr, CommandType.Text, sql, null).ToString();
    }
    protected void GridView_RowCommand(object sender,GridViewCommandEventArgs e)
    {
        #region  审核
        if (e.CommandName == "Yes") {
            GridViewRow gvr = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
            int index = gvr.RowIndex;

            string sql = "select top 1 * from 客户余额表 where eid="+GridView1.DataKeys[index].Values["eid"].ToString() + " order by id desc";
            DataTable dt = SqlHelper.ExecuteDataSet(SqlHelper.connstr,CommandType.Text,sql,null).Tables[0];
            if (dt.Rows.Count > 0)
            {

                sql = "update 客户余额表 set 充值状态=1,余额=@余额,可用余额=@可用余额 where id=@id ";
                try
                {
                    SqlParameter[] paras = new SqlParameter[3];
                    paras[0] = new SqlParameter("id", e.CommandArgument);
                    paras[1] = new SqlParameter("余额", decimal.Parse(dt.Rows[0]["余额"].ToString()) + decimal.Parse(GridView1.DataKeys[index].Values["充值"].ToString()));
                    paras[2] = new SqlParameter("可用余额", decimal.Parse(dt.Rows[0]["可用余额"].ToString()) + decimal.Parse(GridView1.DataKeys[index].Values["充值"].ToString()));
                    SqlHelper.ExecuteNonQuery(SqlHelper.connstr, CommandType.Text, sql, paras);

                    Bind();
                }
                catch
                {
                    RegisterClientScriptBlock("", "<script language=javascript>alert('只允许输入数值，请检查!');</script>");
                }
            }
            else {
                sql = "update 客户余额表 set 充值状态=1,余额=@余额,可用余额=@可用余额 where id=@id ";
                try
                {
                    SqlParameter[] paras = new SqlParameter[3];
                    paras[0] = new SqlParameter("id", e.CommandArgument);
                    paras[1] = new SqlParameter("余额", decimal.Parse(GridView1.DataKeys[index].Values["充值"].ToString()));
                    paras[2] = new SqlParameter("可用余额", decimal.Parse(dt.Rows[0]["可用余额"].ToString()));
                    SqlHelper.ExecuteNonQuery(SqlHelper.connstr, CommandType.Text, sql, paras);

                    Bind();
                }
                catch
                {
                    RegisterClientScriptBlock("", "<script language=javascript>alert('只允许输入数值，请检查!');</script>");
                }
            }
        }
        #endregion

        #region 删除
        if (e.CommandName == "Del") {
            string sql = "delete from 客户余额表 where id="+e.CommandArgument;
            SqlHelper.ExecuteNonQuery(SqlHelper.connstr, CommandType.Text, sql, null);

            Bind();
        }
            
        #endregion
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        CRM.Common.Login.IsLogin();
        if (!IsPostBack)
        {
            Bind();
        }
    }
}
