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_NewsDetailsEdit : System.Web.UI.Page
{
    //********************************
    SqlConnection con;
    SqlTransaction tr;
    static string newsPicUrl = "";
    //********************************
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            C_myClass.MenueItemsCreate(User, panelMenues);
            Label lblTitle = (Label)this.Master.FindControl("lblTitle");
            lblTitle.Text = "ویرایش خبر";
            string conString = RamanConnectionString.LocalConnectionString();
            con = new SqlConnection(conString);
            if (!Page.IsPostBack)
            {
                try
                {
                    bool showInList = false;
                    int numOfVisits = 0;
                    string newsDate = "", subject = "", context = "", lastEditDate = "", lastEditorUser = "";
                    int newsId = Int32.Parse(Request.QueryString["newsId"].ToString());
                    con.Open();
                    SqlCommand cmd = con.CreateCommand();
                    cmd.CommandText = "select newsDate, subject, context, numOfVisits, newsPicUrl, lastEditDate, lastEditorUser, showInList FROM t_News Where newsId = " + newsId;
                    SqlDataReader rdr = cmd.ExecuteReader();
                    if (rdr.HasRows)
                    {
                        while (rdr.Read())
                        {
                            newsDate = ( rdr.GetValue(0) != DBNull.Value ? rdr.GetString(0) : "");
                            subject = (rdr.GetValue(1) != DBNull.Value ? rdr.GetString(1) : "");
                            context = (rdr.GetValue(2) != DBNull.Value ? rdr.GetString(2) : "");
                            numOfVisits = (rdr.GetValue(3) != DBNull.Value ? (int)rdr.GetInt32(3) : 0);
                            newsPicUrl = (rdr.GetValue(4) != DBNull.Value ? rdr.GetString(4) : "");
                            lastEditDate = (rdr.GetValue(5) != DBNull.Value ? rdr.GetString(5) : "");
                            lastEditorUser = (rdr.GetValue(6) != DBNull.Value ? rdr.GetString(6) : "");
                            showInList = (rdr.GetValue(7) != DBNull.Value ? rdr.GetBoolean(7) : false);
                        }
                    }
                    rdr.Close();
                    lblNewsId.Text = newsId.ToString();
                    lblNumOfVisits.Text = numOfVisits.ToString();
                    lblLastEdit.Text = "در تاریخ :" + lastEditDate + " توسط " + lastEditorUser;
                    tbNewsDate.Text = newsDate;
                    tbSubject.Text = subject;
                    tbContext.Text = context;
                    chShowInList.Checked = showInList;
                    string imagesUrl = Request.PhysicalApplicationPath + @"images\";
                    if (!File.Exists(imagesUrl + newsPicUrl))
                    {
                        imgNewsPic.ImageUrl = "~/images/nophotosquare.gif";
                    }
                    else
                    {
                        imgNewsPic.ImageUrl = "~/images/" + newsPicUrl;
                    }
                }
                catch
                {
                }
                finally
                {
                    if (con.State != ConnectionState.Closed)
                        con.Close();
                }
            }
        }
        catch
        {
        }
        finally
        {
        }
    }
    protected void btnOk_Click(object sender, EventArgs e)
    {
        try
        {
            int newsId = Int32.Parse(Request.QueryString["newsId"].ToString());
            bool showInList = (chShowInList.Checked ? true : false);
            string newsDate = tbNewsDate.Text;
            string subject = tbSubject.Text;
            string context = tbContext.Text;
            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);
                }
            }
            string lastEditDate = _UseFull.C_Usefull.FarsiShamsiDate(DateTime.Now);
            string lastEditorUser = "";
            string conString = RamanConnectionString.LocalConnectionString();
            con = new SqlConnection(conString);
            con.Open();
            tr = con.BeginTransaction();
            SqlCommand cmd = con.CreateCommand();
            cmd.Transaction = tr;
            if (Roles.IsUserInRole("manager"))
            {
                lastEditorUser = _UseFull.C_Usefull.FindStringWithStringShart(con, cmd, "t_Managers", "fullName", "userName", User.Identity.Name);
            }
            else if (Roles.IsUserInRole("admin"))
            {
                lastEditorUser = _UseFull.C_Usefull.FindStringWithStringShart(con, cmd, "t_Admins", "fullName", "userName", User.Identity.Name);
            }
            cmd.CommandText = t_NewsUpdateCommand(newsId, newsDate, subject, context, newsPicUrl, lastEditDate, lastEditorUser, showInList);
            cmd.ExecuteNonQuery();
            tr.Commit();
            lblMessage.ForeColor = System.Drawing.Color.Green;
            lblMessage.Text = "با موفقیت ذخیره شد.";
        }
        catch(Exception exc)
        {
            try
            {
                tr.Rollback();
                lblMessage.ForeColor = System.Drawing.Color.Red;
                lblMessage.Text = exc.Message;
            }
            catch
            {
            }
        }
        finally
        {
            if (con.State != ConnectionState.Closed)
                con.Close();
        }
    }

    private string t_NewsUpdateCommand(int NewsId, string NewsDate, string Subject, string Context, string NewsPicUrl, string LastEditDate, string LastEditorUser, bool ShowInList)
    {
        string comm = "update t_News SET " +
            "newsDate = N'" + NewsDate + "' , " +
            "subject = N'" + Subject + "' , " +
            "context = N'" + Context + "' , " +
            "newsPicUrl = N'" + NewsPicUrl + "' , " +
            "lastEditDate = N'" + LastEditDate + "' , " +
            "lastEditorUser = N'" + LastEditorUser + "' , " +
            "showInList = " + ( ShowInList ? 1 : 0)  + " " +
            "WHERE newsId = " + NewsId;
        return comm;
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        try
        {
            string returnUrl = Request.QueryString["returnUrl"].ToString();
            Response.Redirect(returnUrl);
        }
        catch
        {
        }
    }
}
