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 wcfControlMonitorClient
{
    /// <summary>
    /// Creator:Richard.liu
    /// 路径状态维护
    /// </summary>
    public partial class FrmRouteEdit : Form
    {
        
        DBOperator dbo = CStaticClass.dbo;
        DBOperator dboM = CStaticClass.dboM;
        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)
        {
            int a = 0;
            if (MessageBox.Show("您确认要保存路径信息吗?", "操作提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
            {
                return;
            }

            if (int.TryParse(this.tbRouteKindIndex.Text,out a) == 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;
            
            
            Model.MRoute mr=new Model.MRoute();
            mr.RouteID=Convert.ToInt32(this.tbRouteKindIndex.Text);
            mr.Status=Convert.ToInt32(tbStatus.Text);
            
            CStaticClass.WcfControl.BeginSetRouteState(mr, new AsyncCallback(SetRouteStateCallBack), null);

            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
            {
                Model.MRoute mr=new Model.MRoute();
                string errtext = string.Empty;
                
                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++)
                {
                    mr.RouteID=Convert.ToInt32(dv[i]["F_RouteID"]);
                    mr.Status=status;
                    CStaticClass.WcfControl.BeginSetRouteState(mr, new AsyncCallback( SetRouteStateCallBack),null );//20120513
                    
                        
                }
            }
            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.ColumnIndex >= 0) && (e.RowIndex >= 0) && (e.Button == MouseButtons.Right))//20110309
            {
                dataGridView1.ClearSelection();
                dataGridView1.Rows[e.RowIndex].Selected = true;
                dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];

            }
        }
        void SetRouteStateCallBack(IAsyncResult ar)
        {
            string errtext = string.Empty;
            
            if (CStaticClass.WcfControl.EndSetRouteState(out errtext, ar) == false)
            {
                MessageBox.Show(errtext, "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void FrmRouteEdit_FormClosing(object sender, FormClosingEventArgs e)
        {
            _formInstance = null;
        }

        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 = dboM.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;
            }
        }
        

        
    }
}