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.Text;
using System.Net.Mail;
using System.Net;
using System.Drawing;
using System.IO;

public partial class Admin_WLManageOperator : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["AdminEMAILID"] == null)
            {
                Response.Redirect("AdminLogin.aspx");
            }
            if (!IsPostBack)
            {
                BindDdlForWl();
            }
            gvVendor.Visible = false;
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }
    private void BindDdlForWl()
    {
        try
        {
            SqlParameter[] param = new SqlParameter[] { };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udpGetWl", param);
            if (ds.Tables[0].Rows.Count > 0)
            {
                ddl_selectUserId.DataSource = ds;
                ddl_selectUserId.DataTextField = "username";
                ddl_selectUserId.DataValueField = "loginid";
                ddl_selectUserId.DataBind();
            }
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem. Please try again');"), true);
        }
    }
    protected void ddl_selectUserId_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindVendor();
    }

    private void BindVendor()
    {
        SqlParameter[] param = new SqlParameter[]
        {
           new SqlParameter("@Userid",ddl_selectUserId.SelectedValue.Trim()),
        };
        DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_GetOperatorForWL", param);
        if (ds.Tables[0].Rows.Count > 0)
        {
            gvVendor.Visible = true;
            gvVendor.DataSource = ds;
            gvVendor.DataBind();
        }
        else
        {
            gvVendor.Visible = false;
        }
    }

    protected void gvVendor_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string strScript = "SelectDeSelectHeader(" + ((CheckBox)e.Row.Cells[0].FindControl("chkSelect")).ClientID + ");";
                ((CheckBox)e.Row.Cells[0].FindControl("chkSelect")).Attributes.Add("onclick", strScript);
            }
        }
        catch (Exception Ex)
        {
            //report error
        }
    }

    protected void btnAction_Click(object sender, EventArgs e)
    {
        gvVendor.Visible = true;
        try
        {
            Boolean flag = true;
            foreach (GridViewRow rowItem in gvVendor.Rows)
            {
                //CheckBox chk = (CheckBox)(rowItem.Cells[0].FindControl("chkSelect"));
                if (((CheckBox)rowItem.FindControl("chkSelect")).Checked == true)
                {
                    Int32 opID = Convert.ToInt32(gvVendor.DataKeys[rowItem.RowIndex].Values[0]);
                    string status = rowItem.Cells[3].Text.ToString();
                    SqlParameter[] param = new SqlParameter[]
                    {
                        new SqlParameter("@Userid",ddl_selectUserId.SelectedValue.Trim()),
                        new SqlParameter("@opID", opID),
                        new SqlParameter("@status", status)
                    };
                    SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_UpdateOperatorStatusVB", param);                    
                    flag = false;
                }               
            }
            if (flag)
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Please Select Any Operator');"), true);
            }
            else
            {
                BindVendor();
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Updated Successfully');"), true);
            }
        }
        catch (Exception ex)
        {
            //Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some Problem. Please try after some time');"), true);
            throw (ex);
        }
    }
}
