using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DBFactory;
namespace wcfControlMonitorClient
{
    /// <summary>
    /// 20101011新增加出入库站台属性定义
    /// </summary>
    public partial class FrmIOStation : Form
    {
        DBOperator dbo = CStaticClass.dbo;
        DBOperator dboM = CStaticClass.dboM;
        private static FrmIOStation _formInstance;

        public static FrmIOStation FormInstance
        {
            get
            {
                if (_formInstance == null)
                {
                    _formInstance = new FrmIOStation();
                }
                return _formInstance;
            }
            set { _formInstance = value; }
        }
        public FrmIOStation()
        {
            InitializeComponent();
            DataView dv = dbo.ExceSQL("select Device_Index,Device_Name,InOut_Value from T_InOutModel").Tables[0].DefaultView;
            if (dv.Count > 0)
            {
                this.comboBox1.DisplayMember = "Device_Name";
                this.comboBox1.ValueMember = "Device_Index";
                this.comboBox1.DataSource = dv;
            }
            _formInstance = this;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.comboBox1.SelectedValue == null) 
            {
                MessageBox.Show("请选择出入库站台!","操作提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                this.comboBox1.Focus();
                return;
            }
            if (this.comboBox2.Text.Trim()=="")
            {
                MessageBox.Show("请选择出入库属性!", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.comboBox2.Focus();
                return;
            }
            if ("出库站台入库站台".IndexOf(this.comboBox2.Text.Trim()) < 0)
            {
                MessageBox.Show("请选择出入库属性!", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.comboBox2.Focus();
                return;
            }
           // string iotype = " (T_Base_Route.F_RouteKind <> 0) ";
            if (this.comboBox2.Text.Trim() == "入库站台")
            {
               // iotype = " (T_Base_Route.F_RouteKind = 1) ";
                dbo.ExecuteSql("update T_InOutModel set InOut_Value=1  where Device_Index=" + this.comboBox1.SelectedValue);
                //   dboM.ExceSQL("update IO_CONTROL_ROUTE set CONTROL_ROUTE_STATUS=1 where START_DEVICE=" + this.comboBox1.SelectedValue);
                //   dbo.ExecuteSql("update t_base_route set F_Status=0,F_AutoUpdate=0  where F_EndDevice=" + this.comboBox1.SelectedValue);
                ////   dboM.ExceSQL("update IO_CONTROL_ROUTE set CONTROL_ROUTE_STATUS=0 where END_DEVICE=" + this.comboBox1.SelectedValue);
                MessageBox.Show($"修改{this.comboBox1.SelectedValue}站台入库模式成功");
            }
            else 
            {
                //iotype = " (T_Base_Route.F_RouteKind = 2) ";
                dbo.ExecuteSql("update T_InOutModel set InOut_Value=2  where Device_Index=" + this.comboBox1.SelectedValue);
                MessageBox.Show($"修改{this.comboBox1.SelectedValue}站台出库模式成功");
                //  dboM.ExceSQL("update IO_CONTROL_ROUTE set CONTROL_ROUTE_STATUS=1 where END_DEVICE=" + this.comboBox1.SelectedValue);
              //  dbo.ExecuteSql("update t_base_route set F_Status=0,F_AutoUpdate=0  where F_StartDevice=" + this.comboBox1.SelectedValue);
              //  dboM.ExceSQL("update IO_CONTROL_ROUTE set CONTROL_ROUTE_STATUS=0 where START_DEVICE=" + this.comboBox1.SelectedValue);
            }
     //
            //DataView dv = dbo.ExceSQL("SELECT DISTINCT T_Base_Route.F_RouteID, T_Base_Route.F_RouteKind,  "+
            //    "T_Base_Route_Device.F_DeviceIndex FROM T_Base_Route,T_Base_Route_Device where "+
            //    "T_Base_Route.F_RouteID = T_Base_Route_Device.F_RouteID and "+iotype+
            //    " AND (T_Base_Route_Device.F_DeviceIndex = "+this.comboBox1.SelectedValue +")").Tables[0].DefaultView;
            //DataView dvf;
            //    for(int i=0;i<dv.Count;i++)
            //    {
            //        dbo.ExecuteSql("update t_base_route set F_Status=1,F_AutoUpdate=1  where f_routeid=" + dv[i]["F_RouteID"]);
            //        dboM.ExceSQL("update IO_CONTROL_ROUTE set CONTROL_ROUTE_STATUS=1 where CONTROL_ROUTE_ID='" + dv[i]["F_RouteID"] + "' and CONTROL_ROUTE_STATUS=0");
            //        if (dv[i]["F_RouteKind"].ToString() == "1")
            //        {
                        
            //            dvf = dbo.ExceSQL("SELECT DISTINCT T_Base_Route.F_RouteID, T_Base_Route.F_RouteKind,  " +
            //            "T_Base_Route_Device.F_DeviceIndex FROM T_Base_Route,T_Base_Route_Device where " +
            //            "T_Base_Route.F_RouteID = T_Base_Route_Device.F_RouteID and T_Base_Route.F_RouteKind = 2 " +
            //            " AND (T_Base_Route_Device.F_DeviceIndex = " + this.comboBox1.SelectedValue + ")").Tables[0].DefaultView;
            //            for (int f = 0; f < dvf.Count; f++)
            //            {
            //                dbo.ExecuteSql("update t_base_route set F_Status=0,F_AutoUpdate=0  where f_routeid=" + dvf[f]["F_RouteID"]);
            //              //  dboM.ExceSQL("update IO_CONTROL_ROUTE set CONTROL_ROUTE_STATUS=0 where CONTROL_ROUTE_ID='" + dvf[f]["F_RouteID"] + "' and CONTROL_ROUTE_STATUS=1");
            //            }
            //        }
            //        else if (dv[i]["F_RouteKind"].ToString() == "2")
            //        {
                        
            //            dvf = dbo.ExceSQL("SELECT DISTINCT T_Base_Route.F_RouteID, T_Base_Route.F_RouteKind,  " +
            //            "T_Base_Route_Device.F_DeviceIndex FROM T_Base_Route,T_Base_Route_Device where " +
            //            "T_Base_Route.F_RouteID = T_Base_Route_Device.F_RouteID and T_Base_Route.F_RouteKind = 1 " +
            //            " AND (T_Base_Route_Device.F_DeviceIndex = " + this.comboBox1.SelectedValue + ")").Tables[0].DefaultView;
            //            for (int f = 0; f < dvf.Count; f++)
            //            {
            //                dbo.ExecuteSql("update t_base_route set F_Status=0,F_AutoUpdate=0  where f_routeid=" + dvf[f]["F_RouteID"]);
            //                dboM.ExceSQL("update IO_CONTROL_ROUTE set CONTROL_ROUTE_STATUS=0 where CONTROL_ROUTE_ID='" + dvf[f]["F_RouteID"] + "' and CONTROL_ROUTE_STATUS=1");
            //            }
            //        }
                    

            //    }
        }

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


    }
}