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.SqlClient;
using System.Configuration;
//5-1-as-px
namespace NScontract.NSUser
{
    /// <summary>
    /// NSSale 的摘要说明。
    /// </summary>
    public partial class NSSale : System.Web.UI.Page
    {

        //		private string NSNickName;
        //		private string NSDepID;
        SqlConnection NSconn;

        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (Session["NSNickName"] != null)
            {
                //获得数据库连接字符串
                string NSconnection = ConfigurationSettings.AppSettings["strconnection"];
                //创建数据库连接
                NSconn = new SqlConnection(NSconnection);
                if (!IsPostBack)
                {
                    //自定义BindGrid ()方法，加载自己连锁店销售产品的统计信息
                    BindGrid();
                    //得到连锁店名字
                    NSconn.Open();
                    string NSNickName = Session["NSNickName"].ToString();
                    string NSmysql = "select NSDep.* from NSDep,NSUser where NSUser.NSDepID = NSDep.NSDepID and NSUser.NSNickName = '" + NSNickName + "' and NSUser.NSNoAdmin = 0";
                    SqlCommand NScmd2 = new SqlCommand(NSmysql, NSconn);
                    SqlDataReader NSsdr2 = NScmd2.ExecuteReader();
                    while (NSsdr2.Read())
                    {
                        Session["NSDepID"] = NSsdr2["NSDepID"];
                        Session["NSDepName"] = NSsdr2["NSDepName"];
                        NSDepName.Text = Session["NSDepName"].ToString();
                    }
                    NSconn.Close();
                    //加载产品下拉列表框
                    NSconn.Open();
                    string NSmysql1 = "select * from NSBook";
                    SqlCommand NScmd1 = new SqlCommand(NSmysql1, NSconn);
                    SqlDataReader NSdr1 = NScmd1.ExecuteReader();
                    NS_Book.Items.Add(new ListItem("", "-1"));
                    while (NSdr1.Read())
                    {
                        NS_Book.Items.Add(new ListItem(NSdr1["NSBookName"].ToString(), NSdr1["NSBookID"].ToString()));
                    }
                    NSconn.Close();
                }
            }
            else
            {
                Response.Redirect("../Index.aspx");
            }


        }

        //处理分页
        public void NSmyGrid_Page(object sender, DataGridPageChangedEventArgs e)
        {
            NSmyGrid.CurrentPageIndex = e.NewPageIndex;
            BindGrid();
        }

        //加载自己连锁店销售产品的统计信息
        public void BindGrid()
        {
            string NSNickName = Session["NSNickName"].ToString();
            string NSstrsql = "select NSSale.ID,NSDep.NSDepName,NSBookName,NSSale.NSSaleNum,NSSale.NSStartTime,NSSale.NSEndTime from NSSale,NSDep,NSBook,NSUser where NSSale.NSBookID = NSBook.NSBookID and NSSale.NSDepID = NSDep.NSDepID and NSUser.NSDepID = NSDep.NSDepID and NSUser.NSNickName = '" + NSNickName + "'";
            SqlDataAdapter NSsda = new SqlDataAdapter(NSstrsql, NSconn);
            DataSet NSds = new DataSet();
            NSsda.Fill(NSds);
            NSmyGrid.DataSource = NSds;

            NSmyGrid.DataBind();

        }

        #region Web 窗体设计器生成的代码
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {

        }
        #endregion

        protected void NS_ok_Click(object sender, System.EventArgs e)
        {
            if (NS_Book.SelectedItem.Value != "-1")
            {
                string NSDepID = Session["NSDepID"].ToString();

                //创建数据库SqlCommand对象
                SqlCommand NScmd = new SqlCommand("insert_NSSale_1", NSconn);
                //获取存储过程名
                NScmd.CommandType = CommandType.StoredProcedure;
                //向SqlCommand对象添加参数
                NScmd.Parameters.Add(new SqlParameter("@NSBookID", SqlDbType.Int, 10));
                NScmd.Parameters.Add(new SqlParameter("@NSDepID", SqlDbType.VarChar, 50));
                NScmd.Parameters.Add(new SqlParameter("@NSSaleNum", SqlDbType.Decimal, 9));
                NScmd.Parameters.Add(new SqlParameter("@NSStartTime", SqlDbType.DateTime, 8));
                NScmd.Parameters.Add(new SqlParameter("@NSEndTime", SqlDbType.DateTime, 8));
                //向参数赋值
                NScmd.Parameters["@NSDepID"].Value = NSDepID;
                NScmd.Parameters["@NSBookID"].Value = NS_Book.SelectedItem.Value;
                NScmd.Parameters["@NSSaleNum"].Value = NSSaleNum.Text;
                NScmd.Parameters["@NSStartTime"].Value = NSStartTime.Text;
                NScmd.Parameters["@NSEndTime"].Value = NSEndTime.Text;
                NScmd.Connection.Open();
                try
                {
                    //执行sql语句
                    NScmd.ExecuteNonQuery();
                    Response.Redirect("NSSale.aspx");
                }
                catch (SqlException ex)
                {
                    string ss = ex.Message;
                    NS_Warn.Text = "添加失败";
                    NS_Warn.Style["color"] = "red";
                }
                NScmd.Connection.Close();
            }



        }
    }
}

