using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
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 System.Xml.Linq;
using System.Data.SqlClient;
using System.Drawing;

public partial class Admin_AddCoupanDetails : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["AdminEMAILID"] == null)
            {
                Response.Redirect("AdminLogin.aspx");
            }
            if (!IsPostBack)
            {
                BindGridview();
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }    
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            SqlParameter[] CoupanDetail = new SqlParameter[]
            {
                new SqlParameter("@CoupanNo",txtCoupanNo.Text),
                new SqlParameter("@Status","Unused"),
                new SqlParameter("@ErrorResult",SqlDbType.Int),
            };
            CoupanDetail[2].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "up_coupandetails", CoupanDetail);
            int loginvariable = Convert.ToInt32(CoupanDetail[2].Value);
            if (loginvariable == 2)
            {
                lblsucess.Text = "Coupon already exists";
                lblsucess.ForeColor = Color.Red;
            }
            else
            {
                lblsucess.Text = "Coupon add successfully";
                lblsucess.ForeColor = Color.Blue;
            }
            txtCoupanNo.Text = "";
            BindGridview();
        }
        catch (Exception ex)
        {
            throw (ex);
        }
    }


    public void BindGridview()
    {
        try
        {
            SqlParameter[] param = new SqlParameter[]
            {                
                new SqlParameter("@action","select"),
                new SqlParameter("@pIntErrDescOut",SqlDbType.Int)
            };
            param[1].Direction = ParameterDirection.Output;
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "up_get_Coupon_Detail_Admin", param);
            if (ds.Tables[0].Rows.Count > 0)
            {
                GridViewSample.DataSource = ds;
                GridViewSample.DataBind();
            }
            else
            {
                
            }
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem.Please try again');"), true);
            throw (ex);
        }
    }
    protected void GridViewSample_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {

    }
    protected void GridViewSample_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        try
        {
            GridViewSample.EditIndex = -1;
            BindGridview();
        }
        catch (Exception ex)
        {
            throw (ex);
        }    
    }
    protected void GridViewSample_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            Label Id = (Label)GridViewSample.Rows[e.RowIndex].FindControl("lblCustID");
            SqlParameter[] deletedata = new SqlParameter[]
        {                
            new SqlParameter("@Id",Id.Text),
            new SqlParameter("@action","delete"),
            new SqlParameter("@pIntErrDescOut",SqlDbType.Int)
        };
            deletedata[2].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "up_get_Coupon_Detail_Admin", deletedata);
            GridViewSample.EditIndex = -1;
            BindGridview();
        }
        catch (Exception ex)
        {
            throw (ex);
        }    
    }
    protected void GridViewSample_RowEditing(object sender, GridViewEditEventArgs e)
    {
        try
        {
            GridViewSample.EditIndex = e.NewEditIndex;
            BindGridview();
        }
        catch (Exception ex)
        {
            throw (ex);
        }    
    }
    protected void GridViewSample_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            int retVal = 0;
            Label Id = (Label)GridViewSample.Rows[e.RowIndex].FindControl("lblCustID");
            TextBox CouponNo = (TextBox)GridViewSample.Rows[e.RowIndex].FindControl("txtcouponno");

            SqlParameter[] updatdata = new SqlParameter[]
        {                
            new SqlParameter("@Id",Id.Text),
            new SqlParameter("@action","update"),
            new SqlParameter("@CoupanNo",CouponNo.Text),

            new SqlParameter("@pIntErrDescOut",SqlDbType.Int)
        };
            updatdata[3].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "up_get_Coupon_Detail_Admin", updatdata);
            GridViewSample.EditIndex = -1;
            BindGridview();
        }
        catch (Exception ex)
        {
            throw (ex);
        }    
    }
}
