using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DBFactory;
using Microsoft.VisualBasic;
namespace SystemConfig
{
    public partial class FrmRouteEdit : Form
    {
        bool _IfUpdate = false;
        DBOperator dbo = CStaticClass.dbo;
        DBOperator dboMan = new DBOperator("ManConnString", "ManDBFactory");
        private static FrmRouteEdit _formInstance;
        string CommSQL = "SELECT F_RouteID AS 路径索引, F_RouteKind AS 路径类型," +
                " F_RouteName AS 路径名称,F_StartDevice as 起点设备,F_EndDevice as 终点设备,"+
            "F_OutsideAltDevice as 巷道外可变向设备索引,F_InsideAltDevice as 巷道内可变向设备索引,f_status as 路径状态 FROM T_Base_Route ";
        public static FrmRouteEdit FormInstance
        {
            get 
            {
                if (_formInstance==null)
                {
                    _formInstance = new FrmRouteEdit();
                }
                return FrmRouteEdit._formInstance;
            }
            set { FrmRouteEdit._formInstance = value; }
        }
        public FrmRouteEdit()
        {
            InitializeComponent();
            _formInstance = this;
            dbo.Open();
            DataSet ds1;
            ds1 = dbo.ExceSQL("select * from t_base_device");
            if (ds1.Tables[0].DefaultView.Count > 0)
            {
                //cbstartposition
                cbstartposition.ValueMember = "F_DeviceIndex";
                cbstartposition.DisplayMember = "F_DeviceName";
                cbstartposition.DataSource = ds1.Tables[0].DefaultView;

            }
            DataSet ds2;
            ds2 = dbo.ExceSQL("select * from t_base_device");
            if (ds2.Tables[0].DefaultView.Count > 0)
            {

                //cbendposition
                cbendposition.ValueMember = "F_DeviceIndex";
                cbendposition.DisplayMember = "F_DeviceName";
                cbendposition.DataSource = ds2.Tables[0].DefaultView;
            }
            DataSet ds3;
            ds3 = dbo.ExceSQL("select * from t_base_device where F_DeviceKindIndex<=10");
            if (ds3.Tables[0].DefaultView.Count > 0)
            {

                //comboBox1 
                comboBox1.ValueMember = "F_DeviceIndex";
                comboBox1.DisplayMember = "F_DeviceName";
                comboBox1.DataSource = ds3.Tables[0].DefaultView;
            }
        }

        private void btNull_Click(object sender, EventArgs e)
        {
            this.tbRouteKindIndex.Text = "";
            this.tbRouteName.Text = "";
            this.cbRouteKind.SelectedIndex=2;
            this.cbstartposition.Text = "";
            this.cbendposition.Text="";
        }

        private void btQuery_Click(object sender, EventArgs e)
        {
            if (tbContent.Text.Trim() == "") return;
            try
            {
                string df;
                string sql = "";
                switch (this.cbField.Text)
                {


                    case "路径索引":
                        df = "F_RouteID";

                        break;
                    case "路径类型":
                        df = "F_RouteKind";
                        break;
                    
                    case "路径名称":
                        df = "F_RouteName";
                        break;
                    default:
                        return;
                }
                sql =CommSQL+ " WHERE  " + df + " = '" + this.tbContent.Text + "'";
                DataSet ds = dbo.ExceSQL(sql);
                this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void btNullQuery_Click(object sender, EventArgs e)
        {
            this.cbField.Text = "";
            this.tbContent.Text = "";
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确认要保存路径信息吗？", "操作提示：", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
            {
                return;
            }

            if (Information.IsNumeric(this.tbRouteKindIndex.Text) == false)
            {
                MessageBox.Show("路径索引只能是数字类型！", "误操作提示：", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.tbRouteKindIndex.Focus();
                return;
            }
            if (this.cbRouteKind.Text.Trim().Length  ==0)
            {
                MessageBox.Show("路径类型不能输入空值！", "误操作提示：", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.cbRouteKind.Focus();
                return;
            }
            
            if (this.tbRouteName.Text.Trim().Length == 0)
            {
                MessageBox.Show("路径名称不能输入空值！", "误操作提示：", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.tbRouteName.Focus();
                return;
            }
            if (this.cbstartposition.Text.Trim().Length == 0)
            {
                MessageBox.Show("起点设备不能输入空值！", "误操作提示：", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.cbstartposition.Focus();
                return;
            }
            if (this.cbendposition.Text.Trim().Length == 0)
            {
                MessageBox.Show("终点设备不能输入空值！", "误操作提示：", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.cbendposition.Focus();
                return;
            }



            int ki;
            if (this.cbRouteKind.Text == "入库")
            {
                ki = 1;
            }
            else if (this.cbRouteKind.Text == "出库")
            {
                ki = 2;
            }
            else if (this.cbRouteKind.Text == "倒库")//
            {
                if ((tbInsideAltDevice.Text == "0") && (tbOutsideAltDevice.Text == "0"))
                {
                    MessageBox.Show("路径类型是“倒库”时，必须输入一个合法的出入库可变更方向的设备，例如ＲＧＶ或者堆垛机！", "误操作提示：", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.tbInsideAltDevice.Focus();
                    return;
                }
                ki = 3;
            }
            else if (this.cbRouteKind.Text == "库外货物搬运")//
            {
                ki = 9;
            }
            else
            {
                MessageBox.Show("路径类型不是允许范围值！", "误操作提示：", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.cbRouteKind.Focus();
                return;
            }
            string sql;
            DataSet ds;
            if (_IfUpdate == true)
            {//20100208
                sql = "update T_Base_Route  set  F_RouteKind=" + ki + ",F_StartDevice=" + Convert.ToInt32(this.cbstartposition.SelectedValue) + ",F_EndDevice=" + Convert.ToInt32(this.cbendposition.SelectedValue) + "," +
                    "  F_RouteName='" + this.tbRouteName.Text + "',F_InsideAltDevice=" + tbInsideAltDevice.Text + ",F_OutsideAltDevice=" + tbOutsideAltDevice.Text + ",f_status="+tbStatus.Text+" where F_RouteID=" + Convert.ToInt32(this.tbRouteKindIndex.Text);
                dbo.ExceSQL(sql);
                MessageBox.Show("路径信息修改成功！", "操作提示：", MessageBoxButtons.OK, MessageBoxIcon.Information);
                _IfUpdate = false;
            }
            else
            {
                sql = CommSQL + " WHERE  F_RouteID =" + Convert.ToInt32(this.tbRouteKindIndex.Text);
                ds = dbo.ExceSQL(sql);
                if (ds.Tables[0].DefaultView.Count > 0)
                {
                    MessageBox.Show("您输入的路径索引重复！", "误操作提示：", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.tbRouteKindIndex.Focus();
                    return;
                }
                ds.Clear();
                 sql = "INSERT INTO T_Base_Route (F_RouteID,F_RouteKind," +
                    "F_RouteName,F_StartDevice,F_EndDevice,F_InsideAltDevice,F_OutsideAltDevice,f_status)VALUES (" + Convert.ToInt32(this.tbRouteKindIndex.Text) +
                    "," + ki + ",'" + this.tbRouteName.Text + "'," + Convert.ToInt32(this.cbstartposition.SelectedValue) + "," +
                    Convert.ToInt32(this.cbendposition.SelectedValue) + ","+tbInsideAltDevice.Text+","+tbOutsideAltDevice.Text+","+tbStatus.Text+")";
                dbo.ExceSQL(sql);
                MessageBox.Show("路径信息录入成功！", "操作提示：", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            sql = CommSQL + " WHERE  F_RouteID =" + Convert.ToInt32(this.tbRouteKindIndex.Text);
            ds = dbo.ExceSQL(sql);
            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
        }

        private void btBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btSelectOK_Click(object sender, EventArgs e)
        {

        }

        private void tsmEdit_Click(object sender, EventArgs e)
        {
            if (dataGridView1.RowCount <= 0)
            {
                return;
            }

            this.tabControl1.SelectTab("tabPage1");
            this.tbRouteKindIndex.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString() + "";
            if (this.dataGridView1.CurrentRow.Cells[1].Value.ToString() == "1")
            {
                this.cbRouteKind.SelectedIndex = 0;
            }
            else if (this.dataGridView1.CurrentRow.Cells[1].Value.ToString() == "2")
            {
                this.cbRouteKind.SelectedIndex = 1;
            }
            else
            {
                this.cbRouteKind.SelectedIndex = 2;
            }
            this.tbRouteName.Text = this.dataGridView1.CurrentRow.Cells[2].Value.ToString() + "";
            this.cbstartposition.SelectedValue = this.dataGridView1.CurrentRow.Cells[3].Value.ToString() + "";
            this.cbendposition.SelectedValue = this.dataGridView1.CurrentRow.Cells[4].Value.ToString() + "";
            //20100208
            this.tbOutsideAltDevice.Text = this.dataGridView1.CurrentRow.Cells[5].Value.ToString() + "";
            this.tbInsideAltDevice.Text = this.dataGridView1.CurrentRow.Cells[6].Value.ToString() + "";
            this.tbStatus.Text = this.dataGridView1.CurrentRow.Cells[7].Value.ToString() + "";


            _IfUpdate = true;
        }

        private void tsmDel_Click(object sender, EventArgs e)
        {
            if (dataGridView1.RowCount <= 0)
            {
                return;
            }
            if (MessageBox.Show("您确认要删除选中行的“路径索引：" + this.dataGridView1.CurrentRow.Cells[0].Value.ToString() + "”信息吗？", "操作提示：", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
            {
                return;
            }
            dbo.ExceSQL("delete from T_Base_Route  where F_RouteID =" + Convert.ToInt32(this.dataGridView1.CurrentRow.Cells[0].Value));
            button2_Click(sender, e);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            _IfUpdate = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string sql = CommSQL + "";
            DataSet ds = dbo.ExceSQL(sql);
            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
        }

        private void FrmRouteEdit_Load(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            //20100208设置设备所属路径不可用
            if (MessageBox.Show("您确认要设置设备"+this.comboBox1.SelectedValue.ToString()+"所属路径状态不可用吗？", "操作提示：", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
            {
                return;
            }
            try
            {
                DataView dv = dbo.ExceSQL("select distinct F_RouteID from T_Base_Route_Device where F_DeviceIndex=" + this.comboBox1.SelectedValue).Tables[0].DefaultView;
                for (int i = 0; i < dv.Count; i++)
                {
                    dbo.ExecuteSql("update t_base_route set F_Status=0 where f_routeid=" + dv[i]["F_RouteID"]);
                    dboMan.ExceSQL("update IO_CONTROL_ROUTE set CONTROL_ROUTE_STATUS=0 where CONTROL_ROUTE_ID=" + dv[i]["F_RouteID"]);
                        
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        private void button4_Click(object sender, EventArgs e)
        {
            //20100208设置设备所属路径可用
            if (MessageBox.Show("您确认要设置设备" + this.comboBox1.SelectedValue.ToString() + "所属路径状态可用吗？", "操作提示：", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
            {
                return;
            }
            try
            {
                DataView dv = dbo.ExceSQL("select distinct F_RouteID from T_Base_Route_Device where F_DeviceIndex=" + this.comboBox1.SelectedValue).Tables[0].DefaultView;
                for (int i = 0; i < dv.Count; i++)
                {
                    dbo.ExecuteSql("update t_base_route set F_Status=1 where f_routeid=" + dv[i]["F_RouteID"]);
                    dboMan.ExceSQL("update IO_CONTROL_ROUTE set CONTROL_ROUTE_STATUS=1 where CONTROL_ROUTE_ID=" + dv[i]["F_RouteID"]);

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}