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 GcDevicePc.DevParam;
using GcDevicePc.IniParam;

namespace GcDevicePc.ConfigDlg
{
    public partial class RunTableDlg : Form
    {


        public RunTableDlg()
        {
            InitializeComponent();

            int index = this.dgv运行表.Rows.Add();
            dgv运行表.Rows[index].Cells[0].Value = index;
            dgv运行表.Rows[index].Cells[1].Value = "开机";
        }

        public void reflashContent()
        {
            globaldata.m_profileMethod.GetDevState();

            globaldata.m_profileMethod.GetRunTableElseAllPara();

            tb循环次数.Text = CProfileDevice.m_DevParam.runtable.u16CycleTimes.ToString();
            tb等待时间.Text = CProfileDevice.m_DevParam.runtable.u16WaitTime.ToString();

            string folderName = System.Windows.Forms.Application.StartupPath;
            string pathString = System.IO.Path.Combine(folderName, "运行表");
            if (System.IO.Directory.Exists(pathString) == false)
            {
                System.IO.Directory.CreateDirectory(pathString);
            }

            globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_RunTab = System.IO.Path.Combine(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.RunTabFolder, "runtable.ini");
            IniHelper m_writer = new IniHelper(globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_RunTab);

            for (ushort i = 0; i < dgv运行表.RowCount; i++)
            {
                string sValue = m_writer.ReadValue("运行表", i.ToString());
                if (sValue == "")
                {
                    break;
                }
                else
                {
                    int index = this.dgv运行表.Rows.Add();
                    dgv运行表.Rows[i].Cells[0].Value = index.ToString();
                    dgv运行表.Rows[i].Cells[1].Value = sValue;
                }
            }
        }

        public void recordCurData()
        {
            CProfileDevice.m_DevParam.runtable.u16CycleTimes = System.UInt16.Parse(tb循环次数.Text);
            CProfileDevice.m_DevParam.runtable.u16WaitTime = System.UInt16.Parse(tb等待时间.Text);
        }

        private void TextBoxDec_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void btn插入_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDlg = new OpenFileDialog();
            fileDlg.Multiselect = false;
            fileDlg.Title = "请选择方法文件";
            fileDlg.Filter = "所有文件(*.*)|*.*";
            if (fileDlg.ShowDialog() == DialogResult.OK)
            {
                string file = fileDlg.SafeFileName;

                int index = this.dgv运行表.Rows.Add();

                dgv运行表.Rows[index].Cells[0].Value = index;
                dgv运行表.Rows[index].Cells[1].Value = file;
            }
        }

        private void btn删除_Click(object sender, EventArgs e)
        {
            dgv运行表.Rows.RemoveAt((int)dgv运行表.CurrentCell.RowIndex); 
        }

        private void btn保存_Click(object sender, EventArgs e)
        {
            string folderName = System.Windows.Forms.Application.StartupPath;
            string pathString = System.IO.Path.Combine(folderName, "运行表");
            if (System.IO.Directory.Exists(pathString) == false)
            {
                System.IO.Directory.CreateDirectory(pathString);
            }

            globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_RunTab = System.IO.Path.Combine(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.RunTabFolder, "runtable.ini");
            IniHelper m_writer = new IniHelper(globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_RunTab);

            for (ushort i = 0; i < dgv运行表.RowCount; i++)
            {
                if (dgv运行表.Rows[i].Cells[0].Value == null || dgv运行表.Rows[i].Cells[1].Value == null)
                {
                    break;
                }
                else
                {
                    int num = (int)dgv运行表.Rows[i].Cells[0].Value;
                    string skeyNum = num.ToString();
                    string skeyContent = (string)dgv运行表.Rows[i].Cells[1].Value;
                    //stRunTable.StartTemp = (int)dgv程升表.Rows[i].Cells[0].Value;
                    //stRunTable.HeatSpeed = (int)dgv程升表.Rows[i].Cells[1].Value;
                    //stRunTable.TargetTemp = (int)dgv程升表.Rows[i].Cells[2].Value;
                    //stRunTable.BalanceTime = (int)dgv程升表.Rows[i].Cells[3].Value;
                    m_writer.WriteValue("运行表", skeyNum, skeyContent);
                }
            }
        }
    }
}
