using DataAccess;
using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace VMT
{
    public partial class TestMap : System.Web.UI.Page
    {
        string m_SP_FM_SEL_INSPECTION_SUB_TYPE = Properties.Settings.Default.SP_FM_SEL_INSPECTION_SUB_TYPE;
        string m_SP_FM_SEL_EVENTCODE = Properties.Settings.Default.SP_FM_SEL_EVENTCODE;
        string m_SP_FM_SEL_ISSUE_TYPE = Properties.Settings.Default.SP_FM_SEL_ISSUE_TYPE;
        string m_SP_FM_SEL_ISSUE_SUB_TYPE = Properties.Settings.Default.SP_FM_SEL_ISSUE_SUB_TYPE;
        string m_SP_FM_SEL_ISSUE_STATUS = Properties.Settings.Default.SP_FM_SEL_ISSUE_STATUS;
        string m_SP_FM_SEL_ASSIGNEDTO = Properties.Settings.Default.SP_FM_SEL_ASSIGNEDTO;
        string m_SP_FM_SEL_COMP_ASSIGNEDTO = Properties.Settings.Default.SP_FM_SEL_COMP_ASSIGNEDTO;

        string m_SP_FLV_SELECT_INSPECTION_GROUP = Properties.Settings.Default.SP_FLV_SELECT_INSPECTION_GROUP;
        string m_SP_FLV_SEL_ISSUE_STATUS = Properties.Settings.Default.SP_FLV_SEL_ISSUE_STATUS;
        string m_SP_FLV_SEL_FLIGHT_RECORDS = Properties.Settings.Default.SP_FLV_SEL_FLIGHT_RECORDS;
        string m_SP_FLV_SEL_EVENT_CODES = Properties.Settings.Default.SP_FLV_SEL_EVENT_CODES;
        string m_SP_FLV_SEL_LINE_NAME = Properties.Settings.Default.SP_FLV_SEL_LINE_NAME;
        string m_SP_FLV_SEL_ISSUE_TYPE = Properties.Settings.Default.SP_FLV_SEL_ISSUE_TYPE;
        string m_SP_FLV_SEL_ISSUE_SUB_TYPE = Properties.Settings.Default.SP_FLV_SEL_ISSUE_SUB_TYPE;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                bindInspectionGroup();
                bindIssueStatus();
                bindFlight();
                bindAssignedTo();
                bindCrew();
                bindUpdatedIssueStatus();
                bindLineName();
                bindEventCode();
                bindIssueType();
                bindIssueSubType();
            }
        }

        //void bindEventCode()
        //{
        //    try
        //    {
        //        DataAccessUtility.BindDropdownListWithoutConditionViaProcedure(m_SP_FM_SEL_EVENTCODE, ddlEventCode);
        //    }
        //    catch
        //    {
        //    }
        //}

        //void bindIssueType()
        //{
        //    try
        //    {
        //        List<OleDbParameter> OleDbParams = new List<OleDbParameter>();
        //        OleDbParams.Add(new OleDbParameter("V_EVENT_CODE_ID", ddlEventCode.SelectedValue));
        //        //OleDbParams.Add(new OleDbParameter("V_WORK_AREA_ID", ddlWorkArea.SelectedValue));
        //        //OleDbParams.Add(new OleDbParameter("V_INSPECTION_SUB_TYPE", ddlInspSubType.SelectedValue));
        //        DataAccessUtility.BindDropdownListWithConditionViaProcedure(m_SP_FM_SEL_ISSUE_TYPE, OleDbParams, ddlIssueType);
        //    }
        //    catch
        //    {
        //    }
        //}

        void bindIssueType()
        {
            try
            {
                List<OleDbParameter> OleDbParams = new List<OleDbParameter>();
                OleDbParams.Add(new OleDbParameter("V_INSPECTION_SUB_TYPE", ddlInspSubType.SelectedValue));
                OleDbParams.Add(new OleDbParameter("V_ISSUE_STATUS_ID", ddlIssueStatus.SelectedValue));
                OleDbParams.Add(new OleDbParameter("V_FLIGHT_RECORD_ID", ddlFlight.SelectedValue));
                OleDbParams.Add(new OleDbParameter("V_LINE_NAME", ddlLineId.SelectedValue));
                OleDbParams.Add(new OleDbParameter("V_EVENT_CODE_ID", ddlEventCode.SelectedValue));
                DataAccessUtility.BindDropdownListWithConditionViaProcedure(m_SP_FLV_SEL_ISSUE_TYPE, OleDbParams, ddlIssueType);
            }
            catch
            {
            }
        }

        void bindIssueSubType()
        {
            try
            {
                List<OleDbParameter> OleDbParams = new List<OleDbParameter>();
                OleDbParams.Add(new OleDbParameter("V_INSPECTION_SUB_TYPE", ddlInspSubType.SelectedValue));
                OleDbParams.Add(new OleDbParameter("V_ISSUE_STATUS_ID", ddlIssueStatus.SelectedValue));
                OleDbParams.Add(new OleDbParameter("V_FLIGHT_RECORD_ID", ddlFlight.SelectedValue));
                OleDbParams.Add(new OleDbParameter("V_LINE_NAME", ddlLineId.SelectedValue));
                OleDbParams.Add(new OleDbParameter("V_EVENT_CODE_ID", ddlEventCode.SelectedValue));
                OleDbParams.Add(new OleDbParameter("V_ISSUE_ID", ddlIssueType.SelectedValue));
                DataAccessUtility.BindDropdownListWithConditionViaProcedure(m_SP_FLV_SEL_ISSUE_SUB_TYPE, OleDbParams, ddlIssueSubType);
            }
            catch
            {
            }
        }

        void bindInspectionGroup()
        {
            try
            {
                DataAccessUtility.BindDropdownListWithoutConditionViaProcedure(m_SP_FLV_SELECT_INSPECTION_GROUP, ddlInspSubType);
            }
            catch
            {
            }
        }
        void bindIssueStatus()
        {
            try
            {
                List<OleDbParameter> OleDbParams = new List<OleDbParameter>();
                OleDbParams.Add(new OleDbParameter("V_INSPECTION_SUB_TYPE", ddlInspSubType.SelectedValue));
                DataAccessUtility.BindDropdownListWithConditionViaProcedure(m_SP_FLV_SEL_ISSUE_STATUS, OleDbParams, ddlIssueStatus);
            }
            catch
            {
            }
        }
        void bindFlight()
        {
            try
            {
                List<OleDbParameter> OleDbParams = new List<OleDbParameter>();
                OleDbParams.Add(new OleDbParameter("V_INSPECTION_SUB_TYPE", ddlInspSubType.SelectedValue));
                OleDbParams.Add(new OleDbParameter("V_ISSUE_STATUS_ID", ddlIssueStatus.SelectedValue));
                DataAccessUtility.BindDropdownListWithConditionViaProcedure(m_SP_FLV_SEL_FLIGHT_RECORDS, OleDbParams, ddlFlight);
            }
            catch
            {
            }
        }
        void bindLineName()
        {
            try
            {
                List<OleDbParameter> OleDbParams = new List<OleDbParameter>();
                OleDbParams.Add(new OleDbParameter("V_INSPECTION_SUB_TYPE", ddlInspSubType.SelectedValue));
                OleDbParams.Add(new OleDbParameter("V_ISSUE_STATUS_ID", ddlIssueStatus.SelectedValue));
                OleDbParams.Add(new OleDbParameter("V_FLIGHT_RECORD_ID", ddlFlight.SelectedValue));
                DataAccessUtility.BindDropdownListWithConditionViaProcedure(m_SP_FLV_SEL_LINE_NAME, OleDbParams, ddlLineId);
            }
            catch 
            {

                
            }
        }
        void bindEventCode()
        {
            try
            {
                List<OleDbParameter> OleDbParams = new List<OleDbParameter>();
                OleDbParams.Add(new OleDbParameter("V_INSPECTION_SUB_TYPE", ddlInspSubType.SelectedValue));
                OleDbParams.Add(new OleDbParameter("V_ISSUE_STATUS_ID", ddlIssueStatus.SelectedValue));
                OleDbParams.Add(new OleDbParameter("V_FLIGHT_RECORD_ID", ddlFlight.SelectedValue));
                OleDbParams.Add(new OleDbParameter("V_LINE_NAME", ddlLineId.SelectedValue));
                DataAccessUtility.BindDropdownListWithConditionViaProcedure(m_SP_FLV_SEL_EVENT_CODES, OleDbParams, ddlEventCode);
            }
            catch 
            {

               
            }
        }
        void bindAssignedTo()
        {
            try
            {
                DataAccessUtility.BindDropdownListWithoutConditionViaProcedure(m_SP_FM_SEL_ASSIGNEDTO, ddlAssignedTo);
            }
            catch
            {
            }
        }
        void bindCrew()
        {
            try
            {
                List<OleDbParameter> OleDbParams = new List<OleDbParameter>();

                OleDbParams.Add(new OleDbParameter("V_CONTRACTOR_ID", ddlAssignedTo.SelectedValue));
                DataAccessUtility.BindDropdownListWithConditionViaProcedure(m_SP_FM_SEL_COMP_ASSIGNEDTO, OleDbParams, ddlCrew);
            }
            catch
            {
            }
        }
        
        protected void ddlIssueType_SelectedIndexChanged(object sender, EventArgs e)
        {
            bindIssueSubType();
        }

        //protected void ddlIssueType_DataBound(object sender, EventArgs e)
        //{
        //    bindIssueSubType();
        //}

        protected void rblAssign_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rblAssign.SelectedValue == "1") //1-Selected
            {
                //SelectAssignedTo();
            }
        }

        //void SelectAssignedTo()
        //{
        //    int sequencer = 0;
        //    DataTable dt = new DataTable();
        //    try
        //    {
        //        if (hfSequencer.Value != "")
        //        {
        //            sequencer = Convert.ToInt32(hfSequencer.Value);
        //        }
        //        List<OleDbParameter> OleDbParam = new List<OleDbParameter>();

        //        OleDbParam.Add(new OleDbParameter("V_SEQUENCER", sequencer));
        //        dt = DataAccessUtility.SelectDataTableViaStoreProcedureParam(m_SP_FM_SEL_ASSIGNEDTO_SEQ, OleDbParam);
        //        if (dt.Rows.Count > 0)
        //        {
        //            ddlAssignedTo.SelectedValue = dt.Rows[0]["CONTRACTOR_ID"].ToString();
        //        }
        //    }
        //    catch
        //    {
        //    }
        //    finally
        //    {
        //        dt.Clear();
        //        dt.Dispose();
        //    }
        //}

        protected void btnReset_Click(object sender, EventArgs e)
        {
            ddlAssignedTo.ClearSelection();
            txtInspDueDt.Text = "";
        }

        protected void btnAssign_Click(object sender, EventArgs e)
        {
            //int sequencer = 0;
            //int retVal = 0;
            //int company = 0;
            //int crew = 0;

            //if (gdv_list.Rows.Count <= 0)
            //{
            //    ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Please Search to Assign Due Date.')", true);
            //    return;
            //}

            //if (rblAssign.SelectedValue == "1")
            //{
            //    if (Convert.ToInt32(ddlCrew.SelectedValue) == 0)
            //    {
            //        ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Please Select Crew.')", true);
            //        return;
            //    }
            //    else
            //    {
            //        crew = Convert.ToInt32(ddlCrew.SelectedValue.ToString());
            //    }

            //}

            //if (txtInspDueDt.Text == "")
            //{
            //    ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Please Select Inspection Due Date.')", true);
            //    return;
            //}



            //try
            //{
            //    company = Convert.ToInt32(ddlAssignedTo.SelectedValue);
            //    List<OleDbParameter> sqlParams = new List<OleDbParameter>();
            //    if (hfSequencer.Value != "" && rblAssign.SelectedValue == "1")
            //    {
            //        sequencer = Convert.ToInt32(hfSequencer.Value);
            //        sqlParams.Add(new OleDbParameter("V_SEQUENCER", sequencer));
            //        sqlParams.Add(new OleDbParameter("V_ASSIGNEDTO", company));
            //        sqlParams.Add(new OleDbParameter("V_CREW_ID", crew));
            //        sqlParams.Add(new OleDbParameter("V_INSPECTIONDUEDATE", txtInspDueDt.Text.Trim()));
            //        sqlParams.Add(new OleDbParameter("V_LAST_UPDATED_USER_ID", System.Web.HttpContext.Current.User.Identity.Name.Replace("SCANA\\", "").ToUpper()));
            //        retVal = DataAccessUtility.InsertUpdateDeleteViaStoreProcedureParam(m_SP_FM_UPD_ASSIGNEDTO_SEQ, sqlParams);
            //    }
            //}
            //catch { }
            //finally
            //{
            //}

            //if (retVal == 1)
            //{
            //    //javascript function call to update controls
            //    string jsFunc = "LoadDetailsAssignStatus(" + sequencer + ")";
            //    ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "myJsFn", jsFunc, true);

            //    //Rebind the gridtable after updation
            //    string func = "ReloadGridTable()";
            //    ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "myJsFn1", func, true);

            //    bindIssueAssignedTo();
            //    ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Update successful')", true);
            //}
        }

        protected void btnStatusReset_Click(object sender, EventArgs e)
        {
            //dllCurrentStatus.ClearSelection();
            lblCurrentStatus.Text = "";
            ddlUpdatedStatus.ClearSelection();
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            //int sequencer = 0;
            //int retVal = 0;
            //int updstatus = 0;

            //if (gdv_list.Rows.Count <= 0)
            //{
            //    ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Please Search to Assign Status.')", true);
            //    return;
            //}

            //try
            //{
            //    updstatus = Convert.ToInt32(ddlUpdatedStatus.SelectedValue);
            //    List<OleDbParameter> sqlParams = new List<OleDbParameter>();
            //    if (hfSequencer.Value != "" && rblStatus.SelectedValue == "1")
            //    {
            //        sequencer = Convert.ToInt32(hfSequencer.Value);
            //        sqlParams.Add(new OleDbParameter("V_SEQUENCER", sequencer));
            //        sqlParams.Add(new OleDbParameter("V_ISSUE_STATUS_ID", updstatus));
            //        sqlParams.Add(new OleDbParameter("V_LAST_UPDATED_USER_ID", System.Web.HttpContext.Current.User.Identity.Name.Replace("SCANA\\", "").ToUpper()));
            //        // sqlParams.Add(new OleDbParameter("V_RETURN_VALUE", 1));
            //        retVal = DataAccessUtility.InsertUpdateDeleteViaStoreProcedureParam(m_SP_FM_UPD_ISSUE_STATUS_SEQ, sqlParams);
            //    }
            //}
            //catch { }
            //finally
            //{
            //}

            //if (retVal == 1)
            //{
            //    //javascript function call to update controls
            //    string jsFunc = "LoadDetailsAssignStatus(" + sequencer + ")";
            //    ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "myJsFn", jsFunc, true);

            //    //Rebind the gridtable after updation
            //    string func = "ReloadGridTable()";
            //    ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "myJsFn1", func, true);

            //    bindIssueStatus();
            //    ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Update successful')", true);
            //}
        }

        protected void rblStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rblStatus.SelectedValue == "1") //1-Selected
            {
                //SelectStatus();
            }
        }

        protected void gdv_list_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(gdv_list, "Select$" + e.Row.RowIndex);
                e.Row.Attributes["style"] = "cursor:pointer";
                e.Row.ToolTip = "Click to select this row.";
            }
        }
        protected void ddlInspSubType_SelectedIndexChanged(object sender, EventArgs e)
        {
            bindIssueStatus();
        }
        protected void ddlIssueStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            bindFlight();
        }
        protected void ddlIssueStatus_DataBound(object sender, EventArgs e)
        {
            bindFlight();
        }
        void bindUpdatedIssueStatus()
        {
            try
            {
                DataAccessUtility.BindDropdownListWithoutConditionViaProcedure(m_SP_FM_SEL_ISSUE_STATUS, ddlUpdatedStatus);
            }
            catch
            {
            }
        }
        protected void ddlLineId_SelectedIndexChanged(object sender, EventArgs e)
        {
            bindLineName();
        }
        protected void ddlFlight_SelectedIndexChanged(object sender, EventArgs e)
        {
            bindEventCode();
        }
    }
}