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


namespace HteamDd.Web.Admin
{
    public partial class Admin_picture_edit :BaseAdminUI
    {
        HaTeam.Dd.BLL.Dd_Picture bll = new HaTeam.Dd.BLL.Dd_Picture();
        HaTeam.Dd.Model.Dd_Picture model = new HaTeam.Dd.Model.Dd_Picture();
         public string address;
         public string picture_id;
        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() == "edit")
                    {
                        Session["user_name"] = Fetch.Get("user_name");
                        ShowInfo(int.Parse(Request.QueryString["picture_id"].ToString()));
                    }
                }
                if (Fetch.Get("action") != null && Fetch.Get("name") != null)
                {
                    if (Fetch.Get("action").ToString() == "delete")
                    {
                        
                        string address2 = Fetch.Get("address");
                        string de = Fetch.Get("name");//获取要删除的图片的虚拟地址
                        string filepath = Server.MapPath("/")+ de;

                        string[] path = de.Split('/');
                        string UploadPictures = Server.MapPath("/") + path[1] + "/UploadPictures/" + path[3] + "/" + path[4]+"/"+path[5];
                        if (File.Exists(filepath))
                        {
                            File.Delete(filepath);//删除服务器上的缩略图片
                            File.Delete(UploadPictures);//删除服务器上的原图片
                        }
                        string [] image_del_url = address2.Split('|');
                        for (int i = 0; i < image_del_url.Length - 2; i++)
                        {
                            string image_name = Fetch.Get("delete_url");
                            if (image_del_url[i+1] == image_name)
                            {
                                image_del_url[i+1]="";
                                break;
                            }                          
                        }
                        for (int i = 0; i < image_del_url.Length-2; i++)
                        {

                            if (image_del_url[i+1] != "")
                            {
                                ViewState["pic_del_addres"] += image_del_url[i+1] + "|";
                            }
                        }
                       address = "/" + path[1] + "/"+path[2]+"/" + path[3] + "/" + path[4] + "|" + ViewState["pic_del_addres"];
                       string sql = "update dd_picture set picture_address ='" + address + "'where picture_id =" + int.Parse(Fetch.Get("picture_id"));
                       DbHelperSQL.ExecuteSql(sql);
                       ShowInfo(int.Parse(Request.QueryString["picture_id"].ToString()));
                    }
                }
             
            }
        }
        public void ShowInfo(int id)
        {
            picture_id = Fetch.Get("picture_id");
            model = bll.GetModel(id);
            txtName.Text = Session["user_name"].ToString(); 
            txtContent.Text = model.Picture_Discription;
            address = model.Picture_Address;
            txtTag.Text = model.Picture_Tag;
            rbtdelete2.Checked = true;
            if (model.Picture_Verify == 0)
            {
                rbtVerity2.Checked = true;
            }
            else { rbtVerity1.Checked = true; }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            int delete;
            int verify;
            string content = txtContent.Text;
            string tag = txtTag.Text;
            int pictureID = int.Parse(Fetch.Get("picture_id"));
            if (rbtdelete1.Checked == true)
            {
                delete = 1;
            }
            else { delete = 0; }
            if (rbtVerity2.Checked == true)
            {
                verify = 0;

            }
            else { verify = 1; }
            string str = "update dd_picture set picture_discription ='" + content + "',picture_tag ='" + tag + "',picture_delete=" + delete + ",picture_verify =" + verify + "where picture_id =" + pictureID;
            try
            {
                DbHelperSQL.ExecuteSql(str);
                Jscript.AlertAndRedirect("系统提示：修改成功！", "Admin_picture_manage.aspx");
            }
            catch (Exception ex)
            {
                Response.Write(ex);
            }

           
        }
    }
}