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 ControlSystem
{
    /// <summary>
    /// Creator:Richard.liu
    /// 路径状态维护
    /// </summary>
    public partial class FrmRouteEdit : Form
    {
        
        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 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 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;
            }
            
            
            string sql;
            DataSet ds;
            
            sql = "update T_Base_Route  set  f_status="+tbStatus.Text+" where F_RouteID=" + Convert.ToInt32(this.tbRouteKindIndex.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() + "";


        }

        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 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)
        {
            int status = 0;
            string cap = string.Empty;
            if (this.radioButton1.Checked == true)
            {
                status = 0;
                cap = radioButton1.Text;
            }
            else
            {
                status = 1;
                cap = radioButton2.Text;
            }
            //20100208设置设备所属路径是否可用
            if (MessageBox.Show("您确认要设置设备"+this.comboBox1.SelectedValue.ToString()+cap +"吗？", "操作提示：", 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="+status+" where f_routeid=" + dv[i]["F_RouteID"]);
                    dboMan.ExceSQL("update IO_CONTROL_ROUTE set CONTROL_ROUTE_STATUS=" + status + " where CONTROL_ROUTE_ID=" + dv[i]["F_RouteID"]);
                        
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        private void cbField_SelectedIndexChanged(object sender, EventArgs e)
        {
            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 = " select distinct   " + df + " from T_Base_Route";
                DataView dv = dbo.ExceSQL(sql).Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    tbContent.DataSource = null;
                    tbContent.DisplayMember = df;
                    tbContent.ValueMember=df;
                    tbContent.DataSource = dv;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if ((e.RowIndex >= 0) && (e.Button == MouseButtons.Right))
            {
                dataGridView1.ClearSelection();
                dataGridView1.Rows[e.RowIndex].Selected = true;
                dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];

            }
        }

        private void BtSyncRoute_Click(object sender, EventArgs e)
        {
            try
            {
                int syncnum = 0;
                string mes = string.Empty;
                DataView dv = dbo.ExceSQL("select  F_RouteID,F_Status from T_Base_Route").Tables[0].DefaultView;
                for (int i = 0,j=0; i < dv.Count; i++)
                {
                  j=dboMan.ExecuteSql("update IO_CONTROL_ROUTE set CONTROL_ROUTE_STATUS=" + dv[i]["F_Status"] + " where CONTROL_ROUTE_ID=" + dv[i]["F_RouteID"]+ " and CONTROL_ROUTE_STATUS <> " + dv[i]["F_Status"] );
                        if(j>0)
                        {
                            syncnum++;
                        }
                }

                  mes = string.Format("同步管理系统所有路径状态完成,共更新路径状态{0}条!",syncnum);

                 MessageBox.Show(mes, "操作提示：", MessageBoxButtons.OK, MessageBoxIcon.Information);
            
            }
            
                
            catch (Exception ex)
            {
                throw ex;
            }
        }

        

        
    }
}