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_Messages : System.Web.UI.Page
{
    protected void Bind() {
        string sql = "select * from 呼叫表 where eid="+Session["LoginID"].ToString()+" order by id desc";
        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 Gridview1_RowDeleting(object o, GridViewDeleteEventArgs e)
    {
        if (e.RowIndex > -1) {
            if (GridView1.DataKeys[e.RowIndex].Values["处理状态"].ToString().Equals("-1"))
            {
                string price = SqlHelper.ExecuteScalar(SqlHelper.connstr, CommandType.Text, "select price from 短信价格表", null).ToString();

                string sql2 = "update 客户余额表 set 可用余额=(可用余额+" + decimal.Parse(GridView1.DataKeys[e.RowIndex].Values["未发送"].ToString()) * decimal.Parse(price) + ") where id=(select top 1 id from 客户余额表 where 充值状态=1 and eid=" + GridView1.DataKeys[e.RowIndex].Values["eid"].ToString() + " order by id desc)";
                SqlHelper.ExecuteNonQuery(SqlHelper.connstr, CommandType.Text, sql2, null);

                string sql = "delete from 呼叫表 where id=" + GridView1.DataKeys[e.RowIndex].Values["id"].ToString();
                SqlHelper.ExecuteNonQuery(SqlHelper.connstr, CommandType.Text, sql, null);
                Bind();
                RegisterClientScriptBlock("", "<script language=javascript>alert('删除成功!');</script>");
            }
            else {
                RegisterClientScriptBlock("", "<script language=javascript>alert('正在处理中，不能删除!');</script>");
            }
        }
        
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        CRM.Common.Login.IsLogin();
        if (!IsPostBack) {
            Bind();
        }

    }
}
