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

namespace HOZProject
{
    public partial class UControl_Log : UserControl
    {
        public UControl_Log()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Form fParent = this.ParentForm;
            fParent.Close();
        }
        public void ShowProcessLogInfo(string[] strLog)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("HoleName");
            dt.Columns.Add("Time");
            dt.Columns.Add("Message");
            dt.Columns.Add("State");
            foreach (string strItem in strLog)
            {
                string[] Nt = strItem.Split('_');
                DataRow dr = dt.NewRow();
                dr[0] = Nt[0];
                dr[1] = Nt[1];
                dr[2] = Nt[2];
                dr[3] = Nt[3];
                dt.Rows.Add(dr);
            }
            dgvLog.DataSource = dt;
            lblLogCount.Text = dt.Rows.Count.ToString()+"条";
        }

        private void dgvLog_SelectionChanged(object sender, EventArgs e)
        {
            dgvLog.ClearSelection();
        }

        #region 关闭按钮 鼠标操作事件
        private void pbClose_MouseEnter(object sender, EventArgs e)
        {
            pbClose.BackgroundImage = global::HOZProject.Properties.Resources.exit_2_;
        }

        private void pbClose_MouseLeave(object sender, EventArgs e)
        {
            pbClose.BackgroundImage = global::HOZProject.Properties.Resources.exit_2_;
        }
        #endregion

        private void btnExport_Click(object sender, EventArgs e)
        {
            ExportDataToExcel(this.dgvLog);
        }

        public void ExportDataToExcel(DataGridView myDGV)
        {
            string path = "";
            SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.Title = "导出表格";
            saveDialog.Filter = "Excel|*.xls";
            saveDialog.ShowDialog();
            path = saveDialog.FileName;
            if (path.IndexOf(":") < 0) return; //判断是否点击取消
            try
            {
                Thread.Sleep(1000);
                StreamWriter sw = new StreamWriter(path, false, Encoding.GetEncoding("gb2312"));
                StringBuilder sb = new StringBuilder();
                //写入标题
                for (int k = 0; k < myDGV.Columns.Count; k++)
                {
                    if (myDGV.Columns[k].Visible)//导出可见的标题
                    {
                        //"\t"就等于键盘上的Tab,加个"\t"的意思是: 填充完后进入下一个单元格.
                        sb.Append(myDGV.Columns[k].HeaderText.ToString().Trim() + "\t");
                    }
                }
                sb.Append(Environment.NewLine);//换行
                                               //写入每行数值
                for (int i = 0; i < myDGV.Rows.Count; i++)
                {
                    System.Windows.Forms.Application.DoEvents();
                    for (int j = 0; j < myDGV.Columns.Count; j++)
                    {
                        if (myDGV.Columns[j].Visible)//导出可见的单元格
                        {
                            //注意单元格有一定的字节数量限制,如果超出,就会出现两个单元格的内容是一模一样的.
                            //具体限制是多少字节,没有作深入研究.
                            sb.Append(myDGV.Rows[i].Cells[j].Value.ToString().Trim() + "\t");
                        }
                    }
                    sb.Append(Environment.NewLine); //换行
                }
                sw.Write(sb.ToString());
                sw.Flush();
                sw.Close();
                MessageBox.Show("导出成功", "", MessageBoxButtons.OK);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
