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

public partial class ClaimsSummary : System.Web.UI.Page
{
    //string strCardNumber = "";
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            tbl_ClaimSummary.Visible = false;
           
        }
        else
            tbl_ClaimSummary.Visible = true;
        //Button1_Click(sender, e);
        lblError.Text = "";
      
        showGridCoumns();
        
    }
        //'0001' Entered_pending,  '0002' Entered_SamaAccept,  '0003' Entered_SamaReject,
        //      '0071' AS PreApproved_Pending ,'0072' AS PreApproved_SamaAccept,  '0073' AS PreApproved_SamaReject,
        //      '0031' AS Approved_Pending, '0032' AS Approved_SamaAccept,  '0033' Approved_SamaReject,
        //      '0041' as Posted_Pending,'0042' as Posted_SamaAccept,'0043' Posted_SamaReject,
        //      '3011' as SAMAClaim_CPS_Pending,
        //      '3012' as SAMAClaim_CPS_SamaAccept,  /*V 1.6.3*/
        //      '3013' as SAMAClaim_CPS_SamaReject,  /*V 1.6.3*/
        //      '0000' as Entered, '0070' as PreApproved,'0030' as Approved,'0040' as Posted,
        //      '0020' AS Rejected,  '0050' AS Declined, '0053' Declined_SamaReject) /*V 1.6.3*/
  
    private void showGridCoumns()
    {

        GridView1.Columns[0].HeaderText = "AgingDays";
        for (int i = 1; i < 26; i++)
            GridView1.Columns[i].Visible = false;

        if (rdClaimOn.Checked == true)
        {

            GridView1.Columns[4].Visible = true;
            

     
            GridView1.Columns[6].Visible = true;
            GridView1.Columns[9].Visible = true;
            GridView1.Columns[2].Visible = true;
            GridView1.Columns[13].Visible = true;
            GridView1.Columns[16].Visible = true;
            GridView1.Columns[21].Visible = true;

            GridView1.Columns[4].HeaderText = "Entered<br/>CPS pending";
            GridView1.Columns[6].HeaderText = "Pre-Approved <br/>CPS pending";
            GridView1.Columns[9].HeaderText = "Approved <br/>CPS pending";
            GridView1.Columns[2].HeaderText = "Sent to CPS";
            GridView1.Columns[13].HeaderText = "Closed accepted<br/> Posting pending";
            GridView1.Columns[16].HeaderText = "Closed accepted <br/> Posted";
            GridView1.Columns[21].HeaderText = "Sent to CPS <br/> Closed Rejected";
         


        }
        else if(rdClaimOff.Checked==true)
        {

	

            GridView1.Columns[2].Visible = true;
 
            GridView1.Columns[7].Visible = true;
            GridView1.Columns[8].Visible = true;
             GridView1.Columns[10].Visible = true;
            GridView1.Columns[13].Visible = true;
            GridView1.Columns[16].Visible = true;

            GridView1.Columns[11].Visible = true;
     
            GridView1.Columns[24].Visible = true;

             
            GridView1.Columns[2].HeaderText = "CPS claim<br/>received";
             GridView1.Columns[7].HeaderText = "Closed Accepted<br/>Pre-Approval pending";
              GridView1.Columns[8].HeaderText = "Closed Rejected <br/>Entered";
              GridView1.Columns[10].HeaderText = "Closed Accepted <br/> Approval pending";
              GridView1.Columns[13].HeaderText = "Closed Accepted <br/> Posting pending";
              GridView1.Columns[16].HeaderText = "Closed Accepted <br/> GL posted";
             GridView1.Columns[11].HeaderText = " Closed Rejected <br/>  Pre approved";
             GridView1.Columns[24].HeaderText = " Closed Rejected <br/>  Declined";
   
       
        }
        else if (rdClaimOnUS.Checked == true)
        {
            GridView1.Columns[4].Visible = true;
            GridView1.Columns[5].Visible = true;
            GridView1.Columns[9].Visible = true;
            GridView1.Columns[12].Visible = true;
            GridView1.Columns[22].Visible = true;
            GridView1.Columns[23].Visible = true;
         
            GridView1.Columns[4].HeaderText = "Entered";
            GridView1.Columns[5].HeaderText = "Pre-approved";
            GridView1.Columns[9].HeaderText = "Approved <br/>Posting pending";
            GridView1.Columns[12].HeaderText = "Posted";
            GridView1.Columns[22].HeaderText = "Rejected";
            GridView1.Columns[23].HeaderText = "Declined";
         


        }

    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Panel1.Visible = false;
        loadClaims();
        Button1_Click(sender, e);
        GridView1.SelectedIndex = -1;
    }

    private void loadClaims()
    {
        string DBConnection = Helper.dbConnection();

        OracleConnection oracon = new OracleConnection(DBConnection);
        oracon.Open();

        OracleCommand oracom = new OracleCommand();
        oracom.Connection = oracon;

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "claim_process.get_claims_summary";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_on_off_Us_Flag", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_output", OracleType.Cursor).Direction = ParameterDirection.Output;

        if (rdClaimOn.Checked == true)
            oracom.Parameters["p_on_off_Us_Flag"].Value = "on";
        else if(rdClaimOff.Checked==true)
            oracom.Parameters["p_on_off_Us_Flag"].Value = "off";
        else if (rdClaimOnUS.Checked==true)
            oracom.Parameters["p_on_off_Us_Flag"].Value = "on-us";

        
        oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

        OracleDataAdapter da = new OracleDataAdapter(oracom);
        DataSet ds = new DataSet();
        da.Fill(ds);

        oracon.Close();

        GridView1.DataSource = ds;
        GridView1.DataBind();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Select$" + e.Row.RowIndex);
            //e.Row.ToolTip = "Click to select this row.";
            //e.Row.Attributes.Add("style", "cursor: pointer");

            LinkButton _singleClickButton = (LinkButton)e.Row.Cells[26].Controls[0];  
            string _jsSingle = ClientScript.GetPostBackClientHyperlink(_singleClickButton, "");  
            // Add events to each editable cell  
            for (int columnIndex = 1; columnIndex < e.Row.Cells.Count; columnIndex++)
            {
                // Add the column index as the event argument parameter  
                string js = _jsSingle.Insert(_jsSingle.Length - 2, columnIndex.ToString());
                // Add this javascript to the onclick Attribute of the cell  
                e.Row.Cells[columnIndex].Attributes["onclick"] = js;
                // Add a cursor style to the cells  
                e.Row.Cells[columnIndex].Attributes["style"] += "cursor:pointer;cursor:hand;";
            }
        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            if (row.RowIndex == GridView1.SelectedIndex)
            {
                row.BackColor = ColorTranslator.FromHtml("#A1DCF2");
                row.ToolTip = string.Empty;
                //Button1_Click(sender, e);
            }
            else
            {
                row.BackColor = ColorTranslator.FromHtml("#FFFFFF");
                row.ToolTip = "Click to select this row.";
            }
        }

        /************************************** Draw Donut chart code ****************************************/
        Button2_Click(sender, e);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string days = "[";

        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            days += "'" + GridView1.Rows[i].Cells[0].Text + "',";
        }

        days = days.Substring(0, days.Length - 1) + "]";

        List<string> data = new List<string>();

        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            string dataItem = "[";
            int total = 0;
            for (int j = 1; j < GridView1.Rows[i].Cells.Count; j++)
            {
                //if (GridView1.Rows[i].Cells[j].Text.Equals("0"))
                //    dataItem += ",";
                //else
                //dataItem += GridView1.Rows[i].Cells[j].Text + ",";
                int val = 0;
                if (int.TryParse(GridView1.Rows[i].Cells[j].Text,out val))
                    total += int.Parse(GridView1.Rows[i].Cells[j].Text);
            }
            dataItem += total.ToString() + "]";
            data.Add(dataItem);
        }

        string values = "[";

        foreach (string item in data)
        {
            values += item + ",";
        }
        values = values.Substring(0, values.Length - 1) + "]";

        string script = " <script>"+
        "$(document).ready(function () "+
        "{ "+
            "var data = " + values + "; " +

            "var bar = new RGraph.Bar({ "+
                "id: 'cvs', "+
                "data: data, "+
                "options: { "+
                    "textSize: 12, "+
                    "colors: ['Gradient(#ccf:#33f)'], "+
                    "labels: "+days+", "+
                    "numyticks: 5, "+
                    "ylabelsCount: 5, "+
                    "gutterLeft: 35, "+
                    "variant: '3d', "+
                    "labelsAbove: true, " +
                                        "labelsAboveDecimals: 0, " +
                    "strokestyle: 'transparent', "+
                    "hmarginGrouped: 0, "+
                    "shadow: false, "+
                    "gutterBottom: 120, "+
                    "noaxes: true, "+
                    //"ymax: 100 "+
                "} "+
            "}).wave(); "+
        "}); "+
        "</script>";
        ScriptManager.RegisterClientScriptBlock(this, typeof(string), "new window",
                            script, false);



    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        string days = "[";

        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            days += "'" + GridView1.Rows[i].Cells[0].Text + "',";
        }

        days = days.Substring(0, days.Length - 1) + "]";

        List<string> data = new List<string>();

        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            string dataItem = "[";
            int total = 0;
            for (int j = 1; j < GridView1.Rows[i].Cells.Count; j++)
            {
                //if (GridView1.Rows[i].Cells[j].Text.Equals("0"))
                //    dataItem += ",";
                //else
                //dataItem += GridView1.Rows[i].Cells[j].Text + ",";
                int val = 0;
                if (int.TryParse(GridView1.Rows[i].Cells[j].Text, out val))
                    total += int.Parse(GridView1.Rows[i].Cells[j].Text);
            }
            dataItem += total.ToString() + "]";
            data.Add(dataItem);
        }

        string values = "[";

        foreach (string item in data)
        {
            values += item + ",";
        }
        values = values.Substring(0, values.Length - 1) + "]";

        string script = 
        "$(document).ready(function () " +
        "{ " +
            "var data = " + values + "; " +

            "var bar = new RGraph.Bar({ " +
                "id: 'cvs', " +
                "data: data, " +
                "options: { " +
                    "textSize: 12, " +
                    "colors: ['Gradient(#ccf:#33f)'], " +
                    "labels: " + days + ", " +
                    "numyticks: 5, " +
                    "ylabelsCount: 5, " +
                    "gutterLeft: 35, " +
                    "variant: '3d', " +
                    "labelsAbove: true, " +
                                        "labelsAboveDecimals: 0, " +
                    "strokestyle: 'transparent', " +
                    "hmarginGrouped: 0, " +
                    "shadow: false, " +
                    "gutterBottom: 120, " +
                    "noaxes: true, " +
            //"ymax: 100 "+
                "} " +
            "}).draw();" +
        "}); " ;

        string dataItem1 = "[";
        string names = "[";
        for (int j = 1; j < GridView1.Rows[GridView1.SelectedIndex].Cells.Count; j++)
        {
            if (GridView1.Rows[GridView1.SelectedIndex].Cells[j].Text != "0")
            {
                dataItem1 += GridView1.Rows[GridView1.SelectedIndex].Cells[j].Text + ",";
                switch (j)
                {
                    case 1: names += "'Entered',"; break;
                    case 2: names += "'PreApproved',"; break;
                    case 3: names += "'Approved',"; break;
                    case 4: names += "'Declined',"; break;
                    case 5: names += "'AutoEntered',"; break;
                    case 6: names += "'JE Posted',"; break;
                    case 7: names += "'SAMAClaim',"; break;
                }
            }
        }

        dataItem1 = dataItem1.Substring(0, dataItem1.Length - 1) + "]";
        names = names.Substring(0, names.Length - 1) + "]";


        string script1 = " $(document).ready(function () { " +
                                        "var pie = new RGraph.Pie({ " +
                                            "id: 'cvs1', " +
                                            "data: " + dataItem1 + ", " +
                                            "options: { " +
                                                "shadow: true, " +
                                                "shadowOffsetx: 0, " +
                                                "shadowOffsety: 5, " +
                                                "shadowColor: '#aaa', " +
                                                "radius: 50, " +
                                                "variant: 'donut3d', " +
                                                "labels: "+names+", " +
                                                "labelsSticks: true, " +
                                                "labelsSticksLength: 15 " +
                                            "} " +
                                        "}).draw(); " +
                                    "}); " +
                            " ";

        ScriptManager.RegisterClientScriptBlock(this, typeof(string), "new window",
                            script+script1, true);
    }

    protected void GridView1_RowCommand1(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.ToString() == "ColumnClick")
        {
            foreach (GridViewRow r in GridView1.Rows)
            {
                if (r.RowType == DataControlRowType.DataRow)
                {
                    for (int columnIndex = 0; columnIndex < r.Cells.Count; columnIndex++)
                    {
                        r.Cells[columnIndex].Attributes["style"] += "background-color:White;";
                    }
                }
            }
            int selectedRowIndex = Convert.ToInt32(e.CommandArgument.ToString());
            int selectedColumnIndex = Convert.ToInt32(Request.Form["__EVENTARGUMENT"].ToString());
            
            for (int i = 0; i < GridView1.Columns.Count;i++ )
                GridView1.Rows[selectedRowIndex].Cells[i].Attributes["style"] += "background-color:" + Color.CornflowerBlue.Name + ";"; //Color.Chartreuse

            GridView1.Rows[selectedRowIndex].Cells[selectedColumnIndex].Attributes["style"] += "background-color:" + Color.WhiteSmoke.Name + ";";
            
            //lblError.Text = GridView1.Rows[selectedRowIndex].Cells[selectedColumnIndex].Text;
            ViewState["Aging_days"] = GridView1.Rows[selectedRowIndex].Cells[0].Text.Replace("+","") ;
            // 6 with 4 4 with 12  12 with 6
            switch (selectedColumnIndex)
            {
                case 1: ViewState["status"] = "0000"; break;
                case 2: ViewState["status"] = "3011"; break;  
                case 3: ViewState["status"] = "0030"; break;
                case 4: ViewState["status"] = "0001"; break;
                case 5: ViewState["status"] = "0070"; break;
 
                case 6: ViewState["status"] = "0071"; break;
                case 7: ViewState["status"] = "0002"; break;
                case 8: ViewState["status"] = "0003"; break;

                case 9: ViewState["status"] = "0030"; break; 
                case 10: ViewState["status"] = "0072"; break;
                case 11: ViewState["status"] = "0073"; break;
                case 12: ViewState["status"] = "0040"; break; 
                case 13: ViewState["status"] = "0032"; break;
                case 14: ViewState["status"] = "0033"; break;
                case 15: ViewState["status"] = "0041"; break;
                case 16: ViewState["status"] = "0042"; break;
                case 17: ViewState["status"] = "0043"; break;

                case 18: ViewState["status"] = "0071"; break;
                case 19: ViewState["status"] = "0031"; break;

                case 20: ViewState["status"] = "3012"; break;
                case 21: ViewState["status"] = "3013"; break;
                case 22: ViewState["status"] = "0020"; break;
                case 23: ViewState["status"] = "0050"; break;
                case 24: ViewState["status"] = "0053"; break;

      
            }
            Panel1.Visible = true;
            loadClaims(10, 1);

            GridView1.SelectedIndex = selectedRowIndex;
            GridView1_SelectedIndexChanged(sender, e);
        }  
    }

    private void loadClaims(int pageSize, int pageNumber)
    {
        string DBConnection = Helper.dbConnection();

        OracleConnection oracon = new OracleConnection(DBConnection);
        oracon.Open();

        OracleCommand oracom = new OracleCommand();
        oracom.Connection = oracon;

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "claim_process.get_cl_sum_dtl";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_Aging_days", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_claim_status", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_on_off_Us_Flag", OracleType.VarChar).Direction = ParameterDirection.Input;

        oracom.Parameters.Add("p_page_size", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_page_num", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_res_code", OracleType.Number).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_res_type", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_res_msg", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_output", OracleType.Cursor).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_total_pages", OracleType.Number).Direction = ParameterDirection.Output;

        oracom.Parameters["p_Aging_days"].Value = ViewState["Aging_days"];
        oracom.Parameters["p_claim_status"].Value = ViewState["status"];

        if (rdClaimOn.Checked == true)
            oracom.Parameters["p_on_off_Us_Flag"].Value = "on";
        else if (rdClaimOff.Checked == true)
            oracom.Parameters["p_on_off_Us_Flag"].Value = "off";
        else if (rdClaimOnUS.Checked == true)
            oracom.Parameters["p_on_off_Us_Flag"].Value = "on-us";


    
        oracom.Parameters["p_page_size"].Value = pageSize;
        oracom.Parameters["p_page_num"].Value = pageNumber;
        oracom.Parameters["p_lang"].Value = "EN";
        oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

        OracleDataAdapter da = new OracleDataAdapter(oracom);
        DataSet ds = new DataSet();
        da.Fill(ds);

        int nop = int.Parse(oracom.Parameters["p_total_pages"].Value.ToString());
        if (nop == -1 && ds.Tables[0].Rows.Count > 0)
            nop = int.Parse(Math.Ceiling((double.Parse(ds.Tables[0].Rows[0]["ROW_COUNT"].ToString()) / pageSize)).ToString());

        oracon.Close();

        GridView2.DataSource = ds;
        GridView2.DataBind();
        if (pageNumber == 1)
        {
            ddlPages.Items.Clear();
            for (int i = 1; i <= nop; i++)
            {
                ddlPages.Items.Add(i.ToString());
            }
        }
    }
    protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
    {
        loadClaims(10, int.Parse(ddlPages.SelectedValue));
    }
}
