using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net; 

public partial class AddVoteUC : System.Web.UI.UserControl
{
    protected void voteAdd()
    {
        try
        {
            Label4.Visible = false;
            LSC_KhateratDataContext db = new LSC_KhateratDataContext();
            tblVote Addv = new tblVote();
            FirstClass sb = new FirstClass();

            Addv.VoterNameSname = TextBox1.Text.Trim();
            Addv.VoterEmail = TextBox2.Text.Trim();
            Addv.voterWebSite = TextBox4.Text.Trim();
            Addv.VoteBody = TextBox3.Text.Trim();
            Addv.DateOfVoting = sb.dtShamsi();
            Addv.newsID = int.Parse(Request.QueryString["NewsID"].ToString());


            string text1 = Dns.GetHostName(), text2;
            text2 = Dns.GetHostByName(text1).AddressList[0].ToString();
            Addv.SenderIP = DateTime.Now.Ticks.ToString();


            if (Session["UserName"] != null)
            {
                if (int.Parse(Session["UserTypeID"].ToString()) == 1)
                {
                    Addv.allowShowBit = Boolean.Parse("true");
                   
                }
                else
                {
                    Addv.allowShowBit = Boolean.Parse("false");
                }
            }
            else
            {
                Addv.allowShowBit = Boolean.Parse("false");
            }

            db.tblVotes.InsertOnSubmit(Addv);
            db.SubmitChanges();
            texClear();
            grdFill();
            Label4.Visible = true;
            Label4.Text = "نظر شما درج شد و پس از تائید نمایش داده خواهد شد";
            
        }
        catch
        {
            Label4.Visible = true;
            Label4.Text = "شما قبلا یک بار نظر داده اید";
        }
    }

    protected void grdFill()
    {
        FirstClass db = new FirstClass();
        DataTable dt = new DataTable();

        dt = db.dbOut(@"SELECT TOP 100 PERCENT VoteID, newsID, VoterNameSname, VoteBody, DateOfVoting, voterWebSite, allowShowBit FROM tblVotes WHERE (allowShowBit = 1) AND (newsID = '" + int.Parse(Request.QueryString["NewsID"].ToString()) + "')"); 

        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    
    protected void texClear()
    {
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
        TextBox4.Text = "";
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        grdFill();
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        texClear();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        voteAdd();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        grdFill();
    }

    protected void TextBox2_TextChanged(object sender, EventArgs e)
    {

    }
}
