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

namespace DSWeb.box
{
    public partial class BoxReport1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {


            if (!IsPostBack)
            {
                sdate.Text = DateHelper.GetMondayDate().ToString("yyyy-MM-dd");
                edate.Text = DateHelper.GetSundayDate().ToString("yyyy-MM-dd");
                createdata();
            }
        }
        protected void createdata()
        {
            grid.DataSource = retable();
            grid.DataBind();
        }

        protected DataTable retable()
        {
            DataTable dt = new DataTable();
            DataTable dt_b = BaseClass.db.GetSqlStrTable("select * from code_ctn");
            dt.Columns.Add(new DataColumn("船名航次", typeof(string)));
            foreach (DataRow dr in dt_b.Rows)
            {
                dt.Columns.Add(new DataColumn(dr["ctn"].ToString(), typeof(int)));
            }

            DataTable dt_vessel = BaseClass.db.GetSqlStrTable("select vessel,Voyage from container_business where datediff(dd,etd,'" + sdate.Text + "')<=0 and datediff(dd,etd,'" + edate.Text + "')>=0 group by vessel,Voyage");
            foreach (DataRow dr in dt_vessel.Rows)
            {
                DataRow drv = dt.NewRow();
                drv["船名航次"] = dr["vessel"].ToString() + dr["Voyage"].ToString();

                foreach (DataRow dr1 in dt_b.Rows)
                {
                    drv[dr1["ctn"].ToString()] = boxnum(dr["vessel"].ToString(), dr["Voyage"].ToString(),dr1["ctn"].ToString());
                }

                dt.Rows.Add(drv);
            }
            //合计
            DataRow drw = dt.NewRow();
            drw["船名航次"] = "合计";
            foreach (DataRow dr in dt_b.Rows)
            {
                drw[dr["ctn"].ToString()] = dt.Compute("sum([" + dr["ctn"].ToString() + "])", "");
            }
            dt.Rows.Add(drw);
            return dt;
        }
        protected object boxnum(string vessel,string voyage,string ctn)
        {
            int i = 0;
            try {
                i = int.Parse(BaseClass.db.GetSqlStrScalar("select count(*) from container_business where vessel='" + vessel + "' and voyage='" + voyage + "' and (select Cntype from container_info where CntrNo=Box_No)='" + ctn + "'").ToString());
            }
            catch { }
            if (i == 0)
                return DBNull.Value;
            else
            return i;
        }

        protected void ibsave_Click(object sender, ImageClickEventArgs e)
        {
            createdata();
 
            grid.Attributes.Add("style", "vnd.ms-excel.numberformat:@");
            Response.ClearContent();
            Response.Buffer = false;
            Response.Charset = "GB2312";
            Response.AddHeader("content-disposition", "attachment; filename=excel.xls");
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            Response.ContentType = "application/excel";
            this.EnableViewState = false;

            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            grid.RenderControl(htw);
            Response.Write(sw.ToString());

            Response.End();
        }
        public override void VerifyRenderingInServerForm(Control control)
        {
            //base.VerifyRenderingInServerForm(control);
        }
        protected void btsearch_Click(object sender, EventArgs e)
        {
            createdata();
        }
        
    }
}