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.Win.UltraWinGrid;
using Infragistics.Shared;

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

        #region "查询区域"

        private void Query()
        {
            string areaCode = "";
            string areaName = "";
            string areaType = "";
            string strOut = "";

            if (this.ultxt_AreaID.Enabled && this.ultxt_AreaID.Text.Trim().Length > 0)
            {
                areaCode = this.ultxt_AreaID.Text.Trim();
            }
            if (this.ultxt_AreaName.Enabled && this.ultxt_AreaName.Text.Trim().Length > 0)
            {
                areaName = this.ultxt_AreaName.Text.Trim();
            }
            if (this.ucob_kAreas.Enabled && this.ucob_kAreas.Value.ToString().Trim().Length > 0)
            {
                areaType = ucob_kAreas.Value.ToString().Trim();
            }
           
            
            try
            {
                this.ds_Area.Tables["KCJ_AREA"].Clear();
                DataSet ds = (DataSet)Core.Mes.ClientFrameWork.ClientCommon._RemotingHelp.ExecuteMethod("BJZWStuff", "Core.XgMes.Server.JGKC.SteelPlateManager.SetArea",
                            "QueryAreaInfo", new object[] { areaCode, areaName, areaType }, out strOut);

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

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

            }

        }

        #endregion

        #region "新增区域信息"

        private void AddRecode()
        {
            string strOut = "";
            string areaCode = "";
            string areaName = "";
            string areaType = "";

            try
            {
                System.Data.DataTable dt = this.ds_Area.Tables["KCJ_AREA"].Clone();
                System.Data.DataRow row = dt.NewRow();
                row["Creator"] = Core.Mes.ClientFrameWork.ClientCommon._UserInfo.UserName;
                row["LastEditor"] = Core.Mes.ClientFrameWork.ClientCommon._UserInfo.UserName;
                //				row["AreaCode"] = this.ultxt_AreaCodeEdit.Text.Trim();
                row["AreaName"] = this.ultxt_AreaNameEdit.Text.Trim();
                row["AreaExplain"] = this.ultxt_AreaRemark.Text.Trim();
                row["AreaType"] = ulcob_Karea.Value.ToString();

                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.SetArea",
                    "AddOrUpdateArea", new object[] { "add", ds }, out strOut).ToString();
                if (info == "1")
                {
                    MessageBox.Show("新增成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    try
                    {
                        areaName = this.ultxt_AreaNameEdit.Text.Trim();
                        areaType = "0";

                        DataSet dstb = (DataSet)Core.Mes.ClientFrameWork.ClientCommon._RemotingHelp.ExecuteMethod("BJZWStuff", "Core.XgMes.Server.JGKC.SteelPlateManager.SetArea",
                            "QueryAreaInfo", new object[] { areaCode, areaName, areaType }, out strOut);

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

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

                    }
                    catch (System.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 strOut = "";
            string areaCode = "";
            string areaName = "";
            string areaType = "";

            try
            {
                System.Data.DataTable dt = this.ds_Area.Tables["KCJ_AREA"].Clone();
                System.Data.DataRow row = dt.NewRow();
                row["Creator"] = Core.Mes.ClientFrameWork.ClientCommon._UserInfo.UserName;
                row["LastEditor"] = Core.Mes.ClientFrameWork.ClientCommon._UserInfo.UserName;
                row["AreaCode"] = this.ultxt_AreaCodeEdit.Text.Trim();
                row["AreaName"] = this.ultxt_AreaNameEdit.Text.Trim();
                row["AreaExplain"] = this.ultxt_AreaRemark.Text.Trim();
                row["AreaType"] = ulcob_Karea.Value.ToString();

                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.SetArea",
                    "AddOrUpdateArea", new object[] { "update", ds }, out strOut).ToString();

                if (info == "1")
                {
                    MessageBox.Show("修改成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    try
                    {
                        areaCode = this.ultxt_AreaCodeEdit.Text.Trim();
                        areaType = "0";

                        DataSet dstb = (DataSet)Core.Mes.ClientFrameWork.ClientCommon._RemotingHelp.ExecuteMethod("BJZWStuff", "Core.XgMes.Server.JGKC.SteelPlateManager.SetArea",
                            "QueryAreaInfo", new object[] { areaCode, areaName, areaType }, out strOut);

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

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

                    }
                    catch (System.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 ulck_AreaID_CheckedChanged(object sender, System.EventArgs e)
        {
            this.ultxt_AreaID.Enabled = this.ulck_AreaID.Checked;
            if (!this.ulck_AreaID.Checked)
            {
                this.ultxt_AreaID.Clear();
            }

        }

        private void ulck_AreaName_CheckedChanged(object sender, System.EventArgs e)
        {
            this.ultxt_AreaName.Enabled = this.ulck_AreaName.Checked;
            if (!this.ulck_AreaName.Checked)
            {
                this.ultxt_AreaName.Clear();
            }

        }

        //激活行后动作
        private void ulgrid_Area_AfterRowActivate(object sender, System.EventArgs e)
        {

            try
            {
                if (ulgrid_Area.Rows.Count > 0)
                {
                    this.ultxt_AreaCodeEdit.Text = Comm.ObjToStr(this.ulgrid_Area.ActiveRow.Cells["AREACODE"].Value);
                    this.ultxt_AreaNameEdit.Text = Comm.ObjToStr(this.ulgrid_Area.ActiveRow.Cells["AREANAME"].Value);
                    this.ultxt_AreaRemark.Text = Comm.ObjToStr(this.ulgrid_Area.ActiveRow.Cells["AreaExplain"].Value);
                    this.ulcob_Karea.Value = Comm.ObjToStr(this.ulgrid_Area.ActiveRow.Cells["AREATYPE"].Value);

                }
            }
            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":
                    this.AddRecode();
                    break;

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

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

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

        private void FrmSetArea_Load(object sender, EventArgs e)
        {
            ulcob_Karea.Value = "0";
           
        }


        private void chk_Karea_CheckedChanged(object sender, EventArgs e)
        {
            ucob_kAreas.Enabled = chk_Karea.Checked;
        }
    }
}