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 OperatorControl : 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)
        {
            TextBox txtRemark = ((TextBox)e.Row.FindControl("txtRemark"));
            string remark = gvVendor.DataKeys[e.Row.RowIndex].Values[0].ToString();

            txtRemark.Text = remark;
        }
    }

    protected void btnAction_Click(object sender, EventArgs e)
    {
        try
        {
            Boolean flag = true;
            foreach (GridViewRow rowItem in gvVendor.Rows)
            {
                CheckBox chk = (CheckBox)(rowItem.Cells[0].FindControl("chkSelect"));
                if (chk.Checked)
                {
                    Int32 opID = Convert.ToInt32(gvVendor.DataKeys[rowItem.RowIndex].Values[1]);
                    TextBox txtRemark = (TextBox)(rowItem.Cells[5].FindControl("txtRemark"));
                    string status = rowItem.Cells[4].Text.ToString();
                    SqlParameter[] param = new SqlParameter[]
                {
                    new SqlParameter("@opID", opID),
                    new SqlParameter("@status", status),
                    new SqlParameter("@remark", txtRemark.Text.Trim())
                };
                    SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_UpdateOperatorStatus", param);

                    BindVendor();
                    lblMsg.Text = "Updated Successfully";
                    lblMsg.ForeColor = Color.Blue;

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

