using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Globalization;
using System.Data;

namespace CheckSheet
{
    public partial class saveimage1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {

                string equid = string.Empty;
                string inspection = string.Empty;
                string inspectitem=string.Empty;
                string type = string.Empty;

                
                string imgbase64 = Page.Request["image"].ToString().Replace(" ", "+").Replace("sys*", "=");
               
                if(imgbase64.IndexOf("*")!=-1)
                {

                    string[] arr = imgbase64.Split('*');
                    imgbase64 = arr[0];
                    equid = arr[1];
                    inspection = arr[2];
                    type = arr[3];

                    using (FcSql.Function fc = new FcSql.Function())
                    {
                        string str_insp = string.Format("select itemname from InspectionItem Where inspectid='{0}' and equid='{1}'", inspection, equid);
                        DataTable dt = fc.SQL_DataTable(str_insp);
                        if(dt.Rows.Count>0)
                        {
                            inspectitem = dt.Rows[0]["itemname"].ToString();
                        }
                        fc.closeconnection();
                    }



                }
                string UploadFolderPath = "~/piz/" + type + "/";
                string filename = equid + "_" + inspection + "_" + inspectitem + "_" + DateTime.Now.ToString("yyyyMMdd_HHmmss", new CultureInfo("en-US"));
                string path = Server.MapPath(UploadFolderPath) + filename + ".jpg";


                byte[] imageByteArray = Convert.FromBase64String(imgbase64);
                File.WriteAllBytes(path, imageByteArray);


                Response.Cookies["pizinfo"].Value = "../piz/" + type + "/" + filename + ".jpg";




            }

        }
    }
}