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 JuSNS.Model;
using JuSNS.UI.Control;


namespace JuSNS.Web.system.movie
{
    public partial class pic :JuSNS.UI.Page.ManagePage
    {
        protected string sort = "movie";
        JuSNS.Home.Movie movie = new JuSNS.Home.Movie();
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.CacheControl = "no-cache";
            Operate();
            LoadList();
        }

        /// <summary>
        /// 加载分页
        /// </summary>
        /// <param name="PageIndex">当前页数</param>
        protected void LoadList()
        {
            int r, p, PageIndex = 0;
            PageIndex = Convert.ToInt32(Request.QueryString["Page"]);

            MovieInfo movieinfo = movie.GetMovieInfo(Convert.ToInt32(HidMID.Value));
            Lit_MovieName.Text = movieinfo.Title;

            SqlConditionInfo st = new SqlConditionInfo("@MovieID", Convert.ToInt32(HidMID.Value), TypeCode.Int32);

            DataTable dt = JuSNS.Home.UtilPage.GetPage("moviepiclist_aspx", PageIndex, 32, out r, out p, st);
            if (dt != null)
            {
                Rpt_MoviePicList.DataSource = dt;
                Rpt_MoviePicList.DataBind();
            }
            //判断是否调用分页
            if (r > 32)
            {
                DivPage.InnerHtml = PageNavigator.Pagination(PageIndex, p, "pic.aspx?movieid=" + HidMID.Value);
            }
            if (r == 0)
                DivPage.InnerHtml = "暂无电影海报";
        }

        protected string GetUserNames(object userid)
        {
            if (userid.ToString()=="0")
            {
                return "管理员";
            }
            UserInfo mdl = new UserInfo();
            JuSNS.Home.User us = new JuSNS.Home.User();
            mdl = us.GetUserInfo(Convert.ToInt32(userid.ToString()));
            return mdl.Username;
           
        }

        protected string GetLock(object islock,object id)
        {
            if (islock.ToString() == "1")
            {
                return "<a href=\"javascript:lock('"+id.ToString()+"',0)\" title=\"点击审核\"><span style=\"color:Red\">未审核</a></span></a>";
            }
            else
            {
                return "<a href=\"javascript:lock('" + id.ToString() + "',1)\" title=\"点击改为审核状态\">已审核</a>";
            }
        }

        /// <summary>
        /// 操作
        /// </summary>
        protected void Operate()
        {
            string movieid = Request.QueryString["movieid"];
            if (string.IsNullOrEmpty(movieid))
                PageError("参数传递错误!", "index.aspx");
            HidMID.Value = movieid;

            string s_Act = Request.QueryString["action"];
            if (!string.IsNullOrEmpty(s_Act))
            {
                int n = 0;
                string id = Request.QueryString["id"];
                if (string.IsNullOrEmpty(id))
                    PageError("参数传递错误!", "index.aspx");

                switch (s_Act)
                {
                    case "setpic":
                        MoviePosterInfo posterinfo = movie.getMoviePosterInfo(Convert.ToInt32(id));
                        n = movie.SetPosterFile(Convert.ToInt32(HidMID.Value), posterinfo.FileName);
                        if (n == 1)
                            Response.Redirect("pic.aspx?movieid=" + HidMID.Value);
                        else
                            PageError("设置电影海报失败!", "index.aspx");
                        break;
                    case "del":
                        n = movie.DelMoviePoster(Convert.ToInt32(id));
                        if (n == 1)
                            Response.Redirect("pic.aspx?movieid=" + HidMID.Value);
                        else
                            PageError("删除电影海报失败!", "pic.aspx?movieid=" + HidMID.Value);
                        break;
                    case "lock":
                        n = movie.LockMovie(Convert.ToInt32(id), Convert.ToInt32(Request.QueryString["type"]));
                        if (n == 1)
                            Response.Redirect("pic.aspx?movieid=" + HidMID.Value);
                        else
                            PageError("审核电影海报失败!", "pic.aspx?movieid=" + HidMID.Value);
                        break;
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                string Picture = string.Empty;
                string filename = string.Empty;
                string s_savepath = "uploads/movie";
                if (this.supfile.HasFile)
                {
                    JuSNS.Common.Input.UploadFile(this.supfile.PostedFile, s_savepath, out filename);
                    Picture = filename;
                }
                else
                {
                    PageError("请选择海报", "pic.aspx?movieid=" + HidMID.Value);
                }
                MoviePosterInfo mp = new MoviePosterInfo();
                mp.MovieID = Convert.ToInt32(this.HidMID.Value);
                mp.PostIP = JuSNS.Common.Public.GetClientIP();
                mp.PostTime = DateTime.Now;
                mp.UserID = 0;
                mp.FileName = s_savepath + "/" + filename;
                if (movie.AddMoviePoster(mp) > 0)
                {
                    Response.Redirect("pic.aspx?movieid=" + HidMID.Value);
                }
            }
        }
    }
}
