using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using Core.Mes.ClientFrameWork;
using Core.Mes.IBaseInterface;
using System.Data;

using Infragistics.Excel;
using Infragistics.Win.UltraWinGrid;
using Infragistics.Win.UltraWinGrid.ExcelExport;
using Infragistics.Shared;
using Infragistics.Win;


namespace Core.XgMes.Client.JGKC.RollManager
{
    public partial class frmJBZKZH : Mes.ClientFrameWork.FrmBase
    {
        public frmJBZKZH()
        {
            InitializeComponent();
        }
        private string sqlWhereForBJYiYuChuLi;
        DataSet ds = new DataSet();
        private string sqlWhereForBJYiYuChuLi1;
        private void ultraToolbarsManager1_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
        {

            try
            {
                switch (e.Tool.Key)
                {
                    case "btnQuery":
                        sqlWhereForBJYiYuChuLi = "  ";
                        sqlWhereForBJYiYuChuLi1 = "  ";
                        if (consql())
                            displayBJZH();
                        break;
                    case "btnExcel":
                        ExcelForBJYCL();
                        break;
                    case "btnExit":
                        this.Close();
                        break;
                    default:
                        break;
                }
            }
            catch (Exception err)
            {
                System.Diagnostics.Debug.WriteLine(err.ToString());
            }		
        }
        private void displayBJZH()
        {
            string strOut = "";
            try
            {
                object obj = Core.Mes.ClientFrameWork.ClientCommon._RemotingHelp.ExecuteMethod("BJZWJBStuff", "Core.XgMes.Server.JGKC.RollManager.TurnoReport",
                    "getBJZHForQuery",
                    new object[] { sqlWhereForBJYiYuChuLi, sqlWhereForBJYiYuChuLi1 },
                    out strOut) as DataSet;
              if (strOut != "")
                {
                    MessageBox.Show(strOut);
                    return;
                }
                ds=(DataSet)obj;
                this.ultraGrid1.DataSource = (DataSet)obj;/*ds*/
                this.ultraGrid1.UpdateData();
                //this.InitGridSummery(this.ultraGrid1);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }
        }
        private void InitGridSummery(Infragistics.Win.UltraWinGrid.UltraGrid uGrid)
        {
            if (uGrid.Rows.FilteredInRowCount > 0)// && CheckHZ.Checked)		
            {
                for (int i = 0; i < uGrid.DisplayLayout.Bands[0].Summaries.Count; i++)
                {
                    if (uGrid.DisplayLayout.Bands[0].Summaries[i].SummaryType == SummaryType.Sum)
                        uGrid.DisplayLayout.Bands[0].Summaries[i].SummaryDisplayArea =
                            SummaryDisplayAreas.BottomFixed | SummaryDisplayAreas.InGroupByRows;
                    else
                        uGrid.DisplayLayout.Bands[0].Summaries[i].SummaryDisplayArea = SummaryDisplayAreas.BottomFixed;
                }
            }
            else
            {
                for (int i = 0; i < uGrid.DisplayLayout.Bands[0].Summaries.Count; i++)
                {
                    uGrid.DisplayLayout.Bands[0].Summaries[i].SummaryDisplayArea = SummaryDisplayAreas.None;
                }
            }
        }

        private void frmJBZKZH_Load(object sender, EventArgs e)
        {
            try
            {
                //this.Cmb_DateType.Value = "交货日期";
                //((Infragistics.Win.UltraWinGrid.UltraCombo)Cmb_DateType).DisplayLayout.Bands[0].Columns[0].Band.ColHeadersVisible = false;
                //this.InitGridSummery(this.ultraGrid1);
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }	
        }

        private void uCkEditorForTaskNo_CheckedChanged(object sender, EventArgs e)
        {
            this.uEditorForTaskNo.Enabled = this.uCkEditorForTaskNo.Checked;
        }

        private void uEYCLDate_CheckedChanged(object sender, EventArgs e)
        {
            Cmb_DateType.Enabled = this.uEYCLDate.Checked;
            uDateEndTime.Enabled = this.uEYCLDate.Checked;
            uDateStartTime.Enabled = this.uEYCLDate.Checked;
        }

        private void uCkEditorForGroupBy_CheckedChanged(object sender, EventArgs e)
        {
            if (this.uCkEditorForGroupBy.Checked)
            {
                this.ultraGrid1.DisplayLayout.GroupByBox.Hidden = false;
            }
            else
            {
                this.ultraGrid1.DisplayLayout.GroupByBox.Hidden = true;
                this.ultraGrid1.DisplayLayout.Bands[0].SortedColumns.Clear();
            }
        }

        private void uCkEditorForFilter_CheckedChanged(object sender, EventArgs e)
        {
            if (this.uCkEditorForFilter.Checked)
            {
                this.ultraGrid1.DisplayLayout.Override.AllowRowFiltering = Infragistics.Win.DefaultableBoolean.True;
            }
            else
            {
                this.ultraGrid1.DisplayLayout.Override.AllowRowFiltering = Infragistics.Win.DefaultableBoolean.False;
            }	
        }

        private bool consql()
        {
            if (this.uCkEditorForTaskNo.Checked == true)
            {
                if (this.uEditorForTaskNo.Text.Trim() == "")
                {
                    MessageBox.Show("合同号不能为空", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return false;
                }
                else
                {
                    this.sqlWhereForBJYiYuChuLi = this.sqlWhereForBJYiYuChuLi + " AND A.ORD_NO LIKE '" + this.uEditorForTaskNo.Text.Trim() + "%'";
                    sqlWhereForBJYiYuChuLi1 = this.sqlWhereForBJYiYuChuLi1 + " AND B.ORD_NO LIKE '" + this.uEditorForTaskNo.Text.Trim() + "%'";

                }
            }

            

            if (this.uEYCLDate.Checked == true)
            {
                if (this.uDateStartTime.DateTime.CompareTo(this.uDateEndTime.DateTime) > 0)
                {
                    MessageBox.Show("开始时间大于结束时间", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return false;
                }
                else
                {

                    string col = "a.ORD_DEVLMT_DATE";
                    this.sqlWhereForBJYiYuChuLi =  "AND " + col + " >= '"
                                + this.uDateStartTime.DateTime.ToString("yyyyMMdd") + "' AND a.ORD_DEVLMT_DATE <= '"
                                + this.uDateEndTime.DateTime.ToString("yyyyMMdd") + "'";
                    string col1 = "B.DEVLMT_DTIME";
                    this.sqlWhereForBJYiYuChuLi1 = "AND " + col1 + " >= '"
                                + this.uDateStartTime.DateTime.ToString("yyyyMMdd") + "' AND B.DEVLMT_DTIME <= '"
                                + this.uDateEndTime.DateTime.ToString("yyyyMMdd") + "'";
                    

                }
            }
            return true;
        }
        private void ExcelForBJYCL()
        {
            if (this.ultraGrid1.Rows.Count == 0)
            {
                MessageBox.Show("没有数据，不能导出", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                DoExc();
            }
        }

        private void DoExc()
        {
            string strFileName = @"c:\" + this.Text + System.DateTime.Today.ToString("yyMMdd") + ".xls";

            System.Windows.Forms.SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Microsoft Office Excel 工作簿 (*.xls)|*.xls";
            sfd.Title = "导出到Excel文件";
            sfd.FileName = strFileName;

            if (sfd.ShowDialog() == DialogResult.OK)
            {
                strFileName = sfd.FileName;
                Infragistics.Excel.Workbook wb = new Infragistics.Excel.Workbook();
                Infragistics.Excel.Worksheet ws2 = wb.Worksheets.Add("Sheet1");
                ws2.Rows[0].Cells[0].Value = this.ultraGrid1.Text;
                ws2.MergedCellsRegions.Add(0, 0, 0, 29);
                ws2.Rows[0].Cells[0].CellFormat.Alignment = Infragistics.Excel.HorizontalCellAlignment.Center;
                ws2.Rows[0].Cells[0].CellFormat.VerticalAlignment = Infragistics.Excel.VerticalCellAlignment.Center;
                ws2.Rows[0].Cells[0].CellFormat.WrapText = Infragistics.Excel.ExcelDefaultableBoolean.True;
                ws2.Rows[0].Cells[0].CellFormat.Font.Height = 300;
                ws2.Rows[0].Cells[0].CellFormat.Font.UnderlineStyle = Infragistics.Excel.FontUnderlineStyle.Single;
                ws2.Rows[0].Height = 510;

                int int_index = 0;
                try
                {
                    for (int i = 0; i < this.ultraGrid1.DisplayLayout.Bands[0].Columns.Count; i++)
                    {
                        if (this.ultraGrid1.DisplayLayout.Bands[0].Columns[i].Hidden == false)
                        {
                            ws2.Rows[1 + this.ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.OriginY].Cells[this.ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.OriginX].Value = this.ultraGrid1.DisplayLayout.Bands[0].Columns[i].Header.Caption;
                            ws2.MergedCellsRegions.Add(1 + this.ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.OriginY,
                                this.ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.OriginX,
                                1 + this.ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.OriginY +
                                this.ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.SpanY - 1,
                                this.ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.OriginX +
                                this.ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.SpanX - 1);
                            int_index += this.ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.SpanX;
                        }
                    }
                }
                catch (Exception ex)
                { }
                for (int i = 0; i <= 29; i++)
                {
                    for (int j = 1; j <= 2; j++)
                    {
                        ws2.Rows[j].Cells[i].CellFormat.Alignment = Infragistics.Excel.HorizontalCellAlignment.Center;
                        ws2.Rows[j].Cells[i].CellFormat.VerticalAlignment = Infragistics.Excel.VerticalCellAlignment.Center;
                        ws2.Rows[j].Cells[i].CellFormat.WrapText = Infragistics.Excel.ExcelDefaultableBoolean.True;
                        ws2.Rows[j].Cells[i].CellFormat.FillPatternBackgroundColor = Color.LightSteelBlue;
                        ws2.Rows[j].Cells[i].CellFormat.FillPatternForegroundColor = Color.LightSteelBlue;
                        ws2.Rows[j].Cells[i].CellFormat.TopBorderColor = Color.Black;
                        ws2.Rows[j].Cells[i].CellFormat.BottomBorderColor = Color.Black;
                        ws2.Rows[j].Cells[i].CellFormat.LeftBorderColor = Color.Black;
                        ws2.Rows[j].Cells[i].CellFormat.RightBorderColor = Color.Black;
                    }
                }

                for (int i = 0; i < this.ultraGrid1.Rows.Count; i++)
                {
                    for (int j = 0; j < this.ultraGrid1.DisplayLayout.Bands[0].Columns.Count; j++)
                    {
                        if (this.ultraGrid1.Rows[i].Cells[j].Hidden == false && this.ultraGrid1.DisplayLayout.Bands[0].Columns[j].RowLayoutColumnInfo.LabelPosition != LabelPosition.LabelOnly)
                        {
                            ws2.Rows[3 + i].Cells[this.ultraGrid1.DisplayLayout.Bands[0].Columns[j].RowLayoutColumnInfo.OriginX].Value = this.ultraGrid1.Rows[i].Cells[j].Value;
                            ws2.Rows[3 + i].Cells[this.ultraGrid1.DisplayLayout.Bands[0].Columns[j].RowLayoutColumnInfo.OriginX].CellFormat.VerticalAlignment = Infragistics.Excel.VerticalCellAlignment.Center;

                            if (this.ultraGrid1.DisplayLayout.Bands[0].Columns[j].DataType == typeof(System.Decimal))
                            {
                                ws2.Rows[3 + i].Cells[this.ultraGrid1.DisplayLayout.Bands[0].Columns[j].RowLayoutColumnInfo.OriginX].CellFormat.Alignment = Infragistics.Excel.HorizontalCellAlignment.Right;
                            }

                        }

                    }
                }
                Infragistics.Excel.BIFF8Writer.WriteWorkbookToFile(wb, strFileName);
            }
        }

        private void ultraCheckEditor6_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                string RowFilter = "";
                if (ultraCheckEditor5.Checked)
                    RowFilter = "SENDWEIGHT>=WEIGHT-0.1 ";
                if (ultraCheckEditor10.Checked)
                {
                    if (RowFilter.Length > 0)
                        RowFilter = RowFilter + " AND ZL_WEIGHT >= WEIGHT-0.1 and SENDWEIGHT<WEIGHT-0.1 ";
                    else
                        RowFilter = "ZL_WEIGHT >= WEIGHT-0.1 and SENDWEIGHT<WEIGHT-0.1 ";
                }
                if (ultraCheckEditor6.Checked)
                {
                    if (RowFilter.Length > 0)
                        RowFilter = RowFilter + " AND CL_WEIGHT<-0.1 and ZL_WEIGHT<WEIGHT-0.1 ";
                    else
                        RowFilter = "CL_WEIGHT<-0.1 and ZL_WEIGHT<WEIGHT-0.1 ";
                }
                ds.Tables[0].DefaultView.RowFilter = RowFilter;
                SetColor();
            }
            catch (Exception err)
            {
                System.Diagnostics.Debug.WriteLine(err.ToString());
            }
        }

        private void ultraCheckEditor5_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                string RowFilter = "";
                if (ultraCheckEditor6.Checked)
                    RowFilter = "CL_WEIGHT<-0.1 and ZL_WEIGHT<WEIGHT-0.1 ";
                if (ultraCheckEditor10.Checked)
                {
                    if (RowFilter.Length > 0)
                        RowFilter = RowFilter + "AND ZL_WEIGHT >= WEIGHT-0.1 and SENDWEIGHT<WEIGHT-0.1 ";
                    else
                        RowFilter = "ZL_WEIGHT >= WEIGHT-0.1 and SENDWEIGHT<WEIGHT-0.1 ";
                }
                if (ultraCheckEditor5.Checked)
                {
                    if (RowFilter.Length > 0)
                        RowFilter = RowFilter + "and SENDWEIGHT>=WEIGHT-0.1 ";
                    else
                        RowFilter = "SENDWEIGHT>=WEIGHT-0.1 ";
                }
                ds.Tables[0].DefaultView.RowFilter = RowFilter;
                SetColor();
            }
            catch (Exception err)
            {
                System.Diagnostics.Debug.WriteLine(err.ToString());
            }
        }

        private void ultraCheckEditor10_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                string RowFilter = "";
                if (ultraCheckEditor5.Checked)
                    RowFilter = "SENDWEIGHT>=WEIGHT-0.1";
                if (ultraCheckEditor6.Checked)
                {
                    if (RowFilter.Length > 0)
                        RowFilter = RowFilter + "CL_WEIGHT<-0.1 and ZL_WEIGHT<WEIGHT-0.1 ";
                    else
                        RowFilter = "CL_WEIGHT<-0.1 and ZL_WEIGHT<WEIGHT-0.1 ";
                }
                if (ultraCheckEditor10.Checked)
                {
                    if (RowFilter.Length > 0)
                        RowFilter = RowFilter + "AND ZL_WEIGHT >= WEIGHT-0.1 and SENDWEIGHT<WEIGHT-0.1 ";
                    else
                        RowFilter = "ZL_WEIGHT >= WEIGHT-0.1 and SENDWEIGHT<WEIGHT-0.1 ";
                }
                ds.Tables[0].DefaultView.RowFilter = RowFilter;
                SetColor();
            }
            catch (Exception err)
            {
                Console.WriteLine(err.ToString());
            }
        }
        private void SetColor()
        {
            foreach (Infragistics.Win.UltraWinGrid.UltraGridRow Row in this.ultraGrid1.Rows)
            {
                if (Convert.ToDouble(Row.Cells["CL_WEIGHT"].Value) < -0.1)
                {
                    Row.Appearance.ForeColor = Color.DarkGreen;
                }
              
                if (Convert.ToDouble(Row.Cells["ZL_WEIGHT"].Value) - Convert.ToDouble(Row.Cells["WEIGHT"].Value) >= -0.1)
                {
                    Row.Appearance.ForeColor = Color.Red;
                }
                if (Convert.ToDouble(Row.Cells["SENDWEIGHT"].Value) - Convert.ToDouble(Row.Cells["WEIGHT"].Value) >= -0.1)
                {
                    Row.Appearance.ForeColor = Color.Blue;
                }
            }
        }


    }
}