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 FormZakazchik : Form
    {
        public FormZakazchik()
        {
            InitializeComponent();
        }

        void FillListZakaz()
        {
            SqlConnection con = new SqlConnection(Properties.Settings.Default.FabricDeryugoConnectionString);
            con.Open();
            string txtquery =
@"select Izdeliya.nameizd as izd, Zakaz.countizd as countizd, 
Tkani.nametkan as tkan, Furnitura.namefur as fur, Zakaz.countfur as countfur, 
Users.fam as manager
from Zakaz, Users, Tkani, Furnitura, Izdeliya
where Users.iduser = Zakaz.idman and Tkani.idtkan = Zakaz.idtkan 
and Furnitura.idfur = Zakaz.idfur and Izdeliya.idizd = Zakaz.idizd
and Zakaz.idzakazchik = " + LblIdUser.Text;

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

            DgvZakaz.Rows.Clear(); // очистить от старых строк
            // перебирать результаты запроса
            while (sqlrez.Read())
            {
                DgvZakaz.Rows.Add(sqlrez["izd"], sqlrez["countizd"],
                    sqlrez["tkan"], sqlrez["fur"], sqlrez["countfur"],
                    sqlrez["manager"]);
            }
            con.Close();
        }

        private void usersBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.usersBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.fabricDeryugoDataSet);

        }

        private void FormZakazchik_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'fabricDeryugoDataSet.Users' table. You can move, or remove it, as needed.
            this.usersTableAdapter.Fill(this.fabricDeryugoDataSet.Users);
            FillListZakaz();
        }

        private void BtnProfile_Click(object sender, EventArgs e)
        {
            FormProfil frm = new FormProfil();
            // на форму для редактирования профиля передать 
            // фильтр для выборки авторизовавшегося пользователя
            frm.usersBindingSource.Filter = this.usersBindingSource.Filter;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                this.usersTableAdapter.Fill(this.fabricDeryugoDataSet.Users);
            }
        }

        private void BtnLev_Click(object sender, EventArgs e)
        {
            FormPoisk frm = new FormPoisk();
            frm.ShowDialog();
        }

        private void BtnAddZakaz_Click(object sender, EventArgs e)
        {
            FormAddZakaz frm = new FormAddZakaz();
            // передать код авторизовавшегося заказчика
            frm.IdZakazchik = LblIdUser.Text;
            frm.ShowDialog();
            FillListZakaz();
        }
    }
}
