using CasunCommon.BaseUI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PMSUI
{
    public partial class HistoryDataForm : CasunForm
    {
        public HistoryDataForm()
        {
            InitializeComponent();
        }

        private void Init()
        {
            ///////////
            CasunUILib.DgvModel(dataGridView1);
        }

        private void HistoryDataForm_Load(object sender, EventArgs e)
        {
            if (!InitDBNameToShowNameDic())
            {
                this.Close();
                return;
            }

            CasunUILib.ButtonHelps(btHelps);

            gp_Read.Location = new Point(btHelps.Location.X, gp_Read.Location.Y);

            CasunUILib.GroupBoxReadModel(gp_Read, PermissionLevel);
            //CasunUILib.GroupBoxReadWriteModel(gp_Opt, PermissionLevel);

            CasunUILib.splitContainerModel(splitContainer1);
            Init();
            dataGridView1.MultiSelect = true;
            InitDgv();
        }

        /// <summary>
        /// 设置双缓冲
        /// </summary>
        /// <param name="c"></param>
        public static void SetDoubleBuffered(System.Windows.Forms.Control c)
        {   //请参考下面的博客   //http://blogs.msdn.com/oldnewthing/archive/2006/01/03/508694.aspx   
            if (System.Windows.Forms.SystemInformation.TerminalServerSession)
                return;
            System.Reflection.PropertyInfo aProp = typeof(System.Windows.Forms.Control).GetProperty("DoubleBuffered"
                , System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            aProp.SetValue(c, true, null);
        }

        private void InitDgv()
        {
            dataGridView1.Columns.Clear();
            dataGridView1.DataSource = null;
            dtp_StartTime.Text = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
            dtp_EndTime.Text = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
        }

        private void bt_Click(object sender, EventArgs e)
        {
            int a = dtp_StartTime.Value.CompareTo(dtp_EndTime.Value);
            if (a > 0)
            {
                string strMsg = CLanguageManager.LanguageChange("开始时间不能大于或等于结束时间")
                    + "！";
                MessageBox.Show(strMsg);
                return;
            }
            string strOrderby = "order by CreateTime";
            string strWhere = string.Format("[CalledTime] between '{0}' and  '{1}'"
                , dtp_StartTime.Value.ToString("yyyy-MM-dd HH:mm:ss")
                , dtp_EndTime.Value.ToString("yyyy-MM-dd HH:mm:ss")
                );

            string strOrderID = tbOrderID.Text.ToString();

            if (strOrderID != "")
            {
                strWhere = string.Format("[OrderID] = '{0}'"
                    , strOrderID
                    );
                tbOrderID.Text = "";
            }

            dataGridView1.DataSource = CFormColumnManager.GetDGVDataSource(this.Name, "PMS_his_HistoryOrderData",
                strOrderby ,
                strWhere);
        }

        private void btReSendSignal_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 用于事件重复使用
        /// </summary>
        private Dictionary<string, string> DBNameToShowNameDic = new Dictionary<string, string>();
        private bool InitDBNameToShowNameDic()
        {
            if (!CFormColumnManager.GetDBNameToShowNameDic(this.Name, out DBNameToShowNameDic))
            {
                MessageBox.Show(CLanguageManager.LanguageChange("加载界面配置失败，请重启尝试恢复。"));
                return false;
            }
            return true;
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int currentRow = e.RowIndex;
                if (currentRow != -1 && dataGridView1.Rows[currentRow] != null)
                {
                    string strTaskID = dataGridView1.Rows[currentRow].Cells[DBNameToShowNameDic["TaskID"]].Value.ToString();
                    //if (e.ColumnIndex == dataGridView1.Rows[currentRow].Cells[DBNameToShowNameDic["TaskID"]].ColumnIndex)
                    if(strTaskID != "")
                    {
                        HistoryDataForm2_Task frm = new HistoryDataForm2_Task(strTaskID);
                        frm.ShowDialog();

                        //dataGridView1.Rows[currentRow].Cells[e.ColumnIndex].ToolTipText = "12345123125123";
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(CLanguageManager.LanguageChange("更改过设置后请重新打开该子界面"));
            }
        }

        private void btHelps_Click(object sender, EventArgs e)
        {
            MessageBox.Show(PMSUI.Config.CConfigManager.gs_FormHelpConfig.GetFormHelp(this.Name));

        }
    }
}
