using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using System.Data.OleDb;
using System.Data.SqlClient;
using System.IO;

namespace norke.admin
{
	/// <summary>
	/// productImport 的摘要说明。
	/// </summary>
	public partial class productImport : System.Web.UI.Page
	{
    
		protected void Page_Load(object sender, System.EventArgs e)
		{
            //权限检查
            if( Session["adminName"]==null || Session["adminName"].ToString() == String.Empty )
            {
                Response.Write("<font color=#ff0000 style='FONT-SIZE: 12px'>对不起,您没足够权限访问此页!!</font><br>");
                Response.Write("<a href=Default.aspx target=_top style='FONT-SIZE: 12px'>重新登陆</a><br>");
                Response.End();
                return;
            }

            if( !IsPostBack )
            {
                if( Request.QueryString["id"] == null)
                {
                    Response.Write("没有这个产品");
                    Response.End();
                }
                string strID = Request.QueryString["id"].ToString().Trim();
                if( strID == String.Empty )
                {
                    strID = "-1";
                }

                //获取ID对应的产品名称
                DBConn myDB1 = new DBConn();
                string sqlP="select PName from Products where PID=" + strID;
                SqlDataReader dr  = myDB1.getDataReader(sqlP);
            
                if( dr.Read() )
                {
                    lblName.Text = dr["PName"].ToString();
                }
                else
                {
                    Response.Write("没有这个产品");
                    dr.Close();
                    myDB1.Close();
                    Response.End();
                }
                dr.Close();
                myDB1.Close();

                ViewState["PID"] = strID;
            }
		}

		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    

        }
		#endregion

        protected void btnLook_Click(object sender, System.EventArgs e)
        {
            string source = file1.Value;
            if( source.Trim() == String.Empty || Path.GetExtension(file1.PostedFile.FileName)!=".xls" )
            {
                Response.Write("<script>");
                Response.Write("alert('请选择要 查看 的文件!!!');");
                Response.Write("</script>");
                return;
            }
            string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + 
                                source + ";Extended Properties=Excel 8.0";
            string query = "Select * From [Sheet1$]";

            OleDbConnection conn = new OleDbConnection(ConnStr);
            OleDbDataAdapter ad = new OleDbDataAdapter(query,conn);
            DataSet ds = new DataSet();
            ad.Fill(ds,"Book1");
            DataGrid1.DataSource = ds.Tables["Book1"].DefaultView;
            DataGrid1.DataBind();
            ad.Dispose();
            conn.Close();
            conn=null;

            //-- 查看记录总数

            string mySql = "Select count(*) as [count] From [Sheet1$]";
            OleDbConnection myConn = new OleDbConnection(ConnStr);
            myConn.Open();
            OleDbCommand cmd = new OleDbCommand(mySql, myConn);
            OleDbDataReader mydr = cmd.ExecuteReader();
            if( mydr.Read() )
            {
                lblNum.Text = mydr["count"].ToString();
            }
            mydr.Close();
            myConn.Close();
            myConn=null;
        }

        protected void btnInsert_Click(object sender, System.EventArgs e)
        {
            if( ViewState["PID"] == null )
            {
                Response.Write("<script>");
                Response.Write("alert('导入失败!!!');");
                Response.Write("</script>");
                return;
            }
            string strID = ViewState["PID"].ToString();

            string source = file1.Value;
            if( source.Trim() == String.Empty || Path.GetExtension(file1.PostedFile.FileName)!=".xls" )
            {
                Response.Write("<script>");
                Response.Write("alert('请选择要 导入 的文件!!!');");
                Response.Write("</script>");
                return;
            }

            string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + 
                source + ";Extended Properties=Excel 8.0";
            string query = "Select * From [Sheet1$]";

            OleDbConnection conn = new OleDbConnection( ConnStr );
            OleDbDataAdapter ad = new OleDbDataAdapter( query, conn );
            DataSet ds = new DataSet();
            ad.Fill( ds, "Book1" );
            ad.Dispose();
            conn.Close();
            conn=null;
            
            if( ds.Tables["Book1"].Rows.Count > 0 )
            {
                //加密对象
                DCard.Encode myEncode = new DCard.Encode();

                String sql="";

                DataTable dt = new DataTable();
                dt.Columns.Add("卡号");
                dt.Columns.Add("密码");
                dt = ds.Tables["Book1"];
                DataRow [] rows = dt.Select();
                foreach(DataRow dr in rows)
                {
                    sql += "insert into PList(PID,CardNum,CardPassword) values(" + strID + ",'" + dr[0].ToString() + "','" + myEncode.EncryptString(dr[1].ToString()) + "');";
                }
                DBConn myDB = new DBConn();
                int iNum = myDB.Insert( sql );
                myDB.Close();

                DBConn myDB2 = new DBConn();
                string mySql = "update Products set PStock=PStock+" + iNum + " where PID=" + strID;
                myDB2.Update( mySql );
                myDB2.Close();

                Response.Write("<script>");
                Response.Write("alert('成功导入[ " + iNum + "条 ]数据!!!');");
                Response.Write("</script>");

            }
            else
            {
                Response.Write("<script>");
                Response.Write("alert('表里没记录,导入失败!!!');");
                Response.Write("</script>");
                return;
            }

        }


	}
}
