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 System.Runtime.InteropServices;

namespace Core.XgMes.Client.JGKC.SteelPlateManager
{
    public partial class FrmPlan : Mes.ClientFrameWork.FrmBase
    {
        public FrmPlan()
        {
            InitializeComponent();
        }

        private void FrmPlan_Load(object sender, EventArgs e)
        {
            Init();
            uCob_LX.Value = "0";
            cmb_LX.Value = "0";
            dte_EndTime.Value=dte_BeginTime.Value = Dte_PlanTime.Value = DateTime.Now;
        }
      
        private void Init()
        {
            string strOut="";
            string strSql = "";
            DataSet ds_;
            try
            {
                strSql = "SELECT BUYERCODE ID_, BUYERNAME NAME_ FROM SEL_BUYERBASEINFO a UNION SELECT CUST_CD BUYERCODE, CUST_NM BUYERNAME FROM TBZ00_CUSTOMER WHERE  REC_TP='01' AND REC_STS='1'  ";
                     ds_ = (DataSet)ClientCommon._RemotingHelp.ExecuteMethod("BJZWStuff", "Core.XgMes.Server.JGKC.SteelPlateManager.Common", "GetDataSet", new object[] { strSql }, out strOut);
                    ds_.Tables[0].TableName = "SEL_BUYERBASEINFO";

                    Comm.FillComboEditor(ref this.qBuyer, ds_);
                    Comm.FillComboEditor(ref this.cobBuyname, ds_);
                    Comm.SetGridDropDownList(this.gd_Plan, ds_, "SEL_BUYERBASEINFO", "BUYERCODE", "NAME_", "ID_");
                    Comm.SetGridDropDownList(this.ultraGrid2, ds_, "SEL_BUYERBASEINFO", "BUYERCODE", "NAME_", "ID_");

                    strSql = "SELECT INCEPTCORPCODE ID_, INCEPTCORPNAME NAME_ FROM SEL_INCEPTCORPINFO  UNION SELECT CUST_CD INCEPTCORPNAME, CUST_NM BUYERNAME FROM TBZ00_CUSTOMER WHERE  REC_TP='02' AND REC_STS='1'  ";
                     ds_ = (DataSet)ClientCommon._RemotingHelp.ExecuteMethod("BJZWStuff", "Core.XgMes.Server.JGKC.SteelPlateManager.Common", "GetDataSet", new object[] { strSql }, out strOut);
                    ds_.Tables[0].TableName = "SEL_INCEPTCORPINFO";

                    Comm.FillComboEditor(ref this.qIncep,  ds_);
                    Comm.FillComboEditor(ref this.cob_Incop, ds_);
                    Comm.SetGridDropDownList(this.gd_Plan, ds_, "SEL_INCEPTCORPINFO", "INCEPTCORPCODE", "NAME_", "ID_");
                    Comm.SetGridDropDownList(this.ultraGrid2, ds_, "SEL_INCEPTCORPINFO", "INCEPTCORPCODE", "NAME_", "ID_");

                    strSql = "select ID_ ,Name_ from KCJ_BASEDATA where Sort_Code='8091' ";
                    ds_ = (DataSet)ClientCommon._RemotingHelp.ExecuteMethod("BJZWStuff", "Core.XgMes.Server.JGKC.SteelPlateManager.Common", "GetDataSet",
                        new object[] { strSql }, out strOut);
                    if (strOut != "") { MessageBox.Show(strOut); return; }
                    Comm.FillComboEditor(ref this.cmb_pactnoLX, ds_);
                


            }catch(Exception ex)
            {
                MessageBox.Show("错误" + ex, "错误提示");
                return;
            }
        }

        private void chk_CollectTime_CheckedChanged(object sender, EventArgs e)
        {
            dte_BeginTime.Enabled = dte_EndTime.Enabled = chk_CollectTime.Checked;
        }

        private void chk_BuyName_CheckedChanged(object sender, EventArgs e)
        {
            cobBuyname.Enabled = chk_BuyName.Checked;
        }

        private void chk_Incep_CheckedChanged(object sender, EventArgs e)
        {
            cob_Incop.Enabled = chk_Incep.Checked;
        }

        private void chk_LX_CheckedChanged(object sender, EventArgs e)
        {
            cmb_LX.Enabled = chk_LX.Checked;
        }

        private void ultraToolbarsManager1_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
        {
            switch (e.Tool.Key)
            {
                case "QUERY":
                    QueryPlan();
                    break;
                case "PLAN_ADD":
                    AddPlan("1");
                    break;
                case "PALN_DEL":
                    AddPlan("2");
                    break;
                case "FP":
                    queryFP();
                    break;
            }
        }
        private void queryFP()
        {
            string sql = "";
            string strOut = "";
            string strWhere = "";
            if (chk_CollectTime.Checked == false && chk_LX.Checked == false)
            {
                MessageBox.Show("请选择任意条件进行查询!!!!!");
            }
            if (chk_CollectTime.Checked)
            {
                if (dte_BeginTime.Enabled && dte_EndTime.Enabled)
                {
                    strWhere += " AND T.RECODERTIME BETWEEN TO_DATE('" + Convert.ToDateTime(this.dte_BeginTime.Value).ToString("yyyy-MM-dd") + "','yyyy-MM-dd') "
                            + " AND TO_DATE('" + Convert.ToDateTime(this.dte_EndTime.Value).ToString("yyyy-MM-dd") + " 23:59:59','yyyy-MM-dd hh24:mi:ss') ";
                }
            }

            if (chk_LX.Checked)
            {
                strWhere += " AND T.PRODUCT_LX LIKE '" + cmb_LX.Value.ToString().Trim() + "%'";
            }
            DataSet ds = new DataSet();
            sql = "SELECT DISTINCT T.PACTNO,T.PACTNOLX,TO_CHAR(T.RECODERTIME,'YYYY-MM-DD HH24:MI')RECODERTIME,T.RECODER,"
                 + "T.BUYERCODE, "
                 + "T.INCEPECODE,T.PRODUCT_LX "
                 + " FROM KCJ_PACTNOLX_ZW T  WHERE T.ISVALID='1' " + strWhere;
            ds = (DataSet)ClientCommon._RemotingHelp.ExecuteMethod("BJZWStuff", "Core.XgMes.Server.JGKC.SteelPlateManager.Common", "GetDataSet",
                    new object[] { sql }, out strOut);
            if (ds != null)
            {
                ultraGrid2.DataSource = ds;
            }
        }
        private void QueryPlan()
        {
            //string orderNO = "";
            //string billetID = "";
            //string shopSign = "";
            string where = "";
            string strOut = "";




            if (this.cobBuyname.Enabled && this.cobBuyname.Value.ToString().Trim().Length > 0)
                {
                    where += " AND A.BUYERCODE LIKE '" + this.cobBuyname.Value.ToString().Trim() + "%'";
                }

                if (this.cob_Incop.Enabled && this.cob_Incop.Value.ToString().Trim().Length > 0)
                {
                    where += " AND A.INCEPTCORPCODE LIKE '" + this.cob_Incop.Value.ToString().Trim() + "%'";

                }
                if (this.cmb_LX.Enabled && this.cmb_LX.Value.ToString().Trim().Length > 0)
                {
                    where += " AND A.PL LIKE '" + this.cmb_LX.Value.ToString().Trim() + "%'";

                }
               
               
                if (this.dte_BeginTime.Enabled && this.dte_EndTime.Enabled)
                {
                    where += " AND TO_CHAR(A.INCEPTDATE,'YYYY-MM-DD') >='" + Convert.ToDateTime(this.dte_BeginTime.Value).ToString("yyyy-MM-dd") + "' "
                        + " AND TO_CHAR(A.INCEPTDATE,'YYYY-MM-DD') <='" + Convert.ToDateTime(this.dte_EndTime.Value).ToString("yyyy-MM-dd") + "' ";
                }

               
        
         

            try
            {
                
                DataSet ds = (DataSet)Core.Mes.ClientFrameWork.ClientCommon._RemotingHelp.ExecuteMethod("BJZWStuff", "Core.XgMes.Server.JGKC.SteelPlateManager.TurnoffManager",
                    "QueryPlan", new object[] {  where }, out strOut);

                if (strOut == "")
                {
           
                    this.gd_Plan.DataSource = ds;
                    this.gd_Plan.UpdateData();//++
                    Comm.SetGridSumArea(this.gd_Plan);


                }
                else
                {
                    MessageBox.Show(strOut, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }	
        }
        private void AddPlan(string sMode)
        {
            string strOut="";
            System.Collections.ArrayList Billet = new ArrayList();
            if (qBuyer.Value.ToString() == "")
            {
                MessageBox.Show("请选择客户名称!!!!");
                return;
            }
            if (qIncep.Value.ToString() == "")
            {
                MessageBox.Show("请选择收货单位!!!!");
                return;
            }
            if (uCob_LX.Value.ToString() == "")
            {
                MessageBox.Show("请选择产品类型!!!!");
                return;
            }
            if (txt_Weigth.Text.Trim() == "")
            {
                MessageBox.Show("请输入排车重量！！！");
                return;
            }
            Billet.Add(qBuyer.Value.ToString());
            Billet.Add(qIncep.Value.ToString());
            Billet.Add(Dte_PlanTime.DateTime.ToString("yyyy-MM-dd HH:mm:ss"));
            Billet.Add(uCob_LX.Value.ToString());
            Billet.Add(txt_Weigth.Text.ToString().Trim().ToString());
            Billet.Add(txt_CH.Text.ToString().Trim().ToString());
            Billet.Add(txt_Remark.Text.ToString().Trim().ToString());
            Billet.Add(txt_Pactno.Text.ToString().Trim().ToString());
            Billet.Add(ClientCommon._UserInfo.UserName.ToString());
            Billet.Add(sMode);
            Billet.Add(txt_Askid.Text.ToString().Trim());
            string info = Core.Mes.ClientFrameWork.ClientCommon._RemotingHelp.ExecuteMethod("BJZWStuff", "Core.XgMes.Server.JGKC.SteelPlateManager.TurnoffManager",
                       "AddPlan", new object[] { Billet }, out strOut).ToString();
            if (info != "1" || strOut != "")
            {
                MessageBox.Show(strOut);

                return;
            }
            else
            {
                MessageBox.Show("计划制定成功!!!!");
                QueryPlan();
            }
               
        }

        private void gd_Plan_AfterRowActivate(object sender, EventArgs e)
        {
            Dte_PlanTime.Value = Comm.ObjToStr(this.gd_Plan.ActiveRow.Cells["INCEPTDATE"].Value);
            qBuyer.Value = Comm.ObjToStr(this.gd_Plan.ActiveRow.Cells["BUYERCODE"].Value);
            qIncep.Value = Comm.ObjToStr(this.gd_Plan.ActiveRow.Cells["INCEPTCORPCODE"].Value);
            uCob_LX.Value = Comm.ObjToStr(this.gd_Plan.ActiveRow.Cells["PL"].Value);
            txt_Weigth.Text = Comm.ObjToStr(this.gd_Plan.ActiveRow.Cells["WEIGHT"].Value);
            txt_Pactno.Text = Comm.ObjToStr(this.gd_Plan.ActiveRow.Cells["PACTNO"].Value);
            txt_CH.Text = Comm.ObjToStr(this.gd_Plan.ActiveRow.Cells["TRANSNO"].Value);
            txt_Remark.Text = Comm.ObjToStr(this.gd_Plan.ActiveRow.Cells["REMARK"].Value);
            txt_Askid.Text = Comm.ObjToStr(this.gd_Plan.ActiveRow.Cells["ASKPLAN_ID"].Value);
        }

        private void ultraGrid2_AfterRowActivate(object sender, EventArgs e)
        {
            if (this.ultraGrid2.Rows.Count > 0)
            {
                uCob_LX.Value = Comm.ObjToStr(this.ultraGrid2.ActiveRow.Cells["PRODUCT_LX"].Value);
                qBuyer.Value = Comm.ObjToStr(this.ultraGrid2.ActiveRow.Cells["BUYERCODE"].Value);
                qIncep.Value = Comm.ObjToStr(this.ultraGrid2.ActiveRow.Cells["INCEPECODE"].Value);
                txt_Pactno.Text = Comm.ObjToStr(this.ultraGrid2.ActiveRow.Cells["PACTNO"].Value);
            }
        }
    }
}