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

namespace SCAN
{
    public partial class Form_InboundMsgOKCancel : Form
    {
        private List<lib.db.HT_IB> inboundItems;
        private int currentItem; //
        private lib.LogWriter logger = lib.LogWriter.getInstance();

        public Form_InboundMsgOKCancel(string message, Color color, List<lib.db.HT_IB> inboundItems, int selectedIndex)
        {
            InitializeComponent();

            lblMessage.Text = message;
            lblMessage.ForeColor = color;
            this.inboundItems = inboundItems;
            this.currentItem = selectedIndex;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {         
            //Form frm_edit = new Form_EditQtyInboundSimple(inboundItems, currentItem);
            //Form frm_edit = new Form_InboundSimpleConfirm("", Color.Blue, inboundItems, currentItem);
            //frm_edit.ShowDialog();

            try
            {
                string item_id = inboundItems.ElementAt(currentItem).__COLUMNS["item_id"];
                string customer_id = inboundItems.ElementAt(currentItem).__COLUMNS["customer_id"];
                string product_date = inboundItems.ElementAt(currentItem).__COLUMNS["product_date"];
                string create_date = inboundItems.ElementAt(currentItem).__COLUMNS["create_date"];
                string act_type = inboundItems.ElementAt(currentItem).__COLUMNS["act_type"];

                /*
                string sql = @"UPDATE HT_IB 
                    SET status=''
                        , update_date='{0}'
                        , update_time='{1}'
                        , update_by='{2}' 
                    WHERE item_id='{3}' AND customer_id='{4}' AND product_date='{5}' AND create_date='{6}' AND act_type='{7}' AND status='Confirm' AND sync=0 ";
                List<string[]> rows = (new lib.DbTable()).executeQuery(String.Format(sql
                    , DateTime.Now.ToString("yyMMdd")
                    , DateTime.Now.ToString("HHmmss")
                    , Form_Login._Form_LoginInstance.userName
                    , item_id
                    , customer_id
                    , product_date
                    , create_date
                    , act_type
                    ));
                 * 
                */


                (new lib.db.HT_IB()).remove(String.Format("product_date='{0}' AND item_id='{1}' AND customer_id='{2}' AND status='{3}' AND sync='{4}' AND create_date='{5}' AND act_type='{6}'"
                                                 , product_date
                                                 , item_id
                                                 , customer_id
                                                 , "Confirm"
                                                 , "0"
                                                 , create_date
                                                 , act_type
                                            ));

                lib.db.HT_IB ib = inboundItems.ElementAt(currentItem);
                ib.__COLUMNS["status"] = "";

                // update item back to buffer list
                inboundItems[currentItem] = ib;
                Form_InboundSimple.inboundItems = inboundItems;

                this.Close();
                return;
            }
            catch (Exception ex) {
                logger.Log(ex.Message);
            }
        }

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