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;
using System.IO;

public partial class rl_admins_pg_NewsCreate : System.Web.UI.Page
{
    //****************************
    SqlConnection con;
    SqlTransaction tr;
    //****************************
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            C_myClass.MenueItemsCreate(User, panelMenues);
            Label lblTitle = (Label)this.Master.FindControl("lblTitle");
            lblTitle.Text = "خبر جدید";
            if (!Page.IsPostBack)
            {
                imgNewsPic.ImageUrl = "~/images/nophotosquare.gif";
                string today = _UseFull.C_Usefull.FarsiShamsiDate(DateTime.Now);
                tbNewsDate.Text = today;
                tbSubject.Focus();
            }
        }
        catch
        {
        }
    }

    protected void btnOk_Click(object sender, EventArgs e)
    {
        try
        {
            string conString = RamanConnectionString.LocalConnectionString();
            con = new SqlConnection(conString);
            SqlCommand cmd = con.CreateCommand();
            con.Open();
            tr = con.BeginTransaction();
            cmd.Transaction = tr;
            /////////////////////////////////////////
            bool showInList = (chShowInList.Checked ? true : false);
            string newsDate = tbNewsDate.Text;
            string subject = tbSubject.Text;
            string context = tbContext.Text;
            string addedBy = "";
            int numOfVisits = 0;
            string newsPicUrl = "";
            string lastEditorUser = "";
            if (Roles.IsUserInRole("manager"))
            {
                addedBy = _UseFull.C_Usefull.FindStringWithStringShart(con, cmd, "t_Managers", "fullName", "userName", User.Identity.Name);
                lastEditorUser = _UseFull.C_Usefull.FindStringWithStringShart(con, cmd, "t_Managers", "fullName", "userName", User.Identity.Name);
            }
            else if (Roles.IsUserInRole("admin"))
            {
                addedBy = _UseFull.C_Usefull.FindStringWithStringShart(con, cmd, "t_Admins", "fullName", "userName", User.Identity.Name);
                lastEditorUser = _UseFull.C_Usefull.FindStringWithStringShart(con, cmd, "t_Admins", "fullName", "userName", User.Identity.Name);
            }
            if (FileUpload1.HasFile)
            {
                string fileName = FileUpload1.FileName;
                if (C_myClass.IsValidFileType(fileName))
                {
                    string ext = C_myClass.FileExtenssion(fileName);
                    string photoesPath = Request.PhysicalApplicationPath + @"images\";
                    if (File.Exists(photoesPath + _UseFull.C_Usefull.Serialize(fileName, 7).Trim() + @"." + ext))
                    {
                        Random rnd = new Random();
                        int randomNum = rnd.Next(1, 9999999);
                        fileName += randomNum.ToString();
                    }
                    fileName = _UseFull.C_Usefull.Serialize(fileName, 7).Trim();
                    newsPicUrl = fileName + @"." + ext;
                    FileUpload1.SaveAs(Request.PhysicalApplicationPath + @"images\" + newsPicUrl);
                }
                else
                {
                    newsPicUrl = "nophotosquare.gif";
                }
            }
            else
            {
                newsPicUrl = "nophotosquare.gif";
            }
            string lastEditDate = _UseFull.C_Usefull.FarsiShamsiDate(DateTime.Now);
            ////////////////////////////////////////////////////////////////////////////////////// insert :
            cmd.CommandText = t_NewsInsertCommand(newsDate, subject, context, numOfVisits, addedBy, newsPicUrl, lastEditDate, lastEditorUser, showInList);
            cmd.ExecuteNonQuery();
            tr.Commit();
            lblMessage.ForeColor = System.Drawing.Color.Green;
            lblMessage.Text = "با موفقیت ذخیره شد.";
        }
        catch
        {
            try
            {
                tr.Rollback();
                lblMessage.ForeColor = System.Drawing.Color.Red;
                lblMessage.Text = "عملیات ذخیره سازی با شکست مواجه شد.";
            }
            catch
            {
            }
        }
        finally
        {
            if (con.State != ConnectionState.Closed)
                con.Close();
        }
    }

    private string t_NewsInsertCommand(string NewsDate, string Subject, string Context, int NumOfVisits, string AddedBy, string NewsPicUrl, string LastEditDate, string LastEditorUser, bool ShowInList)
    {
        string comm = "insert into t_News (newsDate, subject, context, numOfVisits, addedBy, newsPicUrl, lastEditDate, lastEditorUser, showInList) VALUES(N'" +
            NewsDate + "', N'" +
            Subject + "', N'" +
            Context + "', " +
            NumOfVisits + ", N'" +
            AddedBy + "', N'" +
            NewsPicUrl + "', N'" +
            LastEditDate + "', N'" +
            LastEditorUser + "', " +
            (ShowInList ? 1 : 0) + " )";
        return comm;
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        try
        {
            string returnUrl = Request.QueryString["returnUrl"].ToString();
            Response.Redirect(returnUrl);
        }
        catch
        {
        }
    }
}
