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;

namespace JuSNS.Web.system.movie
{
    public partial class edit : JuSNS.UI.Page.ManagePage
    {
        protected string sort = "movie";
        JuSNS.Home.Movie movie = new JuSNS.Home.Movie();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                getInfo();
        }

        /// <summary>
        /// 加载页面初始信息
        /// </summary>
        protected void getInfo()
        {
            string id = Request.QueryString["ID"];
            if (string.IsNullOrEmpty(id))
                PageError("参数传递错误!", "index.aspx");

            JuSNS.Model.MovieInfo movieinfo = movie.GetMovieInfo(Convert.ToInt32(id));
            HidID.Value = id;

            for (int i = DateTime.Now.Year; i >= 1950; i--)
            {
                ListItem itm = new ListItem();
                itm.Value = i.ToString();
                itm.Text = i.ToString();
                if (i == movieinfo.YearOnShow)
                    itm.Selected = true;
                YearOnShow.Items.Add(itm);
            }

            DataTable dt = movie.getDictMovieSortList();

            if (dt != null && dt.Rows.Count > 0)
            {
                this.MovieSort.DataSource = dt;
                this.MovieSort.DataTextField = "SortName";
                this.MovieSort.DataValueField = "ID";
                this.MovieSort.DataBind();
                this.MovieSort.SelectedIndex = 0;
                getSelect(MovieSort, movieinfo.SortID.ToString());
            }
            dt.Clear(); dt.Dispose();

            Title.Text = movieinfo.Title;
            Director.Text = movieinfo.Director;
            Billing.Text = movieinfo.Billing;
            Gut.Value = movieinfo.Gut;
            Img.Src = "../../"+movieinfo.PosterFile;
            haibaoimg.Value = movieinfo.PosterFile;
            zt_dy.Value = movieinfo.zt_dy;
            zt_dy1.Value = movieinfo.zt_dy1;
            zt_url.Text = movieinfo.zt_url;
            zt_url1.Text = movieinfo.zt_url1;
            hits.Text = movieinfo.hits.ToString();
        }

        public void getSelect(DropDownList select, string str)
        {
            for (int i = 0; i < select.Items.Count; i++)
            {
                if (str == select.Items[i].Value)
                {
                    select.SelectedValue = select.Items[i].Value;
                    break;
                }
            }
        }


        /// <summary>
        /// 执行修改电影过程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            JuSNS.Model.MovieInfo movieinfo = new JuSNS.Model.MovieInfo();
            string s_moveSort = Request.Form["MovieSort"];

            string s_savepath = "uploads/Movie";
            string Picture = haibaoimg.Value;
            string filename = string.Empty;
            if (this.supfile.HasFile)
            {
                JuSNS.Common.Input.UploadFile(this.supfile.PostedFile, s_savepath, out filename);
                Picture = filename;
                movieinfo.PosterFile = s_savepath + "/" + Picture;
            }
            else
            {
                movieinfo.PosterFile = Picture;
            }
            movieinfo.MovieID = Convert.ToInt32(HidID.Value);
            movieinfo.Title = Title.Text;
            movieinfo.Director = Director.Text;
            movieinfo.Billing = Billing.Text;
            movieinfo.YearOnShow = Convert.ToInt32(Request.Form["YearOnShow"]);
            movieinfo.Gut = Gut.Value;
            movieinfo.Comments = 0;
            movieinfo.Seen = 0;
            movieinfo.Wanna = 0;
            movieinfo.Wonna = 0;
            movieinfo.Score = 0;
            movieinfo.zt_dy = zt_dy.Value;
            movieinfo.zt_dy1 = zt_dy1.Value;
            movieinfo.zt_url = zt_url.Text;
            movieinfo.zt_url1 = zt_url1.Text;
            movieinfo.hits = Convert.ToInt32(this.hits.Text);
            movieinfo.SortID = Convert.ToInt32(this.MovieSort.SelectedValue);
            movieinfo.CreatTime = DateTime.Now;
            int n = movie.EditMovie(movieinfo);
            if (n > 0)
            {
                Response.Redirect("index.aspx");
            }
            else
            {
                PageError("添加失败!", "edit.aspx?ID=" + HidID.Value);
            }
        }
    }
}
