using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DBFactory;

namespace wcfControlMonitorClient
{
    
    public partial class FrmTaskManage : Form
    {//20151120
        DBOperator dbo = CStaticClass.dbo;

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

        public FrmTaskManage()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            button_Refresh_Click(null, EventArgs.Empty);

        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if ((e.ColumnIndex >= 0) && (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 tsmEdit_Click(object sender, EventArgs e)
        {
            if (dataGridView1.RowCount <= 0)
            {
                return;
            }

            this.tbUserID.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString() + "";
            this.tbMaxCount.Text = this.dataGridView1.CurrentRow.Cells[1].Value.ToString() + "";
            this.tbExeMaxCount.Text = this.dataGridView1.CurrentRow.Cells[2].Value.ToString() + "";
            this.tbEmergencyExit.Text="0";//无应急出口
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            try
            {
                int GetCount, ExeCount,Exit;
                if (tbUserID.Text=="")
                {

                    MessageBox.Show("请先选定要修改的记录!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (int.TryParse(tbMaxCount.Text.Trim(), out GetCount) == false
                    || int.TryParse(tbEmergencyExit.Text.Trim(), out Exit) == false
                 ||(int.TryParse(tbExeMaxCount.Text.Trim(), out ExeCount) == false))
                {

                    MessageBox.Show("请输入有效的数字!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }


                string sql = string.Format("update T_Base_TaskCount set F_MAXCOUNT={0},F_EXECOUNT={1},F_EmergencyExit={2}  where F_DeviceIndex={3}", GetCount, ExeCount,Exit, tbUserID.Text);

                int i_ret = dbo.ExecuteSql(sql);
                if (i_ret == 1)
                {
                    MessageBox.Show("保存成功!", "操作提示:");
                    button_Refresh_Click(null, EventArgs.Empty);
                }
                else
                {
                    MessageBox.Show("保存失败!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            catch(Exception  ex )
            {
                throw ex;
            }
        }

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



        private void button_Refresh_Click(object sender, EventArgs e)
        {
            string CommomSql = "select F_DeviceIndex as 终点设备编号, F_MAXCOUNT as 获取任务数量,F_EXECOUNT as 执行任务数量, F_REMARK as 描述 from T_Base_TaskCount where F_CHECKGOODS is not null";


            DataView dv = dbo.ExceSQL(CommomSql).Tables[0].DefaultView;
            this.dataGridView1.DataSource = dv;
        }
    }
}