using AR;
using Project.Dialog;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Web;
using System.Windows.Forms;

namespace Project
{
    public partial class Model_Operation : Form
    {

        public string Value = string.Empty;
        public Model_Operation()
        {
            InitializeComponent();
            this.KeyPreview = true;
            this.KeyDown += (s1, e1) => { if (e1.KeyCode == Keys.Escape) this.Close(); };
            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormClosed += __Closed;
            this.FormClosing += FModelV_FormClosing;
            this.dv.CellContentClick += dv_CellContentClick;
            //if (COMM.SETTING.Data.FullScreen) this.WindowState = FormWindowState.Maximized;
            //this.WindowState = FormWindowState.Normal;
        }

        private void FModelV_FormClosing(object sender, FormClosingEventArgs e)
        {
            //Pub.sm.setNewStep(StateMachine.eSystemStep.IDLE);
            if (hasChanged())
            {
                var dlg = UTIL.MsgQ("변경된 자료가 있습니다.\n" +
                   "진행하면 변경된 자료는 손실됩니다\n" +
                   "진행 할까요?");

                if (dlg != DialogResult.Yes)
                {
                    e.Cancel = true;
                    return;
                }
            }
        }

        private void __Closed(object sender, FormClosedEventArgs e)
        {

            this.ds1.OPModel.TableNewRow -= Model_TableNewRow;
        }

        private void __Load(object sender, EventArgs e)
        {
            this.ds1.Clear();
            this.ds1.OPModel.Merge(PUB.mdm.dataSet.OPModel);
            this.ds1.OPModel.TableNewRow += Model_TableNewRow;
            this.tmDisplay.Start();

            //var MotList = PUB.mdm.dataSet.MCModel.GroupBy(t => t.Title).Select(t => t.Key).ToList();
            //comboBox1.Items.AddRange(MotList.ToArray());

            if (this.ds1.OPModel.Rows.Count < 1)
            {
                var newdr = this.ds1.OPModel.NewOPModelRow();
                newdr.Title = "New Model";
                this.ds1.OPModel.AddOPModelRow(newdr);
                UTIL.MsgI("신규 모델 정보가 추가되었습니다.\n\n최초 설정 이므로 각 상태값을 확인 하세요.");
            }

            //dispal current
            if (PUB.Result.isSetvModel)
            {
                arLabel18.Text = PUB.Result.vModel.Title;
                //find data
                var datas = this.ds1.OPModel.Select("", this.bs.Sort);
                for (int i = 0; i < datas.Length; i++)
                {
                    if (datas[i]["Title"].ToString() == PUB.Result.vModel.Title)
                    {
                        this.bs.Position = i;
                        break;
                    }
                }
            }
            else arLabel18.Text = "--";
            //chekauth(false);

            //checkBox1.Enabled = checkBox4.Checked;
            //checkBox2.Enabled = checkBox4.Checked;
            //checkBox3.Enabled = checkBox4.Checked;

            if (PUB.UserAdmin == false)
            {
                //오퍼레이터는 다 못한다
                dv.EditMode = DataGridViewEditMode.EditProgrammatically;
                this.btAdd.Visible = false;
                this.btSave.Visible = false;
                this.btCopy.Visible = false;
                this.toolStripSeparator1.Visible = false;
                this.btDel.Visible = false;
            }

            if (PUB.sm.Step != eSMStep.IDLE)
            {
                btDel.Enabled = false;
                //btCopy.Enabled = false;
                toolStripButton10.Visible = false;
                //Util.MsgE("삭제/복사 작업은 대기 상태일때 가능 합니다");
            }

            //assig check events
            foreach (var c in tabPage1.Controls)
            {
                if (c.GetType() != typeof(CheckBox)) continue;
                var chk = c as CheckBox;
                if (chk.Tag == null) continue;
                chk.Click += Chk_Option_Check;
            }
            foreach (var c in grpApplySidinfo.Controls)
            {
                if (c.GetType() != typeof(CheckBox)) continue;
                var chk = c as CheckBox;
                if (chk.Tag == null) continue;
                chk.Click += Chk_Sidinfo_Check;
            }
            foreach (var c in grpapplyjob.Controls)
            {
                if (c.GetType() != typeof(CheckBox)) continue;
                var chk = c as CheckBox;
                if (chk.Tag == null) continue;
                chk.Click += Chk_JobInfo_Check;
            }
            foreach (var c in GrpSidConvData.Controls)
            {
                if (c.GetType() != typeof(CheckBox)) continue;
                var chk = c as CheckBox;
                if (chk.Tag == null) continue;
                chk.Click += Chk_SIDConvData_Check;
            }
        }

        private void Chk_Option_Check(object sender, EventArgs e)
        {
            var drv = bs.Current as DataRowView;
            if (drv == null) return;
            var dr = drv.Row as DataSet1.OPModelRow;
            var chk = sender as CheckBox;
            dr.vOption = UTIL.SetBitState(dr.vOption, int.Parse(chk.Tag.ToString()), chk.Checked);
            dr.EndEdit();

            bs_CurrentChanged_1(null, null);
        }
        private void Chk_Sidinfo_Check(object sender, EventArgs e)
        {
            var drv = bs.Current as DataRowView;
            if (drv == null) return;
            var dr = drv.Row as DataSet1.OPModelRow;
            var chk = sender as CheckBox;
            dr.vSIDInfo = UTIL.SetBitState(dr.vSIDInfo, int.Parse(chk.Tag.ToString()), chk.Checked);
            dr.EndEdit();

            bs_CurrentChanged_1(null, null);
        }
        private void Chk_JobInfo_Check(object sender, EventArgs e)
        {
            var drv = bs.Current as DataRowView;
            if (drv == null) return;
            var dr = drv.Row as DataSet1.OPModelRow;
            var chk = sender as CheckBox;
            dr.vJobInfo = UTIL.SetBitState(dr.vJobInfo, int.Parse(chk.Tag.ToString()), chk.Checked);
            dr.EndEdit();

            bs_CurrentChanged_1(null, null);
        }
        private void Chk_SIDConvData_Check(object sender, EventArgs e)
        {
            var drv = bs.Current as DataRowView;
            if (drv == null) return;
            var dr = drv.Row as DataSet1.OPModelRow;
            var chk = sender as CheckBox;
            dr.vSIDConv = UTIL.SetBitState(dr.vSIDConv, int.Parse(chk.Tag.ToString()), chk.Checked);
            dr.EndEdit();

            bs_CurrentChanged_1(null, null);
        }
        #region "Mouse Form Move"

        private Boolean fMove = false;
        private Point MDownPos;

        private void LbTitle_DoubleClick(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Maximized) this.WindowState = FormWindowState.Normal;
            else this.WindowState = FormWindowState.Maximized;
        }
        private void LbTitle_MouseMove(object sender, MouseEventArgs e)
        {
            if (fMove)
            {
                Point offset = new Point(e.X - MDownPos.X, e.Y - MDownPos.Y);
                this.Left += offset.X;
                this.Top += offset.Y;
                offset = new Point(0, 0);
            }
        }
        private void LbTitle_MouseUp(object sender, MouseEventArgs e)
        {
            fMove = false;
        }
        private void LbTitle_MouseDown(object sender, MouseEventArgs e)
        {
            MDownPos = new Point(e.X, e.Y);
            fMove = true;
        }

        #endregion

        void EntertoTab(Control ctl)
        {
            if (ctl.HasChildren)
            {
                foreach (var item in ctl.Controls)
                {
                    if (item.GetType() == typeof(TextBox))
                    {
                        var tb = item as TextBox;
                        tb.KeyDown += (s1, e1) =>
                        {
                            if (e1.KeyCode == Keys.Enter) SendKeys.Send("{TAB}");
                        };
                    }
                }
            }
        }

        void Model_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {

            e.Row["Title"] = DateTime.Now.ToString("yyMMddHHmmss");
            e.Row["BCD_1D"] = true;
            e.Row["BCD_QR"] = false;
            e.Row["BCD_DM"] = true;
            //e.Row["guid"] = Guid.NewGuid().ToString();
            //e.Row["MotionModel"] = "Default";
            //  e.Row["RowCount"] = 5;
            //  e.Row["ColCount"] = 0;
            //   e.Row["Light"] = 50;
        }

        private void modelBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {

        }

        private void bs_CurrentChanged(object sender, EventArgs e)
        {
            //z range 을 표시한다.

            //z position 영역을 문자로 변환한다.
            //this.list
            var drv = this.bs.Current as DataRowView;
            if (drv == null) return;
            var dr = drv.Row as DataSet1.MCModelRow;
        }

        private void tmDisplay_Tick(object sender, EventArgs e)
        {
            if (VAR.BOOL[eVarBool.Use_Conveyor])
            {
                btConvOk.BackColor = Color.Lime;
                btConvOff.BackColor = Color.White;
            }
            else
            {
                btConvOk.BackColor = Color.White;
                btConvOff.BackColor = Color.Lime;
            }

        }


        private void dv_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }

        private void btSelect_Click(object sender, EventArgs e)
        {

        }
        Boolean hasChanged()
        {
            this.Validate();
            this.bs.EndEdit();
            var chg = this.ds1.OPModel.GetChanges();
            if (chg == null || chg.Rows.Count < 1) return false;
            return true;
        }
        void selectModel()
        {
            //select
            this.Invalidate();
            this.bs.EndEdit();
            var drv = this.bs.Current as DataRowView;
            if (drv == null) return;
            var dr = drv.Row as DataSet1.OPModelRow;
            if (dr.Title == "") return;
            this.Value = dr.Title;
            SETTING.User.useConv = VAR.BOOL[eVarBool.Use_Conveyor];
            SETTING.User.Save();
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void toolStripButton2_Click_1(object sender, EventArgs e)
        {
            //iocontrol
            var f = new Dialog.Quick_Control();
            f.TopMost = true;
            f.Opacity = 0.95;
            f.Show();
        }

        private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {

        }



        //private void tbFind_KeyDown(object sender, KeyEventArgs e)
        //{
        //    if (e.KeyCode == Keys.Enter)
        //    {
        //        findModel();
        //    }
        //}

        //void findModel()
        //{
        //    try
        //    {
        //        if (this.tbFind.Text.Trim() == "")
        //        {
        //            this.bs.Filter = "";
        //            this.tbFind.BackColor = Color.White;
        //        }
        //        else
        //        {
        //            string filter = "title like '%{0}%'";
        //            filter = string.Format(filter, tbFind.Text.Trim());
        //            this.bs.Filter = filter;
        //            this.tbFind.BackColor = Color.SkyBlue;
        //            this.tbFind.SelectAll();
        //        }
        //    }
        //    catch (Exception Ex)
        //    {
        //        this.bs.Filter = "";
        //        this.tbFind.BackColor = Color.Tomato;
        //        Util.MsgE(Ex.Message);
        //    }
        //}
        //private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        //{
        //    var f = new Dialog.fTouchKeyFull("INPUT - ", this.tbFind.Text.Trim());
        //    if (f.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;
        //    var inputLot = f.tbInput.Text.Trim().ToUpper();
        //    //if (inputLot == "") return;
        //    tbFind.Text = inputLot;
        //    findModel();
        //    tbFind.Focus();
        //    tbFind.SelectAll();
        //}


        private void button23_Click(object sender, EventArgs e)
        {

        }



        private void toolStripButton2_Click(object sender, EventArgs e)
        {

        }

        void dv_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dv.Columns[e.ColumnIndex].DataPropertyName.ToLower() == "spn")
            {
                //var drv = dv.Rows[e.RowIndex].DataBoundItem as DataRowView;
                //var dr = drv.Row as DataSet1.ModelRow;
                //var spn = dr.SPN;
                //spn = selectSPn(spn);
                //if(spn != "")
                //{
                //    dr.SPN = spn;
                //    dv.EndEdit();
                //}
            }
        }
        //string selectSPn(string selectedname)
        //{
        //    var od = new OpenFileDialog();
        //    od.InitialDirectory = COMM.SETTING.Data.SPNPath;
        //    od.FileName = selectedname;
        //    if (od.ShowDialog() != System.Windows.Forms.DialogResult.OK) return "";
        //    var fi = new System.IO.FileInfo(od.FileName);
        //    if (COMM.SETTING.Data.SPNPath != fi.Directory.FullName)
        //    {
        //        COMM.SETTING.Data.SPNPath = fi.Directory.FullName;
        //        COMM.SETTING.Data.Save();
        //    }
        //    var SPN = fi.Name.Replace(fi.Extension, "");
        //    return SPN;
        //}

        private void toolStripButton3_Click(object sender, EventArgs e)
        {


            //if(COMM.SETTING.Data.SPNPath == "")
            //{
            //    Util.MsgE("SEM 파일 저장 경로가 입력되지 않았습니다\n"+
            //        "환경설정에서 해당 값을 입력할 수 있습니다");
            //    return;
            //}
            //Util.RunExplorer(COMM.SETTING.Data.SPNPath);
            //return;



            //var drv = this.bs.Current as DataRowView;
            //if (drv == null) return;
            //var dr = drv.Row as DataSet1.ModelRow;

            //var spn = selectSPn(dr.SPN);
            //if(spn != "")
            //{
            //    dr.SPN = spn;
            //    dr.EndEdit();
            //}
        }

        private void tbClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //private void visXCountLabel_Click(object sender, EventArgs e)
        //{
        //    //jopgspeed
        //    var value = double.Parse(visXCountTextBox.Text);
        //    value = Pub.ChangeValuePopup(value, "속도 입력");
        //    visXCountTextBox.Text = value.ToString();
        //}

        //private void visYCountLabel_Click(object sender, EventArgs e)
        //{
        //    //jopgspeed
        //    var value = double.Parse(visYCountTextBox.Text);
        //    value = Pub.ChangeValuePopup(value, "속도 입력");
        //    visYCountTextBox.Text = value.ToString();
        //}

        //private void visXTermLabel_Click(object sender, EventArgs e)
        //{
        //    var value = double.Parse(visXTermTextBox.Text);
        //    value = Pub.ChangeValuePopup(value, "속도 입력");
        //    visXTermTextBox.Text = value.ToString();
        //}

        //private void visYTermLabel_Click(object sender, EventArgs e)
        //{
        //    var value = double.Parse(visYTermTextBox.Text);
        //    value = Pub.ChangeValuePopup(value, "속도 입력");
        //    visYTermTextBox.Text = value.ToString();
        //}

        //private void label1_Click(object sender, EventArgs e)
        //{
        //    var c = int.Parse(textBox1.Text);
        //    var cd = new ColorDialog();
        //    cd.Color = Color.FromArgb(c);
        //    if (cd.ShowDialog() == DialogResult.OK)
        //    {
        //        textBox1.Text = cd.Color.ToArgb().ToString();
        //    }
        //}

        //private void label2_Click(object sender, EventArgs e)
        //{
        //    var c = int.Parse(textBox2.Text);
        //    var cd = new ColorDialog();
        //    cd.Color = Color.FromArgb(c);
        //    if (cd.ShowDialog() == DialogResult.OK)
        //    {
        //        textBox2.Text = cd.Color.ToArgb().ToString();
        //    }
        //}

        //private void label4_Click(object sender, EventArgs e)
        //{
        //    var c = int.Parse(textBox4.Text);
        //    var cd = new ColorDialog();
        //    cd.Color = Color.FromArgb(c);
        //    if (cd.ShowDialog() == DialogResult.OK)
        //    {
        //        textBox4.Text = cd.Color.ToArgb().ToString();
        //    }
        //}

        //private void label3_Click(object sender, EventArgs e)
        //{
        //    var c = int.Parse(textBox3.Text);
        //    var cd = new ColorDialog();
        //    cd.Color = Color.FromArgb(c);
        //    if (cd.ShowDialog() == DialogResult.OK)
        //    {
        //        textBox3.Text = cd.Color.ToArgb().ToString();
        //    }
        //}

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            var tb = sender as TextBox;
            var str = tb.Text.Trim();
            if (str.isEmpty()) str = "0";
            var c = Color.FromArgb(int.Parse(str));
            if (str.Equals("0") == false) tb.BackColor = c;
            else tb.BackColor = Color.Black;
        }

        //private void button2_Click(object sender, EventArgs e)
        //{
        //    Pub.dev_lightT.SetRGBValue(textBox3.BackColor);
        //}

        //private void button4_Click(object sender, EventArgs e)
        //{
        //    Pub.dev_lightB.SetRGBValue(textBox1.BackColor);
        //}

        //private void button3_Click(object sender, EventArgs e)
        //{
        //    Pub.dev_lightB.SetRGBValue(textBox4.BackColor);
        //}

        //private void button1_Click(object sender, EventArgs e)
        //{
        //    Pub.dev_lightT.SetRGBValue(textBox2.BackColor);
        //}

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {

        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            var drv = this.bs.Current as DataRowView;
            if (drv == null) return;
            var dlg = UTIL.MsgQ("현재 선택된 자료를 삭제하시겠습니까?");
            if (dlg != System.Windows.Forms.DialogResult.Yes) return;
            bs.RemoveCurrent();
            //this.ds1.Model.AcceptChanges();
        }

        private void toolStripButton8_Click(object sender, EventArgs e)
        {
            var drv = this.bs.Current as DataRowView;
            if (drv == null) return;
            var dr = drv.Row as DataSet1.OPModelRow;
            var dlg = UTIL.MsgQ(string.Format("다음 모델 정보를 복사하시겠습니까?\n\n모델명 : {0}", dr.Title));
            if (dlg != System.Windows.Forms.DialogResult.Yes) return;

            var newdr = this.ds1.OPModel.NewOPModelRow();
            UTIL.CopyData(dr, newdr);
            newdr.Title += "-copy-";
            newdr.idx = this.ds1.OPModel.OrderByDescending(t => t.idx).FirstOrDefault().idx + 1;
            newdr.EndEdit();
            this.ds1.OPModel.AddOPModelRow(newdr);
            if (this.bs.Count > 0) this.bs.Position = this.bs.Count - 1;


            ////detailcopy
            //var childs = ds1.OPModel.Where(t => t.pidx == dr.idx).ToArray();
            //foreach (var dr2 in childs)
            //{
            //	var newdr2 = this.ds1.OPModel.NewOPModelRow();
            //	Util.CopyData(dr2, newdr2);
            //	newdr2.pidx = newdr.idx;
            //	newdr2.EndEdit();
            //	newdr2.idx = this.ds1.OPModel.OrderByDescending(t => t.idx).FirstOrDefault().idx + 1;
            //	this.ds1.OPModel.AddOPModelRow(newdr2);
            //}
            //if (this.bs.Count > 0) this.bs.Position = this.bs.Count - 1;
        }


        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            var f = new AR.Dialog.fTouchKeyFull("모델명을 입력하세요", DateTime.Now.ToString("yyyyMMddHHmmss"));
            if (f.ShowDialog() == DialogResult.OK)
            {
                var newdr = this.ds1.OPModel.NewOPModelRow();
                newdr.Title = f.tbInput.Text.Trim();
                newdr.Motion = "Default";
                this.ds1.OPModel.AddOPModelRow(newdr);
                if (this.bs.Count > 0) this.bs.Position = this.bs.Count - 1;
            }
        }

        private void bs_CurrentChanged_1(object sender, EventArgs e)
        {
            //값이바뀌면 허용 바코드 목록을 업데이트 해준다.
            var drv = bs.Current as DataRowView;
            if (drv == null) return;
            var dr = drv.Row as DataSet1.OPModelRow;

            if (dr.IsbOwnZPLNull())
            {
                dr.bOwnZPL = false;
                dr.EndEdit();
            }

            //this.tabControl1.Visible = !dr.Title.StartsWith("101");
            //this.panel6.Visible = !dr.Title.StartsWith("101");
            // this.panel7.Visible = !dr.Title.StartsWith("101");

            //update checkbox
            foreach (var c in tabPage1.Controls)
            {
                if (c.GetType() != typeof(CheckBox)) continue;
                var chk = c as CheckBox;
                if (chk.Tag == null) continue;
                var val = UTIL.GetBitState(dr.vOption, int.Parse(chk.Tag.ToString()));
                if (chk.Checked != val) chk.Checked = val;
            }
            foreach (var c in grpApplySidinfo.Controls)
            {
                if (c.GetType() != typeof(CheckBox)) continue;
                var chk = c as CheckBox;
                if (chk.Tag == null) continue;
                var val = UTIL.GetBitState(dr.vSIDInfo, int.Parse(chk.Tag.ToString()));
                if (chk.Checked != val) chk.Checked = val;
            }
            foreach (var c in grpapplyjob.Controls)
            {
                if (c.GetType() != typeof(CheckBox)) continue;
                var chk = c as CheckBox;
                if (chk.Tag == null) continue;
                var val = UTIL.GetBitState(dr.vJobInfo, int.Parse(chk.Tag.ToString()));
                if (chk.Checked != val) chk.Checked = val;
            }
            foreach (var c in GrpSidConvData.Controls)
            {
                if (c.GetType() != typeof(CheckBox)) continue;
                var chk = c as CheckBox;
                if (chk.Tag == null) continue;
                var val = UTIL.GetBitState(dr.vSIDConv, int.Parse(chk.Tag.ToString()));
                if (chk.Checked != val) chk.Checked = val;
            }

        }


        private void toolStripButton9_Click(object sender, EventArgs e)
        {
            //button - save
            this.Validate();
            this.bs.EndEdit();

            if (hasChanged() == false)
            {
                PUB.log.Add("모델저장:변경된 사항이 없습니다");
                return;
            }

            if (PUB.PasswordCheck() == false)
            {
                UTIL.MsgE("Password incorrect");
                return;
            }


            //z position 영역을 문자로 변환한다.
            var drv = this.bs.Current as DataRowView;
            if (drv == null) return;
            var dr = drv.Row as DataSet1.OPModelRow;
            dr.EndEdit();
            this.ds1.AcceptChanges();


            PUB.mdm.dataSet.OPModel.Clear();
            PUB.mdm.dataSet.OPModel.Merge(this.ds1.OPModel);
            PUB.mdm.dataSet.AcceptChanges();
            PUB.mdm.SaveModelV();
        }

        private void toolStripButton10_Click(object sender, EventArgs e)
        {
            if (PUB.sm.Step == eSMStep.RUN || PUB.sm.Step == eSMStep.WAITSTART || PUB.sm.Step == eSMStep.PAUSE)
            {
                UTIL.MsgE("현재 동작(대기) 중이므로 모델을 변경 할 수 없습니다");
                return;
            }

            if (hasChanged())
            {
                var dlg = UTIL.MsgQ("변경된 자료가 있습니다.\n" +
                    "진행하면 변경된 자료는 손실됩니다\n" +
                    "진행 할까요?");
                if (dlg != DialogResult.Yes) return;
            }
            selectModel();
        }

        private void chkApplySidInfo_CheckStateChanged(object sender, EventArgs e)
        {
            var chk = sender as CheckBox;
            chk.ForeColor = chk.Checked ? Color.Blue : Color.Gray;

            if (chk.Name.Equals(chkApplyJobInfo.Name))
            {
                grpapplyjob.Enabled = chk.Checked;
            }
            if (chk.Name.Equals(chkApplySidInfo.Name))
            {
                grpApplySidinfo.Enabled = chk.Checked;
            }
            if (chk.Name.Equals(chkApplySIDConvData.Name))
            {
                GrpSidConvData.Enabled = chk.Checked;
            }
        }

        private void btConvOk_Click(object sender, EventArgs e)
        {
            VAR.BOOL[eVarBool.Use_Conveyor] = true;
        }

        private void btConvOff_Click(object sender, EventArgs e)
        {
            VAR.BOOL[eVarBool.Use_Conveyor] = false;
        }

        private void tbBypassSID_TextChanged(object sender, EventArgs e)
        {
            var tb = sender as TextBox;
            tb.BackColor = tb.TextLength > 0 ? Color.Gold : Color.WhiteSmoke;
        }

        private void checkBox32_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            var drv = bs.Current as DataRowView;
            if (drv == null) return;
            var dr = drv.Row as DataSet1.OPModelRow;

            if (chkOwnZPL.Checked == false)
            {
                UTIL.MsgE("개별프린트코드가 해제되어있으므로 실제 적용되지 않습니다");
            }

            var idx = dr.idx;
            var fn = UTIL.MakePath("Model", idx.ToString(), "zpl.txt");
            var fnBase = UTIL.MakePath("zpl.txt");
            if (System.IO.File.Exists(fnBase) && System.IO.File.Exists(fn) == false)
            {
                UTIL.MsgI("전용 출력파일(ZPL.TXT)이 존재하지 않아 기본에서 복사합니다");
                var fi = new System.IO.FileInfo(fn);
                if (fi.Directory.Exists == false) fi.Directory.Create();
                System.IO.File.Copy(fnBase, fn, true);
            }
            using (var f = new fZPLEditor(fn))
            {
                f.ShowDialog();
            }
        }
    }
}

