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

public partial class Admin_VendorDiversion : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["AdminEMAILID"] == null)
            {
                Response.Redirect("AdminLogin.aspx");
            }
            if (!Page.IsPostBack)
            {
                BindVendor();
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }
    private void BindVendor()
    {
        DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_GetOperatorVendor");

        gvVendor.DataSource = ds;
        gvVendor.DataBind();
    }
    protected void gvVendor_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList ddlVendor = ((DropDownList)e.Row.FindControl("ddlVendor"));
            DropDownList ddlVendorSMS = ((DropDownList)e.Row.FindControl("ddlVendorsms"));
            string key = gvVendor.DataKeys[e.Row.RowIndex].Values[0].ToString();
            string vendor = gvVendor.DataKeys[e.Row.RowIndex].Values[1].ToString();
            string vendorsms = gvVendor.DataKeys[e.Row.RowIndex].Values[3].ToString();
             switch (key)
            {
                case "E":
                    ddlVendor.Items.Clear();
                    ddlVendor.Items.Insert(0, "eGenApi");
                    ddlVendor.Items.Insert(1, "ApiWorld");
                    ddlVendor.Items.FindByText(vendor).Selected = true;

                    ddlVendorSMS.Items.Clear();
                    ddlVendorSMS.Items.Insert(0, "eGenApi");
                    ddlVendorSMS.Items.Insert(1, "ApiWorld");
                    ddlVendorSMS.Items.FindByText(vendorsms).Selected = true;

                    break;
                case "EX":
                    ddlVendor.Items.Clear();
                    ddlVendor.Items.Insert(0, "eGenApi");
                    ddlVendor.Items.Insert(1, "ExcelRecharge");
                    ddlVendor.Items.Insert(2, "ApiWorld");
                    ddlVendor.Items.FindByText(vendor).Selected = true;

                    ddlVendorSMS.Items.Clear();
                    ddlVendorSMS.Items.Insert(0, "eGenApi");
                    ddlVendorSMS.Items.Insert(1, "ExcelRecharge");
                    ddlVendorSMS.Items.Insert(2, "ApiWorld");
                    ddlVendorSMS.Items.FindByText(vendorsms).Selected = true;
                    break;
                case "EW":
                    ddlVendor.Items.Clear();
                    ddlVendor.Items.Insert(0, "eGenApi");
                    ddlVendor.Items.Insert(1, "IWTApi");
                    ddlVendor.Items.Insert(2, "ApiWorld");
                    ddlVendor.Items.FindByText(vendor).Selected = true;

                    ddlVendorSMS.Items.Clear();
                    ddlVendorSMS.Items.Insert(0, "eGenApi");
                    ddlVendorSMS.Items.Insert(1, "IWTApi");
                    ddlVendorSMS.Items.Insert(2, "ApiWorld");
                    ddlVendorSMS.Items.FindByText(vendorsms).Selected = true;
                    break;
                case "EXW":
                    ddlVendor.Items.Clear();
                    ddlVendor.Items.Insert(0, "eGenApi");
                    ddlVendor.Items.Insert(1, "ExcelRecharge");
                    ddlVendor.Items.Insert(2, "IWTApi");
                    ddlVendor.Items.Insert(3, "ApiWorld");
                    ddlVendor.Items.FindByText(vendor).Selected = true;

                    ddlVendorSMS.Items.Clear();
                    ddlVendorSMS.Items.Insert(0, "eGenApi");
                    ddlVendorSMS.Items.Insert(1, "ExcelRecharge");
                    ddlVendorSMS.Items.Insert(2, "IWTApi");
                    ddlVendorSMS.Items.Insert(3, "ApiWorld");
                    ddlVendorSMS.Items.FindByText(vendorsms).Selected = true;
                    break;
            }
        }
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            Boolean flag = true;
            foreach (GridViewRow rowItem in gvVendor.Rows)
            {
                CheckBox chk = (CheckBox)(rowItem.Cells[1].FindControl("chkSelect"));
                if (chk.Checked)
                {
                    Int32 opID = Convert.ToInt32(gvVendor.DataKeys[rowItem.RowIndex].Values[2]);
                    DropDownList ddlProvider = (DropDownList)(rowItem.Cells[4].FindControl("ddlVendor"));
                    DropDownList ddlProvidersms = (DropDownList)(rowItem.Cells[4].FindControl("ddlVendorsms"));
                    SqlParameter[] param = new SqlParameter[]
                {
                    new SqlParameter("@opID", opID),
                    new SqlParameter("@serviceProvider",ddlProvider.SelectedItem.Text),
                    new SqlParameter("@serviceProvidersms",ddlProvidersms.SelectedItem.Text),
                };
                    SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_UpdateOperatorVendor", param);

                    lblMsg.Text = "Vendor Updated Successfully";
                    lblMsg.ForeColor = Color.Blue;

                    BindVendor();
                    flag = false;
                }
                else if (flag)
                {
                    lblMsg.Text = "Please Select Any Operator";
                    lblMsg.ForeColor = Color.Red;
                }
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }
}

