UsrMacro.cs 6.6 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Asa.FaceControl;
using BLL;
using DocumentFormat.OpenXml.Wordprocessing;
using Model;
using SmartScan.PlusSettingFrm;

namespace SmartScan
{
    public partial class UsrMacro : UserControl, ISetMenu
    {
        private readonly List<string> keyCopy;

        public UsrMacro()
        {
            InitializeComponent();
            //CmbResetidBy.Tag = "not";
            CmbResetidBy.Items.AddRange(new string[] { Language.Dialog("ResetidByNone"), Language.Dialog("ResetidByDay"), Language.Dialog("ResetidByMonth") });
            keyCopy = new(BLLCommon.macroKey);
            LstKey.Items.AddRange(keyCopy.ToArray());
            setRIkey();
            TxtReelIDMatch.Text = BLLCommon.config.ReelIDMatch;
            TxtPrefix.Text = BLLCommon.config.ReelIDPrefix;
            TxtPostfix.Text = BLLCommon.config.ReelIDPostfix;
            NumReelIDPlaces.Value = BLLCommon.config.ReelIDPlaces;
            ChkReelIDFillZero.Checked = BLLCommon.config.ReelIDFillZero;
            int sel = Config.REEL_ID_AutoResetStrategy;
            if (sel < 0 || sel > CmbResetidBy.Items.Count - 1)
            {
                Config.REEL_ID_AutoResetStrategy = 0;
                sel = 0;
            }
            CmbResetidBy.SelectedIndex = sel;
            Asa.FaceControl.Language.SetLanguage(this);
        }

        public Asa.FaceControl.FacePanel GetPanel()
        {
            return facePanel1;
        }

        public void Save()
        {
            BLLCommon.macroKey.Clear();
            BLLCommon.macroKey.AddRange(keyCopy);
            System.IO.File.WriteAllLines(FilePath.CONFIG_MACRO_KEY, BLLCommon.macroKey.ToArray());

            BLLCommon.config.ReelIDMatch = TxtReelIDMatch.Text;
            BLLCommon.config.ReelIDPrefix = TxtPrefix.Text;
            BLLCommon.config.ReelIDPostfix = TxtPostfix.Text;
            BLLCommon.config.ReelIDPlaces = (int)NumReelIDPlaces.Value;
            BLLCommon.config.ReelIDFillZero = ChkReelIDFillZero.Checked;
            Config.REEL_ID_AutoResetStrategy = CmbResetidBy.SelectedIndex;
            BLLCommon.config.Save();
        }
        void setRIkey()
        {
            LstKey.Items.Clear();
            LstKey.Items.AddRange(keyCopy.ToArray());
            for (int i = 0; i < LstKey.Items.Count; i++)
            {
                if (LstKey.Items[i] == BLLCommon.config.ReelIDKeyWord)
                    LstKey.Items[i] = "[RI]" + LstKey.Items[i];
            }
        }


        private void LstKey_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (LstKey.SelectedIndex >= 0)
                TxtKey.Text = keyCopy[LstKey.SelectedIndex];
        }

        private void BtnAddKey_Click(object sender, EventArgs e)
        {
            string text = TxtKey.Text;
            int index = keyCopy.FindIndex(match => match == text);
            if (index == -1)
            {
                keyCopy.Add(text);
                LstKey.Items.Add(text);
            }
            else
            {
                string hint = Asa.FaceControl.Language.Dialog("KeyExists");
                hint = hint.Replace("[name]", text);
                new Asa.FaceControl.FaceMessageBox("", hint, MessageBoxButtons.OK).ShowDialog();
            }
        }

        private void BtnDelKey_Click(object sender, EventArgs e)
        {
            if (LstKey.SelectedIndex == -1) return;
            keyCopy.RemoveAt(LstKey.SelectedIndex);
            LstKey.Items.RemoveAt(LstKey.SelectedIndex);

            var keyname= ConfigHelper.Config.Get("Label_Key");
            if (!string.IsNullOrWhiteSpace(keyname))
            {
               UsrKeywordlabeling.Deletskeyname();
            }
        }

        private void BtnUpdateKey_Click(object sender, EventArgs e)
        {
            if (LstKey.SelectedIndex == -1) return;
            string text = TxtKey.Text;
            int index = keyCopy.FindIndex(match => match == text);
            if (index == -1)
            {
                keyCopy[LstKey.SelectedIndex] = text;
                var oldname=LstKey.Items[LstKey.SelectedIndex];
                var keyname = ConfigHelper.Config.Get("Label_Key");
                if (!string.IsNullOrWhiteSpace(keyname))
                {
                    //ConfigHelper.Config.Set("Label_Key", "Part No");
                    if (oldname == keyname)
                    {
                        UsrKeywordlabeling.Updatekeyname(text);
                    }                 
                }
                LstKey.Items[LstKey.SelectedIndex] = text;
                setRIkey();
            }
            else
            {
                string hint = Asa.FaceControl.Language.Dialog("KeyExists");
                hint = hint.Replace("[name]", text);
                new Asa.FaceControl.FaceMessageBox("", hint, MessageBoxButtons.OK).ShowDialog();
            }

        }

        private void BtnAppendKey_Click(object sender, EventArgs e)
        {
            if (LstKey.SelectedIndex == -1) return;
            string key = "[" + LstKey.Text + "]";
            TxtReelIDMatch.AppendText(key);
        }

        private void btn_up_Click(object sender, EventArgs e)
        {
            if (LstKey.SelectedIndex == -1 || LstKey.SelectedIndex == 0) return;
            keyCopy.Insert(LstKey.SelectedIndex - 1, keyCopy[LstKey.SelectedIndex]);
            keyCopy.RemoveAt(LstKey.SelectedIndex + 1);
            LstKey.Items.Clear();
            LstKey.Items.AddRange(keyCopy.ToArray());
            LstKey.SelectedIndex = LstKey.SelectedIndex - 1;

        }

        private void btn_down_Click(object sender, EventArgs e)
        {
            if (LstKey.SelectedIndex == -1 || LstKey.SelectedIndex == keyCopy.Count - 1) return;
            keyCopy.Insert(LstKey.SelectedIndex + 2, keyCopy[LstKey.SelectedIndex]);
            keyCopy.RemoveAt(LstKey.SelectedIndex);
            LstKey.Items.Clear();
            LstKey.Items.AddRange(keyCopy.ToArray());
            LstKey.SelectedIndex = LstKey.SelectedIndex + 1;
        }

        private void btn_setriid_Click(object sender, EventArgs e)
        {
            if (LstKey.SelectedIndex == -1) return;
            if (keyCopy[LstKey.SelectedIndex] == BLLCommon.config.ReelIDKeyWord)
                BLLCommon.config.ReelIDKeyWord = "";
            else
                BLLCommon.config.ReelIDKeyWord = keyCopy[LstKey.SelectedIndex];
            setRIkey();
        }

        private void btn_adddatetime_Click(object sender, EventArgs e)
        {
            string key = "[datetime:yyyyMMddHHmmss]";
            TxtReelIDMatch.AppendText(key);
        }   
    }
}