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;

public partial class Maintenance_Others_Announcement : System.Web.UI.Page {
    //the function code for localization on this page
    protected override void InitializeCulture() {
        ProfileCommon p = (ProfileCommon)this.Context.Profile;

        if (Request.Form.Get("ctl00$selLanguage") != null)
            p.lang = Request.Form.Get("ctl00$selLanguage").ToString();
        if (p.lang == String.Empty)
            p.lang = Session["Localization"].ToString();

        if (p.lang.Length > 0) {
            this.Culture = p.lang;
            this.UICulture = System.Globalization.CultureInfo.CreateSpecificCulture(p.lang).Name;
        }
    }
    //select theme
    protected void Page_PreInit(object sender, EventArgs e) {
        Page.Theme = Profile.UI.Theme;

        // 沒有權限
        if (!SES_Common.Authorize(Request.Url.ToString(), Profile.UserName)) {
            Response.Redirect("/SEST/Common/NoAuthorization.aspx");
        }
    }


    protected void Page_Load(object sender, EventArgs e) {
        txtWriter.Text = new HR_EmployeesData().getUserNameByAccount(Profile.UserName);
        txtStartDate.Text = DateTime.Today.ToString("yyyy/MM/dd");
        txtEndDate.Text = DateTime.Today.ToString("yyyy/MM/dd");

        // Set the attribut to open sub page 
        hlStartDate.Attributes.Add("onclick", "openSubPageCalendar('" + txtStartDate.ClientID + "')");
        hlStartDate.Attributes.Add("style", "cursor: hand");
        hlEndDate.Attributes.Add("onclick", "openSubPageCalendar('" + txtEndDate.ClientID + "')");
        hlEndDate.Attributes.Add("style", "cursor: hand");

        // 新增 file upload 的 component
        FileSaveToServer.Attributes.Add("onchange", "addFile2('" + tdMyFiles.ClientID + "'," + Convert.ToInt32(FileSaveToServer.Width.Value) + ")");

    }


    protected void btnSubmit_Click(object sender, EventArgs e) {
        //if (Session["Done@Application_TEC_OnCall"] == "") {
        string sWriter = "", sPostFrom = "", sStartDate = "", sEndDate = "", sImportance = "",
            sSubject = "", sContents = "", sFilePath = "";
        // 由 javascript 所產生的值無法直接由 aspnet 的 code取得
        if (Request[txtWriter.ClientID.Replace('_', '$')] != null)
            sWriter = Request[txtWriter.ClientID.Replace('_', '$')].ToString().Trim();
        if (Request[ddlPostFrom.ClientID.Replace('_', '$')] != null)
            sPostFrom = Request[ddlPostFrom.ClientID.Replace('_', '$')].ToString().Trim();
        if (Request[txtStartDate.ClientID.Replace('_', '$')] != null)
            sStartDate = Request[txtStartDate.ClientID.Replace('_', '$')].ToString().Trim();
        if (Request[txtEndDate.ClientID.Replace('_', '$')] != null)
            sEndDate = Request[txtEndDate.ClientID.Replace('_', '$')].ToString().Trim();
        if (Request[ddlImportance.ClientID.Replace('_', '$')] != null)
            sImportance = Request[ddlImportance.ClientID.Replace('_', '$')].ToString().Trim();
        if (Request[txtSubject.ClientID.Replace('_', '$')] != null)
            sSubject = Request[txtSubject.ClientID.Replace('_', '$')].ToString().Trim();
        if (Request[txtContents.ClientID.Replace('_', '$')] != null)
            sContents = Request[txtContents.ClientID.Replace('_', '$')].ToString().Trim();
        // 由 javascript 所產生的值無法直接由 aspnet 的 code取得
        //sWriter = txtWriter.Text.Trim();
        //sPostFrom = ddlPostFrom.Text.Trim();
        //sStartDate = txtStartDate.Text.Trim();
        //sEndDate = txtEndDate.Text.Trim();
        //sImportance = ddlImportance.Text.Trim();
        //sSubject = txtSubject.Text.Trim();
        //sContents = txtContents.Text.Trim();


        if (sFilePath != "")
            sFilePath = sFilePath.Substring(1);


        // insert
        try {

            // insert to database
            string sAnnouncementID = (new Common_AnnouncementData().Insert(sWriter, sPostFrom, sStartDate, sEndDate, sImportance, sSubject, sContents, sFilePath)).ToString();
            
            // 處理檔案之儲存
            // 1.儲存檔案至 SERVER, 2.儲存路徑至資料庫 ( <<<<< 實際儲存位置 >>>>> )
            HttpFileCollection uploadedFiles = Request.Files;
            string sTemp = "", sSingleFilePath = "", sFileName = "";
            Common_FilesStorageData file = new Common_FilesStorageData();
            for (int i = 0; i < uploadedFiles.Count; i++) {
                HttpPostedFile userPostedFile = uploadedFiles[i];
                if (userPostedFile.ContentLength > 0) {
                    sTemp = Root.Text.pcString.AddBackSlash(System.Configuration.ConfigurationManager.ConnectionStrings["FilesStoragePath"].ConnectionString);
                    sTemp = Root.Storage.pcDirFile.CreateFolderByDate(sTemp, System.DateTime.Today, Root.Storage.pcDirFile.CreateFolderType.Cascade, Root.Storage.pcDirFile.CreateDateBase.Day, false);
                    // file name
                    sFileName = System.IO.Path.GetFileName(userPostedFile.FileName);
                    sSingleFilePath = System.IO.Path.Combine(sTemp + sFileName);
                    if (sSingleFilePath != "") {
                        // 1.儲存檔案至 SERVER (同檔名會覆蓋)
                        userPostedFile.SaveAs(sSingleFilePath);//filepath + "\\" + System.IO.Path.GetFileName(userPostedFile.FileName));
                        // 2.儲存路徑至資料庫 (先刪除再儲存)
                        file.Delete("Announcement", sAnnouncementID, sFileName);
                        file.Insert("Announcement", sAnnouncementID, sFileName, sSingleFilePath, "", "");
                    }
                }
            }

            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Back", SES_Common.BackScript(Request.Url.ToString(), -1));
        }
        catch (Exception ex) {
            Response.Redirect("/SEST/Common/ErrorPage.aspx?BackPageCount=-1&ErrorMessage=" + ex.Message.Replace("\r\n", "=>"));
        }
    }
    protected void btnReset_Click(object sender, EventArgs e) {
        
    }
}
