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_OutboundSimpleAllConfirm : Form
    {

        private static lib.LogWriter logger = lib.LogWriter.getInstance();

        private List<lib.db.HT_OB> checkInDB;
        private List<lib.db.HT_OB> outbounditems;
        private int waitConfirm; // zero base item index
        bool isModified = false;

        // Master table
        private Dictionary<string, int> __PackageTypes = lib.db.PackageType.getBoxSizeList();
        private Dictionary<string, lib.db.Item> __ItemList = lib.db.Item.getItemList();

        public Form_OutboundSimpleAllConfirm(string message, Color color, List<lib.db.HT_OB> outbounditems, int waitConfirm, string type)
        {
            InitializeComponent();

            //lblMessage.Text = message;
            lblMessage.ForeColor = color;
            this.outbounditems = outbounditems;
            this.waitConfirm = waitConfirm;
            lblWaitConfirm.Text = waitConfirm.ToString() + " row";
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            //btnOk.Enabled = false;
            //btnClose.Enabled = false;
            lblWaitConfirm.Text = "Waitting...";
            lblWaitConfirm.Refresh();
            //btnOk.Refresh();
            //btnClose.Refresh();

            int success = 0;
            string error_no = "";
            for (int i = 0; i < outbounditems.Count; i++)
            {
                lib.db.HT_OB ib = outbounditems.ElementAt(i);
                //Dictionary<string, string> cols = new Dictionary<string, string>(ib.__COLUMNS);
                //checkInDB = new List<lib.db.HT_OB>();
                if(ib.__COLUMNS["status"]=="")
                {
                    
                  /*  List<string[]> rows = (new lib.db.HT_OB()).get(
                    (new lib.db.HT_OB()).__COLUMNS
                    , String.Format("item_id='{0}' AND customer_id='{1}' AND status='{2}' AND sync='{3}' AND create_date='{4}'"
                        , ib.__COLUMNS["item_id"]
                        , ib.__COLUMNS["customer_id"]
                        , ""
                        , "0"
                        , ib.__COLUMNS["create_date"]
                        )
                    , null, null, null);
                     
                   */

                    // confirm ครั้งแรก
                   // if (rows.Count() == 0)
                   // {
                        ib.__COLUMNS["location_id"] = "A1";
                        ib.__COLUMNS["location_dvsn_id"] = "1";
                        ib.__COLUMNS["status"] = "Confirm";

                        ib.__COLUMNS.Remove("id");
                        ib.__COLUMNS.Remove("update_date");
                        ib.__COLUMNS.Remove("update_time");
                        ib.__COLUMNS.Remove("update_by");

                        int ret = ib.add(ib.__COLUMNS);

                        logger.Log("ret=" + ret);
                        if (ret == 1)
                        {
                            // update location last
                            /*(new lib.db.Item()).set(
                                new Dictionary<string, string> { { "location_last", "A1" } },
                                "id='" + ib.__COLUMNS["item_id"] + "'"
                                );
                             * */

                            // update item back to buffer list
                            outbounditems[i] = ib;
                            ++success;
                        }
                   // }

                    // เคย confirm แล้ว และมีการแก้ไข
                    /*
                    if (rows.Count() == 1)
                    {
                        string sql = @"UPDATE HT_OB 
                    SET status='{0}'
                        , pkg_qty_act='{1}'
                        , pc_qty_act='{2}'
                        , update_date='{3}'
                        , update_time='{4}'
                        , update_by='{5}' 
                    WHERE item_id='{6}' AND customer_id='{7}' AND create_date='{8}' AND status='' AND sync=0 ";
                        List<string[]> rows_upd = (new lib.DbTable()).executeQuery(String.Format(sql
                            , "Confirm"
                            , ib.__COLUMNS["pkg_qty_act"]
                            , ib.__COLUMNS["pc_qty_act"]
                            , DateTime.Now.ToString("yyMMdd")
                            , DateTime.Now.ToString("HHmmss")
                            , Form_Login._Form_LoginInstance.userName
                            , ib.__COLUMNS["item_id"]
                            , ib.__COLUMNS["customer_id"]
                            , ib.__COLUMNS["create_date"]
                            ));

                        ib.__COLUMNS["status"] = "Confirm";
                        // update item back to buffer list
                        outbounditems[i] = ib;
                        ++success;
                    }

                    if (rows.Count() > 1)
                    {
                        error_no += i.ToString() + ", ";
                    }
                     */

                }
            } // end loop

            Form_OutboundSimple.outboundItems = outbounditems;

            Form frmMsg = new Form_Msg("Confrimed " + success.ToString() + " row", Color.Blue);
            frmMsg.ShowDialog();
            frmMsg.Dispose();
            this.Close();
            return;

        }

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