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;

namespace Core.XgMes.Client.JGKC.SteelPlateManager
{
    public partial class FrmSetButtress : Core.Mes.ClientFrameWork.FrmBase
    {
        public FrmSetButtress()
        {
            InitializeComponent();
        }
        private void ulck_Area_CheckedChanged(object sender, System.EventArgs e)
        {
            this.ulcob_Area.Enabled = this.ulck_Area.Checked;

        }

        private void ulbut_Clear_Click(object sender, System.EventArgs e)
        {
            this.ultxt_ButressName.Clear();
            this.ultxt_ButressRemark.Clear();

        }

        private void ulgrid_Butress_AfterRowActivate(object sender, System.EventArgs e)
        {
            try
            {
                if (ulgrid_Butress.Rows.Count > 0)
                {
                    this.ultxt_ButressName.Text = Comm.ObjToStr(this.ulgrid_Butress.ActiveRow.Cells["BUTTRESS"].Value);
                    this.ulcob_AreaEdit.Value = Comm.ObjToStr(this.ulgrid_Butress.ActiveRow.Cells["AREACODE"].Value);
                    this.ulcob_ButressSort.Value = Comm.ObjToStr(this.ulgrid_Butress.ActiveRow.Cells["BUTTRESSKIND"].Value);
                    this.ultxt_ButressRemark.Text = Comm.ObjToStr(this.ulgrid_Butress.ActiveRow.Cells["BUTTRESSEXPLAIN"].Value);
                    this.ultxt_buttressCode.Text = Comm.ObjToStr(this.ulgrid_Butress.ActiveRow.Cells["BUTTRESSCODE"].Value);
                    this.uCob_kButreess.Value = Comm.ObjToStr(this.ulgrid_Butress.ActiveRow.Cells["BUTTRESSTYPE"].Value);


                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }

        }
      
        private void FrmSetButtress_Load(object sender, System.EventArgs e)
        {
            string strOut = "";
            string outStr = "";
            uCob_kButreess.Value = "0";
            ArrayList al = new ArrayList();

            try
            {

                string strSql = "";

                strSql = "select ID_,NAME_  from KCJ_BASEDATA_ZW  where SORT_CODE = '8002' ";

                DataSet ds_ = (DataSet)ClientCommon._RemotingHelp.ExecuteMethod("BJZWStuff", "Core.XgMes.Server.JGKC.SteelPlateManager.Common", "GetDataSet", new object[] { strSql }, out strOut);
                ds_.Tables[0].TableName = "KCJ_BASEDATA";

                Comm.Init_ComboControl(this.ulcob_ButressSort, "KCJ_BASEDATA", "NAME_", "ID_", ref ds_);


                strSql = "select ID_,NAME_  from KCJ_BASEDATA_ZW  where SORT_CODE = '8001' ";

               

                al = (ArrayList)ClientCommon._RemotingHelp.ExecuteMethod("BJZWStuff", "Core.XgMes.Server.JGKC.SteelPlateManager.Common", "GetAreaCode", new object[] { "0" }, out outStr);
                if (al.Count != 0 && outStr == "")
                {
                    for (int i = 0; i < al.Count; i++)
                    {
                        this.ulcob_Area.Items.Add(Convert.ToString(al[i]));
                        this.ulcob_AreaEdit.Items.Add(Convert.ToString(al[i]));
                    }
                }

                //GRID 列绑定DropDownList
                string sqlstr = "";

                sqlstr = "SELECT ID_ FLBM,NAME_ FLMC FROM KCJ_BASEDATA_ZW WHERE SORT_CODE = '8002'";

                DataSet dstb = (DataSet)Core.Mes.ClientFrameWork.ClientCommon._RemotingHelp.ExecuteMethod("BJZWStuff", "Core.XgMes.Server.JGKC.SteelPlateManager.Common",
                    "GetDataSet", new object[] { sqlstr }, out strOut);
                Comm.SetGridDropDownList(this.ulgrid_Butress, dstb, "KCJ_BUTTRESS", "BUTTRESSKIND", "FLMC", "FLBM");

                //初始化GRID
                this.Query();

            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }


        }

        private void ultraToolbarsManager1_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
        {
            switch (e.Tool.Key)
            {
                case "Add":
                    if (Comm.ObjToStr(this.ulcob_ButressSort.Value) == "")
                    {
                        MessageBox.Show("请选择垛位类型!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    this.AddRecode();
                    break;

                case "Rework":
                    this.updateRecode();
                    break;

                case "Query":
                    this.Query();
                    break;

                case "Close":
                    Comm.CloseForm(this);
                    break;
            }

        }

        #region "查询"
        private void Query()
        {
            string areaCode = "";
            string buttressCode = "";
            string areaType = "";
            string strOut = "";

            try
            {
                int t = this.ulcob_Area.SelectedIndex;

                if (this.ulcob_Area.Enabled && this.ulcob_Area.SelectedIndex >= 0)
                {
                    areaCode = this.ulcob_Area.Value.ToString();
                }
                if (this.uCob_Butress.Enabled && this.uCob_Butress.SelectedIndex >= 0)
                {
                    areaType = this.uCob_Butress.Value.ToString();
                }
                this.ds_Butress.Tables["KCJ_BUTTRESS"].Clear();
                string strRole = ClientCommon._UserInfo.UserRoleID.ToString();

                DataSet ds = (DataSet)Core.Mes.ClientFrameWork.ClientCommon._RemotingHelp.ExecuteMethod("BJZWStuff", "Core.XgMes.Server.JGKC.SteelPlateManager.SetButtress",
                    "QueryButtress", new object[] { buttressCode, areaCode, areaType }, out strOut);

                if (strOut == "")
                {
                    this.ds_Butress.Merge(ds);
                    this.ds_Butress.Tables["KCJ_BUTTRESS"].AcceptChanges();
                    this.ulgrid_Butress.UpdateData();//++++
                }
                else
                {
                    MessageBox.Show(strOut, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }

            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());

            }

        }

        #endregion

        #region "新增垛位"
        private void AddRecode()
        {
            string buttressCode = "";
            string areaCode = "";
            string strOut = "";

            try
            {
                System.Data.DataTable dt = this.ds_Butress.Tables["KCJ_BUTTRESS"].Clone();
                System.Data.DataRow row = dt.NewRow();
                row["Creator"] = Core.Mes.ClientFrameWork.ClientCommon._UserInfo.UserName;
                row["LastEditor"] = Core.Mes.ClientFrameWork.ClientCommon._UserInfo.UserName;
                row["ButtressType"] = uCob_kButreess.Value.ToString();//垛位类型0：原料 1：成品
                row["Buttress"] = this.ultxt_ButressName.Text.Trim();//垛位名
                row["ButtressExplain"] = this.ultxt_ButressRemark.Text.Trim();//垛位说明
                row["AreaCode"] = this.ulcob_AreaEdit.Value.ToString();
                row["ButtressKind"] = Comm.ObjToStr(this.ulcob_ButressSort.Value);

                dt.Rows.Add(row);
                DataSet ds = new DataSet();//参数
                ds.Merge(dt);
                ds.AcceptChanges();


                string info = Core.Mes.ClientFrameWork.ClientCommon._RemotingHelp.ExecuteMethod("BJZWStuff", "Core.XgMes.Server.JGKC.SteelPlateManager.SetButtress",
                    "AddOrUpdateButtress", new object[] { "add", ds }, out strOut).ToString();
                if (info == "1")
                {
                    MessageBox.Show("新增成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    try
                    {
                        buttressCode = this.ultxt_ButressName.Text.Trim();
                        DataSet dstb = (DataSet)Core.Mes.ClientFrameWork.ClientCommon._RemotingHelp.ExecuteMethod("BJZWStuff", "Core.XgMes.Server.JGKC.SteelPlateManager.SetButtress",
                            "QueryButtress", new object[] { buttressCode, areaCode, uCob_kButreess.Value.ToString() }, out strOut);

                        if (strOut == "")
                        {
                            dstb.Tables[0].TableName = "KCJ_BUTTRESS";
                            this.ds_Butress.Clear();
                            this.ds_Butress.Merge(dstb);
                            this.ds_Butress.Tables["KCJ_AREA"].AcceptChanges();

                        }
                        else
                        {
                            MessageBox.Show(strOut, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }

                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.WriteLine(ex.ToString());
                    }


                }
                else if (info.Length == 0)
                {
                    MessageBox.Show("编辑区域填写的信息有错误，新增失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    MessageBox.Show(info, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }

            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }

        }

        #endregion

        #region "更新垛位"
        private void updateRecode()
        {

            string buttressCode = "";
            string areaCode = "";
            string strOut = "";

            try
            {
                System.Data.DataTable dt = this.ds_Butress.Tables["KCJ_BUTTRESS"].Clone();
                System.Data.DataRow row = dt.NewRow();
                //				row["Creator"] = Core.Mes.ClientFrameWork.ClientCommon._UserInfo.UserName;
                row["LastEditor"] = Core.Mes.ClientFrameWork.ClientCommon._UserInfo.UserName;
                row["ButtressType"] = uCob_kButreess.Value.ToString();//垛位类型0：原料 1：成品
                row["Buttress"] = this.ultxt_ButressName.Text.Trim();//垛位名
                row["ButtressCode"] = this.ultxt_buttressCode.Text.Trim();//垛位编码
                row["ButtressExplain"] = this.ultxt_ButressRemark.Text.Trim();//垛位说明
                row["AreaCode"] = this.ulcob_AreaEdit.Value.ToString();
                row["ButtressKind"] = Comm.ObjToStr(this.ulcob_ButressSort.Value);

                dt.Rows.Add(row);
                DataSet ds = new DataSet();//参数
                ds.Merge(dt);
                ds.AcceptChanges();


                string info = Core.Mes.ClientFrameWork.ClientCommon._RemotingHelp.ExecuteMethod("BJZWStuff", "Core.XgMes.Server.JGKC.SteelPlateManager.SetButtress",
                    "AddOrUpdateButtress", new object[] { "update", ds }, out strOut).ToString();
                if (info == "1")
                {
                    MessageBox.Show("修改成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    try
                    {
                        buttressCode = this.ultxt_buttressCode.Text.Trim();
                        DataSet dstb = (DataSet)Core.Mes.ClientFrameWork.ClientCommon._RemotingHelp.ExecuteMethod("BJZWStuff", "Core.XgMes.Server.JGKC.SteelPlateManager.SetButtress",
                            "QueryButtress", new object[] { buttressCode, areaCode, uCob_kButreess.Value.ToString() }, out strOut);

                        if (strOut == "")
                        {
                            dstb.Tables[0].TableName = "KCJ_BUTTRESS";
                            this.ds_Butress.Clear();
                            this.ds_Butress.Merge(dstb);
                            this.ds_Butress.Tables["KCJ_AREA"].AcceptChanges();

                        }
                        else
                        {
                            MessageBox.Show(strOut, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }

                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.WriteLine(ex.ToString());
                    }


                }
                else if (info.Length == 0)
                {
                    MessageBox.Show("编辑区域填写的信息有错误，新增失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    MessageBox.Show(info, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }

            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }




        }
        #endregion

        private void chk_kButress_CheckedChanged(object sender, EventArgs e)
        {
            uCob_Butress.Enabled = chk_kButress.Checked;
        }

       
    }
}