private void ConfirmPodp(ReplenishContentItem ordc, DataRow[] r2, string message, DataRow[] PTLrow)
        {
            //DataRow[] r2 = replenishTable.Select("Desc_id = " + ordc.RefID);

            /*if (r2.Length > 0)
             * {*/
            r2[0][3] = 1;
            r2[0][4] = message;
            (this.Owner as MainWindow).sqlQueriesToSend.Add("UPDATE ptl_database.newcomingsdescription SET status=1, statusPotpitka=1, datetimeProcessed=CURRENT_TIMESTAMP, quantPotpitka=" + ordc.QuantityPodpitka
                                                            + ", userId=" + (this.Owner as MainWindow).currentUser + " WHERE idNewComingsDescription=" + r2[0][0] + ";");
            ordc.Confirmed = true;
            //}

            DataRow[] rw = dt.Select("Gateway = " + PTLrow[0][2] + " AND ID = " + PTLrow[0][3]);
            if (rw.Length > 0)
            {
                int index = dt.Rows.IndexOf(rw[0]);
                if (action_list[index].Count > 0)
                {
                    action_list[index].RemoveAt(0);
                }
                if (action_list[index].Count > 0)
                {
                    SendToPTL(action_list[index][0]);
                }
            }

            bool all_confirmed = true;

            foreach (ReplenishContentItem o in OrdersPanel.Children.OfType <ReplenishContentItem>())
            {
                if (!o.Confirmed)
                {
                    all_confirmed = false;
                }
            }
            if (all_confirmed)
            {
                this.Confirmed = true;
            }
        }
        public ReplenishmentWindow(NewComing replenishTable, DataTable PTLS)
        {
            InitializeComponent();
            //expandedItem = null;
            cart_barcode        = replenishTable.TableName;
            this.replenishTable = replenishTable;
            PTLs = PTLS;

            this.Owner      = (MainWindow)Application.Current.MainWindow;
            this.ResizeMode = ResizeMode.NoResize;
            this.Closing   += (se, e) =>
            {
                if (!Confirmed && !(Owner as MainWindow).close)
                {
                    MessageBox.Show("Тауарлар толығымен орналастырылмады!");
                }
            };
            this.Loaded += (se, ev) =>
            {
                Owner.IsEnabled = false;
                if (!Start())
                {
                    Close();
                }
            };

            string[] st = { "Gateway", "ID" };

            dt = PTLs.DefaultView.ToTable(true, st);
            dt.Columns[0].ColumnName = "Gateway";
            dt.Columns[1].ColumnName = "ID";

            action_list = new List <Tuple <int, int, string> > [dt.Rows.Count];
            for (int ind = 0; ind < dt.Rows.Count; ind++)
            {
                action_list[ind] = new List <Tuple <int, int, string> >();
            }



            foreach (DataRow row in replenishTable.Rows)
            {
                if (Convert.ToInt32(row[3]) == 0)
                {
                    DataRow[] r = PTLs.Select("Global = " + row[1]);
                    if (r.Length > 0)
                    {
                        DataRow[] rw = dt.Select("Gateway = " + r[0][2] + " AND ID = " + r[0][3]);
                        if (rw.Length > 0)
                        {
                            int    index                = dt.Rows.IndexOf(rw[0]);
                            string short_series         = row[9].ToString().Substring(Math.Max(0, row[9].ToString().Length - 4));
                            Tuple <int, int, string> t2 = new Tuple <int, int, string>(Convert.ToInt32(row[0]), Convert.ToInt32(row[2]), short_series);
                            action_list[index].Add(t2);
                        }
                    }
                }
            }


            orderTab.Header = "Корзина " + cart_barcode;
            int i = 1;

            //OrdersPanel.Visibility = Visibility.Collapsed;
            string[] s = { "" };
            foreach (DataRow row in replenishTable.Rows)
            {
                ReplenishContentItem ordc = new ReplenishContentItem(i, Convert.ToInt32(row[0]), s, row[6].ToString(), row[5].ToString(), row[9].ToString(), Convert.ToInt32(row[2]), Convert.ToInt32(row[1]));

                if (Convert.ToInt32(row[3]) == 0)
                {
                    ordc.QuantityReplenished = Convert.ToInt32(row[2]);
                }
                else if (Convert.ToInt32(row[3]) == 1)
                {
                    ordc.Confirmed           = true;
                    ordc.QuantityReplenished = Convert.ToInt32(row[4]);
                }
                else if (Convert.ToInt32(row[3]) == 2)
                {
                    ordc.QuantityReplenished = Convert.ToInt32(row[4]);
                    ordc.Expanded            = true;
                }
                ordc.ShowButtonPressed += (se, ev) =>
                {
                    DataRow[] r2 = replenishTable.Select("Desc_id = " + (se as ReplenishContentItem).RefID);
                    if (r2.Length > 0)
                    {
                        DataRow[] r = PTLs.Select("Global = " + r2[0][1]);
                        if (r.Length > 0)
                        {
                            ConfirmORDC(se as ReplenishContentItem, r2, (se as ReplenishContentItem).QuantityReplenished.ToString(), r);
                            //((MainWindow)Application.Current.MainWindow).ptlconn.ClearNode(Convert.ToInt32(r[0][2]), Convert.ToInt32(r[0][3]));

                            /*DataRow[] newrows = PTLs.Select("Gateway = " + r[0][2] + "AND ID = " + r[0][3]);
                             *
                             * foreach (DataRow newrow in newrows)
                             * {
                             *  DataRow[] dr = replenishTable.Select("PTL = " + newrow[1]);
                             *  if (dr.Length > 0 && !Convert.ToBoolean(dr[0][3]))
                             *  {
                             *      Direction dir = Direction.Up;
                             *      if (Convert.ToBoolean(newrow[4]))
                             *          dir = Direction.Down;
                             *      ((MainWindow)Application.Current.MainWindow).ptlconn.SetLEDStatus(Convert.ToInt32(newrow[2]), Convert.ToInt32(newrow[3]), LEDColor.Amber, LEDStatus.On);
                             *      ((MainWindow)Application.Current.MainWindow).ptlconn.DisplayQuantity(Convert.ToInt32(newrow[2]), Convert.ToInt32(newrow[3]), Convert.ToInt32(dr[0][2]), dir, 0, 0);
                             *  }
                             * }*/
                        }
                    }
                };
                ordc.ConfirmButtonPressed += (se, ev) =>
                {
                    DataRow[] r2 = replenishTable.Select("Desc_id = " + (se as ReplenishContentItem).RefID);
                    if (r2.Length > 0)
                    {
                        DataRow[] r = PTLs.Select("Global = " + r2[0][1]);
                        if (r.Length > 0)
                        {
                            ConfirmPodp((se as ReplenishContentItem), r2, (se as ReplenishContentItem).QuantityPodpitka.ToString(), r);
                        }
                    }
                };
                ordc.CancelButtonPressed += (se, ev) => // HERE
                {
                    ordc.cancellation = true;
                    DataRow[] r2 = replenishTable.Select("Desc_id = " + (se as ReplenishContentItem).RefID);
                    if (r2.Length > 0)
                    {
                        r2[0][3] = 1;
                        (this.Owner as MainWindow).sqlQueriesToSend.Add("UPDATE ptl_database.newcomingsdescription "
                                                                        + "SET status=1, statusPotpitka=0 WHERE idNewComingsDescription=" + r2[0][0] + ";");
                        ordc.Confirmed = true;

                        DataRow[] PTLrow = PTLs.Select("Global = " + r2[0][1]);
                        if (PTLrow.Length > 0)
                        {
                            DataRow[] rw = dt.Select("Gateway = " + PTLrow[0][2] + " AND ID = " + PTLrow[0][3]);
                            if (rw.Length > 0)
                            {
                                ((MainWindow)Application.Current.MainWindow).ptlconn.ClearNode(Convert.ToInt32(PTLrow[0][2]), Convert.ToInt32(PTLrow[0][3]));
                                int index = dt.Rows.IndexOf(rw[0]);
                                if (action_list[index].Count > 0)
                                {
                                    action_list[index].RemoveAt(0);
                                }
                                if (action_list[index].Count > 0)
                                {
                                    SendToPTL(action_list[index][0]);
                                }
                            }
                        }
                    }


                    bool all_confirmed = true;
                    foreach (ReplenishContentItem o in OrdersPanel.Children.OfType <ReplenishContentItem>())
                    {
                        if (!o.Confirmed)
                        {
                            all_confirmed = false;
                        }
                    }
                    if (all_confirmed)
                    {
                        this.Confirmed = true;
                    }
                };

                /*ordc.ExpandMe += (se, ev) =>
                 * {
                 *  if (expandedItem != null)
                 *  {
                 *      expandedItem.Expanded = false;
                 *  }
                 *  expandedItem = (se as ReplenishContentItem);
                 *  expandedItem.Expanded = true;
                 * };*/
                OrdersPanel.Children.Add(ordc);
                i++;
            }
        }
        private void ConfirmORDC(ReplenishContentItem ordc, DataRow[] r, string message, DataRow[] PTLrow)
        {
            /*DataRow[] r2 = replenishTable.Select("PTL = " + r[0][1]);
             * if (r2.Length > 0)
             * {*/

            DataRow[] rw = dt.Select("Gateway = " + PTLrow[0][2] + " AND ID = " + PTLrow[0][3]);
            if (Convert.ToInt32(message) == Convert.ToInt32(r[0][2]))
            {
                r[0][3] = 1;
                r[0][4] = message.Trim();
                (this.Owner as MainWindow).sqlQueriesToSend.Add("UPDATE ptl_database.newcomingsdescription SET status=1, datetimeProcessed=CURRENT_TIMESTAMP, actQuant=" + message.Trim() //HERE
                                                                + " WHERE idNewComingsDescription=" + r[0][0] + ";");
                ordc.Confirmed = true;

                if (rw.Length > 0)
                {
                    ((MainWindow)Application.Current.MainWindow).ptlconn.ClearNode(Convert.ToInt32(PTLrow[0][2]), Convert.ToInt32(PTLrow[0][3]));
                    int index = dt.Rows.IndexOf(rw[0]);
                    if (action_list[index].Count > 0)
                    {
                        action_list[index].RemoveAt(0);
                    }
                    if (action_list[index].Count > 0)
                    {
                        SendToPTL(action_list[index][0]);
                    }
                }
            }
            else if (Convert.ToInt32(message) < Convert.ToInt32(r[0][2]))
            {
                r[0][3] = 2;
                r[0][4] = message.Trim();
                (this.Owner as MainWindow).sqlQueriesToSend.Add("UPDATE ptl_database.newcomingsdescription SET status=2, datetimeProcessed=CURRENT_TIMESTAMP, actQuant=" + message.Trim()
                                                                + " WHERE idNewComingsDescription=" + r[0][0] + ";");
                ordc.Expanded = true;
                if (rw.Length > 0)
                {
                    Direction dir = Direction.Up;
                    if (Convert.ToBoolean(PTLrow[0][4]))
                    {
                        dir = Direction.Down;
                    }

                    ((MainWindow)Application.Current.MainWindow).ptlconn.ClearNode(Convert.ToInt32(PTLrow[0][2]), Convert.ToInt32(PTLrow[0][3]));
                    ((MainWindow)Application.Current.MainWindow).ptlconn.SetLEDStatus(Convert.ToInt32(PTLrow[0][2]), Convert.ToInt32(PTLrow[0][3]), LEDColor.Cyan, LEDStatus.Blink_1sec);
                    //  ((MainWindow)Application.Current.MainWindow).ptlconn.DisplayQuantity(Convert.ToInt32(PTLrow[0][2]), Convert.ToInt32(PTLrow[0][3]), -(Convert.ToInt32(r[0][2]) - Convert.ToInt32(message)), dir, 0, 0);
                }
            }
            //}
            bool all_confirmed = true;

            foreach (ReplenishContentItem o in OrdersPanel.Children.OfType <ReplenishContentItem>())
            {
                if (!o.Confirmed)
                {
                    all_confirmed = false;
                }
            }
            if (all_confirmed)
            {
                this.Confirmed = true;
            }
        }