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

public partial class Admin_ManageTrainID : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["AdminEMAILID"] == null)
            {
                Response.Redirect("AdminLogin.aspx");
            }
            if (!IsPostBack)
            {
                gvDetails.Visible = false;
                lblMsg.Visible = false;
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }
    protected void btnFind_Click(object sender, EventArgs e)
    {
        if (txtAgentID.Text == "")
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Please Enter Agent ID');"), true);
            return;
        }
        else if(ddlService.SelectedValue=="0")
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Please Select Service');"), true);
            return;
        }
        else
        {
            SqlParameter[] warlock = new SqlParameter[]
            {
                new SqlParameter("@AgentID",txtAgentID.Text.Trim())
            };
            DataSet Ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "SetTrainUserID", warlock);
            if (Ds.Tables[0].Rows.Count > 0)
            {
                gvDetails.Visible=true;
                gvDetails.DataSource = Ds.Tables[0];
                gvDetails.DataBind();
               
                lblMsg.Text = "";
            }
            else
            {
                gvDetails.Visible = false;
                gvDetails.DataSource = null;
                gvDetails.DataBind();
             
                lblMsg.Text = "";
            }
        }
    }
    protected void gvDetails_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Upd")
        {
            int index = Convert.ToInt32(e.CommandArgument.ToString());
            TextBox txtUserid = (TextBox)gvDetails.Rows[index].FindControl("txtTrainUserId");
            TextBox txtAEPSUserid = (TextBox)gvDetails.Rows[index].FindControl("txtPayworldAEPS");
            TextBox txtEzyTap = (TextBox)gvDetails.Rows[index].FindControl("txtEzyTap");
            HiddenField hf1 = (HiddenField)gvDetails.Rows[index].FindControl("hf1");
            //HiddenField hf2 = (HiddenField)gvDetails.Rows[index].FindControl("hf2");
            string MappUserId = "";
            if(ddlService.SelectedValue == "Train")
            {
                MappUserId = txtUserid.Text.Trim();
            }
            else if(ddlService.SelectedValue == "PayworldAEPS")
            {
                MappUserId = txtAEPSUserid.Text.Trim();
            }
            else
            {
                MappUserId = txtEzyTap.Text.Trim();
            }
            if (true)
            {
                SqlParameter[] warlock = new SqlParameter[3];
                warlock[0] = new SqlParameter("@AgentID", hf1.Value);
                warlock[1] = new SqlParameter("@Type", ddlService.SelectedValue);
                warlock[2] = new SqlParameter("@ServiceAGID", MappUserId);

                //string sql = "update Login_detail set TrainID='" + txtUserid.Text.Trim() + "' where USERNAME='" + hf1.Value + "'";
                int a = SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "SetTrainUserID" , warlock);
                if (a > 0)
                {
                    lblMsg.Visible = true;
                    lblMsg.ForeColor = System.Drawing.Color.Red;
                    lblMsg.Text = "Congratulations!! "+ ddlService.SelectedItem.Text + " User ID has been Set to given Agent.";
                   
                }
            }
        }
    }
}