using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//需引入的命名空间
using DotNet.FrameWork.Data;
using System.Data;
using System.Data.SqlClient;
using DotNet.FrameWork.Utils;
using DotNet.FrameWork.Common.Web;
using DotNet.FrameWork.Page;
using System.IO;
using GetCommand;

namespace HteamDd.Web.Admin
{
    public partial class Admin_picture_recycle :BaseAdminUI
    {
        HaTeam.Dd.Model.Dd_Picture model = new HaTeam.Dd.Model.Dd_Picture();
        HaTeam.Dd.BLL.Dd_Picture bll = new HaTeam.Dd.BLL.Dd_Picture();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Fetch.Get("action") != null && Fetch.Get("picture_id") != null)
                {
                    if (Fetch.Get("action").ToString() == "renew")
                    {
                        model = bll.GetModel(int.Parse(Fetch.Get("picture_id")));
                        model.Picture_Delete = 0;
                        Session["pic_address"] = model.Picture_Address;
                        bll.Update(model);
                    }

                }
                Session["selectvalue"] = this.DropSelect.SelectedValue;
                DataBindRepeaterList();

            }
        }
        public void DataBindRepeaterList()
        {
            DropSelect.SelectedValue = Convert.ToString(Session["selectvalue"]);
            this.txtSearch.Value = Convert.ToString(Session["search"]);
            string str = "picture_delete = 1";
            if (txtSearch.Value != "")
            {
                if (DropSelect.SelectedValue == "user_id")
                {
                    str += "and user_id like'%" + GetIDByName() + "%'";
                }
                else
                {
                    str += " and " + DropSelect.SelectedValue + " like '%" + txtSearch.Value + "%'";
                }

            }
            if (selectstart.Value != "" && selectend.Value != "")
            {
                if (Convert.ToDateTime(selectstart.Value) == Convert.ToDateTime(selectend.Value))
                {
                    str += "and CONVERT(varchar(30),picture_datetime,120) like '%" + selectstart.Value.ToString() + "%'";
                }
                else
                {
                    str += "and picture_datetime between'" + selectstart.Value.ToString() + "'and'" + selectend.Value.ToString() + "'";

                }
            }
            int pagesize = pagelist.PageSize;
            int pagecurrentindex = pagelist.CurrentPageIndex;
            pagelist.RecordCount = int.Parse(PagerProc.GetListPage("dd_picture", "picture_id", pagesize, pagecurrentindex, 1, 1, str).Tables[0].Rows[0][0].ToString());
            DataSet ds = PagerProc.GetListPage("dd_picture", "picture_id", pagesize, pagecurrentindex, 0, 1, str);
            this.rplist.DataSource = ds;
            this.rplist.DataBind();
        }
        private void Operate()
        {
            string names = Fetch.Post("primarykey");
            if (names != string.Empty)
            {
                names = names.Replace("'", "");
                names = "'" + names.Replace(",", "','") + "'";

                if (this.renew.Checked)
                {
                    DbHelperSQL.ExecuteSql("update dd_picture set picture_delete =0 where picture_delete=1 and picture_id in (" + names + ")");
                }

                if (this.delete.Checked)
                {
                    //string str = "select picture_address from Dd_picture where picture_id in (" + names + ")";
                    //SqlDataReader dr = DbHelperSQL.ExecuteReader(str);
                    //while (dr.Read())
                    //{
                    //    string url = dr["picture_address"].ToString();
                    //    string[] address = url.Split('|');
                    //    for (int i = 0; i < address.Length - 1; i++)
                    //    {
                    //        string filepath = Server.MapPath("/") + address[0] +"/"+ address[i+1];
                    //        if (File.Exists(filepath))
                    //        {
                    //            File.Delete(filepath);
                    //        }
                    //    }
                    //}
                    string url = Session["pic_address"].ToString();
                    string  [] image=url.Split('|');
                    string[] path = image[0].Split('/');
                    for (int i = 0; i < image.Length-2; i++)
                    {
                        DeleImage.image_server_del(image[0], image[i+1]);
                        DeleImage.image_server_del("/" + path[1] + "/UploadPictures/" + path[3] + "/" + path[4], image[i + 1]);//删除服务器上的原图
                    }                  
                    DbHelperSQL.ExecuteSql("delete from dd_picture where picture_id in (" + names + ")");
                }
                Response.Redirect(Fetch.CurrentUrl.ToString());
            }

        }
        /// <summary>
        /// 根据名字获得ID
        /// </summary>
        /// <returns></returns>
        public string GetIDByName()
        {
            string str = "select user_id from dd_user where user_blogname like'%" + this.txtSearch.Value + "%'";
            string idna = Convert.ToString(DbHelperSQL.GetSingle(str)).ToString();
            return idna;
        }
        /// <summary>
        /// 根据ID获得名字
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetNameByID(string id)
        {
            if (id != "")
            {
                string str = "select user_blogname from dd_user where user_id =" + int.Parse(id);
                string name = Convert.ToString(DbHelperSQL.GetSingle(str));
                return name;
            }
            else
            {
                return "";
            }
        }
        /// <summary>
        /// 获得字符串长度
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public string Substring(string content)
        {
            if (content.Length > 15)
            {
                return content.Substring(0, 15) + "...";
            }
            else
                return content;
        }    
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Session["search"] = this.txtSearch.Value;
            DataBindRepeaterList();
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            Operate();
        }

        protected void DropSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["selectvalue"] = this.DropSelect.SelectedValue;
        }

        protected void pagelist_PageChanged(object sender, EventArgs e)
        {
            DataBindRepeaterList();
        }
    }
}