protected void detailmodel_name_TextChanged(object sender, EventArgs e)
 {
     Support support = new Support();
     var model = (from m in support.models
                  where m.id == id
                  select m).FirstOrDefault();
     model.name = detailmodel_name.Text;
     support.SaveChanges();
 }
        protected void detailmodel_interface_SelectedIndexChanged(object sender, EventArgs e)
        {
            Support support = new Support();
            var model = (from m in support.models
                         where m.id == id
                         select m).FirstOrDefault();

            model._interface = Convert.ToInt32(detailmodel_interface.Text);
            support.SaveChanges();
        }
 protected void detailperson2_delete_Click(object sender, EventArgs e)
 {
     using (Support p = new Support())
     {
         var target = (from x in p.people
                       where x.id == id
                       select x).FirstOrDefault();
         p.people.Remove(target);
         p.SaveChanges();
     }
     Response.Redirect(prevPage);
 }
 protected void detailmodel_cord_TextChanged(object sender, EventArgs e)
 {
     Support support = new Support();
     var model = (from m in support.models
                  where m.id == id
                  select m).FirstOrDefault();
     if (!String.IsNullOrEmpty(detailmodel_cord.Text))
     {
         model.cord = detailmodel_cord.Text;
         support.SaveChanges();
     }
 }
        protected void detailmodel_delete_Click(object sender, EventArgs e)
        {
            using (Support s = new Support())
            {
                var target = (from x in s.models
                              where x.id == id
                              select x).FirstOrDefault();
                s.models.Remove(target);
                s.SaveChanges();
            }

            Response.Redirect("~/Admin/Model");
        }
 protected void detailfile2_name_TextChanged(object sender, EventArgs e)
 {
     try
     {
         using (Support support = new Support())
         {
             var name = (from c in support.Files
                         where c.id == id
                         select c).FirstOrDefault();
             name.name = detailfile2_name.Text;
             support.SaveChanges();
         }
     }
     catch (System.Exception ex)
     {
         Response.Write(ex);
     }
 }
        protected void detailperson2_email_TextChanged(object sender, EventArgs e)
        {
            try
            {
                using (Support personnel = new Support())
                {
                    person target = (from x in personnel.people
                                     where x.id == id
                                     select x).FirstOrDefault();

                    target.email = detailperson2_email.Text;
                    personnel.SaveChanges();
                }
            }
            catch (System.Exception ex)
            {
                Response.Write(ex);
            }
        }
        //active
        protected void detailperson2_active_SelectedIndexChanged(object sender, EventArgs e)
        {
            using (Support personnel = new Support())
            {
                person target = (from x in personnel.people
                                 where x.id == id
                                 select x).FirstOrDefault();

                if (detailperson2_active.SelectedValue == "0")
                {
                    target.ed = new DateTime(3000, 1, 1);
                }
                else if (detailperson2_active.SelectedValue == "1")
                {
                    target.ed = DateTime.Now;
                }

                personnel.SaveChanges();
            }
        }
        //submit
        protected void Unnamed2_Click(object sender, EventArgs e)
        {
            try
            {
                role role = new role()
               {
                   name = detailrole_name.Text,
                   description = detailrole_desc.Text,

               };

                Support p = new Support();
                p.roles.Add(role);
                p.SaveChanges();

                Response.Redirect(prevPage);
            }
            catch (System.Exception ex)
            {
                Response.Write(ex);
            }
        }
        //delete
        protected void detailafile2_delete_Click(object sender, EventArgs e)
        {
            using (Support s = new Support())
            {
                var target = (from x in s.Files
                              where x.id == id
                              select x).FirstOrDefault();
                s.Files.Remove(target);
                s.SaveChanges();
            }

            //SiteMapNode currentNode = SiteMap.CurrentNode.Clone(true);
            //string parentURL = currentNode.ParentNode.Url;
            //using (Support s = new Support())
            //{
            //    var target = (from x in s.Files
            //                  where x.id == id
            //                  select x).FirstOrDefault();

            //    Response.Redirect("/Detail/DetailAction2" + "?id=" + _action + "&case_id=" + _incident);
            //}
            Response.Redirect(prevPage);
        }
        protected void detailmember2_org_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                using (Support personnel = new Support())
                {
                    member target = (from x in personnel.members
                                     where x.id == id
                                     select x).FirstOrDefault();

                    target.org = Convert.ToInt32(detailmember2_org.SelectedValue.ToString());
                    personnel.SaveChanges();
                }
            }
            catch (System.Exception ex)
            {
                Response.Write(ex);
            }
        }
        protected void detailrole2_description_TextChanged(object sender, EventArgs e)
        {
            try
            {
                using (Support personnel = new Support())
                {
                    role target = (from x in personnel.roles
                                   where x.id == id
                                   select x).FirstOrDefault();

                    target.description = detailrole2_description.Text;
                    personnel.SaveChanges();

                }
            }
            catch (System.Exception ex)
            {
                Response.Write(ex);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Cookies["user"] != null)
            {
                string decoded_username = Encoding.UTF8.GetString(MachineKey.Unprotect(Convert.FromBase64String(Request.Cookies["user"]["username"]), "ProtectCookieUsername"));
                string decoded_role = Encoding.UTF8.GetString(MachineKey.Unprotect(Convert.FromBase64String(Request.Cookies["user"]["role"]), "ProtectCookieRole"));

                if (decoded_role == "admin" || decoded_role == "support")
                {

                }

                else
                {
                    Response.Redirect("~/");
                }
            }

            else
            {
                Response.Redirect("~/");
            }

            int isNew = Convert.ToInt32(Request.QueryString["new"]);
            if (!String.IsNullOrEmpty(Request.QueryString["id"]))
            {
                id = Convert.ToInt32(Request.QueryString["id"]);
                //Response.Write(id);
            }
            _incident = Convert.ToInt32(Request.QueryString["incident"]);
            _action = Convert.ToInt32(Request.QueryString["action"]);
            _sender = Convert.ToInt32(Request.QueryString["sender"]);

            if (!IsPostBack)
            {

                if (prevPage == String.Empty)
                {
                    prevPage = Request.UrlReferrer.ToString();
                }

                SiteMap.SiteMapResolve += new SiteMapResolveEventHandler(SiteMap_File);
                if (isNew == 1)
                {
                    file_model fi = new file_model()
                    {
                        action = _action,
                        incident = _incident,
                        sender = _sender,
                        dt = DateTime.Now,
                        name = "change it",
                    };

                    Support s = new Support();
                    s.Files.Add(fi);
                    s.SaveChanges();
                    id = fi.id;
                    detailfile2_date.Text = fi.dt.ToString("MM/dd/yyyy");
                    Support support = new Support();
                    var sen = (from p in support.people
                               join m in support.members
                               on p.id equals m.person
                               where m.id == fi.sender
                               select p).FirstOrDefault();
                    detailfile2_sender.Text = sen.lastname + sen.firstname;
                    var p_query = from p in support.Incidents where p.id == fi.incident select p;
                    detailfile2_case.Text = p_query.FirstOrDefault().description;
                }
                else
                {

                    try
                    {
                        Support support = new Support();
                        Support personnel = new Support();
                        var file = from a in support.Files
                                   where a.id == id
                                   select new
                                   {
                                       a.incident,
                                       a.name,
                                       a.sender,
                                       a.fpath,
                                       a.note,
                                       a.dt,
                                       a.action
                                   };

                        _action = file.FirstOrDefault().action;
                        _incident = file.FirstOrDefault().incident;
                        //date
                        if (file.FirstOrDefault().dt != null)
                        {
                            detailfile2_date.Text = file.FirstOrDefault().dt.ToString("MM/dd/yyyy");
                        }
                        else
                        {
                            detailfile2_date.Text = "No date...";
                        }

                        //name
                        if (!String.IsNullOrEmpty(file.FirstOrDefault().name))
                        {
                            name = file.FirstOrDefault().name.ToString();
                            detailfile2_name.Text = name;
                        }
                        else
                        {
                            name = "No name...";
                            detailfile2_name.Text = name;
                        }

                        //case
                        List<int> all_cases = (from x in support.Incidents select x.id).ToList();
                        if (!all_cases.Contains(Convert.ToInt32(file.FirstOrDefault().incident.ToString())))
                        {
                            detailfile2_case.Text = "No case...";
                        }
                        else
                        {
                            var p_query = from p in support.Incidents where p.id == file.FirstOrDefault().incident select p;
                            detailfile2_case.Text = p_query.FirstOrDefault().description;
                        }

                        //sender
                        List<int> all_members = (from x in personnel.members select x.id).ToList();
                        if (!all_members.Contains(Convert.ToInt32(file.FirstOrDefault().sender.ToString())))
                        {
                            detailfile2_sender.Text = "No sender...";
                        }
                        else
                        {
                            int s = Convert.ToInt32(file.FirstOrDefault().sender.ToString());
                            var sename = from r in personnel.people
                                         join m in personnel.members
                                         on r.id equals m.person
                                         where m.id == s
                                         select r;
                            detailfile2_sender.Text = sename.FirstOrDefault().lastname + " " + sename.FirstOrDefault().firstname;
                        }

                        //path
                        if (!String.IsNullOrEmpty(file.FirstOrDefault().fpath))
                        {
                            path = file.FirstOrDefault().fpath.ToString();
                            detailfile2_path.Text = path;
                        }
                        else
                        {
                            path = "No path...";
                            detailfile2_path.Text = path;
                        }

                        //note
                        if (!String.IsNullOrEmpty(file.FirstOrDefault().note))
                        {
                            note = file.FirstOrDefault().note.ToString();
                            detailfile2_note.Text = note;
                        }
                        else
                        {
                            note = "No note...";
                            detailfile2_note.Text = note;
                        }

                    }
                    catch (Exception ex)
                    {
                        Response.Write(ex);
                    }
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            //to avoid duplication in dropdown lists
            if (IsPostBack)
            {
                detailaction2_sender.DataSourceID = "";
                Items.Clear();
            }

            if (Request.Cookies["user"] != null)
            {
                string decoded_username = Encoding.UTF8.GetString(MachineKey.Unprotect(Convert.FromBase64String(Request.Cookies["user"]["username"]), "ProtectCookieUsername"));
                string decoded_role = Encoding.UTF8.GetString(MachineKey.Unprotect(Convert.FromBase64String(Request.Cookies["user"]["role"]), "ProtectCookieRole"));
                person_id = Convert.ToInt32(Encoding.UTF8.GetString(MachineKey.Unprotect(Convert.FromBase64String(Request.Cookies["user"]["person"]), "ProtectCookiePerson")));
                case_id = Convert.ToInt32(Request.QueryString["case_id"]);

                if (decoded_role == "admin" || decoded_role == "support")
                {

                }

                else
                {
                    Response.Redirect("~/");
                }
            }

            else
            {
                Response.Redirect("~/");
            }

            if (!String.IsNullOrEmpty(Request.QueryString["id"]))
            {
                id = Convert.ToInt32(Request.QueryString["id"]);
            }

            int isNew = Convert.ToInt32(Request.QueryString["new"]);

            if (!IsPostBack)
            {

                SiteMap.SiteMapResolve += new SiteMapResolveEventHandler(SiteMap_SiteMapResolve);
                if (isNew == 1)
                {
                    action_model action = new action_model()
                    {
                        dt = DateTime.Now,
                        incident = case_id,
                        description = "default"
                    };

                    Support s = new Support();
                    s.Actions.Add(action);
                    s.SaveChanges();
                    id = action.id;
                    detailaction2_date.Text = action.dt.ToString("MM/dd/yyyy");

                    Support support = new Support();
                    var p_query = from p in support.Incidents where p.id == action.incident select p;
                    detailaction2_case.Text = p_query.FirstOrDefault().description;
                }
                else
                {
                    try
                    {
                        Support support = new Support();
                        Support personnel = new Support();
                        var action = from a in support.Actions
                                     where a.id == id
                                     select new
                                     {
                                         a.incident,
                                         a.receiver,
                                         a.sender,
                                         a.description,
                                         a.dt
                                     };

                        //date
                        if (action.FirstOrDefault().dt != null)
                        {
                            detailaction2_date.Text = action.FirstOrDefault().dt.ToString("MM/dd/yyyy");
                        }
                        else
                        {
                            detailaction2_date.Text = "No date...";
                        }

                        //description
                        if (String.IsNullOrEmpty(action.FirstOrDefault().description) || action.FirstOrDefault().description == "default")
                        {
                            detailaction2_desc.Text = "";
                        }
                        else
                        {
                            desc = action.FirstOrDefault().description.ToString();
                            detailaction2_desc.Text = desc;
                        }

                        //case
                        List<int> all_cases = (from x in support.Incidents select x.id).ToList();
                        if (!all_cases.Contains(Convert.ToInt32(action.FirstOrDefault().incident.ToString())))
                        {
                            detailaction2_case.Text = "No case...";
                        }
                        else
                        {
                            var p_query = from p in support.Incidents where p.id == action.FirstOrDefault().incident select p;
                            detailaction2_case.Text = p_query.FirstOrDefault().description;
                        }

                        //sender
                        List<int> all_members = (from x in personnel.members select x.id).ToList();
                        if (!all_members.Contains(Convert.ToInt32(action.FirstOrDefault().sender.ToString())))
                        {
                            detailaction2_sender.SelectedValue = "";
                        }
                        else
                        {
                            int s = Convert.ToInt32(action.FirstOrDefault().sender.ToString());
                            detailaction2_sender.SelectedValue = action.FirstOrDefault().sender.ToString();
                        }

                        //receiver
                        if (!String.IsNullOrEmpty(action.FirstOrDefault().receiver))
                        {
                            detailaction2_receiver.Text = action.FirstOrDefault().receiver.ToString();
                        }
                        else
                        {
                            // detailaction2_desc.Text = desc;
                        }

                        ////receiver
                        //if (!all_members.Contains(Convert.ToInt32(action.FirstOrDefault().receiver.ToString())))
                        //{
                        //    detailaction2_receiver.Text = "No receiver...";
                        //}
                        //else
                        //{
                        //    int s = Convert.ToInt32(action.FirstOrDefault().receiver.ToString());
                        //    var rename = from r in personnel.people
                        //                 join m in personnel.members
                        //                 on r.id equals m.person
                        //                 where m.id == s
                        //                 select r;
                        //    detailaction2_receiver.Text = rename.FirstOrDefault().lastname + " " + rename.FirstOrDefault().firstname;
                        //}

                    }
                    catch (Exception ex)
                    {
                        Response.Write(ex);
                    }
                }

            }
            detailaction2_sender.AppendDataBoundItems = true;
        }
        ////edit
        //protected void Unnamed_Click(object sender, EventArgs e)
        //{
        //    detailaction2_cancel.Visible = true;
        //    detailaction2_submit.Visible = true;
        //    detailaction2_desc.ReadOnly = false;
        //}
        //submit
        //protected void detailaction2_submit_Click(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        using (Support support = new Support())
        //        {
        //            action_model target = (from x in support.Actions
        //                                 where x.id == id
        //                                 select x).FirstOrDefault();
        //            target.description = detailaction2_desc.Text;
        //            support.SaveChanges();
        //        }
        //        detailaction2_desc.ReadOnly = true;
        //        detailaction2_submit.Visible = false;
        //        detailaction2_cancel.Visible = false;
        //    }
        //    catch (System.Exception ex)
        //    {
        //        Response.Write(ex);
        //    }
        //}
        ////cancel
        //protected void detailaction2_cancel_Click(object sender, EventArgs e)
        //{
        //    detailaction2_desc.ReadOnly = true;
        //    detailaction2_submit.Visible = false;
        //    detailaction2_cancel.Visible = false;
        //    detailaction2_desc.Text = desc;
        //}
        //delete
        protected void detailaction2_delete_Click(object sender, EventArgs e)
        {
            using (Support s = new Support())
            {
                var target = (from x in s.Actions
                              where x.id == id
                              select x).FirstOrDefault();
                s.Actions.Remove(target);
                s.SaveChanges();
            }

            SiteMapNode currentNode = SiteMap.CurrentNode.Clone(true);
            string parentURL = currentNode.ParentNode.Url;
            Response.Redirect(parentURL);
        }
 protected void detailaction2_receiver_TextChanged(object sender, EventArgs e)
 {
     try
     {
         using (Support support = new Support())
         {
             var receiver = (from c in support.Actions
                             where c.id == id
                             select c).FirstOrDefault();
             receiver.receiver = detailaction2_receiver.Text;
             support.SaveChanges();
         }
     }
     catch (System.Exception ex)
     {
         Response.Write(ex);
     }
 }
        protected void detailAction2_sender_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (detailaction2_sender.SelectedValue == "")
            {
                return;
            }

            try
            {
                using (Support support = new Support())
                {
                    var se = (from c in support.Actions
                              where c.id == id
                              select c).FirstOrDefault();
                    se.sender = Convert.ToInt32(detailaction2_sender.SelectedValue.ToString());
                    support.SaveChanges();
                }
            }
            catch (System.Exception ex)
            {
                Response.Write(ex);
            }
        }
        ////edit
        //protected void Unnamed4_Click(object sender, EventArgs e)
        //{
        //    detailcase2_cancel.Visible = true;
        //    detailcase2_submit.Visible = true;
        //    //detailcase2_isclosed_dp.Enabled = true;
        //    //detailcase2_sales_dp.Enabled = true;
        //    detailcase2_supportteam_dp.Enabled = true;
        //    detailcase2_technote_dp.Enabled = true;
        //    detailcase2_allcompany_dp.Enabled = true;
        //    detailcase2_faq_dp.Enabled = true;
        //    detailcase2_inst_dp.Enabled = true;
        //    //  detailcase2_handler.Enabled = true;
        //    //detailcase2_isclosed_dp.BorderColor = System.Drawing.Color.Red;
        //    //detailcase2_sales_dp.BorderColor = System.Drawing.Color.Red;
        //    detailcase2_supportteam_dp.BorderColor = System.Drawing.Color.Red;
        //    detailcase2_technote_dp.BorderColor = System.Drawing.Color.Red;
        //    detailcase2_allcompany_dp.BorderColor = System.Drawing.Color.Red;
        //    detailcase2_faq_dp.BorderColor = System.Drawing.Color.Red;
        //    detailcase2_inst_dp.BorderColor = System.Drawing.Color.Red;
        //    // detailcase2_handler.BorderColor = System.Drawing.Color.Red;
        //    Support support = new Support();
        //    var cases = (from c in support.Incidents
        //                 where c.id == id
        //                 select c).FirstOrDefault();
        //    //detailcase2_sales_dp.SelectedValue = cases.sales.ToString();
        //    detailcase2_supportteam_dp.SelectedValue = cases.supportteam.ToString();
        //    detailcase2_technote_dp.SelectedValue = cases.technote.ToString();
        //    detailcase2_allcompany_dp.SelectedValue = cases.allcompany.ToString();
        //    detailcase2_faq_dp.SelectedValue = cases.faq.ToString();
        //    detailcase2_inst_dp.SelectedValue = cases.instruction.ToString();
        //    // detailcase2_handler.SelectedValue = cases.handler.ToString();
        //    detailcase2_other.ReadOnly = false;
        //    detailcase2_desc.ReadOnly = false;
        //    detailcase2_other.ForeColor = System.Drawing.Color.Red;
        //    detailcase2_desc.ForeColor = System.Drawing.Color.Red;
        //}
        ////go back
        //protected void Unnamed1_Click(object sender, EventArgs e)
        //{
        //    Response.Redirect(prevPage);
        //}
        //submit
        protected void detailcase2_submit_Click(object sender, EventArgs e)
        {
            try
            {
                using (Support support = new Support())
                {
                    var cases = (from c in support.Incidents
                                 where c.id == id
                                 select c).FirstOrDefault();

                    List<int> all_cases = (from x in support.Incidents select x.id).ToList();

                    List<Incident> children = new List<Incident>();

                    foreach (Incident i in support.Incidents)
                    {
                        if (i.parent == id)
                        {
                            children.Add(i);
                        }
                    }

                    //do not close the current case if there is a child which is not closed
                    //if (detailcase2_isclosed_dp.SelectedValue == "1")
                    //{
                    //    if (children.Count == 0)
                    //    {
                    //        cases.isclosed = Convert.ToBoolean((Convert.ToInt32(detailcase2_isclosed_dp.SelectedValue)));
                    //    }
                    //    else
                    //    {
                    //        int count = 0;

                    //        foreach (Incident c in children)
                    //        {
                    //            if (c.isclosed == true)
                    //            {
                    //                count++;

                    //            }
                    //        }

                    //        if (count < children.Count)
                    //        {
                    //            Response.Write("can not close this case, because its child is still active!");
                    //        }
                    //        else
                    //        {
                    //            cases.isclosed = Convert.ToBoolean((Convert.ToInt32(detailcase2_isclosed_dp.SelectedValue)));
                    //        }

                    //    }

                    //}
                    //else
                    //{
                    //    cases.isclosed = Convert.ToBoolean((Convert.ToInt32(detailcase2_isclosed_dp.SelectedValue)));
                    //}

                    // cases.sales = Convert.ToInt32(detailcase2_sales_dp.SelectedValue.ToString());
                    //cases.supportteam = Convert.ToInt32(detailcase2_supportteam_dp.SelectedValue.ToString());
                    //cases.technote = Convert.ToInt32(detailcase2_technote_dp.SelectedValue.ToString());
                    //cases.allcompany = Convert.ToInt32(detailcase2_allcompany_dp.SelectedValue.ToString());
                    //cases.faq = Convert.ToInt32(detailcase2_faq_dp.SelectedValue.ToString());
                    //cases.instruction = Convert.ToInt32(detailcase2_inst_dp.SelectedValue.ToString());

                    //if (detailcase2_handler.SelectedValue != "")
                    //{
                    //    cases.handler = Convert.ToInt32(detailcase2_handler.SelectedValue.ToString());

                    //}

                    cases.description = detailcase2_desc.Text;
                    cases.other = detailcase2_other.Text;

                    cases.lastupdatedate = DateTime.Now;
                    support.SaveChanges();
                }

                // detailcase2_cancel.Visible = false;
                // detailcase2_submit.Visible = false;
                //// detailcase2_isclosed_dp.Enabled = false;
                // //detailcase2_sales_dp.Enabled = false;
                // detailcase2_supportteam_dp.Enabled = false;
                // detailcase2_technote_dp.Enabled = false;
                // detailcase2_allcompany_dp.Enabled = false;
                // // detailcase2_handler.Enabled = false;
                // detailcase2_faq_dp.Enabled = false;
                // detailcase2_inst_dp.Enabled = false;
                // detailcase2_other.ReadOnly = true;
                // detailcase2_desc.ReadOnly = true;

                // //detailcase2_isclosed_dp.BorderColor = System.Drawing.Color.LightGray;
                // //detailcase2_sales_dp.BorderColor = System.Drawing.Color.LightGray;
                // detailcase2_supportteam_dp.BorderColor = System.Drawing.Color.LightGray;
                // detailcase2_technote_dp.BorderColor = System.Drawing.Color.LightGray;
                // detailcase2_allcompany_dp.BorderColor = System.Drawing.Color.LightGray;
                // detailcase2_faq_dp.BorderColor = System.Drawing.Color.LightGray;
                // detailcase2_inst_dp.BorderColor = System.Drawing.Color.LightGray;
                //  detailcase2_handler.BorderColor = System.Drawing.Color.LightGray;

                //detailcase2_other.ForeColor = System.Drawing.Color.Black;
                //detailcase2_desc.ForeColor = System.Drawing.Color.Black;
            }

            catch (System.Exception ex)
            {
                Response.Write(ex);
            }
        }
        //submit
        protected void Unnamed3_Click(object sender, EventArgs e)
        {
            try
            {
                action_model action = new action_model()
                {
                    dt = DateTime.Now,
                    incident = case_id,
                    description = detailaction_desc.Text,
                };

                if (detailaction_receiver.SelectedValue != "")
                {
                    action.sender = Convert.ToInt32(detailaction_sender.SelectedValue.ToString());
                }

                //if (detailaction_receiver.SelectedValue != "")
                //{
                //    action.receiver = Convert.ToInt32(detailaction_receiver.SelectedValue.ToString());
                //}

                Support s = new Support();
                s.Actions.Add(action);
                s.SaveChanges();

                Response.Redirect(prevPage);
            }
            catch (System.Exception ex)
            {
                Response.Write(ex);
            }
        }
 protected void detailcase2_other_TextChanged(object sender, EventArgs e)
 {
     try
     {
         using (Support support = new Support())
         {
             var cases = (from c in support.Incidents
                          where c.id == id
                          select c).FirstOrDefault();
             cases.other = detailcase2_other.Text;
             if (isNew == 0)
             {
                 cases.lastupdatedate = DateTime.Now;
             }
             support.SaveChanges();
         }
     }
     catch (System.Exception ex)
     {
         Response.Write(ex);
     }
 }
        //submit
        protected void Unnamed2_Click(object sender, EventArgs e)
        {
            //int  parent;
            //Support s = new Support();

            //      if (detailorg_parent.SelectedValue == "")
            //      {
            //          try
            //          {
            //              org_model orgs = new org_model()
            //              {
            //                  name = detailorg_name.Text,
            //                  parent = null
            //              };

            //   //s.orgs.Add(orgs);
            //              s.SaveChanges();

            //              Response.Redirect(prevPage);
            //          }
            //          catch (System.Exception ex)
            //          {
            //              Response.Write(ex);
            //          }
            //      }

            //      else
            //      {
            //          parent = Convert.ToInt32(detailorg_parent.SelectedValue);
            //          try
            //          {
            //              org_model orgs = new org_model()
            //              {
            //                  name = detailorg_name.Text,
            //                  parent = parent
            //              };

            ////s.orgs.Add(orgs);
            //              s.SaveChanges();

            //              Response.Redirect(prevPage);
            //          }
            //          catch (System.Exception ex)
            //          {
            //              Response.Write(ex);
            //          }
            //      }

            Support p = new Support();
            if (!String.IsNullOrEmpty(detailorg_name.Text))
            {
                try
                {
                    org newOrg = new org()
                    {
                        name = detailorg_name.Text,
                        description = detailorg_desc.Text
                    };

                    if (parent_id != null)
                    {
                        newOrg.parent = parent_id;
                    }

                    p.orgs.Add(newOrg);
                    p.SaveChanges();

                    Response.Redirect("~/Admin/org.aspx");
                }
                catch (System.Exception ex)
                {
                    Response.Write(ex);
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            //get the id of the case
            if (!String.IsNullOrEmpty(Request.QueryString["id"]))
            {
                id = Convert.ToInt32(Request.QueryString["id"]);
                //Response.Write(id);
            }

            //check if user cookie exists, means if a user logged in
            if (Request.Cookies["user"] != null)
            {
                string decoded_username = Encoding.UTF8.GetString(MachineKey.Unprotect(Convert.FromBase64String(Request.Cookies["user"]["username"]), "ProtectCookieUsername"));
                string decoded_role = Encoding.UTF8.GetString(MachineKey.Unprotect(Convert.FromBase64String(Request.Cookies["user"]["role"]), "ProtectCookieRole"));
                drafter_id = Convert.ToInt32(Encoding.UTF8.GetString(MachineKey.Unprotect(Convert.FromBase64String(Request.Cookies["user"]["person"]), "ProtectCookiePerson")));

                if (decoded_role == "admin" || decoded_role == "support")
                {

                }

                else
                {
                    Response.Redirect("~/");
                }
            }

            else
            {
                Response.Redirect("~/");
            }

            //string SolutionURI = "DetailAction2?case_id=" + id + "&new=1";
            //detailcase2_addsolution.Attributes.Add("onClick", string.Format("window.open('{0}'),'_self'", SolutionURI));

            string ChildURI = "DetailCase2?new=1&parent=" + id;
            detailcase2_children_add.Attributes.Add("onClick", string.Format("window.open('{0},'_self)", ChildURI));

            //set the multiview initially shows other panel
            Other_Tab.CssClass = "Clicked";
            Report_Tab.CssClass = "Initial";
            Files_Tab.CssClass = "Initial";
            Children_Tab.CssClass = "Initial";
            Customer_Tab.CssClass = "Initial";
            MainView.ActiveViewIndex = 0;

            //to avoid duplication in dropdown lists
            if (IsPostBack)
            {
                detailcase2_handler.DataSourceID = "";
                Items.Clear();
            }

            //add 'none' to some dropdown lists to enable none selection
            detailcase2_handler.AppendDataBoundItems = true;

            //check if it is a new case
            if (!String.IsNullOrEmpty(Request.QueryString["new"]))
            {
                isNew = Convert.ToInt32(Request.QueryString["new"]);
            }
            //check if this case is a child of another case
            if (Request.QueryString["parent"] != null && Convert.ToInt32(Request.QueryString["parent"]) != 0)
            {
                parent_id = Convert.ToInt32(Request.QueryString["parent"]);
            }

            //detailcase2_interface.Items.Insert(0, new ListItem("--Select Department--", "-1"));
            //Response.Write(parent_id);
            DateTime lastupdatedDate = new DateTime(1970, 1, 1);
            if (!IsPostBack)
            {
                //ListItem itemToRemove = detailcase2_interface.Items.FindByText("--Interface--");
                //if (itemToRemove != null)
                //{
                //    itemToRemove.Attributes.Add("style", "color:gray;");
                //    itemToRemove.Attributes.Add("disabled", "true");
                //    itemToRemove.Value = "-1";
                //}

                //ListItem itemToRemove2 = detailcase2_handler.Items.FindByText("");
                //if (itemToRemove != null)
                //{
                //    itemToRemove2.Enabled = false;
                //}

                detailcase2_interface.AppendDataBoundItems = true;

                if (isNew == 1)
                {
                    Support s = new Support();
                    Incident newCase = new Incident()
                    {
                        reportdate = DateTime.Now,
                        lastupdatedate = lastupdatedDate,
                        enddate = DateTime.Now,
                        sales = 0,
                        supportteam = 0,
                        technote = 0,
                        instruction = 0,
                        faq = 0,
                        allcompany = 0,
                        drafter = drafter_id,
                        description = "default",
                        model = 19
                    };

                    s.Incidents.Add(newCase);

                    //load parent
                    if (parent_id != 0)
                    {
                        newCase.parent = parent_id;
                        //before doing other operations, check if the parent still exists.
                        List<int> all_cases = (from x in s.Incidents select x.id).ToList();
                        if (newCase.parent != null)
                        {
                            if (!all_cases.Contains(Convert.ToInt32(newCase.parent.ToString())))
                            {
                                //  detailcase2_parent.Text = "No parent...";
                            }

                            else
                            {
                                var qparent = from p in s.Incidents
                                              where p.id == (int)newCase.parent
                                              select p;
                                if (qparent.FirstOrDefault().description.Length > 10)
                                {
                                    detailcase2_parent.Text = qparent.FirstOrDefault().description.Substring(0, 10);
                                }
                                else
                                {
                                    detailcase2_parent.Text = qparent.FirstOrDefault().description;
                                }

                                //inherited parent's model
                                newCase.model = qparent.FirstOrDefault().model;

                                var mo = (from m in s.models
                                          where m.id == newCase.model
                                          select m).FirstOrDefault();
                                detailcase2_interface.SelectedValue = mo._interface.ToString();
                                String strConnString = ConfigurationManager
                                    .ConnectionStrings["SupportConnectionString"].ConnectionString;
                                String strQuery = "SELECT [cord] FROM [model] WHERE ([interface] = @interface)";
                                SqlConnection con = new SqlConnection(strConnString);
                                SqlCommand cmd = new SqlCommand();
                                cmd.Parameters.AddWithValue("@interface",
                                    Convert.ToInt32(detailcase2_interface.SelectedItem.Value.ToString()));
                                cmd.CommandType = CommandType.Text;
                                cmd.CommandText = strQuery;
                                cmd.Connection = con;
                                try
                                {
                                    con.Open();
                                    detailcase2_cord.DataSource = cmd.ExecuteReader();
                                    detailcase2_cord.DataTextField = "cord";
                                    detailcase2_cord.DataValueField = "cord";
                                    detailcase2_cord.DataBind();
                                }
                                catch (Exception ex)
                                {
                                    Response.Write(ex);
                                }
                                finally
                                {
                                    con.Close();
                                    con.Dispose();
                                }
                                detailcase2_cord.SelectedValue = mo.cord.ToString();

                                detailcase2_interface.Enabled = false;
                                detailcase2_cord.Enabled = false;
                            }
                        }
                        else
                        {
                            detailcase2_parent.Text = "No parent...";
                        }
                    }
                    s.SaveChanges();
                    id = newCase.id;

                    detailcase2_reportdate.Text = newCase.reportdate.ToString("MM/dd/yyyy");
                    var p_query = from p in s.people where p.id == newCase.drafter select p;
                    detailcase2_drafter.Text = p_query.FirstOrDefault().lastname + " " + p_query.FirstOrDefault().firstname;
                    //Response.Write(id);
                }
                else
                {
                    try
                    {
                        Support support = new Support();
                        Support personnel = new Support();
                        var cases = (from c in support.Incidents
                                     where c.id == id
                                     select new
                                     {
                                         c.allcompany,
                                         c.description,
                                         c.enddate,
                                         c.faq,
                                         c.instruction,
                                         c.lastupdatedate,
                                         c.other,
                                         c.reportdate,
                                         c.sales,
                                         c.supportteam,
                                         c.technote,
                                         c.handler,
                                         c.drafter,
                                         c.parent,
                                         c.cuscom,
                                         c.cusname,
                                         c.discom,
                                         c.dispname,
                                         c.model
                                     }).FirstOrDefault();

                        //parent
                        //before doing other operations, check if the parent still exists.
                        List<int> all_cases = (from x in support.Incidents select x.id).ToList();
                        if (cases.parent != null)
                        {
                            if (!all_cases.Contains(Convert.ToInt32(cases.parent.ToString())))
                            {
                                detailcase2_parent.Text = "N/A";
                            }

                            else
                            {
                                var qparent = from p in support.Incidents
                                              where p.id == (int)cases.parent
                                              select p;
                                if (qparent.FirstOrDefault().description.Length > 10)
                                {
                                    detailcase2_parent.Text = qparent.FirstOrDefault().description.Substring(0, 10);
                                }
                                else
                                {
                                    detailcase2_parent.Text = qparent.FirstOrDefault().description;
                                }
                            }
                        }
                        else
                        {
                            detailcase2_parent.Text = "N/A";
                        }

                        //customer name
                        if (!String.IsNullOrEmpty(cases.cusname))
                        {
                            detailcase2_cusname.Text = "" + cases.cusname.ToString();
                        }
                        else
                        {
                            detailcase2_cusname.Text = "";
                        }

                        //customer company
                        if (!String.IsNullOrEmpty(cases.cusname))
                        {
                            detailcase2_cuscom.Text = "" + cases.cuscom.ToString();
                        }
                        else
                        {
                            detailcase2_cuscom.Text = "";
                        }

                        //distrabution company
                        if (!String.IsNullOrEmpty(cases.discom))
                        {
                            detailcase2_discom.Text = "" + cases.discom.ToString();
                        }
                        else
                        {
                            detailcase2_discom.Text = "";
                        }

                        //distributor
                        if (!String.IsNullOrEmpty(cases.cusname))
                        {
                            detailcase2_disname.Text = "" + cases.dispname.ToString();
                        }
                        else
                        {
                            detailcase2_disname.Text = "";
                        }

                        //before doing other operations, check if the model still exists.
                        List<int> all_models = (from x in support.models select x.id).ToList();
                        if (cases.model != null)
                        {
                            var par = (from c in support.Incidents
                                       where c.id == cases.parent
                                       select c).FirstOrDefault();

                            int mod = Convert.ToInt32(cases.model.ToString());
                            if (all_models.Contains(mod))
                            {
                                using (Support s = new Support())
                                {
                                    var target = (from x in s.models
                                                  where x.id == mod
                                                  select x).FirstOrDefault();
                                    detailcase2_interface.SelectedValue = target._interface.ToString();
                                }
                                detailcase2_cord.Items.Clear();

                                detailcase2_cord.AppendDataBoundItems = true;
                                String strConnString = ConfigurationManager
                                    .ConnectionStrings["SupportConnectionString"].ConnectionString;
                                String strQuery = "SELECT [cord] FROM [model] WHERE model.interface = @interface";
                                SqlConnection con = new SqlConnection(strConnString);
                                SqlCommand cmd = new SqlCommand();
                                cmd.Parameters.AddWithValue("@interface",
                                    Convert.ToInt32(detailcase2_interface.SelectedItem.Value.ToString()));
                                cmd.CommandType = CommandType.Text;
                                cmd.CommandText = strQuery;
                                cmd.Connection = con;
                                try
                                {
                                    con.Open();
                                    detailcase2_cord.DataSource = cmd.ExecuteReader();
                                    detailcase2_cord.DataTextField = "cord";
                                    detailcase2_cord.DataValueField = "cord";
                                    detailcase2_cord.DataBind();
                                    if (detailcase2_cord.Items.Count >= 1)
                                    {
                                        //Response.Write("haha");
                                        detailcase2_cord.Enabled = true;

                                        if (par != null)
                                        {
                                            //Response.Write("hoho");
                                            using (Support s = new Support())
                                            {
                                                int par_mod = par.model.Value;

                                                var target = (from x in s.models
                                                              where x.id == par_mod
                                                              select x).FirstOrDefault();
                                                detailcase2_interface.SelectedValue = target._interface.ToString();
                                                detailcase2_cord.Text = target.cord.ToString();

                                                detailcase2_interface.Enabled = false;
                                                detailcase2_cord.Enabled = false;

                                                var ca = (from c in s.Incidents
                                                          where c.id == id
                                                          select c).FirstOrDefault();

                                                if (ca.model != par_mod)
                                                {
                                                    //Response.Write("hehe");
                                                    ca.model = par_mod;
                                                    s.SaveChanges();
                                                }
                                            }
                                        }

                                        else
                                        {
                                            using (Support s = new Support())
                                            {
                                                //Response.Write("hiahia");
                                                var target = (from x in s.models
                                                              where x.id == mod
                                                              select x).FirstOrDefault();
                                                detailcase2_cord.SelectedValue = target.cord.ToString();
                                            }
                                        }
                                    }
                                    else
                                    {
                                        detailcase2_cord.Enabled = false;
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Response.Write(ex);
                                }
                                finally
                                {
                                    con.Close();
                                    con.Dispose();
                                }
                            }
                        }

                        //report date
                        if (cases.reportdate != null)
                        {
                            detailcase2_reportdate.Text = "" + cases.reportdate.ToString("MM/dd/yyyy");
                        }
                        else
                        {
                            detailcase2_reportdate.Text = "";
                        }

                        //decription
                        if (String.IsNullOrEmpty(cases.description) || cases.description == "default")
                        {
                            detailcase2_desc.Text = "";
                        }
                        else
                        {
                            detailcase2_desc.Text = cases.description.ToString();

                        }

                        //is closed
                        if (cases.enddate > cases.reportdate)
                        {
                            detailcase2_isclosed_cb.Checked = true;
                        }
                        else
                        {
                            detailcase2_isclosed_cb.Checked = false;
                        }

                        //sales
                        if (cases.sales.HasValue)
                        {
                            detailcase2_sales_rb.SelectedIndex = cases.sales.Value;
                        }
                        else
                        {
                            detailcase2_sales_rb.SelectedIndex = 0;
                        }

                        //support team
                        if (cases.supportteam.HasValue)
                        {
                            detailcase2_supportteam_rb.SelectedIndex = cases.supportteam.Value;
                        }
                        else
                        {
                            detailcase2_supportteam_rb.SelectedIndex = 0;
                        }

                        //tech note
                        if (cases.technote.HasValue)
                        {
                            detailcase2_technote_rb.SelectedIndex = cases.technote.Value;
                        }
                        else
                        {
                            detailcase2_technote_rb.SelectedIndex = 0;
                        }

                        //instruction
                        if (cases.instruction.HasValue)
                        {
                            detailcase2_inst_rb.SelectedIndex = cases.instruction.Value;
                        }
                        else
                        {
                            detailcase2_inst_rb.SelectedIndex = 0;
                        }

                        //faq
                        if (cases.faq.HasValue)
                        {
                            detailcase2_faq_rb.SelectedIndex = cases.faq.Value;
                        }
                        else
                        {
                            detailcase2_faq_rb.SelectedIndex = 0;
                        }

                        //all company
                        if (cases.allcompany.HasValue)
                        {
                            detailcase2_allcompany_rb.SelectedIndex = cases.allcompany.Value;
                        }
                        else
                        {
                            detailcase2_allcompany_rb.SelectedIndex = 0;
                        }

                        //detailcase2_sales_dp.SelectedValue = cases.sales.ToString();
                        //detailcase2_supportteam_dp.SelectedValue = cases.supportteam.ToString();
                        //detailcase2_technote_dp.SelectedValue = cases.technote.ToString();
                        //detailcase2_allcompany_dp.SelectedValue = cases.allcompany.ToString();
                        //detailcase2_faq_dp.SelectedValue = cases.faq.ToString();
                        //detailcase2_inst_dp.SelectedValue = cases.instruction.ToString();

                        //other
                        if (!String.IsNullOrEmpty(cases.other))
                        {
                            other = cases.other.ToString();
                            detailcase2_other.Text = other;
                        }
                        else
                        {
                            detailcase2_other.Text = "";
                        }

                        //drafter
                        List<int> all_person = (from x in personnel.people select x.id).ToList();
                        if (!all_person.Contains(Convert.ToInt32(cases.drafter.ToString())))
                        {
                            detailcase2_drafter.Text = "";
                        }
                        else
                        {
                            int d = cases.drafter;
                            var p_query = from p in personnel.people where p.id == d select p;
                            detailcase2_drafter.Text = "" + p_query.FirstOrDefault().lastname + " " + p_query.FirstOrDefault().firstname;
                        }

                        //handler
                        List<int> all_members = (from x in personnel.members select x.id).ToList();
                        if (cases.handler != null)
                        {
                            if (all_members.Contains(Convert.ToInt32(cases.handler.ToString())))
                            {
                                detailcase2_handler.SelectedValue = cases.handler.ToString();
                            }
                            else
                            {
                                detailcase2_handler.SelectedValue = "";
                            }
                        }
                        else
                        {
                            detailcase2_handler.SelectedValue = "";
                        }
                    }
                    catch (Exception ex)
                    {
                        Response.Write(ex);
                    }
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Cookies["user"] != null)
            {
                string decoded_username = Encoding.UTF8.GetString(MachineKey.Unprotect(Convert.FromBase64String(Request.Cookies["user"]["username"]), "ProtectCookieUsername"));
                string decoded_role = Encoding.UTF8.GetString(MachineKey.Unprotect(Convert.FromBase64String(Request.Cookies["user"]["role"]), "ProtectCookieRole"));

                if (decoded_role == "admin")
                {

                }

                else
                {
                    Response.Redirect("~/");
                }
            }

            else
            {
                Response.Redirect("~/");
            }

            if (!String.IsNullOrEmpty(Request.QueryString["id"]))
            {
                id = Convert.ToInt32(Request.QueryString["id"]);
            }

            int isNew = Convert.ToInt32(Request.QueryString["new"]);

            if (!IsPostBack)
            {
                if (isNew == 1)
                {
                        model NewModel = new model()
                        {
                            _interface=1,
                            cord = "this is a new item"
                        };

                        Support s = new Support();
                        s.models.Add(NewModel);
                        s.SaveChanges();
                        id = NewModel.id;
                }
                else
                {
                    try
                    {
                        Support support = new Support();
                        var model = from m in support.models
                                    where m.id == id
                                    select new
                                    {
                                        m.name,
                                        m._interface,
                                        m.cord
                                    };

                        //name
                        if (!String.IsNullOrEmpty(model.FirstOrDefault().name))
                        {
                            name = model.FirstOrDefault().name.ToString();
                            detailmodel_name.Text = name;
                        }
                        else
                        {
                            name = "No name...";
                        }

                        //cord
                        desc = model.FirstOrDefault().cord.ToString();
                        detailmodel_cord.Text = desc;

                        utils u = new utils();

                        //interface
                        detailmodel_interface.Text = u.interfaceUntil(model.FirstOrDefault()._interface);

                    }
                    catch (Exception ex)
                    {
                        Response.Write(ex);
                    }

                    using (Support s = new Support())
                    {
                        var target = (from x in s.models
                                      where x.id == id
                                      select x).FirstOrDefault();
                        detailmodel_interface.SelectedValue = target._interface.ToString();
                    }
                }
            }
        }
Beispiel #24
0
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                /*
                //Get the LinkButton control in the first cell
                LinkButton _doubleClickButton = (LinkButton)e.Row.Cells[0].Controls[0].FindControl("CaseDoubleClick");
                //Get the javascript which is assigned to this LinkButton
                string _jsDouble =
                ClientScript.GetPostBackClientHyperlink(_doubleClickButton, "");
                //Add this JavaScript to the ondblclick Attribute of the row
                e.Row.Attributes["ondblclick"] = _jsDouble;
                */

                //if (!String.IsNullOrEmpty(e.Row.Cells[7].Text))
                //{
                //    int num = Convert.ToInt32(e.Row.Cells[7].Text);
                //    e.Row.Cells[7].Text = workstatus(num);
                //    e.Row.Cells[7].BackColor = statusColor(num);
                //}

                //if (!String.IsNullOrEmpty(e.Row.Cells[8].Text))
                //{
                //    int num = Convert.ToInt32(e.Row.Cells[8].Text);
                //    e.Row.Cells[8].Text = workstatus(num);
                //    e.Row.Cells[8].BackColor = statusColor(num);
                //}

                //if (!String.IsNullOrEmpty(e.Row.Cells[9].Text))
                //{
                //    int num = Convert.ToInt32(e.Row.Cells[9].Text);
                //    e.Row.Cells[9].Text = workstatus(num);
                //    e.Row.Cells[9].BackColor = statusColor(num);
                //}

                //if (!String.IsNullOrEmpty(e.Row.Cells[10].Text))
                //{
                //    int num = Convert.ToInt32(e.Row.Cells[10].Text);
                //    e.Row.Cells[10].Text = workstatus(num);
                //    e.Row.Cells[10].BackColor = statusColor(num);
                //}

                //if (!String.IsNullOrEmpty(e.Row.Cells[11].Text))
                //{
                //    int num = Convert.ToInt32(e.Row.Cells[11].Text);
                //    e.Row.Cells[11].Text = workstatus(num);
                //    e.Row.Cells[11].BackColor = statusColor(num);
                //}

                //if (!String.IsNullOrEmpty(e.Row.Cells[12].Text))
                //{
                //    int num = Convert.ToInt32(e.Row.Cells[12].Text);
                //    e.Row.Cells[12].Text = workstatus(num);
                //    e.Row.Cells[12].BackColor = statusColor(num);
                //}

                //Response.Write(e.Row.Cells[8].Text +"c8");
                //Response.Write(e.Row.Cells[9].Text +"c9");

                //the checked box item

                //if (showtechnote.Checked == false)
                //    GridView1.Columns[9].Visible = false;
                //else if (showtechnote.Checked == true)
                //    GridView1.Columns[9].Visible = true;

                //if (showsales.Checked == false)
                //    GridView1.Columns[7].Visible = false;
                //else if (showsales.Checked == true)
                //    GridView1.Columns[7].Visible = true;

                //if (showsupportteam.Checked == false)
                //    GridView1.Columns[8].Visible = false;
                //else if (showsupportteam.Checked == true)
                //    GridView1.Columns[8].Visible = true;

                //if (showinstruction.Checked == false)
                //    GridView1.Columns[10].Visible = false;
                //else if (showinstruction.Checked == true)
                //    GridView1.Columns[10].Visible = true;

                //if (showfaq.Checked == false)
                //    GridView1.Columns[11].Visible = false;
                //else if (showfaq.Checked == true)
                //    GridView1.Columns[11].Visible = true;

                //if (showallcompany.Checked == false)
                //    GridView1.Columns[12].Visible = false;
                //else if (showallcompany.Checked == true)
                //    GridView1.Columns[12].Visible = true;

                //if (showother.Checked == false)
                //    GridView1.Columns[13].Visible = false;
                //else if (showother.Checked == true)
                //    GridView1.Columns[13].Visible = true;

                //Response.Write(e.Row.Cells[1].Text);
                string QueryString = "~/Detail/DetailCase2.aspx?id=" + e.Row.Cells[1].Text;
                string NavigateURL = ResolveUrl(QueryString);
                e.Row.Attributes.Add("onClick", string.Format("window.open('{0}')", NavigateURL));
                e.Row.Style.Add("cursor", "pointer");

                try
                {
                    Support support = new Support();
                    Support personnel = new Support();
                    int rid = Convert.ToInt32(e.Row.Cells[1].Text);
                    var cases = from c in support.Incidents
                                where c.id == rid
                                select new
                                {
                                    c.allcompany,
                                    c.model,
                                    c.description,
                                    c.enddate,
                                    c.faq,
                                    c.instruction,
                                    c.lastupdatedate,
                                    c.other,
                                    c.reportdate,
                                    c.sales,
                                    c.supportteam,
                                    c.technote,
                                    c.handler,
                                    c.drafter,
                                    c.parent,
                                };
                    //description
                    if (cases.FirstOrDefault().description != null)
                    {
                        //if (Encoding.GetEncoding("utf-8").GetBytes(cases.FirstOrDefault().description).Length > 30)
                        //{
                        //    e.Row.Cells[3].Text = cases.FirstOrDefault().description.Substring(0, 30) + "...";
                        //}
                    }
                    if (cases.FirstOrDefault().model != null)
                    {
                        //interface and cord
                        using (Support mod = new Support())
                        {
                            var par = (from c in support.Incidents
                                       where c.id == cases.FirstOrDefault().parent
                                       select c).FirstOrDefault();

                            if (par != null)
                            {
                                using (Support s = new Support())
                                {
                                    int par_mod = par.model.Value;

                                    var target = (from x in s.models
                                                  where x.id == par_mod
                                                  select x).FirstOrDefault();

                                    var ca = (from c in s.Incidents
                                              where c.id == rid
                                              select c).FirstOrDefault();

                                    if (ca.model != par_mod)
                                    {
                                        ca.model = par_mod;
                                        s.SaveChanges();
                                    }
                                }
                            }

                            System.Web.UI.WebControls.Image img = e.Row.FindControl("ImageInterface") as System.Web.UI.WebControls.Image;
                            var option = (from o in support.models
                                          where o.id == cases.FirstOrDefault().model
                                          select o).FirstOrDefault();
                            utils u = new utils();

                            img.ImageUrl = u.interfaceUntil(option._interface);

                            Label lbCord = e.Row.FindControl("TextBoxCord") as Label;
                            lbCord.Text = option.cord;
                        }
                    }
                    //drafter
                    List<int> all_person = (from x in personnel.people select x.id).ToList();
                    if (!all_person.Contains(Convert.ToInt32(cases.FirstOrDefault().drafter.ToString())))
                    {
                        e.Row.Cells[5].Text = "No drafter...";
                    }
                    else
                    {
                        int d = cases.FirstOrDefault().drafter;
                        var p_query = from p in personnel.people where p.id == d select p;
                        e.Row.Cells[5].Text = p_query.FirstOrDefault().lastname + " " + p_query.FirstOrDefault().firstname;
                    }

                    //report
                    //all company
                    if (cases.FirstOrDefault().allcompany == 1)
                    {
                        (e.Row.Cells[6].FindControl("list_allcompany") as Label).Visible = true;
                        (e.Row.Cells[6].FindControl("list_allcompany") as Label).ForeColor = statusColor(1);
                    }
                    else if (cases.FirstOrDefault().allcompany == 2)
                    {
                        (e.Row.Cells[6].FindControl("list_allcompany") as Label).Visible = true;
                        (e.Row.Cells[6].FindControl("list_allcompany") as Label).ForeColor = statusColor(2);
                    }
                    //all company
                    if (cases.FirstOrDefault().sales == 1)
                    {
                        (e.Row.Cells[6].FindControl("list_sales") as Label).Visible = true;
                        (e.Row.Cells[6].FindControl("list_sales") as Label).ForeColor = statusColor(1);
                    }
                    else if (cases.FirstOrDefault().sales == 2)
                    {
                        (e.Row.Cells[6].FindControl("list_sales") as Label).Visible = true;
                        (e.Row.Cells[6].FindControl("list_sales") as Label).ForeColor = statusColor(2);
                    }
                    //instruction
                    if (cases.FirstOrDefault().instruction == 1)
                    {
                        (e.Row.Cells[6].FindControl("list_instruction") as Label).Visible = true;
                        (e.Row.Cells[6].FindControl("list_instruction") as Label).ForeColor = statusColor(1);
                    }
                    else if (cases.FirstOrDefault().instruction == 2)
                    {
                        (e.Row.Cells[6].FindControl("list_instruction") as Label).Visible = true;
                        (e.Row.Cells[6].FindControl("list_instruction") as Label).ForeColor = statusColor(2);
                    }

                    //tech note
                    if (cases.FirstOrDefault().technote == 1)
                    {
                        (e.Row.Cells[6].FindControl("list_technote") as Label).Visible = true;
                        (e.Row.Cells[6].FindControl("list_technote") as Label).ForeColor = statusColor(1);
                    }
                    else if (cases.FirstOrDefault().technote == 2)
                    {
                        (e.Row.Cells[6].FindControl("list_technote") as Label).Visible = true;
                        (e.Row.Cells[6].FindControl("list_technote") as Label).ForeColor = statusColor(2);
                    }

                    //support team
                    if (cases.FirstOrDefault().supportteam == 1)
                    {
                        (e.Row.Cells[6].FindControl("list_supportteam") as Label).Visible = true;
                        (e.Row.Cells[6].FindControl("list_supportteam") as Label).ForeColor = statusColor(1);
                    }
                    else if (cases.FirstOrDefault().supportteam == 2)
                    {
                        (e.Row.Cells[6].FindControl("list_supportteam") as Label).Visible = true;
                        (e.Row.Cells[6].FindControl("list_supportteam") as Label).ForeColor = statusColor(2);
                    }
                    //faq
                    if (cases.FirstOrDefault().faq == 1)
                    {
                        (e.Row.Cells[6].FindControl("list_faq") as Label).Visible = true;
                        (e.Row.Cells[6].FindControl("list_faq") as Label).ForeColor = statusColor(1);
                    }
                    else if (cases.FirstOrDefault().faq == 2)
                    {
                        (e.Row.Cells[6].FindControl("list_faq") as Label).Visible = true;
                        (e.Row.Cells[6].FindControl("list_faq") as Label).ForeColor = statusColor(2);
                    }

                    if(cases.FirstOrDefault().enddate > cases.FirstOrDefault().reportdate)
                    {
                        e.Row.BorderStyle = BorderStyle.Solid;
                        e.Row.BorderColor = Color.DarkGray;
                    }

                    //if (!String.IsNullOrEmpty(e.Row.Cells[8].Text))
                    //{
                    //    int num = Convert.ToInt32(e.Row.Cells[8].Text);
                    //    e.Row.Cells[8].Text = workstatus(num);
                    //    e.Row.Cells[8].BackColor = statusColor(num);
                    //}

                    //if (!String.IsNullOrEmpty(e.Row.Cells[9].Text))
                    //{
                    //    int num = Convert.ToInt32(e.Row.Cells[9].Text);
                    //    e.Row.Cells[9].Text = workstatus(num);
                    //    e.Row.Cells[9].BackColor = statusColor(num);
                    //}

                    //if (!String.IsNullOrEmpty(e.Row.Cells[10].Text))
                    //{
                    //    int num = Convert.ToInt32(e.Row.Cells[10].Text);
                    //    e.Row.Cells[10].Text = workstatus(num);
                    //    e.Row.Cells[10].BackColor = statusColor(num);
                    //}

                    //if (!String.IsNullOrEmpty(e.Row.Cells[11].Text))
                    //{
                    //    int num = Convert.ToInt32(e.Row.Cells[11].Text);
                    //    e.Row.Cells[11].Text = workstatus(num);
                    //    e.Row.Cells[11].BackColor = statusColor(num);
                    //}

                    //if (!String.IsNullOrEmpty(e.Row.Cells[12].Text))
                    //{
                    //    int num = Convert.ToInt32(e.Row.Cells[12].Text);
                    //    e.Row.Cells[12].Text = workstatus(num);
                    //    e.Row.Cells[12].BackColor = statusColor(num);

                    /*
                    //handler
                    List<int> all_members = (from x in personnel.members select x.id).ToList();
                    if (cases.FirstOrDefault().handler != null)
                    {
                        if (!all_members.Contains(Convert.ToInt32(cases.FirstOrDefault().handler.ToString())))
                        {
                            e.Row.Cells[7].Text = "No handler";
                        }
                        else
                        {
                            int mem = Convert.ToInt32(cases.FirstOrDefault().handler.ToString());
                            var m_query = from me in personnel.people
                                          join m in personnel.members
                                              on me.id equals m.person
                                          where m.id == mem
                                          select me;
                            e.Row.Cells[7].Text = m_query.FirstOrDefault().lastname + " " + m_query.FirstOrDefault().firstname; ;
                        }
                    }
                    else
                    {
                        e.Row.Cells[7].Text = "No handler...";
                    }
                    */

                    ////files
                    //List<int> all_files = (from x in support.Files select x.incident).ToList();
                    //if (!all_files.Contains(Convert.ToInt32(rid)))
                    //{
                    //    e.Row.Cells[14].Text = "No attachment";
                    //}
                    //else
                    //{
                    //    var f_query = from f in support.Files
                    //                  where f.incident == rid
                    //                  select f.name;
                    //    e.Row.Cells[14].Text = f_query.FirstOrDefault().ToString();
                    //}

                    //action
                    if (!actnum.ContainsKey(Convert.ToInt32(rid)))
                    {
                        e.Row.Cells[7].Text = "N/A";
                    }
                    else
                    {
                        e.Row.Cells[7].Text = actnum[rid].ToString();
                    }

                    e.Row.Cells[1].Visible = false;
                }

                catch (Exception ex)
                {
                    Response.Write(ex);
                }
            }

            if (e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[1].Visible = false;
            }
        }
        protected void Unnamed3_Click(object sender, EventArgs e)
        {
            try
            {
                file_model file = new file_model()
                {
                    name = detailfile_name.Text,
                    fpath = detailfile_path.Text,
                    dt = DateTime.Now,
                    sender = Convert.ToInt32(person_id),
                    incident = case_id,
                    note = detailfile_note.Text
                };

                Support s = new Support();
                s.Files.Add(file);
                s.SaveChanges();

                Response.Redirect(prevPage);
            }
            catch (System.Exception ex)
            {
                Response.Write(ex);
            }
        }
        protected void Reset_Click(object sender, EventArgs e)
        {
            user users = new user();

            Support user_model = new Support();

            List<string> all_users = (from x in user_model.users select x.username).ToList();
            var valid = (from v in user_model.users
                         where v.username == decoded_username
                         select v).FirstOrDefault();

            if (valid.password == Password.Text)
            {

                if (NewPassword.Text == ConfirmPassword.Text)
                {

                    valid.password = ConfirmPassword.Text;
                    user_model.SaveChanges();
                    if (HttpContext.Current.Request.Cookies["user"] != null)
                    {
                        HttpCookie aCookie = HttpContext.Current.Request.Cookies["user"];
                        aCookie.Expires = DateTime.Now.AddDays(-10);
                        aCookie.Value = "";
                        HttpContext.Current.Response.Cookies.Add(aCookie);
                    }
                    Response.Redirect("~/Account/ResetPasswordConfirmation");
                    return;
                }
            }
            else
            {
                ErrorMessage.Text = "Wrong Password";
            }
        }
 protected void detailaction2_desc_TextChanged(object sender, EventArgs e)
 {
     try
     {
         //Response.Write(id);
         using (Support support = new Support())
         {
             var desc = (from c in support.Actions
                         where c.id == id
                         select c).FirstOrDefault();
             desc.description = detailaction2_desc.Text;
             support.SaveChanges();
         }
     }
     catch (System.Exception ex)
     {
         Response.Write(ex);
     }
 }
        ////edit
        //protected void Unnamed_Click(object sender, EventArgs e)
        //{
        //    detailorg2_submit.Visible = true;
        //    detailorg2_cancel.Visible = true;
        //    detailorg2_name.ReadOnly = false;
        //    detailorg2_desc.ReadOnly = false;
        //}
        ////submit
        //protected void detailorg2_submit_Click(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        using (Support personnel = new Support())
        //        {
        //            org target = (from x in personnel.orgs
        //                                where x.id == id
        //                                select x).FirstOrDefault();
        //            target.name = detailorg2_name.Text;
        //            target.description = detailorg2_desc.Text;
        //            personnel.SaveChanges();
        //        }
        //        detailorg2_name.ReadOnly = true;
        //        detailorg2_desc.ReadOnly = true;
        //        detailorg2_submit.Visible = false;
        //        detailorg2_cancel.Visible = false;
        //    }
        //    catch (System.Exception ex)
        //    {
        //        Response.Write(ex);
        //    }
        //}
        ////cancel
        //protected void detailorg2_cancel_Click(object sender, EventArgs e)
        //{
        //    detailorg2_name.ReadOnly = true;
        //    detailorg2_desc.ReadOnly = true;
        //    detailorg2_submit.Visible = false;
        //    detailorg2_cancel.Visible = false;
        //    detailorg2_name.Text = name;
        //    detailorg2_desc.Text = desc;
        //}
        protected void detailorg2_delete_Click(object sender, EventArgs e)
        {
            using (Support p = new Support())
            {
                List<org> orgs = p.orgs.ToList();

                var target = (from x in p.orgs
                              where x.id == id
                              select x).FirstOrDefault();
                foreach(org o in orgs)
                {
                    if (o.parent == target.id)
                    {
                        Response.Write("This department has child department, to delete, please delete child department first...");
                        return;
                    }
                }
                p.orgs.Remove(target);
                p.SaveChanges();
            }
            Response.Redirect(prevPage);
        }
        //submit
        protected void Unnamed4_Click(object sender, EventArgs e)
        {
            DateTime startDate = DateTime.Parse(detailmember_startdate.Text);
            DateTime endDate = new DateTime(3000, 1, 1);
            Support support = new Support();

            try
            {
                member member = new member()
                {
                    person = person_id,
                    sd = startDate,
                    ed = endDate,
                    org = Convert.ToInt32(detailmember_org.SelectedValue.ToString()),
                    role = Convert.ToInt32(detailmember_role.SelectedValue.ToString())
                };

                Support p = new Support();
                p.members.Add(member);
                p.SaveChanges();

                Response.Redirect("~/Admin/Member");
            }
            catch (System.Exception ex)
            {
                Response.Write(ex);
            }
        }
        protected void detailcase2_isclosed_cb_CheckedChanged(object sender, EventArgs e)
        {
            using (Support support = new Support())
            {
                var cases = (from c in support.Incidents
                             where c.id == id
                             select c).FirstOrDefault();

                if (detailcase2_isclosed_cb.Checked)
                {
                    List<Incident> all_cases = support.Incidents.ToList();
                    foreach (Incident cc in all_cases)
                    {
                        if (cc.parent == cases.id)
                        {
                            if (cc.enddate == cc.reportdate)
                            {
                                detailcase2_isclosed_cb.Checked = false;
                                Response.Write("Due to one of this case's children is still enable, this case can not be closed!");
                                return;
                            }
                        }
                    }
                    if (!(cases.enddate > cases.reportdate))
                    {
                        cases.enddate = DateTime.Now;
                    }

                }

                else if (!detailcase2_isclosed_cb.Checked)
                {
                    if (cases.enddate > cases.reportdate)
                    {
                        cases.enddate = cases.reportdate;
                        //var par = (from c in support.Incidents
                        //           where c.id == cases.parent
                        //           select c).FirstOrDefault();

                        Incident p = support.Incidents.Where(x => x.id == cases.id).FirstOrDefault();
                        //Response.Write(p.id);
                        enableParents(cases);
                    }
                }

                if (isNew == 0)
                {
                    cases.lastupdatedate = DateTime.Now;
                }

                support.SaveChanges();
            }
        }