using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using CRVM.CIDExcuter;
using CRVM.Entity;
using System.Text.RegularExpressions;

namespace CRVM
{
    public partial class OriginRollForm : Form
    {
        Machine reviewMac = null;
        public OriginRollForm(Machine reviewMac)
        {
            this.reviewMac = reviewMac;
            InitializeComponent();
        }

        private void RollInfo_Load(object sender, EventArgs e)
        {
            Inital();
        }

        private void Inital()
        {
            if (reviewMac == null)
            {
                return;
            }
            int standCount = SysParam.Instance.SpeedChannelCount+1;
            for (int standno = 1; standno < standCount; standno++)
            {
                TextBox textbox_BU = (TextBox)panel2.Controls["dia_B_U_" + standno];
                textbox_BU.Text = reviewMac.Stand[standno - 1].rollDiameterBU.ToString();
                TextBox textbox_BL = (TextBox)panel2.Controls["dia_B_L_" + standno];
                textbox_BL.Text = reviewMac.Stand[standno - 1].rollDiameterBL.ToString();
                TextBox textbox_MU = (TextBox)panel2.Controls["dia_M_U_" + standno];
                textbox_MU.Text = reviewMac.Stand[standno - 1].rollDiameterMU.ToString();
                TextBox textbox_ML = (TextBox)panel2.Controls["dia_M_L_" + standno];
                textbox_ML.Text = reviewMac.Stand[standno - 1].rollDiameterML.ToString(); ;
                TextBox textbox_WU = (TextBox)panel2.Controls["dia_W_U_" + standno];
                textbox_WU.Text = reviewMac.Stand[standno - 1].rollDiameterWU.ToString();
                TextBox textbox_WL = (TextBox)panel2.Controls["dia_W_L_" + standno];
                textbox_WL.Text = reviewMac.Stand[standno - 1].rollDiameterWL.ToString();
            }
            //DataTable createdates = null;
            //try
            //{
            //    createdates = DbHelper.GetInstance("").SelCreateTimeRolData();
            //}
            //catch (Exception ex)
            //{
            //    label_alarm.Text = "无法获取本地轧辊数据，请查看本地数据库连接状态！";
            //    label_alarm.Visible = true;
            //}

            //if (createdates == null)
            //{
            //    return;
            //}
            //else
            //{
            //    List<string> list = new List<string>();
            //    for (int i = 0; i < createdates.Rows.Count; i++)
            //    {
            //        list.Add(createdates.Rows[i]["createdate"].ToString());
            //    }
            //    combox_createdates.DataSource = list;
            //    if (list.Count > 0)
            //    {
            //        combox_createdates.SelectedIndex = 0;
            //    }
            //}
        }

        private void DiameterView(string createdate)
        {
            DataTable data = null;
            try
            {
                data = DbHelper.GetInstance("").SelRollDataByCreateDate(createdate);
            }
            catch (Exception)
            {
                label_alarm.Text = "无法获取本地轧辊数据，请查看本地数据库连接状态！";
                label_alarm.Visible = true;
            }

            if (data != null)
            {
                for (int i = 0; i < data.Rows.Count; i++)
                {
                    string controlName = "dia_" + data.Rows[i]["rolltype"] + "_" + data.Rows[i]["rollposition"] + "_" + data.Rows[i]["standno"];
                    TextBox box = (TextBox)panel2.Controls[controlName];
                    if (box != null)
                    {
                        box.Text = data.Rows[i]["diametermiddle"].ToString();
                    }
                }
            }
        }

        private void combox_createdates_SelectedIndexChanged(object sender, EventArgs e)
        {
            ClearDiameter();
            DiameterView(combox_createdates.Text.Trim());
        }

        private void ClearDiameter()
        {
            foreach (Control item in panel2.Controls)
            {
                if (item.GetType().ToString().Contains("TextBox"))
                {
                    TextBox textbox = (TextBox)item;
                    textbox.Text = "";
                }
            }
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            if (IsInputOk())
            {
                if (reviewMac == null)
                {
                    return;
                }
                int standCount = SysParam.Instance.SpeedChannelCount+1;
                for (int standno = 1; standno < standCount; standno++)
                {
                    TextBox textbox_BU = (TextBox)panel2.Controls["dia_B_U_" + standno];
                    reviewMac.Stand[standno - 1].rollDiameterBU = Convert.ToDouble(textbox_BU.Text.Trim());
                    TextBox textbox_BL = (TextBox)panel2.Controls["dia_B_L_" + standno];
                    reviewMac.Stand[standno - 1].rollDiameterBL = Convert.ToDouble(textbox_BL.Text.Trim());
                    TextBox textbox_MU = (TextBox)panel2.Controls["dia_M_U_" + standno];
                    reviewMac.Stand[standno - 1].rollDiameterMU = Convert.ToDouble(textbox_MU.Text.Trim());
                    TextBox textbox_ML = (TextBox)panel2.Controls["dia_M_L_" + standno];
                    reviewMac.Stand[standno - 1].rollDiameterML = Convert.ToDouble(textbox_ML.Text.Trim());
                    TextBox textbox_WU = (TextBox)panel2.Controls["dia_W_U_" + standno];
                    reviewMac.Stand[standno - 1].rollDiameterWU = Convert.ToDouble(textbox_WU.Text.Trim());
                    TextBox textbox_WL = (TextBox)panel2.Controls["dia_W_L_" + standno];
                    reviewMac.Stand[standno - 1].rollDiameterWL = Convert.ToDouble(textbox_WL.Text.Trim());
                }
                MessageShowForm form = new MessageShowForm("提示", "轧辊直径设置成功！");
                form.ShowDialog();
            }
        }

        private bool IsInputOk()
        {
            foreach (Control item in panel2.Controls)
            {
                if (item.GetType().ToString().Contains("TextBox"))
                {
                    TextBox textbox = (TextBox)item;
                    double value = 0d;
                    if (!double.TryParse(textbox.Text.Trim(), out value))
                    {
                        string name = textbox.Name;
                        string standno = name.Substring(8, 1);
                        string rollposition = name.Substring(6, 1);
                        string rolltype = name.Substring(4, 1);
                        if (rollposition == "U")
                        {
                            rollposition = "上";
                        }
                        else
                        {
                            rollposition = "下";
                        }

                        if (rolltype == "B")
                        {
                            rolltype = "支撑辊";
                        }
                        else if (rolltype == "M")
                        {
                            rolltype = "中间辊";
                        }
                        else if (rolltype == "W")
                        {
                            rolltype = "工作辊";
                        }
                        string msg = standno + "机架" + rolltype + rollposition + "辊直径输入格式不正确！";
                        MessageShowForm form = new MessageShowForm("提示", msg);
                        form.ShowDialog();
                        return false;
                    }
                }
            }
            return true;
        }
    }
}
