using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Web.UI;
namespace FineUIPro.Web.Controls
{
    public partial class DataImportControl : UserControl
    {
        public delegate void userEvent(object sender, EventArgs arg); //定义事件委托
        public event userEvent Audit_Click; //定义审核事件
        public event userEvent Import_Click; //定义导入事件
        /// 
        /// 模板路径
        /// 
        public string TemplatePath
        {
            get
            {
                return (string)ViewState["Template"];
            }
            set
            {
                ViewState["Template"] = value;
            }
        }
        /// 
        /// 上传文件Url
        /// 
        public string UpLoadAttachUrl
        {
            get
            {
                return (string)ViewState["UpLoadAttachUrl"];
            }
            set
            {
                ViewState["UpLoadAttachUrl"] = value;
            }
        }
        //错误集合
        public List ErrorInfoList
        {
            get
            {
                return (List)Session["ErrorInfoList"];
            }
            set
            {
                Session["ErrorInfoList"] = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.hdCheckResult.Text = string.Empty;
                this.hdFileName.Text = string.Empty;
                if (ErrorInfoList != null)
                {
                    ErrorInfoList.Clear();
                }
            }
        }
        //方法用于绑定Grid
        public void BindGrid()
        {
            this.gvErrorInfo.DataSource = ErrorInfoList;
            this.gvErrorInfo.DataBind();
        }
        protected void btnAudit_Click(object sender, EventArgs e)
        {
            if (this.Audit_Click != null)
            {
                if (this.fuAttachUrl.HasFile == false)
                {
                    String.Format("showControlInfo('{0}');", "请您选择Excel文件"); 
                    return;
                }
                string IsXls = Path.GetExtension(this.fuAttachUrl.FileName).ToString().Trim().ToLower();
                if (IsXls != ".xls")
                {
                    String.Format("showControlInfo('{0}');", "只可以选择Excel文件!");
                    return;
                }
                if (ErrorInfoList != null)
                {
                    ErrorInfoList.Clear();
                }
                string rootPath = Server.MapPath("~/");
                string initFullPath = rootPath + Const.ExcelUrl;
                if (!Directory.Exists(initFullPath))
                {
                    Directory.CreateDirectory(initFullPath);
                }
                this.hdFileName.Text = BLL.Funs.GetNewFileName() + IsXls;
                string filePath = initFullPath + this.hdFileName.Text;
                this.fuAttachUrl.PostedFile.SaveAs(filePath);
                this.Audit_Click(this, e);
            }
        }
        protected void btnImport_Click(object sender, EventArgs e)
        {
            if (this.Import_Click != null)
            {
                this.Import_Click(this, e);
            }
        }
        #region 下载模板
        /// 
        /// 下载模板按钮
        /// 
        /// 
        /// 
        protected void btnDownLoad_Click(object sender, EventArgs e)
        {
            string rootPath = Server.MapPath("~/");
            string uploadfilepath = rootPath + TemplatePath;
            string filePath = TemplatePath;
            string fileName = Path.GetFileName(filePath);
            FileInfo info = new FileInfo(uploadfilepath);
            long fileSize = info.Length;
            Response.ClearContent();
            Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
            Response.ContentType = "excel/plain";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.AddHeader("Content-Length", fileSize.ToString().Trim());
            Response.TransmitFile(uploadfilepath, 0, fileSize);
            Response.End();
        } 
        #endregion
    }
}