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 TEC_Forms_ProblemResponse : 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) {
        hlFABID.Attributes.Add("onclick", "openSubPageFABID('" + txtFABID.ClientID + "')");
        hlFABID.Attributes.Add("onmouseover", "this.style.cursor='hand'");

        hlToolID.Attributes.Add("onclick", "openSubPageToolID('" + txtSESToolNo.ClientID + "', " + txtFABID.ClientID + ",'False' )");
        hlToolID.Attributes.Add("onmouseover", "this.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 sProblemSource = "", sProposer = "", sFABID = "", sToolID = "", sDescription = "", sRootCause = "";
        string sTemp = "";

        int iWorkID = 0 ;
        try {
            sProblemSource = ddlProblemSource.Text;
            sProposer = txtApplicant.Text;
            sFABID = txtFABID.Text;
            sToolID = txtSESToolNo.Text;
            sDescription = txtDescription.Text;
            sRootCause = txtRootCause.Text;

            // 儲存至 MainWorks
            if (sProblemSource != "" && sProposer != "" && sFABID != "" && sToolID != "") {
                iWorkID = new TEC_MainWorksData().Insert(null, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), sProblemSource,
                    sProposer, sFABID, sToolID, sDescription, sRootCause, "Pending", null, null, null, null);
            }
            // 儲存附加檔案
            HttpFileCollection uploadedFiles = Request.Files;
            string 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);
                        // 2.儲存路徑至資料庫 (先刪除再儲存)
                        file.Delete("MainWorks", iWorkID.ToString(), sFileName);
                        file.Insert("MainWorks", iWorkID.ToString(), sFileName, sSingleFilePath, "", "");
                    }
                }
            }

            //Response.Redirect("ProblemResponse.aspx?WorkID=" + iWorkID.ToString());
        }
        catch (Exception ex) {
            Response.Redirect("/SEST/Common/ErrorPage.aspx?BackPageCount=-1&ErrorMessage=" + ex.Message.Replace("\r\n", "=>"));
            return;
        }
    }

    protected void txtApplicant_PreRender(object sender, EventArgs e) {
        if (Profile.UserName.Length <= 15) {
            txtApplicant.Text = new HR_EmployeesData().getUserNameByAccount(Profile.UserName);
            txtApplicant.Enabled = false;
        }
    }

}
