using System;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Net.NetworkInformation;
using System.Reflection;
using System.Windows.Forms;

namespace PaintingPC
{
    public partial class FrmChooseDamnPosition : Form
    {
        public string BarCode = "";
        public static string DamnPosition = "";    //抛光位置
        public static string Reason = "";  //原因
        private int count = 0;  //缺陷图选中个数
        public FrmChooseDamnPosition(string barcode)
        {
            BarCode = barcode;
            InitializeComponent();
        }

        private void FrmChooseDamnPosition_Load(object sender, EventArgs e)
        {
            DamnPosition = "";
            Reason = "";

            int row = 0, col = 0;
            string picture = "";
            try
            {
                #region 缺陷原因

                try
                {
                    int drow = 0, dcol = 6; //行,列

                    DataTable dt3 = Function.SearchDefectInfo(ConfigurationManager.AppSettings["Station"].Trim());
                    if (dt3 != null && dt3.Rows.Count > 0)
                    {
                        drow = Convert.ToInt32(Math.Ceiling((double)dt3.Rows.Count / dcol));
                        Label[] dLb = new Label[7];
                        string[] dstr = new string[dt3.Rows.Count];


                        for (int i = 0; i < dt3.Rows.Count; i++)
                        {
                            dstr[i] = dt3.Rows[i]["DefectName"].ToString();
                        }

                        for (int i = 0; i < dcol; i++)  //列
                        {
                            for (int j = 0; j < drow; j++)   //行
                            {
                                dLb[i] = new Label();
                                if ((i + dcol * j) < dstr.Length)
                                {
                                    dLb[i].Text = dstr[i + dcol * j].ToString();
                                    dLb[i].Font = new Font(dLb[i].Font.FontFamily, 24, FontStyle.Bold);
                                    dLb[i].Size = new Size(150, 50);
                                    dLb[i].Location = new Point(20 + i * (dLb[i].Size.Width + 15), 5 + j * (dLb[i].Size.Height + 20));
                                    dLb[i].BorderStyle = BorderStyle.FixedSingle;
                                    dLb[i].BackColor = Color.Transparent;
                                    dLb[i].TextAlign = ContentAlignment.MiddleCenter;
                                    panel2.Controls.Add(dLb[i]);
                                    dLb[i].Click += new EventHandler(dLbRoom_Click);
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.WriteErrLogBase(ex.ToString(), MethodBase.GetCurrentMethod().Name);
                }

                #endregion

                #region 加载图片
                try
                {
                    DataTable dt = Function.SearchInfoByBarCode(BarCode);

                    if (dt != null && dt.Rows.Count > 0)
                    {
                        row = Convert.ToInt32(dt.Rows[0]["Rows"].ToString());
                        col = Convert.ToInt32(dt.Rows[0]["Cols"].ToString());
                        picture = dt.Rows[0]["PicturePath"].ToString();
                    }
                    dt.Dispose();

                    if (!string.IsNullOrEmpty(picture))
                    {
                        if (!File.Exists(System.IO.Directory.GetCurrentDirectory() + @"\" + picture))
                        {
                            string strImageURL = Function.GetImgPath() + picture;

                            System.Net.WebClient webClient = new System.Net.WebClient();
                            webClient.DownloadFile(strImageURL, Directory.GetCurrentDirectory() + @"\" + picture);
                        }

                        FileStream fs = new FileStream(System.Environment.CurrentDirectory + "\\" + picture,
                            FileMode.Open,
                            FileAccess.Read); //获取图片文件流
                        Image img = Image.FromStream(fs); // 文件流转换成Image格式
                        pictureBox2.Image = img; //给 图片框设置要显示的图片
                        fs.Close(); // 关闭流,释放图片资源 
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.WriteErrLogBase(ex.ToString(), MethodBase.GetCurrentMethod().Name);
                }
                #endregion

                #region 缺陷图划分区域

                Label[] lb = new Label[5];
                string[] str = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };

                for (int i = 0; i < col; i++)
                {
                    for (int j = 0; j < row; j++)
                    {
                        lb[i] = new Label();
                        lb[i].Text = str[i + col * j].ToString();
                        lb[i].Font = new Font(lb[i].Font.FontFamily, 32, FontStyle.Bold);

                        lb[i].Size = new Size(pictureBox2.Width / col, pictureBox2.Height / row);
                        lb[i].Location = new Point(0 + i * lb[i].Size.Width, 0 + j * lb[i].Size.Height);
                        lb[i].BorderStyle = BorderStyle.FixedSingle;
                        lb[i].BackColor = Color.Transparent;
                        lb[i].TextAlign = ContentAlignment.MiddleCenter;
                        pictureBox2.Controls.Add(lb[i]);

                        lb[i].Click += new EventHandler(lblRoom_Click);
                    }
                }

                #endregion
            }
            catch (Exception ex)
            {
                LogHelper.WriteLogManager(ex);
                LogHelper.WriteErrLogBase(ex.ToString(), MethodBase.GetCurrentMethod().Name);
            }
            
        }

        /// <summary>
        /// 缺陷图点击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void lblRoom_Click(object sender, EventArgs e)
        {
            Label a = (Label)sender;
            string aa = a.Text;
            if (a.BackColor == Color.Transparent)
            {
                a.BackColor = Color.FromArgb(150, System.Drawing.Color.Chartreuse);
                count++;
            }
            else
            {
                a.BackColor = Color.Transparent;
                count--;
            }
        }

        /// <summary>
        /// 缺陷原因点击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void dLbRoom_Click(object sender, EventArgs e)
        {
            if (count == 0)
            {
                MessageBox.Show("请选择缺陷区域!");
                return;
            }
            Label a = (Label)sender;
            string aa = a.Text;
            if (a.BackColor == Color.Transparent)
            {
                a.BackColor = Color.Chartreuse;
                Reason = Reason + a.Text + ";";
            }
            else
            {
                a.BackColor = Color.Transparent;
                Reason = Reason.Replace(a.Text.Trim() + ";", "");
            }
        }

        /// <summary>
        /// 返回按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBack_Click(object sender, EventArgs e)
        {
            DamnPosition = "";
            Reason = "";
            this.Close();
        }

        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            DamnPosition = "";
            //Reason = "";

            foreach (Control c in pictureBox2.Controls)
            {
                Label b = c as Label;
                if (b != null && b.BackColor != Color.Transparent)
                {
                    DamnPosition += b.Text + ";";
                }
            }

            //foreach (Control c in panel2.Controls)
            //{
            //    Label b = c as Label;
            //    if (b!= null && b.BackColor != Color.Transparent)
            //    {
            //        Reason += b.Text + ";";
            //    }
            //}

            if (!string.IsNullOrWhiteSpace(DamnPosition) && !string.IsNullOrWhiteSpace(Reason))
            {
                this.Close();
            }
            else
            {
                MessageBox.Show("必须点选缺陷位置和缺陷原因,如果不选请点击返回按钮");
            }
        }
    }
}