// Async Get Method
 public void Get_Bids_Async()
 {
     // async thread
     Task.Run(() =>
         {
             // return list of Bids
             lw_Bids_Worker BWkr = new lw_Bids_Worker();
             return BWkr.Get_Bids_List(ref _strMsg);
         })
         .ContinueWith(task => bvmMod_List = task.Result, context);
 }
 // Async Add Method
 public void Add_Bids_Async(lw_Bids_Model bMod)
 {
     // async thread
     Task.Run(() =>
         {
             // add bids record
             lw_Bids_Worker BWkr = new lw_Bids_Worker();
             return BWkr.Add_Bids_Rec(bMod);
         })
         .ContinueWith(task => AddResult = task.Result, context);
 }
 // Async Get Method
 public void Get_BidsByDate_Async(string strDate)
 {
     // async thread
     Task.Run(() =>
     {
         // return list of Bids
         lw_Bids_Worker BWkr = new lw_Bids_Worker();
         return BWkr.Get_Bids_ByBidSentDate_List(strDate);
     })
         .ContinueWith(task => bvmMod_List = task.Result, context);
 }
 // Aysnc Update Method
 public void Update_Bids_Async(lw_Bids_Model bMod)
 {
     // aysnc thread
     Task.Run(() =>
         {
             lw_Bids_Worker BWkr = new lw_Bids_Worker();
             return BWkr.Update_Bid_rec(bMod);
         })
         .ContinueWith(task => UpdateResult = task.Result, context);
 }
        // constructor
        public Win_LWClient(string ActiveStatus)
        {
            InitializeComponent();
            strMsg = "";
            _ActiveStatus = ActiveStatus;
            Title = _ActiveStatus + " Clients";

            // worker
            BWkr = new lw_Bids_Worker();
            LWCWkr = new lw_Client_Worker();
            LWCCWkr = new lw_ClientContact_Worker();
            LWCLWkr = new lw_ClientLocations_Worker();
            INVWkr = new lw_Invoice_Worker();
            WOWkr = new lw_WorkOrders_Worker();
            

            // LISTENER: Selected data changed
            // Handle on Client selection changed events
            uc_ClientsList.OnClient_SelectedChanged += (o, e) =>
            {
                var datagrid = (DataGrid)o;
                var itm = datagrid.SelectedItem;
                if ((itm is lw_Client_Model) == false) return;
                strMsg = "";

                lw_Client_Model selectedContent = (lw_Client_Model)itm;

                uc_ClientAssocTblDisplay.uc_ClientTabControlRef.uc_ClientDetail.DataContext = selectedContent;

                labelStatus.Content = "Client Account Number: " + selectedContent.AccNum;
                Int64 numID = selectedContent.AccNum;

                // display ClientContact list 
                List<lw_ClientContacts_Model> ccMod_List = LWCCWkr.Get_ClientContactsByAccNum_List(numID);
                uc_ClientAssocTblDisplay.uc_ClientTabControlRef.uc_ClientContactList.DataContext = ccMod_List;

                // display clientlocation list 
                List<lw_ClientLocations_Model> lwclMod_List = LWCLWkr.Get_ClientLocation_ByAccNum_List(numID);
                uc_ClientAssocTblDisplay.uc_ClientsLocationList.DataContext = lwclMod_List;

                // display Bids based on AccNum, list 
                List<lw_Bids_Model> lwbMod_List = BWkr.Get_Bids_ByAccNum_List(numID);
                uc_ClientAssocTblDisplay.uc_ClientTabControlRef.uc_BidsList.DataContext = lwbMod_List;

                // display WorkOrders based on AccNum, list 
                List<lw_WorkOrder_Model> woMod_List_accnNum = WOWkr.Get_WorkerOrders_AccNum_List(numID);
                uc_ClientAssocTblDisplay.uc_ClientTabControlRef.uc_WOList.DataContext = woMod_List_accnNum;

                // display Invoices based on AccNum, list 
                List<lw_Invoice_Model> iMod_List_accnNum = INVWkr.Get_Invoice_ByAccNum_List(numID);
                uc_ClientAssocTblDisplay.uc_ClientTabControlRef.uc_InvList.DataContext = iMod_List_accnNum;

                // change to an Update configuration
                uc_ClientAssocTblDisplay.uc_ClientTabControlRef.uc_ClientDetail.Execute_UpdateButtonConfiguration();
            };



            // LISTENER: OnClientLocation_SELECTED 
            // Handle on selection changed events
            uc_ClientAssocTblDisplay.uc_ClientsLocationList.OnClientLocation_SELECTED += (o, e) =>
            {
                var datagrid = (DataGrid)o;
                var itm = datagrid.SelectedItem;
                if ((itm is lw_ClientLocations_Model) == false) return;
                strMsg = "";

                lw_ClientLocations_Model selectedContent = (lw_ClientLocations_Model)itm;

                string strClLocID = selectedContent.ClientLocationID;
                
                // display Contacts based on ClientLocationID, list 
                List<lw_ClientContacts_Model> ccMod_List_contact = LWCCWkr.Get_ClientContacts_ByLocationId_List(strClLocID);
                uc_ClientAssocTblDisplay.uc_ClientTabControlRef.uc_ClientContactList.DataContext = ccMod_List_contact;

                // display Bids based on ClientLocationID, list 
                List<lw_Bids_Model> lwbMod_List_clLocID = BWkr.Get_Bids_ByClientLocation_List(strClLocID);
                uc_ClientAssocTblDisplay.uc_ClientTabControlRef.uc_BidsList.DataContext = lwbMod_List_clLocID;

                // display WorkOrders based on LocationID, list 
                List<lw_WorkOrder_Model> woMod_List_LocID = WOWkr.Get_WorkerOrders_ByLocationID_List(strClLocID);
                uc_ClientAssocTblDisplay.uc_ClientTabControlRef.uc_WOList.DataContext = woMod_List_LocID;

                // display Invoices based on BillLocation, list 
                List<lw_Invoice_Model> iMod_List_LocID = INVWkr.Get_Invoice_ByBillLocation_List(strClLocID);
                uc_ClientAssocTblDisplay.uc_ClientTabControlRef.uc_InvList.DataContext = iMod_List_LocID;
            };



            // LISTENER: ADD Enabled
            uc_ClientAssocTblDisplay.uc_ClientTabControlRef.uc_ClientDetail.OnClient_CANCEL += (o, e) =>
            {
                labelStatus.Content = "ADD is Enabled.";
                // Display Client List
                List<lw_Client_Model> cMod_List_ae = LWCWkr.Get_Client_List(ref strMsg, _ActiveStatus);
                uc_ClientsList.DataContext = cMod_List_ae;
            };



            // LISTENER: CANCEL
            uc_ClientAssocTblDisplay.uc_ClientTabControlRef.uc_ClientDetail.OnClient_CANCEL += (o, e) =>
            {
                // Not necessary to redisplay data to grid.
                // Grid paramater has been set: IsReadOnly="True";
                labelStatus.Content = "Canceled.";
            };



            // LISTENER: DELETE
            uc_ClientAssocTblDisplay.uc_ClientTabControlRef.uc_ClientDetail.OnClient_DELETE += (o, e) =>
            {
                labelStatus.Content = o.ToString();
                // Display Client List
                List<lw_Client_Model> cMod_List_del = LWCWkr.Get_Client_List(ref strMsg, _ActiveStatus);
                uc_ClientsList.DataContext = cMod_List_del;

                // 
            };



            // LISTENER: ADD
            uc_ClientAssocTblDisplay.uc_ClientTabControlRef.uc_ClientDetail.OnClient_ADD += (o, e) =>
            {
                // display message
                labelStatus.Content = o.ToString();
                // Display Client List
                List<lw_Client_Model> cMod_List_add = LWCWkr.Get_Client_List(ref strMsg, _ActiveStatus);
                uc_ClientsList.DataContext = cMod_List_add;
            };



            // LISTENER: UPDATE
            uc_ClientAssocTblDisplay.uc_ClientTabControlRef.uc_ClientDetail.OnClient_UPDATE += (o, e) =>
            {
                labelStatus.Content = o.ToString();
                // Display Client List
                List<lw_Client_Model> cMod_List_upd = LWCWkr.Get_Client_List(ref strMsg, _ActiveStatus);
                uc_ClientsList.DataContext = cMod_List_upd;
            };



            // Listening for ViewModel Property Change
            // --------------------------------
            // Loading the data grid MVVM style
            // --------------------------------
            clVM.PropertyChanged += (o, e) =>
            {
                // the View Model is notifying us that a property was updated
                // checking for a specific property returned by the View Model
                if (e.PropertyName == "Client_List")
                {
                    uc_ClientsList.DataContext = clVM.cvmMod_List;
                    labelStatus.Content = "Retrieved " + clVM.cvmMod_List.Count + " Clients.";

                    // turn off busy Indicator
                    busy_Indicator.IsBusy = false;

                    // change key board focus
                    Keyboard.Focus(txtSearchClientName);
                }
            };

            // Telling the ViewModel to retieve data
            clVM.Get_Clients_Async(_ActiveStatus);
            labelStatus.Content = "Retrieving data...";
        }  // end of constructor        
        // constructor
        public Win_LWBids()
        {
            InitializeComponent();

            // worker
            LWBWkr = new lw_Bids_Worker();
            
            // LISTENER: Selected data changed
            // Handle on ChemList selection changed events
            uc_BidsList.OnBid_SELECTED += (o, e) =>
            {
                FlowDocument flwDoc = new FlowDocument();

                var p = new Paragraph();
                var run = new Run();

                var datagrid = (DataGrid)o;
                var itm = datagrid.SelectedItem;
                if ((itm is lw_Bids_Model) == false) return;

                lw_Bids_Model selectedContent = (lw_Bids_Model)itm;

                uc_BidsDetail.DataContext = selectedContent;

                // rtf (RichText Formatting) for RichTextBox
                // MemoryStream uses System.IO
                if (selectedContent.Narrative.Trim() == "")
                {
                    uc_BidsDetail.rtbNarrative.Document = flwDoc;
                }
                else
                {
                    MemoryStream ms = new MemoryStream(ASCIIEncoding.Default.GetBytes(selectedContent.Narrative));
                    uc_BidsDetail.rtbNarrative.Selection.Load(ms, DataFormats.Rtf);
                }
                //

                uc_BidsDetail.Execute_UpdateButtonConfiguration();
                labelStatus.Content = "Bid ID: " + selectedContent.BidID;
                
                flwDoc = null;
            };




            // LISTENER: Selection by Sent Date
            uc_BidsList.OnBidSentDate_SELECTED += (o, e) =>
            {

                // turn off busy Indicator
                busy_Indicator.IsBusy = true;

                string strDate = o.ToString();
                bidsVM.Get_BidsByDate_Async(strDate);

                labelStatus.Content = "Retrieving data...";
            };



            // LISTENER: CANCEL
            uc_BidsDetail.OnBid_CANCEL += (o, e) =>
            {
                // Re - Display Bid List 
                // this is an example of reusing 
                // Get_Bids_Async, actually this will
                // not be used on a CANCEL. No need to 
                // redisplay list because the list Grids
                // have the parameter IsReadOnly="True";
                busy_Indicator.IsBusy = false;
                labelStatus.Content = "Cancel";
            };



            // Listening for ViewModel Property Change
            // --------------------------------
            // Loading the data grid MVVM style
            // --------------------------------
            bidsVM.PropertyChanged += (o, e) =>
                {
                    // the View Model is notifying us that a property was updated
                    // checking for a specific property returned by the View Model
                    if (e.PropertyName == "BidsList")
                    {
                        uc_BidsList.DataContext = bidsVM.bvmMod_List;
                        labelStatus.Content = "Retrieved " + bidsVM.bvmMod_List.Count + " Bids.";
                        
                        // turn off busy Indicator
                        busy_Indicator.IsBusy = false;
                    }
                };



            // Telling the ViewModel to retieve data
            bidsVM.Get_Bids_Async();
            labelStatus.Content = "Retrieving data...";
        }