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


namespace CheckSheet.setting
{
    public partial class Upload_linegroup_CSV : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected string UploadFolderPath = "~/Uploads/";
     
        private void _fSort(string _file)
        {


            ArrayList arr = new ArrayList();
            ArrayList arrcpn = new ArrayList();
            string sError = string.Empty;

            string FullPath = _file;
            string MyPath = System.IO.Path.GetDirectoryName(FullPath);
            string myFilename = System.IO.Path.GetFileName(FullPath);


            string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;" +
              "Data Source=" + FullPath +
              ";Extended Properties='Excel 8.0;'";
            string CmdText = "select * from [Sheet1$]";





            OleDbConnection Con = new OleDbConnection(ConnStr);
            OleDbDataAdapter adptr = new OleDbDataAdapter(CmdText, Con);
            DataTable dt = new DataTable();
            adptr.Fill(dt);
            ArrayList ardate = new ArrayList();
            string _date = string.Empty;
            string cmdhd = string.Empty;

            using (FcSql.Function fc = new FcSql.Function())
            {
            foreach (DataRow dr in dt.Rows)
            {

              
                    try
                    {

                        string linegroupid = string.Empty;
                       
                        string lamp = dr[0].ToString();
                        if (lamp.Length > 0)
                        {
                            string linegroupcode = dr[1].ToString();
                            string linename = dr[2].ToString();
                            string model = dr[3].ToString();
                            string partnumber = dr[4].ToString();
                            string lampid = string.Empty;


                            string userid = Request.Cookies["userid"].Value;


                            string chkid = string.Format("select linegroupid from LineGroupMaster Where linegroupcode='{0}' and model='{1}' and lamp='{2}' ", linegroupcode, model, lamp);
                            DataTable dtchkid = fc.SQL_DataTable(chkid.ToString());
                            if (dtchkid.Rows.Count > 0)
                            {
                                linegroupid = dtchkid.Rows[0]["linegroupid"].ToString();
                                
                            }
                      
                            chkid = string.Format("select lampid from LineGroupMaster Where lamp='{0}' ", lamp);
                            dtchkid = fc.SQL_DataTable(chkid.ToString());
                            if (dtchkid.Rows.Count > 0)
                            {
                               
                                lampid = dtchkid.Rows[0]["lampid"].ToString();
                            }
                            if (lampid.Length == 0)
                            {
                               lampid = clsControlUI.create_master_id("LineGroupMaster", "lampid");



                            }


                            if (linegroupid.Length == 0)
                            {

                                linegroupid = clsControlUI.create_master_id("LineGroupMaster", "linegroupid");


                            }



                            string crdate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", new CultureInfo("en-US"));

                            string inst_mt = string.Format(" insert into LineGroupMaster (linegroupid,linegroupcode, linename, flagdel, create_user, create_datetime,partnumber,model,lamp,lampid) VALUES ('" + linegroupid + "','" + linegroupcode + "','" + linename + "',0,'" + userid + "','" + crdate + "','" + partnumber + "','" + model + "','" + lamp + "','"+lampid+"')");
                            string up_mt = string.Format(" update LineGroupMaster SET linename= '" + linename + "',update_user='" + userid + "', update_datetime= '" + crdate + "',partnumber='" + partnumber + "',model='" + model + "',linegroupcode='" + linegroupcode + "',lamp='" + lamp + "',lampid='"+lampid+"'  Where linegroupid= '" + linegroupid + "' ");

                            cmdhd = fc.ExecSQLStatement(inst_mt.ToString());
                            if (cmdhd.ToLower().IndexOf("duplicate") != -1)
                            {
                                cmdhd = fc.ExecSQLStatement(up_mt.ToString());
                            }

                            fc.closeconnection();
                        }

                    }
                    catch { };

                }



            }


        }

        protected void AsyncFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
        {
            string filename = "linegroup_" + DateTime.Now.ToString("yyyyMMddHHmmss", new CultureInfo("en-US"));
            AsyncFileUpload2.SaveAs(Server.MapPath(this.UploadFolderPath) + filename);

            string path = this.Server.MapPath(@"Uploads").Replace("\\setting","");

            _fSort(path + "\\" + filename);
        }
    }
}