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;
using System.Resources;
using System.Collections;
using System.ServiceModel;
namespace wcfControlMonitorClient
{
    /// <summary>
    /// Creator:Richard.liu
    /// 设备状态维护
    /// </summary>
    public partial class FrmDeviceInfoEdit : Form
    {
        string Commsql = "SELECT  T_Base_Device.F_DeviceIndex AS 设备索引, " +
                  " T_Base_Device.F_DeviceName AS 设备名称, " +
                  " T_Base_Device.F_DeviceKindIndex AS 设备类型索引, " +
                  " T_Base_Device_Kind.F_DeviceKindName AS 设备类型名称, " +
                  " F_LockedState AS 执行状态,F_errorcode as 设备状态, " +
                  " F_ManTaskReserve as 调度预约锁,F_HaveGoods as 是否逻辑有物" +
            " FROM  T_Base_Device,T_Base_Device_Kind";
        private bool _laneIndex = false;

        public bool LaneIndex
        {
            get { return _laneIndex; }
            set { _laneIndex = value; }
        }
        private bool _laneGateDeviceIndex = false;

        public bool LaneGateDeviceIndex
        {
            get { return _laneGateDeviceIndex; }
            set { _laneGateDeviceIndex = value; }
        }
        private bool _isSelected = false;

        public bool IsSelected
        {
            get { return _isSelected; }
            set { _isSelected = value; }
        }
        private static FrmDeviceInfoEdit _formInstance;

        public static FrmDeviceInfoEdit FormInstance
        {
            get 
            {
                if (_formInstance == null)
                {
                    _formInstance = new FrmDeviceInfoEdit();
                    
                }
                return _formInstance;
            }
            set { FrmDeviceInfoEdit._formInstance = value; }
        }
        DBOperator dbo = CStaticClass.dbo;
        Model.MDevice mdev;
        public FrmDeviceInfoEdit()
        {
            InitializeComponent();
            _formInstance = this;
            dbo.Open();
        }

       

        
        

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

                switch (this.cbField.Text)
                {
                    case "设备索引":
                        df = "F_DeviceIndex";
                        break;
                    case "设备名称":
                        df = "F_DeviceName";
                        break;
                    case "设备类型索引":
                        df = "T_Base_Device.F_DeviceKindIndex";
                        break;
                    case "设备类型名称":
                        df = "F_DeviceKindName";
                        break;
                    
                    default:
                        return;
                }
                //20100208
                string sql = Commsql+" WHERE " +
                   " T_Base_Device.F_DeviceKindIndex =  T_Base_Device_Kind.F_DeviceKindIndex " +
                   " and  " + 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 btBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        

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

            this.tabControl1.SelectTab("tabPage1");
            this.tbDeviceIndex.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString()+ "";
            this.tbDeviceName.Text = this.dataGridView1.CurrentRow.Cells[1].Value.ToString() + "";
            this.tbDeviceKindIndex.Text = this.dataGridView1.CurrentRow.Cells[2].Value.ToString() + "";
            
            if(this.dataGridView1.CurrentRow.Cells[4].Value.ToString() + ""=="0")
            {
                this.cbLockedState.Text = "0:空闲";
            }
            else if (this.dataGridView1.CurrentRow.Cells[4].Value.ToString() + "" == "-1")
            {
                this.cbLockedState.Text  = "-1:停用";
            }
            else
            {
                this.cbLockedState.Text = this.dataGridView1.CurrentRow.Cells[4].Value.ToString() + "";
            }
            if (this.dataGridView1.CurrentRow.Cells[5].Value.ToString() + "" == "0")
            {
                this.cbErrorCode.Text = "0:正常";
            }
            
            else
            {
                this.cbErrorCode.Text = this.dataGridView1.CurrentRow.Cells[5].Value.ToString() + "";
            }
            if (this.dataGridView1.CurrentRow.Cells[6].Value.ToString() + "" == "0")
            {
                this.cbManTaskReserve.Text = "0:空闲";
            }

            else
            {
                this.cbManTaskReserve.Text = this.dataGridView1.CurrentRow.Cells[6].Value.ToString() + "";
            }
            if (this.dataGridView1.CurrentRow.Cells[7].Value.ToString() + "" == "1")
            {
                this.cbHaveGoods.Text = "有";
            }

            else
            {
                this.cbHaveGoods.Text = "无";
            }
        }

    

       
        private void button2_Click(object sender, EventArgs e)
        {
            //20100208
            string sql = Commsql + " WHERE " +
                   " T_Base_Device.F_DeviceKindIndex =  T_Base_Device_Kind.F_DeviceKindIndex " ;
            DataSet ds = dbo.ExceSQL(sql);
           this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
           
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            //增加提示:
            if (MessageBox.Show("您确认要修改设备状态吗?", "操作提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
            {
                return;
            }
            int a = 0;
            int lockstate = 0;
            int errorcode = 0;//20100208
            bool haveGoods = false;
            int manTaskReserve = 0;
            if (this.tbDeviceIndex.Text.Trim().Length == 0)
            {
                MessageBox.Show("设备索引不允许输入空值!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.tbDeviceIndex.Focus();
                return;
            }
            if (this.cbLockedState.Text.Trim().Length == 0)
            {
                MessageBox.Show("执行状态不允许输入空值!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.cbLockedState.Focus();
                return;
            }
            if (this.cbErrorCode.Text.Trim().Length == 0)
            {
                MessageBox.Show("设备状态不允许输入空值!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.cbErrorCode.Focus();
                return;
            }
            if (this.cbHaveGoods.Text.Trim().Length == 0)
            {
                MessageBox.Show("设备逻辑有物不允许输入空值!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.cbHaveGoods.Focus();
                return;
            }
            if (this.cbManTaskReserve.Text.Trim().Length == 0)
            {
                MessageBox.Show("调度预约锁不允许输入空值!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.cbManTaskReserve.Focus();
                return;
            }


            if (this.cbLockedState.Text == "0:空闲")
            {
                lockstate=0;
                errorcode = 0;//20100208
            }
            else if (this.cbLockedState.Text == "-1:停用")
            {
                lockstate =-1 ;
                errorcode = 998;//20100208
            }
            else
            {
                if (int.TryParse(this.cbLockedState.Text,out a) == true)
                {
                    lockstate = Convert.ToInt32(this.cbLockedState.Text);
                }
                else
                {
                    MessageBox.Show("执行状态只允许输入“0:空闲”,“-1:停用”或者整型设备指令索引!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.cbLockedState.Focus();
                    return;
                }
                errorcode = 0;//20100208
            }
            if (this.cbErrorCode.Text == "0:正常")
            {
                errorcode = 0;//20100208
            }
            
            else
            {
                if (int.TryParse(this.cbErrorCode.Text,out a) == true)
                {
                    errorcode = Convert.ToInt32(this.cbErrorCode.Text);
                }
                else
                {
                    MessageBox.Show("设备状态只允许输入“0:正常”或者整型故障编码!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.cbErrorCode.Focus();
                    return;
                }
            }

            if (this.cbManTaskReserve.Text == "0:空闲")
            {
                manTaskReserve = 0;
            }

            else
            {
                if (int.TryParse(this.cbManTaskReserve.Text,out a) == true)
                {
                    manTaskReserve = Convert.ToInt32(this.cbManTaskReserve.Text);
                }
                else
                {
                    MessageBox.Show("设备状态只允许输入“0:正常”或者整型故障编码!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.cbManTaskReserve.Focus();
                    return;
                }
            }
            if (this.cbHaveGoods.Text == "有")
            {
                haveGoods = true;
            }
            else if (this.cbHaveGoods.Text == "无")
            {
                haveGoods = false;
            }
            else
            {
                MessageBox.Show("只能选择逻辑有物或者无物!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.cbHaveGoods.Focus();
                return;
               
            }
            try
            {
                string sql;
                DataSet ds;
               
                MessageBox.Show("设备信息修改成功!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
                sql = Commsql + " WHERE " +
                   " T_Base_Device.F_DeviceKindIndex =  T_Base_Device_Kind.F_DeviceKindIndex " +
                   " and F_DeviceIndex= " + Convert.ToInt32(this.tbDeviceIndex.Text.Trim());
                ds = dbo.ExceSQL(sql);
                this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
                mdev = Model.CGetInfo.GetDeviceInfo(Convert.ToInt32(this.tbDeviceIndex.Text.Trim()));
                if (lockstate == -1)
                {
                    mdev.RunState = 4;
                }
                else
                {
                    mdev.RunState = lockstate;
                }
                mdev.ManTaskReserve = manTaskReserve;
                mdev.ErrorCode = errorcode;
                CStaticClass.WcfControl.BeginSetDeviceState(mdev, new AsyncCallback(SetDeviceStateCallBack), null);
                object[] ob = new object[4] { lockstate, errorcode, manTaskReserve, Convert.ToInt32(this.tbDeviceIndex.Text.Trim()) };
                dbo.ExecuteSql(string.Format("UPDATE T_Base_Device SET F_LockedState ={0},f_errorcode={1},F_ManTaskReserve ={2}  where F_DeviceIndex= {3}", ob));
                CStaticClass.WcfControl.BeginSetLogicHaveGoods(haveGoods, Convert.ToInt32(this.tbDeviceIndex.Text.Trim()), new AsyncCallback(SetLogicHaveGoodsCallBack), null);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //20100208
            string sql = Commsql + " WHERE F_LockedState=-1 AND " +
                   " T_Base_Device.F_DeviceKindIndex =  T_Base_Device_Kind.F_DeviceKindIndex ";
            DataSet ds = dbo.ExceSQL(sql);
            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string sql = Commsql + " WHERE F_errorcode>0 AND " +
                   " T_Base_Device.F_DeviceKindIndex =  T_Base_Device_Kind.F_DeviceKindIndex ";
            DataSet ds = dbo.ExceSQL(sql);
            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string sql = Commsql + " WHERE F_ManTaskReserve>0 AND " +
                   " T_Base_Device.F_DeviceKindIndex =  T_Base_Device_Kind.F_DeviceKindIndex ";
            DataSet ds = dbo.ExceSQL(sql);
            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
        }

        private void cbLockedState_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cbLockedState.Text == "0:空闲")
            {
                this.cbErrorCode.Text = "0:正常";
            }
            else if (this.cbLockedState.Text == "-1:停用")
            {
                this.cbErrorCode.Text = "998";
            }
        }

        private void cbField_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string df = "";

                switch (this.cbField.Text)
                {
                    case "设备索引":
                        df = "F_DeviceIndex";
                        break;
                    case "设备名称":
                        df = "F_DeviceName";
                        break;
                    case "设备类型索引":
                        df = "F_DeviceKindIndex";
                        break;
                    case "设备类型名称":
                        df = "F_DeviceKindName";
                        break;

                    default:
                        return;
                }
                //20100208
                string sql = "select distinct " + df + " from T_Base_Device ";
                DataView dv = dbo.ExceSQL(sql).Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    this.tbContent.DataSource = null;
                    this.tbContent.DisplayMember = df;
                    this.tbContent.ValueMember= df;
                    this.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 SetDeviceStateCallBack(IAsyncResult ar)
        {
            string errtext = string.Empty;
            this.BeginInvoke(new MethodInvoker(delegate()
            {
                if (CStaticClass.WcfControl.EndSetDeviceState(out errtext, ar) == false)
                {
                    MessageBox.Show(errtext, "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                
            }));
        }
        void SetLogicHaveGoodsCallBack(IAsyncResult ar)
        {
            string errtext = string.Empty;
            this.BeginInvoke(new MethodInvoker(delegate()
            {
                
                if (CStaticClass.WcfControl.EndSetLogicHaveGoods(out errtext, ar) == false)
                {
                    MessageBox.Show(errtext, "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }));
        }

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

        

       
        
    }
}