using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;

public partial class Admin_Magiclantern_MagiclanternForm : System.Web.UI.Page
{
    //获得上传文件夹
    string UploadFileName = ConfigurationManager.AppSettings["UploadName"].ToString().Trim();
    public static string fileRoot;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            fileRoot = Server.MapPath(UploadFileName);
            disableDownload();
            if (Request.QueryString["uid"] != "" && Request.QueryString["uid"] != null)
            {
                DataTable table = new DataTable();
                string uid=Request.QueryString["uid"];
                string sql = "select * from A_Magiclantern where id="+uid+" ";
                if (ISDatabase.SelectDatebase() == 1)
                    table = DbHelp.DataTableSql(sql);
                else
                    table = DbHelperOleDb.DataTableSql(sql);

                if (table.Rows.Count > 0)
                {
                    this.id.Text = uid;
                    this.Magiclanname.Text = table.Rows[0]["MagicName"].ToString();
                    this.MaiclanUrl.Text = table.Rows[0]["MagicUrl"].ToString();
                    this.ImageID.Text = table.Rows[0]["MagicImage"].ToString();
                }

            }

        }
    }


    protected void btnSubmit_Click(object sender, EventArgs e)
    {

        if (this.Magiclanname.Text.Length < 1)
        {
            string sb = "<script>$(function () { $.ligerDialog.alert(\"请输入幻灯名称\", \"提示\",\"success\",function (result) { window.location.href ='Magiclanternlist.aspx'; }); });</script>";
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "Messager", sb.ToString());
            return;
        }
        if (this.MaiclanUrl.Text.Length < 1)
        {
            string sb = "<script>$(function () { $.ligerDialog.alert(\"请输入连接地址？\", \"提示\",\"success\",function (result) { window.location.href ='Magiclanternlist.aspx'; }); });</script>";
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "Messager", sb.ToString());
            return;
        }
        string Magiclanname = this.Magiclanname.Text;
        string MaiclanUrl = this.MaiclanUrl.Text;
        string MagicImage = this.ImageID.Text;
        string AddTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        int result; 
        string sql;
        if (this.id.Text != "" && this.id.Text != null)
        {    
           string uid=this.id.Text;
           sql = "update A_Magiclantern set MagicName='" + Magiclanname + "',MagicUrl='" + MaiclanUrl + "',MagicImage='" + MagicImage + "' where id=" + uid + "";
        }
        else 
        {
            sql = "insert into A_Magiclantern(MagicName,MagicUrl,MagicImage,AddTime) values('" + Magiclanname + "','" + MaiclanUrl + "','" + MagicImage + "','" + AddTime + "') ";
        }
        if (ISDatabase.SelectDatebase() == 1)
            result = DbHelp.ExecuteSql(sql);
        else
            result = DbHelperOleDb.ExecuteSql(sql);

        if (result > 0)
        {
            string sb = "<script>$(function () { $.ligerDialog.alert(\"幻灯图片添加成功\", \"提示\",\"success\",function (result) { window.location.href ='Magiclanternlist.aspx'; }); });</script>";
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "Messager", sb.ToString());
        }
        else
        {
            string sb = "<script>$(function () { $.ligerDialog.alert(\"添加胡幻灯图片失败？\", \"提示\",\"success\",function (result) { window.location.href ='Magiclanternlist.aspx'; }); });</script>";
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "Messager", sb.ToString());
        }

    }


    protected void btn_upload_Click(object sender, EventArgs e)
    {

        string PicName = DateTime.Now.ToString("yyyyMMddHHmmss");
        Random rnd = new Random();
        int rndNum = rnd.Next(10000000, 99999999);
        PicName = PicName + rndNum.ToString();

        try
        {
            //上传文件并指定上传目录的路径  

            string fileName = System.IO.Path.GetFileName(FileUpLoad1.PostedFile.FileName);
            string fileFix = fileName.Remove(0, fileName.LastIndexOf(".") + 1).ToLower();
            //保存的文件名
            fileName = PicName + "." + fileFix;
            string fullName = fileRoot + "\\" + fileName;
            FileUpLoad1.PostedFile.SaveAs(fullName);
            //lblMessage.Text = "上传成功!";
            string sb = "<script>$(function () { $.ligerDialog.alert(\"图片上传成功!\", \"提示\"); });</script>";
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "Messager", sb.ToString());
            this.showimg.InnerHtml = "<img src=\"" + UploadFileName + fileName + "\" alt=\"点击查看大图\" width=\"50\" height=\"40\"/>";

            //文件名
            this.ImageID.Text = fileName;
            enableDownload();    //允许下载删除

        }
        catch (Exception ex)
        {
            string sb = "<script>$(function () { $.ligerDialog.alert(\"图片上传未成功，请检查路径。\", \"提示\",\"success\",function (result) { window.location.href ='Magiclanternlist.aspx'; }); });</script>";
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "Messager", sb.ToString());
            return;
        }
    }


    //允许下载和删除按钮
    private void enableDownload()
    {
        this.uploadfile.Visible = false;
        this.uploadlist.Visible = true;
        this.btn_download.Enabled = true;
        this.btn_delete.Enabled = true;
    }
    //禁用下载和删除按钮
    private void disableDownload()
    {
        this.uploadlist.Visible = false;
        this.btn_download.Enabled = false;
        this.btn_delete.Enabled = false;
        this.uploadfile.Visible = true;
    }



    protected void btn_download_Click(object sender, EventArgs e)
    {

    }
    protected void btn_delete_Click(object sender, EventArgs e)
    {

    }
}