using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Core.XgMes.Client.StorageBase;
using System.Collections;

namespace Core.XgMes.Client.JGKC.SteelPlateManager
{
    public partial class frmKCJListQuery : Core.Mes.ClientFrameWork.FrmBase
    {
        public frmKCJListQuery()
        {
            InitializeComponent();
        }
        string DBFileName = "";
        DataSet _FrmDataSet = new DataSet();
        public string sstyle = "";
        private void frmKCJListQuery_Load(object sender, EventArgs e)
        {
            try
            {
                this.DBFileName = this.Key.Split('@')[2];
                sstyle=this.Key.Split('@')[3];
                InitFrmDataSet();
            }
            catch (System.Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }
        }

        private void InitFrmDataSet()
        {
            try
            {
                if (DBFileName.Trim().Length > 0)
                {
                    object obj = StorageBaseClass.GetEntityInfo(DBFileName, this.ProductName);
                    if (obj != null)
                    {
                        _FrmDataSet = obj as System.Data.DataSet;
                        _FrmDataSet.Tables["KCH_TURNOFFLIST_VIEW"].PrimaryKey = null;
                        _FrmDataSet.Tables["KCH_TURNOFFLIST_VIEW"].Constraints.Clear();
                        Init_Frm();
                    }
                }
            }
            catch (System.Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }
        }

        private void Init_Frm()
        {
            StorageGridHelper.SetGrid(this.Grd_Report, ref _FrmDataSet, "KCH_TURNOFFLIST_VIEW", new string[] { "KCH_TURNOFFLIST_VIEW" });
            this.Grd_Report.DisplayLayout.Override.RowSelectors = Infragistics.Win.DefaultableBoolean.False;
            this.ctlKCJListQuery1.MyDataSet = _FrmDataSet;            
            foreach (Infragistics.Win.UltraWinGrid.UltraGridColumn col in this.Grd_Report.DisplayLayout.Bands[0].Columns)
            {
                col.RowLayoutColumnInfo.SpanY = 1;
            }
        }
        
        //查询
        private void Query()
        {
            try
            {
                string strerr;
                string where = this.ctlKCJListQuery1.WhereStr;
                string Swhere = this.ctlKCJListQuery1.SendWhereStr;
                string CWhere = this.ctlKCJListQuery1.CSendWhereStr;
               
            
                if (sstyle == "1")
                {

                    Swhere = Swhere.Replace(" AND B.AREA!='虚拟区域' ", " AND B.AREA NOT IN ('线棒材区域')");


                    CWhere = CWhere.Replace(" AND B.AREA!='虚拟区域' ", " AND B.AREA NOT IN ('线棒材区域')");
                    
                }
                else
                {

                    Swhere = Swhere.Replace(" AND B.AREA!='虚拟区域' ", " AND B.AREA  IN ('板材区域')");



                    CWhere = CWhere.Replace(" AND B.AREA!='虚拟区域' ", " AND B.AREA  IN ('板材区域')");


                    where = where + " AND B.AREA  IN ('板材区域','线棒材区域')";
                }
                System.Collections.ArrayList WhereList = new ArrayList();
                WhereList.Add(where);
                WhereList.Add(Swhere);
                WhereList.Add(CWhere);

                System.Collections.ArrayList KSelete = this.ctlKCJListStat1.SelectStr;
                System.Collections.ArrayList SSelect = this.ctlKCJListStat1.SendSelectStr;
                System.Collections.ArrayList KGroup = this.ctlKCJListStat1.Groupby;
                System.Collections.ArrayList SGroup = this.ctlKCJListStat1.SendGroupby;

                System.Collections.ArrayList SelectList = new ArrayList();
                if (KSelete != null)
                {
                    SelectList.Add(KSelete[0]);
                }
                else
                {
                    WhereList.RemoveAt(0);
                }
                if (SSelect != null)
                {
                    for (int i = 0; i < SSelect.Count; i++)
                    {
                        SelectList.Add(SSelect[i]);
                    }
                }

                System.Collections.ArrayList GroupList = new ArrayList();
                if (KGroup != null)
                    GroupList.Add(KGroup[0]);
                if (SGroup != null)
                {
                    for (int i = 0; i < SGroup.Count; i++)
                    {
                        GroupList.Add(SGroup[i]);
                    }
                }

                _FrmDataSet.Tables["KCH_TURNOFFLIST_VIEW"].Clear();
                object obj = null;

                if (SelectList != null)
                {
                    obj = Core.Mes.ClientFrameWork.ClientCommon._RemotingHelp.ExecuteMethod("BJZWStuff", "Core.XgMes.Server.JGKC.SteelPlateManager.TurnoffReportList",
                        "GetStorageData", new object[]{SelectList,
														 WhereList,
														 GroupList}, out strerr);
                }
                if (obj != null)
                {
                    foreach (System.Data.DataColumn col in this._FrmDataSet.Tables["KCH_TURNOFFLIST_VIEW"].Columns)
                    {
                        if (!(obj as System.Data.DataSet).Tables[0].Columns.Contains(col.ColumnName))
                        {
                            this.Grd_Report.DisplayLayout.Bands[0].Columns[col.ColumnName].Hidden = true;                            
                        }
                        else
                        {
                            this.Grd_Report.DisplayLayout.Bands[0].Columns[col.ColumnName].Hidden = false;
                        }
                    }
                    _FrmDataSet.Merge((obj as System.Data.DataSet).Copy());
                    _FrmDataSet.AcceptChanges();

                    if (this._FrmDataSet.Tables["KCH_TURNOFFLIST_VIEW"].Columns["THEORYWEIGHTZL"].ColumnName == "THEORYWEIGHTZL")
                    {
                        this.Grd_Report.DisplayLayout.Bands[0].Columns["THEORYWEIGHTZL"].Hidden = true;
                    }
                    if (this._FrmDataSet.Tables["KCH_TURNOFFLIST_VIEW"].Columns["INSTROAGESTEEL1"].ColumnName == "INSTROAGESTEEL1")
                    {
                        this.Grd_Report.DisplayLayout.Bands[0].Columns["INSTROAGESTEEL1"].Hidden = true;
                    }
                    if (this._FrmDataSet.Tables["KCH_TURNOFFLIST_VIEW"].Columns["THEORYWEIGHT1"].ColumnName == "THEORYWEIGHT1")
                    {
                        this.Grd_Report.DisplayLayout.Bands[0].Columns["THEORYWEIGHT1"].Hidden = true;
                    }
                  
                }
            }
            catch (System.Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }
        }
        
        //导出
        private void Export()
        {
            try
            {
                string filename = @"库存查询.xls";
                ultraGridExcelExporter.Export(Grd_Report, filename);
                System.Diagnostics.ProcessStartInfo st = new System.Diagnostics.ProcessStartInfo(filename);
                st.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
                System.Diagnostics.Process.Start(st);
            }
            catch (System.Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }
        }

        private void chk_fs_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < this.ctlKCJListStat1.Controls.Count; i++)
                {
                    try
                    {
                        Infragistics.Win.UltraWinEditors.UltraCheckEditor a = (Infragistics.Win.UltraWinEditors.UltraCheckEditor)this.ctlKCJListStat1.Controls[i];
                        if (a.Name != "chk_StatSend" || a.Name != "chk_StatSendOnly")
                            a.Checked = this.chk_fs.Checked;
                    }
                    catch { }
                }
            }
            catch { }
        }

        private void ultraToolbarsManager1_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
        {
            switch (e.Tool.Key)
            {
                case "tlbtnQuery":
                    Query();
                    break;
                case "tlbtnExport":
                    Export();
                    break;
                case "tlbtnExt":
                    this.Close();
                    break;
            }
        }
    }
}