using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace СУБД_Швейная_фабрика
{
    public partial class FormPoisk : Form
    {
        public FormPoisk()
        {
            InitializeComponent();
        }
        static int Poisk(string t1, string t2)
        {
            int CntLev = Math.Abs(t1.Length - t2.Length);
            // перебор до последнего символа более короткой строки
            for (int i = 0; i <= Math.Min(t1.Length, t2.Length) - 1; i++)
            {
                if (t1[i] != t2[i])
                    CntLev++;
            }
            return CntLev;
        }

        List<string> LstTkani = new List<string>();

        private void FormPoisk_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(Properties.Settings.Default.FabricDeryugoConnectionString);
            con.Open();
            string TxtQuery = "select nametkan from Tkani";

            SqlCommand query1 = new SqlCommand(TxtQuery, con);
            // выполнить запрос
            SqlDataReader res = query1.ExecuteReader();

            while (res.Read())
                LstTkani.Add(res["nametkan"].ToString());
            con.Close();

            BtnShowAll_Click(sender, e);
        }

        private void BtnShowAll_Click(object sender, EventArgs e)
        {
            DgvTkani.Rows.Clear();
            foreach (string Tkan in LstTkani)
                DgvTkani.Rows.Add(Tkan);
        }

        private void BtnFind_Click(object sender, EventArgs e)
        {
            DgvTkani.Rows.Clear();
            foreach (string Tkan in LstTkani)
                if (Poisk(Tkan, TbxFind.Text) <= 3)
                    DgvTkani.Rows.Add(Tkan);
        }
    }
}
