using Microsoft.VisualBasic;
using RGD.DataService;
using RGD.DBUtility;
using System;
using System.Data;
using System.Windows.Forms;

namespace RGD.WCS
{
    public partial class FrmDeviceInfoEdit : Form
    {
        private Model.MDevice mdev;
        private 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 static FrmDeviceInfoEdit _formInstance;

        public static FrmDeviceInfoEdit FormInstance
        {
            get
            {
                if (_formInstance == null)
                {
                    _formInstance = new FrmDeviceInfoEdit();
                }
                return _formInstance;
            }
            set { FrmDeviceInfoEdit._formInstance = value; }
        }

        public FrmDeviceInfoEdit()
        {
            InitializeComponent();
            _formInstance = this;
        }

        private void FrmDeviceInfoEdit_Load(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// 显示全部设备
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            string sql = Commsql + " WHERE T_Base_Device.F_DeviceKindIndex=T_Base_Device_Kind.F_DeviceKindIndex ";
            DataSet ds = DbHelperSQL.Query(sql);
            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
        }

        /// <summary>
        /// 显示停用设备
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            string sql = Commsql + " WHERE F_LockedState=-1 AND T_Base_Device.F_DeviceKindIndex =  T_Base_Device_Kind.F_DeviceKindIndex ";
            DataSet ds = DbHelperSQL.Query(sql);
            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
        }

        /// <summary>
        /// 显示故障设备
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        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 = DbHelperSQL.Query(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 = DbHelperSQL.Query(sql);
            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
        }

        /// <summary>
        /// 开始查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btQuery_Click(object sender, EventArgs e)
        {
            if (tbContent.Text.Trim() == "") return;
            int index = 0;
            bool isint = int.TryParse(this.tbContent.Text.Trim(), out index);
            bool ifcheck = false;
            try
            {
                string df = "";

                switch (this.cbField.Text)
                {
                    case "设备索引":
                        df = "F_DeviceIndex";
                        ifcheck = true;
                        break;

                    case "设备名称":
                        df = "F_DeviceName";
                        break;

                    case "设备类型索引":
                        df = "T_Base_Device.F_DeviceKindIndex";
                        ifcheck = true;
                        break;

                    case "设备类型名称":
                        df = "F_DeviceKindName";
                        break;

                    default:
                        return;
                }
                if (ifcheck == true && isint == false)
                {
                    this.tbContent.Text = string.Empty;
                }
                string sql = Commsql + " WHERE T_Base_Device.F_DeviceKindIndex =  T_Base_Device_Kind.F_DeviceKindIndex and  " + df + "= '" + this.tbContent.Text + "' ";
                DataSet ds = DbHelperSQL.Query(sql);
                this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 保存修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btSave_Click(object sender, EventArgs e)
        {
            //增加提示：
            if (MessageBox.Show("您确认要修改设备状态吗？", "操作提示：", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
            {
                return;
            }
            int lockstate = 0;
            int errorcode = 0;//20100208
            int haveGoods = 0;
            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 = 98;//20100208
            }
            else
            {
                if (Information.IsNumeric(this.cbLockedState.Text) == 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 (Information.IsNumeric(this.cbErrorCode.Text) == 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 (Information.IsNumeric(this.cbManTaskReserve.Text) == true)
                {
                    manTaskReserve = Convert.ToInt32(this.cbManTaskReserve.Text);
                }
                else
                {
                    MessageBox.Show("设备状态只允许输入“0:正常”或者整型故障编码！", "误操作提示：", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.cbManTaskReserve.Focus();
                    return;
                }
            }
            if (this.cbHaveGoods.Text == "有")
            {
                haveGoods = 1;
            }
            else if (this.cbHaveGoods.Text == "无")
            {
                haveGoods = 0;
            }
            else
            {
                MessageBox.Show("只能选择逻辑有物或者无物！", "误操作提示：", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.cbHaveGoods.Focus();
                return;
            }
            try
            {
                string sql;
                DataSet ds;
                //20100208
                sql = "UPDATE T_Base_Device SET F_LockedState =" + lockstate + ",f_errorcode=" + errorcode + ",F_ManTaskReserve =" + manTaskReserve + ",F_HaveGoods=" + haveGoods + "  where F_DeviceIndex= " + Convert.ToInt32(this.tbDeviceIndex.Text.Trim());
                DbHelperSQL.ExecuteSql(sql);
                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 = DbHelperSQL.Query(sql);
                this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
                mdev = BaseDeviceService.GetDeviceInfo(Convert.ToInt32(this.tbDeviceIndex.Text.Trim()));
                if (lockstate == -1)
                {
                    mdev.RunState = 4;
                }
                else
                {
                    mdev.RunState = lockstate;
                }
                BaseDeviceService.SetDeviceInfo(mdev);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        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;
                }
                string sql = "select distinct " + df + " from T_Base_Device ";
                DataView dv = DbHelperSQL.Query(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;
            }
        }

        /// <summary>
        /// 执行状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        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 = "98";
            }
        }

        /// <summary>
        /// 修改设备
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        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 dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                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];
                }
            }
            catch (Exception)
            {
            }
        }
    }
}