using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class pg_ContactUs : System.Web.UI.Page
{
    //***********************************
    SqlConnection con;
    SqlCommand cmd;
    //***********************************
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!Page.IsPostBack)
            {
                Label lblTitle = (Label)this.Master.FindControl("lblTitle");
                lblTitle.Text = "ارتباط با ما";
                C_myClass.MenueItemsCreate(User, panelMenues);
            }
        }
        catch
        {
        }
    }
    protected void btnOk_Click(object sender, EventArgs e)
    {
        try
        {
            string letterType = "", senderRole = "",subject = "", letterDate = "", senderName = "", context = "", status = "";
            subject = tbSubject.Text;
            letterType = (cmbType.SelectedIndex > 0 ? cmbType.SelectedItem.ToString() : "");
            letterDate = _UseFull.C_Usefull.FarsiShamsiDate(DateTime.Now);
            status = "unread";
            context = Editor1.Content;
            string conString = RamanConnectionString.LocalConnectionString();
            con = new SqlConnection(conString);
            cmd = con.CreateCommand();
            if (Roles.IsUserInRole("student"))
            {
                senderRole = "student";
                senderName = _UseFull.C_Usefull.FindStringWithStringShart(con, cmd, "t_Students", "fullName", "userName", User.Identity.Name);
            }
            else if (Roles.IsUserInRole("admin"))
            {
                senderRole = "admin";
                senderName = _UseFull.C_Usefull.FindStringWithStringShart(con, cmd, "t_Admins", "fullName", "userName", User.Identity.Name);
            }
            else if (Roles.IsUserInRole("manager"))
            {
                senderRole = "manager";
                senderName = _UseFull.C_Usefull.FindStringWithStringShart(con, cmd, "t_Managers", "fullName", "userName", User.Identity.Name);
            }
            else if (Roles.IsUserInRole("teacher"))
            {
                senderRole = "teacher";
                senderName = _UseFull.C_Usefull.FindStringWithStringShart(con, cmd, "t_Teachers", "fullName", "userName", User.Identity.Name);
            }
            else if (Roles.IsUserInRole("user"))
            {
                senderRole = "user";
                senderName = _UseFull.C_Usefull.FindStringWithStringShart(con, cmd, "t_Users", "fullName", "userName", User.Identity.Name);
            }
            try
            {
                con.Open();
                cmd.CommandText = "insert into t_Letters (letterType, subject, letterDate, senderName, context, status, senderRole) values(N'" +
                    letterType + "', N'" +
                    subject + "', N'" +
                    letterDate + "', N'" +
                    senderName + "', N'" +
                    context + "', N'" +
                    status + "', N'" +
                    senderRole + "')";
                cmd.ExecuteNonQuery();
                lblMessage.ForeColor = System.Drawing.Color.Green;
                lblMessage.Text = "با موفقیت ارسال شد.";
            }
            catch(Exception ex)
            {
                lblMessage.ForeColor = System.Drawing.Color.Red;
                //lblMessage.Text = ex.Message;
                lblMessage.Text = "خطا در ارسال نامه.";
            }
            finally
            {
                if (con.State != ConnectionState.Closed)
                    con.Close();
            }
        }
        catch(Exception exc)
        {
            lblMessage.ForeColor = System.Drawing.Color.Red;
            //lblMessage.Text = exc.Message;
            lblMessage.Text = "خطا در ارسال نامه.";
        }
    }
}
