/*Este metodo desbloque los controloes y obtiene el codigo siguiente del nombramiento externo*/
 protected void btnNew_Click(object sender, ImageClickEventArgs e)
 {
     unlockControls();
     loadDay();
     getFunctionary();
     txtCode.Text = ExternalDesignationBLL.getInstance().getNextCode().ToString();
 }
        /*Este metodo obtiene los datos del formulario y los envia al BLL para agregarlos a la bd*/
        protected void btnSave_Click(object sender, ImageClickEventArgs e)
        {
            Int32 records = -1;

            if (validateData())
            {
                ExternalDesignation oExternalDesignation = new ExternalDesignation();
                oExternalDesignation.code          = Int32.Parse(txtCode.Text);
                oExternalDesignation.oTeacher      = new Teacher();
                oExternalDesignation.oTeacher.code = Int32.Parse(cboFunctionary.SelectedValue);
                oExternalDesignation.position      = txtPosition.Text;
                oExternalDesignation.location      = txtWorkPlace.Text;
                string[] formats = { "M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt",
                                     "MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss",
                                     "M/d/yyyy hh:mm tt",   "M/d/yyyy hh tt",
                                     "M/d/yyyy h:mm",       "M/d/yyyy h:mm",
                                     "MM/dd/yyyy hh:mm",    "M/dd/yyyy hh:mm" };
                DateTime initial;
                /*Este if convierte el formato de la fecha que toma del usuario de mes/dia/año a  dia/mes/año*/
                if (DateTime.TryParseExact(txtStartDesignation.Text + " 00:00:00", formats, new CultureInfo("es-ES"),
                                           DateTimeStyles.None, out initial))
                {
                    oExternalDesignation.initial_day = initial;
                }
                DateTime final;
                if (DateTime.TryParseExact(txtEndDesignation.Text + " 00:00:00", formats, new CultureInfo("es-ES"),
                                           DateTimeStyles.None, out final))
                {
                    oExternalDesignation.final_day = final;
                }

                oExternalDesignation.hours    = Int32.Parse(cboHoursDisignation.SelectedValue);
                oExternalDesignation.journeys = (List <Entities.Journey>)Session["listDesignation"];

                if (ExternalDesignationBLL.getInstance().exists(oExternalDesignation.code))
                {
                    //if we want implement modify
                    //records = ExternalDesignationBLL.getInstance().modify(oExternalDesignation);
                }
                else
                {
                    records = ExternalDesignationBLL.getInstance().insert(oExternalDesignation);
                }

                blockControls();
                Session["listDesignation"] = null;
                fillGvDesignation();
                cleanGvDays();
                if (records >= 0)
                {
                    lblMessage.Text = "Datos almacenados correctamente.";
                }
            }
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            Int32 records = ExternalDesignationBLL.getInstance().delete(externalDesignation_id);

            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "confirmMessage", "$('#confirmMessage').modal('toggle');", true);

            if (records > 0)
            {
                lblMessage.Text = "Nombramiento externo eliminado correctamente.";
            }
            Response.Redirect("externalDesignation.aspx");
            fillGvDesignation();
        }
        /*Este metodo simplemente llama al obtener horas del profesor*/
        protected void cboFunctionary_SelectedIndexChanged(object sender, EventArgs e)
        {
            Int32 hours = 0;

            cboHoursDisignation.Items.Clear();
            hours = ExternalDesignationBLL.getInstance().getHours(Int32.Parse(cboFunctionary.SelectedValue));
            ListItem oItemS = new ListItem("---Seleccione---", "0");

            cboHoursDisignation.Items.Add(oItemS);
            int i = hours / 5;

            for (int j = 5; j <= hours; j = j + 5)
            {
                ListItem oItem = new ListItem(j.ToString() + " horas", j.ToString());
                cboHoursDisignation.Items.Add(oItem);
            }
        }
        private void fillhours()
        {
            Int32 hours = 0;

            cboHours.Items.Clear();
            hours = ExternalDesignationBLL.getInstance().getHours(Int32.Parse(cboTeacher.SelectedValue));
            ListItem oItemS = new ListItem("---Seleccione---", "0");

            cboHours.Items.Add(oItemS);
            int i = hours / 5;

            for (int j = 5; j <= hours; j = j + 5)
            {
                ListItem oItem = new ListItem(j.ToString() + " horas", j.ToString());
                cboHours.Items.Add(oItem);
            }
        }
        protected void btnReport_Click(object sender, EventArgs e)
        {
            try
            {
                List <Entities.ExternalDesignation> listExternalDesignation = ExternalDesignationBLL.getInstance().getAll();
                System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
                text::Document         pdfDoc       = new text::Document(text::PageSize.A4, 10, 10, 10, 10);
                pdfDoc.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());
                PdfWriter.GetInstance(pdfDoc, memoryStream);
                pdfDoc.Open();

                String imagepath           = Server.MapPath("../../images/page-icons");
                iTextSharp.text.Image deas = iTextSharp.text.Image.GetInstance(imagepath + "/DEAS-logo.jpg");
                deas.ScaleToFit(140f, 120f);
                //Give space before image
                deas.SpacingBefore = 10f;
                //Give some space after the image
                deas.SpacingAfter = 1f;
                deas.Alignment    = text::Element.ALIGN_LEFT;
                pdfDoc.Add(deas);

                text::Paragraph title = new text::Paragraph();
                title.Font      = text::FontFactory.GetFont("dax-black", 32, new text::BaseColor(0, 51, 102));
                title.Alignment = text::Element.ALIGN_CENTER;
                title.Add("\n\n Reporte de Nombramientos Externos\n\n");
                pdfDoc.Add(title);


                if (listExternalDesignation.Count > 0)
                {
                    foreach (Entities.ExternalDesignation pExternalDesignation in listExternalDesignation)
                    {
                        PdfPTable oPTable = new PdfPTable(5);
                        oPTable.TotalWidth = 100;
                        oPTable.AddCell("Funcionario");
                        oPTable.AddCell("Cargo");
                        oPTable.AddCell("Lugar de Trabajo");
                        oPTable.AddCell("Inicio - Fin");
                        oPTable.AddCell("Horas");
                        oPTable.AddCell(pExternalDesignation.oTeacher.name + " " + pExternalDesignation.oTeacher.lastName);
                        oPTable.AddCell(pExternalDesignation.position);
                        oPTable.AddCell(pExternalDesignation.location);
                        oPTable.AddCell(pExternalDesignation.initial_day.ToShortDateString() + " " + pExternalDesignation.final_day.ToShortDateString());
                        oPTable.AddCell(pExternalDesignation.hours.ToString());
                        pdfDoc.Add(oPTable);

                        PdfPTable oPTable2 = new PdfPTable(7);
                        oPTable2.TotalWidth    = 100;
                        oPTable2.SpacingAfter  = 30f;
                        oPTable2.SpacingBefore = 5f;
                        oPTable2.AddCell("Lunes");
                        oPTable2.AddCell("Martes");
                        oPTable2.AddCell("Miércoles");
                        oPTable2.AddCell("Jueves");
                        oPTable2.AddCell("Viernes");
                        oPTable2.AddCell("Sábado");
                        oPTable2.AddCell("Domingo");

                        Int32 count = 1;
                        while (count <= 7)
                        {
                            Boolean ind = false;

                            for (Int32 i = 0; i < pExternalDesignation.journeys.Count; i++)
                            {
                                if (pExternalDesignation.journeys[i].day.code == count)
                                {
                                    ind = true;
                                    oPTable2.AddCell(pExternalDesignation.journeys[i].start + " / " + pExternalDesignation.journeys[i].finish);
                                }
                            }

                            if (!ind)
                            {
                                oPTable2.AddCell("00:00" + " / " + "00:00");
                            }
                            count++;
                        }

                        pdfDoc.Add(oPTable2);
                    }
                }
                else
                {
                    PdfPTable oPTable = new PdfPTable(5);
                    oPTable.TotalWidth    = 100;
                    oPTable.SpacingBefore = 20f;
                    oPTable.SpacingAfter  = 30f;
                    oPTable.AddCell("Funcionario");
                    oPTable.AddCell("Cargo");
                    oPTable.AddCell("Lugar de Trabajo");
                    oPTable.AddCell("Inicio - Fin");
                    oPTable.AddCell("Horas");
                    PdfPCell cell = new PdfPCell(new text::Phrase("No existen nombramientos externos registrados."));
                    cell.Colspan             = 5;
                    cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
                    oPTable.AddCell(cell);
                    pdfDoc.Add(oPTable);
                }


                pdfDoc.Close();

                byte[] bytes = memoryStream.ToArray();
                memoryStream.Close();
                Response.Clear();
                Response.ContentType = "application/pdf";
                Response.AddHeader("Content-Disposition", "attachment; filename=Nombramiento_Externo.pdf");
                Response.ContentType = "application/pdf";
                Response.Buffer      = true;
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.BinaryWrite(bytes);
                Response.End();
                Response.Close();
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }
 protected void fillGvDesignation()
 {
     gvExternalDesignation.DataSource = ExternalDesignationBLL.getInstance().getAll();
     gvExternalDesignation.DataBind();
 }