public int InsertClaimPayment(Claim_ML ml)
        {
            try
            {
                con = new SqlConnection(connectionString);
                con.Open();

                cmd = new SqlCommand("sp_InsertClaimPayment", con) { CommandType = CommandType.StoredProcedure };
                cmd.Parameters.AddWithValue("@OtherCosts", ml.otherCosts);
                cmd.Parameters.AddWithValue("@GarageCosts", ml.garageCosts);
                cmd.Parameters.AddWithValue("@Deductions", ml.Deductions);
                cmd.Parameters.AddWithValue("@PaymentNotes",ml.paymentNotes);

                SqlParameter retval = cmd.Parameters.Add("@PaymentId", SqlDbType.Int);
                retval.Direction = ParameterDirection.ReturnValue;
                cmd.ExecuteNonQuery();
                int paymentId = (int)cmd.Parameters["@PaymentId"].Value;

                insertSparepartPayment(ml, paymentId);

                return paymentId;
            }
            catch (SqlException ex)
            {
                return 0;
            }
            finally
            {
                con.Close();
                con.Dispose();
            }
        }
        public int insertClaim(Claim_ML ml)
        {
            try
            {
                Claim_DL claim = new Claim_DL();
               return claim.insertClaim(ml,claim.InsertClaimPayment(ml),claim.insertDriverDetails(ml),claim.insertThirdPartyDetails(ml));

            }
            catch
            {
                throw;
            }
        }
        protected void ASPxButton1_Click(object sender, EventArgs e)
        {     
            Claim_ML ml = new Claim_ML();
            ml.spareParts = new List<SparepartPayment_ML>();

            for (int i = 0; i < 3; i++)
            {
                ml.spareParts.Add(new SparepartPayment_ML()
                {
                    sparePartId = i,
                    sparePartQty = 2,
                    sparePartCost = 1000
                });
            }
        
                    ml.policyId = 4;
                    ml.location = "Badulla";
                    ml.reason = "Fell asleep";
                    ml.knockedOn = "tree";
                    ml._3rdVehicleNo = "253-3402";
                    ml._3rdOwnerName = "Ishanka";
                    ml._3rdAddress = "199/4, Keppetipola Road,Badulla";
                    ml._3rdContactNo = "0716405220";
                    ml._3rdRenewalDate =DateTime.Now;
                    ml._3rdSpecialNotes = "";
                    ml._3rdVictimName = "Yas";
                    ml._3rdVictimAddress = "";
                    ml._3rdDamageNature = "rear buffer";
                    ml._3rdClaimant = "Ceylinco";
                    ml._3rdAmountClaimed = 0;
                    ml.isDriverOwner = "No";
                    ml.driverName = "Lakshan";
                    ml.driverLicense ="asdasdasd";
                    ml.licenseCat = "A";
                    ml.licenseExpreDate = DateTime.Now;
                    ml.driverNIC = "912701395v";
                    ml.purchaseDate = DateTime.Now;
                    ml.VehicleUsedFor = "Private";
                    ml.rentCompanyName = "";
                    ml.rentAmount = 0;
                    ml.garageCosts = 324234.324f;
                    ml.otherCosts = 324.43f;

            Claim_BL bl = new Claim_BL();
            //bool returValue = bl.insertClaim(ml);
           // ASPxTextBox1.Text = returValue.ToString();
        }
        public int insertClaim(Claim_ML ml, int paymentId, int driverId, int _3rdpartyId)
       {
           try
           {
               var date = DateTime.Now;
               con = new SqlConnection(connectionString);
               con.Open();

               cmd = new SqlCommand("sp_InsertClaim", con) { CommandType = CommandType.StoredProcedure };
               cmd.Parameters.AddWithValue("@policyId", ml.policyId);
               cmd.Parameters.AddWithValue("@location", ml.location);
               cmd.Parameters.AddWithValue("@reason", ml.reason);
               cmd.Parameters.AddWithValue("@knockedOn", ml.knockedOn);
               cmd.Parameters.AddWithValue("@thirdPartyDeatilId", _3rdpartyId);
               cmd.Parameters.AddWithValue("@driverId", driverId);
               cmd.Parameters.AddWithValue("@paymentId", paymentId);
               cmd.Parameters.AddWithValue("@isDriverOwner",ml.isDriverOwner);
               cmd.Parameters.AddWithValue("@vehicleUsage", ml.VehicleUsedFor);
               cmd.Parameters.AddWithValue("@rentCompany", ml.rentCompanyName);
               cmd.Parameters.AddWithValue("@rentAmount", ml.rentAmount);
               cmd.Parameters.AddWithValue("@date", date);
               cmd.Parameters.AddWithValue("@empid", ml.empid);

               SqlParameter retval = cmd.Parameters.Add("@ClaimId", SqlDbType.Int);
               retval.Direction = ParameterDirection.ReturnValue;

               cmd.ExecuteNonQuery();
               int claimId = (int)cmd.Parameters["@ClaimId"].Value;
               return claimId;
           }
           catch (SqlException ex)
           {
               return -1;
           }
           finally
           {
               con.Close();
               con.Dispose();
           }
       }
        public int createClaimObject(string jstring)
        {
            try
            {
                var sparelist = new List<SparepartPayment_ML>();
                var obj = JObject.Parse(jstring);
                var jArray = JObject.Parse(jstring);
                var sparelistArry = JArray.Parse(jArray["spareParts"].ToString());

                foreach (var item in sparelistArry)
                {
                    sparelist.Add(new SparepartPayment_ML()
                    {
                        sparePartId = (int)item["SparepartId"],
                        sparePartCost = (double)item["SparepartCost"],
                        sparePartQty = (double)item["SparepartQty"]
                    });
                }

                var claimObj = new Claim_ML(Convert.ToInt32(jArray["policyId"].ToString()),
                   jArray["location"].ToString(),
                   jArray["reason"].ToString(),
                   jArray["knockedOn"].ToString(),
                   jArray["_3rdVehicleNo"].ToString(),
                   jArray["_3rdOwnerName"].ToString(),
                   jArray["_3rdAddress"].ToString(),
                   jArray["_3rdContactNo"].ToString(),
                   Convert.ToDateTime(jArray["_3rdRenewalDate"].ToString()),
                   jArray["_3rdSpecialNotes"].ToString(),
                   jArray["_3rdVictimName"].ToString(),
                   jArray["_3rdVictimAddress"].ToString(),
                   jArray["_3rdDamageNature"].ToString(),
                   jArray["_3rdClaimant"].ToString(),
                   Convert.ToDouble(jArray["_3rdAmountClaimed"].ToString()),
                   jArray["isDriverOwner"].ToString(),
                   jArray["driverName"].ToString(),
                   jArray["driverLicense"].ToString(),
                   jArray["licenseCat"].ToString(),
                   Convert.ToDateTime(jArray["licenseExpreDate"].ToString()),
                   jArray["driverNIC"].ToString(),
                   Convert.ToDateTime(jArray["purchaseDate".ToString()]),
                   jArray["VehicleUsedFor"].ToString(),
                   jArray["rentCompanyName"].ToString(),
                   0,
                   sparelist,
                   jArray["garageCosts"].ToString(),
                   jArray["otherCosts"].ToString(),
                   jArray["paymentNotes"].ToString(),
                   jArray["Deductions"].ToString(),
                   jArray["empid"].ToString()
                   );

                   var claimId = insertClaim(claimObj);

                   return claimId;
            }
            catch (Exception)
            {
                
                throw;
            }
            
        }
 private async void btnSubmit_Click(object sender, RoutedEventArgs e)
 {
     Regex pattern = new Regex("^[0-9]+$");
     if (!pattern.IsMatch(txtgarageCost.Text))
     {
         ShowMessageBox("Please enter a valid garage cost");
         return;
     }
     if (!pattern.IsMatch(txtDeductions.Text))
     {
         ShowMessageBox("Please enter a valid deduction value");
         return;
     }
     if (!pattern.IsMatch(txtOtherCosts.Text))
     {
         ShowMessageBox("Please enter a valid other cost");
         return;
     }
     LoadingBar.IsEnabled = true;
     LoadingBar.Visibility = Visibility.Visible;
     var ml = new Claim_ML(
          (App.Current as App).policyid,
          (App.Current as App).location,
          (App.Current as App).reason,
          (App.Current as App).knockedOn,
          (App.Current as App)._3rdVehicleRegno,
          (App.Current as App)._3rdOwnerName,
          (App.Current as App)._3rdAddress,
          (App.Current as App)._3rdContact,
          (App.Current as App)._3rdRenewalDate,
          (App.Current as App)._3rdSpecialNotes,
          (App.Current as App).victimName,
          (App.Current as App).victimAddress,
          (App.Current as App).damageNature,
          (App.Current as App)._3rdClaimant,
          (App.Current as App)._3rdClaimAmount,
          (App.Current as App).isDriverOwner,
          (App.Current as App).drivername,
          (App.Current as App).driverLicense,
          (App.Current as App).driverCategories,
          (App.Current as App).driverLicenseExpire,
          (App.Current as App).driverNIC,
          (App.Current as App).dateOfPrchase,
          (App.Current as App).vehicleUsage,
          (App.Current as App).rentName,
          (App.Current as App).rentAmount,
          SparepartList,
          txtgarageCost.Text,
          txtOtherCosts.Text,
          txtPayamentNotes.Text,
          txtDeductions.Text,
          (App.Current as App).empid
          );
     var result = JsonConvert.SerializeObject(ml);
     var returnClaimId = await new ServiceReference1.Service1Client().InsertClaimAsync(result);
     if (returnClaimId != -1)
     {
         UploadPhotos(returnClaimId.ToString());
     }
     
     if (returnClaimId != -1)
     {
         LoadingBar.IsEnabled = false;
         LoadingBar.Visibility = Visibility.Collapsed;
         var msg = String.Format("Your claim Id reference is {0}", returnClaimId.ToString());
         var messageDialog = new Windows.UI.Popups.MessageDialog(msg);
         messageDialog.Title = "Successful";
         await messageDialog.ShowAsync();
         Frame.Navigate(typeof(Views.claimSuccess));
     }
     else
     {
         LoadingBar.IsEnabled = false;
         LoadingBar.Visibility = Visibility.Collapsed;
         var msg = String.Format("Claiming failed.Please re submit.");
         var messageDialog = new Windows.UI.Popups.MessageDialog(msg);
         messageDialog.Title = "Failed";
         await messageDialog.ShowAsync();
     }
     
 }
        public void insertSparepartPayment(Claim_ML ml,int paymentId) {
            try
            {
                con = new SqlConnection(connectionString);
                con.Open();

                if (ml.spareParts !=null)
                {
                    foreach (var spare in ml.spareParts)
                    {
                        cmd = new SqlCommand("sp_InsertSparePartPayment", con) { CommandType = CommandType.StoredProcedure };
                        cmd.Parameters.AddWithValue("@PaymentID", paymentId);
                        cmd.Parameters.AddWithValue("@sparePartId", spare.sparePartId);
                        cmd.Parameters.AddWithValue("@sprePartQty", spare.sparePartQty);
                        cmd.Parameters.AddWithValue("@sparePartCost", spare.sparePartCost);
                        cmd.ExecuteNonQuery();
                    }
                }
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
                con.Dispose();
            }
        }
        public int insertDriverDetails(Claim_ML ml) {
            try
            {
                con = new SqlConnection(connectionString);
                con.Open();

                cmd = new SqlCommand("sp_insertDriver", con) { CommandType = CommandType.StoredProcedure };
                cmd.Parameters.AddWithValue("@driverName", ml.driverName);
                cmd.Parameters.AddWithValue("@licenceNo", ml.driverLicense);
                cmd.Parameters.AddWithValue("@ExpirationDate", ml.licenseExpreDate);
                cmd.Parameters.AddWithValue("@Classes", ml.licenseCat);
                cmd.Parameters.AddWithValue("@driverNIC", ml.driverNIC); 

                SqlParameter retval = cmd.Parameters.Add("@driverId", SqlDbType.Int);
                retval.Direction = ParameterDirection.ReturnValue;
                cmd.ExecuteNonQuery();
                int driverId = (int)cmd.Parameters["@driverId"].Value;

                return driverId;
            }
            catch (SqlException ex)
            {
                return 0;
            }
            finally
            {
                con.Close();
                con.Dispose();
            }
        }
        public int insertThirdPartyDetails(Claim_ML ml) {

            try
            {
                con = new SqlConnection(connectionString);
                con.Open();

                cmd = new SqlCommand("sp_insertThirdPartyDetail", con) { CommandType = CommandType.StoredProcedure };
                cmd.Parameters.AddWithValue("@_3rdVehicleNo", ml._3rdVehicleNo);
                cmd.Parameters.AddWithValue("@_3rdOwnerName", ml._3rdOwnerName);
                cmd.Parameters.AddWithValue("@_3rdAddress", ml._3rdAddress);
                cmd.Parameters.AddWithValue("@_3rdRenewalDate", DateTime.Now);
                cmd.Parameters.AddWithValue("@_3rdSpecialNotes", ml._3rdSpecialNotes);
                cmd.Parameters.AddWithValue("@_3rdVictimName", ml._3rdVictimName);
                cmd.Parameters.AddWithValue("@_3rdVictimAddress", ml._3rdVictimAddress);
                cmd.Parameters.AddWithValue("@_3rdDamageNature", ml._3rdDamageNature);
                cmd.Parameters.AddWithValue("@_3rdClaimant", ml._3rdClaimant);
                cmd.Parameters.AddWithValue("@_3rdAmountClaimed", ml._3rdAmountClaimed);

                SqlParameter retval = cmd.Parameters.Add("@thirdPartyDetailId", SqlDbType.Int);
                retval.Direction = ParameterDirection.ReturnValue;
                cmd.ExecuteNonQuery();
                int thirdPartyDetailId = (int)cmd.Parameters["@thirdPartyDetailId"].Value;

                return thirdPartyDetailId;
            }
            catch (SqlException ex)
            {
                return 0;
            }
            finally
            {
                con.Close();
                con.Dispose();
            }
        }