//
//  WorkFlowVC.m
//  Gilliatte
//
//  Created by Lead on 12/08/16.
//  Copyright © 2016 Lead infosoft. All rights reserved.
//

#import "WorkFlowVC.h"
#import "SignatureView.h"
#import "IQUIView+IQKeyboardToolbar.h"
#import "Base64.h"
#import "ApiUtillity.h"
#import "AFNetworking.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "GilliatteVC.h"

@interface WorkFlowVC (){
    NSTimer *timerForSlider;
    NSUInteger tag;
    
    NSMutableArray *getWorkFlowData;
    NSString *imageData;
    
    //Edited
    BOOL isEdited;
    NSMutableArray *EditedArray;
}
@end

@implementation WorkFlowVC

#pragma mark- ViewDidload
- (void)viewDidLoad {
    [super viewDidLoad];
    
    getWorkFlowData = [[NSMutableArray alloc]init];
    
    [self SetPrevNextButton];
    
    //timerForSlider in .h file
//    timerForSlider = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateSlider) userInfo:nil repeats:YES];
    
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(calculateData) userInfo:nil repeats:YES];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self.signatureView setLineWidth:2.0];
    self.signatureView.foregroundLineColor = [UIColor colorWithRed:243.0/255.0 green:13.0/255.0 blue:41.0/255.0 alpha:1.0f];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

-(void)viewWillAppear:(BOOL)animated{
    _view_SignatureView.hidden = true;
    [self getWorkFlowData];

    
}

#pragma mark- All Methode
-(void)SendWorkFlowData{
    
    NSString *Date,*UID,*ProID,*JobID;
    NSData *GetUserData;
    if ((GetUserData = [[NSUserDefaults standardUserDefaults] objectForKey:@"USER_PROFILE"])) {
        NSMutableArray *savedArray = [NSKeyedUnarchiver unarchiveObjectWithData:GetUserData];
        NSMutableArray *UserArray = [[NSMutableArray alloc]init];
        UserArray = savedArray;
        
        NSLog(@"%@",UserArray);
        UID = [NSString stringWithFormat:@"%@",[UserArray valueForKey:@"uid"]];
    }
    if ((Date = [[NSUserDefaults standardUserDefaults] objectForKey:@"SELECTED_DATE"]))
    {
        NSArray *DateSeperate = [Date componentsSeparatedByString:@"/"];
        NSString *Day,*Month,*Year,*OriginalDate;
        OriginalDate = [[NSString alloc]init];
        Day = [DateSeperate objectAtIndex:0];
        Month = [DateSeperate objectAtIndex:1];
        Year = [DateSeperate objectAtIndex:2];
        
        OriginalDate = [OriginalDate stringByAppendingString:Year];
        OriginalDate = [OriginalDate stringByAppendingString:@"-"];
        OriginalDate = [OriginalDate stringByAppendingString:Month];
        OriginalDate = [OriginalDate stringByAppendingString:@"-"];
        OriginalDate = [OriginalDate stringByAppendingString:Day];
        
        Date = OriginalDate;
    }
    else{
        return;
    }
    
    if (!(ProID = [[NSUserDefaults standardUserDefaults] objectForKey:@"PROJECT_ID"]))
    {
        return;
    }
    if ((!(JobID = [[NSUserDefaults standardUserDefaults] objectForKey:@"JOB_ID"]))) {
        JobID = @"-1";
    }
    
    NSString *WorkForce,*Super,*Clerk,*Bricklayer,*Carpenters,*CementMasons,*Electricians,*IronWorkers,*Laborers,*OperatingEng,*Plumbers,*PipeFilters,*SheetMetal,*TruckDrivers,*Miscellaneous,*Total,*EquipmentRental,*Desc;
    
    WorkForce = _txt_WorkForce.text;
    Super = _txt_Super.text;
    Clerk = _txt_Clerk.text;
    Bricklayer = _txt_Bricklayer.text;
    Carpenters = _txt_Carpenters.text;
    CementMasons = _txt_CementMasons.text;
    Electricians = _txt_Electricians.text;
    IronWorkers = _txt_IronWorkers.text;
    Laborers = _txt_Laborers.text;
    OperatingEng = _txt_OperatingEng.text;
    Plumbers = _txt_Plumbers.text;
    PipeFilters = _txt_PipeFilters.text;
    SheetMetal = _txt_sheetmatles.text;
    TruckDrivers = _txt_truckdrivers.text;
    Total = _txt_Total.text;
    NSString *Drywall=_txt_drywall.text;
    NSString *Flooring=_txt_florring.text;
    NSString *insulators=_txt_insulators.text;
    NSString *Painters=_txt_painters.text;
    NSString *Roofes=_txt_roofers.text;
    Miscellaneous=_txt_miscworkers.text;
    EquipmentRental=_txt_EquipmentRental.text;
    
    
    
    /*
     finishers	----------------------- OPTIONAL
     flooring	----------------------- OPTIONAL
     insulators	----------------------- OPTIONAL
     painters	----------------------- OPTIONAL
     roofers		----------------------- OPTIONAL
     */
    
    if ([_textview_Write.text  isEqual: @""] && [_textview_Write.text  isEqual: @"Write Here..."]) {
        return;
    }
    Desc = _textview_Write.text;
    imageData = [self encodeToBase64String:_imageview_Sign.image];
    
    [ApiUtillity setSVProgressHUDView:self.view];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    [manager.requestSerializer setTimeoutInterval:150.0];

    
    NSDictionary *params = @{@"date":Date, @"uid":UID, @"project_id":ProID, @"job_id":JobID, @"super":Super, @"clerk":Clerk, @"brick_layers":Bricklayer, @"carpenters":Carpenters, @"cement":CementMasons, @"electricians":Electricians, @"iron_workers":IronWorkers, @"labors":Laborers, @"engineers":OperatingEng, @"plumbers":Plumbers, @"pipe_fitters":PipeFilters, @"sheet_metals":SheetMetal, @"truck_drivers":TruckDrivers, @"misc_workers":Miscellaneous, @"total":Total, @"equipment_rental":EquipmentRental, @"description":Desc, @"sign":imageData,@"finishers":Drywall,@"flooring":Flooring,@"insulators":insulators,@"painters":Painters,@"roofers":Roofes,};
    
    [manager POST:@"http://p4bdesigns.com/Gilliatteweb/app/api3/workflow_save.php" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject != nil) {
            NSString *Suceess;
            if ((Suceess = [responseObject valueForKey:@"success"])) {
                NSLog(@"WorkFlow record created");
                
                UIAlertView *alert = [[UIAlertView alloc]init];
                alert = [[UIAlertView alloc]initWithTitle:@"Success" message:@"Daily Report has been submitted." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                [alert show];
                
                
                [self.navigationController popViewControllerAnimated:true];
            }
        }
        [ApiUtillity dissmissSVProgressHUDView:self.view];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
        NSInteger statusCode = error.code;
        NSLog(@"%d",(int)statusCode);
        
        if(statusCode == -1001) {
            // request timed out
            NSLog(@"request timed out");
            [ApiUtillity setSVProgressHUDInfoView:self.view];
        }
        else if (statusCode == -1005){
            [ApiUtillity setSVProgressHUDInfoView:self.view];
        }
        else if (statusCode == -1009 || statusCode == -1004) {
            // no internet connectivity
            NSLog(@"No Internet Connection");
            UIAlertView *alert = [[UIAlertView alloc]init];
            alert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"No Internet Connection" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
            [alert show];
        }
            [ApiUtillity dissmissSVProgressHUDView:self.view];
        
    }];
    
}

-(void)SendEditedWorkFlowData{
    
    NSString *Date,*UID,*ProID,*JobID;
    NSData *GetUserData;
    if ((GetUserData = [[NSUserDefaults standardUserDefaults] objectForKey:@"USER_PROFILE"])) {
        NSMutableArray *savedArray = [NSKeyedUnarchiver unarchiveObjectWithData:GetUserData];
        NSMutableArray *UserArray = [[NSMutableArray alloc]init];
        UserArray = savedArray;
        
        NSLog(@"%@",UserArray);
        UID = [NSString stringWithFormat:@"%@",[UserArray valueForKey:@"uid"]];
    }
    if ((Date = [[NSUserDefaults standardUserDefaults] objectForKey:@"SELECTED_DATE"]))
    {
        NSArray *DateSeperate = [Date componentsSeparatedByString:@"/"];
        NSString *Day,*Month,*Year,*OriginalDate;
        OriginalDate = [[NSString alloc]init];
        Day = [DateSeperate objectAtIndex:0];
        Month = [DateSeperate objectAtIndex:1];
        Year = [DateSeperate objectAtIndex:2];
        
        OriginalDate = [OriginalDate stringByAppendingString:Year];
        OriginalDate = [OriginalDate stringByAppendingString:@"-"];
        OriginalDate = [OriginalDate stringByAppendingString:Month];
        OriginalDate = [OriginalDate stringByAppendingString:@"-"];
        OriginalDate = [OriginalDate stringByAppendingString:Day];
        
        Date = OriginalDate;
    }
    else{
        return;
    }
    
    if (!(ProID = [[NSUserDefaults standardUserDefaults] objectForKey:@"PROJECT_ID"]))
    {
        return;
    }
    if ((!(JobID = [[NSUserDefaults standardUserDefaults] objectForKey:@"JOB_ID"]))) {
        JobID = @"-1";
    }
    
    NSString *WorkForce,*Super,*Clerk,*Bricklayer,*Carpenters,*CementMasons,*Electricians,*IronWorkers,*Laborers,*OperatingEng,*Plumbers,*PipeFilters,*SheetMetal,*TruckDrivers,*Miscellaneous,*Total,*EquipmentRental,*Desc;
    
    WorkForce = _txt_WorkForce.text;
    Super = _txt_Super.text;
    Clerk = _txt_Clerk.text;
    Bricklayer = _txt_Bricklayer.text;
    Carpenters = _txt_Carpenters.text;
    CementMasons = _txt_CementMasons.text;
    Electricians = _txt_Electricians.text;
    IronWorkers = _txt_IronWorkers.text;
    Laborers = _txt_Laborers.text;
    OperatingEng = _txt_OperatingEng.text;
    Plumbers = _txt_Plumbers.text;
    PipeFilters = _txt_PipeFilters.text;
    Total = _txt_Total.text;
    EquipmentRental = _txt_EquipmentRental.text;
    NSString *Drywall=_txt_drywall.text;
    NSString *Flooring=_txt_florring.text;
    NSString *insulators=_txt_insulators.text;
    NSString *Painters=_txt_painters.text;
    NSString *Roofes=_txt_roofers.text;
    Miscellaneous=_txt_miscworkers.text;
    SheetMetal = _txt_sheetmatles.text;
    TruckDrivers = _txt_truckdrivers.text;
    Miscellaneous = _txt_miscworkers.text;
    
    
    
    if ([_textview_Write.text  isEqual: @""] && [_textview_Write.text  isEqual: @"Write Here..."]) {
        return;
    }
    Desc = _textview_Write.text;
    imageData = [self encodeToBase64String:_imageview_Sign.image];
    
    //, @"work_force":WorkForce
    
    [ApiUtillity setSVProgressHUDView:self.view];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *params = @{@"date":Date, @"uid":UID, @"project_id":ProID, @"job_id":JobID, @"super":Super, @"clerk":Clerk, @"brick_layers":Bricklayer, @"carpenters":Carpenters, @"cement":CementMasons, @"electricians":Electricians, @"iron_workers":IronWorkers, @"labors":Laborers, @"engineers":OperatingEng, @"plumbers":Plumbers, @"pipe_fitters":PipeFilters, @"sheet_metals":SheetMetal, @"truck_drivers":TruckDrivers, @"misc_workers":Miscellaneous, @"total":Total, @"equipment_rental":EquipmentRental, @"description":Desc, @"sign":imageData,@"finishers":Drywall,@"flooring":Flooring,@"insulators":insulators,@"painters":Painters,@"roofers":Roofes,};
    
    
    [manager POST:@"http://p4bdesigns.com/Gilliatteweb/app/api3/workflow_save.php" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject != nil) {
            NSString *Suceess;
            if ((Suceess = [responseObject valueForKey:@"success"])) {
                NSLog(@"WorkFlow record Updated.");
                
                UIAlertView *alert = [[UIAlertView alloc]init];
                alert = [[UIAlertView alloc]initWithTitle:@"Success" message:@"Daily Report has been submitted." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                [alert show];
                [self.navigationController popViewControllerAnimated:true];
            }
        }
        [ApiUtillity dissmissSVProgressHUDView:self.view];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
        NSInteger statusCode = error.code;
        NSLog(@"%d",(int)statusCode);
        
        if(statusCode == -1001) {
            // request timed out
            NSLog(@"request timed out");
            [ApiUtillity setSVProgressHUDInfoView:self.view];
        }
        else if (statusCode == -1005){
            [ApiUtillity setSVProgressHUDInfoView:self.view];
        }
        else if (statusCode == -1009 || statusCode == -1004) {
            // no internet connectivity
            NSLog(@"No Internet Connection");
            UIAlertView *alert = [[UIAlertView alloc]init];
            alert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"No Internet Connection" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
            [alert show];
        }
            [ApiUtillity dissmissSVProgressHUDView:self.view];
        
    }];
    
}

-(void) getWorkFlowData{
    
    NSString *ProID,*JobID,*Date,*UID;
    NSData *GetUserData;
    if ((GetUserData = [[NSUserDefaults standardUserDefaults] objectForKey:@"USER_PROFILE"])) {
        NSMutableArray *savedArray = [NSKeyedUnarchiver unarchiveObjectWithData:GetUserData];
        NSMutableArray *UserArray = [[NSMutableArray alloc]init];
        UserArray = savedArray;
        
        NSLog(@"%@",UserArray);
        UID = [NSString stringWithFormat:@"%@",[UserArray valueForKey:@"uid"]];
    }
    if (!(Date = [[NSUserDefaults standardUserDefaults] objectForKey:@"SELECTED_DATE"]))
    {
        return;
    }
    else{
        NSArray *DateSeperate = [Date componentsSeparatedByString:@"/"];
        NSString *Day,*Month,*Year,*OriginalDate;
        OriginalDate = [[NSString alloc]init];
        Day = [DateSeperate objectAtIndex:0];
        Month = [DateSeperate objectAtIndex:1];
        Year = [DateSeperate objectAtIndex:2];
        
        OriginalDate = [OriginalDate stringByAppendingString:Year];
        OriginalDate = [OriginalDate stringByAppendingString:@"-"];
        OriginalDate = [OriginalDate stringByAppendingString:Month];
        OriginalDate = [OriginalDate stringByAppendingString:@"-"];
        OriginalDate = [OriginalDate stringByAppendingString:Day];
        
        Date = OriginalDate;
    }
    if (!(ProID = [[NSUserDefaults standardUserDefaults] objectForKey:@"PROJECT_ID"]))
    {
        return;
    }
    if ((!(JobID = [[NSUserDefaults standardUserDefaults] objectForKey:@"JOB_ID"]))) {
        JobID = @"-1";
    }
    
    [ApiUtillity setSVProgressHUDView:self.view];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *params = @{@"date":Date,@"uid":UID,@"project_id":ProID,@"job_id":JobID};
    
    [manager POST:@"http://p4bdesigns.com/Gilliatteweb/app/api3/workflow_get.php" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject != nil) {
            NSLog(@"%@",responseObject);
            NSString *Suceess;
            if ((Suceess = [responseObject valueForKey:@"success"])) {
                if ([Suceess  isEqual: @"ok"]) {
//                [ApiUtillity dissmissSVProgressHUDViewWithSuccess:self.view];
                    
//                    NSMutableArray *dataArray;
                    getWorkFlowData = [[responseObject valueForKey:@"records"] firstObject];
                    
                    if (getWorkFlowData.count != 0) {
                        /*
                        for (NSDictionary *Dic in dataArray) {
                            [getWorkFlowData addObject:Dic];
                        }*/
                        
                        isEdited = 1;
                        
                        NSString *WorkForce,*Super,*Clerk,*Bricklayer,*Carpenters,*CementMasons,*Electricians,*IronWorkers,*Laborers,*OperatingEng,*Plumbers,*PipeFilters,*SheetMetal,*TruckDrivers,*Miscellaneous,*Total,*EquipmentRental,*Desc,*img;
                        NSString *Drywall;
                        NSString *Flooring;
                        NSString *insulators;
                        NSString *Painters;
                        NSString *Roofes;
                        
                        
                        
                        WorkForce = [getWorkFlowData valueForKey:@"work_force"];
                        Super = [getWorkFlowData valueForKey:@"super"];
                        Clerk = [getWorkFlowData valueForKey:@"clerk"];
                        Bricklayer = [getWorkFlowData valueForKey:@"brick_layers"];
                        Carpenters = [getWorkFlowData valueForKey:@"carpenters"];
                        CementMasons = [getWorkFlowData valueForKey:@"cement"];
                        Electricians = [getWorkFlowData valueForKey:@"electricians"];
                        IronWorkers = [getWorkFlowData valueForKey:@"iron_workers"];
                        Laborers = [getWorkFlowData valueForKey:@"labors"];
                        OperatingEng = [getWorkFlowData valueForKey:@"engineers"];
                        Plumbers = [getWorkFlowData valueForKey:@"plumbers"];
                        PipeFilters = [getWorkFlowData valueForKey:@"pipe_fitters"];
                        SheetMetal = [getWorkFlowData valueForKey:@"sheet_metals"];
                        TruckDrivers = [getWorkFlowData valueForKey:@"truck_drivers"];
                        Miscellaneous = [getWorkFlowData valueForKey:@"misc_workers"];
                        Total = [getWorkFlowData valueForKey:@"total"];
                        EquipmentRental = [getWorkFlowData valueForKey:@"equipment_rental"];
                        Drywall=[getWorkFlowData valueForKey:@"finishers"];
                        Flooring=[getWorkFlowData valueForKey:@"flooring"];
                        insulators=[getWorkFlowData valueForKey:@"insulators"];
                        Painters=[getWorkFlowData valueForKey:@"painters"];
                        Roofes=[getWorkFlowData valueForKey:@"roofers"];
                        
                        /*
                         
                       
                         finishers	----------------------- OPTIONAL
                         flooring	----------------------- OPTIONAL
                         insulators	----------------------- OPTIONAL
                         painters	----------------------- OPTIONAL
                         roofers		----------------------- OPTIONAL

                         {
                         "brick_layers" = 0;
                         carpenters = 0;
                         cement = 0;
                         clerk = 0;
                         date = "2017-04-27";
                         description = "Write Here...";
                         electricians = 0;
                         engineers = 0;
                         "equipment_rental" = 0;
                         finishers = 0;
                         flooring = 0;
                         id = 81;
                         insulators = 0;
                         "iron_workers" = 0;
                         "job_id" = "-1";
                         labors = 0;
                         "misc_workers" = 0;
                         painters = 0;
                         "pipe_fitters" = 0;
                         plumbers = 0;
                         "project_id" = 21;
                         roofers = 0;
                         "sheet_metals" = 0;
                         sign = "http://p4bdesigns.com/Gilliatteweb/app/images/uploads/default-img.gif";
                         stamp = "2017-04-27 03:04:52";
                         status = active;
                         super = 0;
                         total = 0;
                         "truck_drivers" = 0;
                         uid = 49;
                         "work_force" = 0;
                         }

                         */
                        

                        
                        Desc = [getWorkFlowData valueForKey:@"description"];
                        img = [getWorkFlowData valueForKey:@"sign"];
                        
                        
                        
                        //[_indicator startAnimating];
                        [_imageview_Sign sd_setImageWithURL:[NSURL URLWithString:img] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                            imageData = [self encodeToBase64String:_imageview_Sign.image];
                            //[_indicator stopAnimating];
                        }];
                        
                        _textview_Write.text = Desc;
                        
                        _txt_WorkForce.text = [NSString stringWithFormat:@"%@",WorkForce];
                        _txt_Super.text = [NSString stringWithFormat:@"%@",Super];
                        _txt_Clerk.text = [NSString stringWithFormat:@"%@",Clerk];
                        _txt_Bricklayer.text = [NSString stringWithFormat:@"%@",Bricklayer];
                        _txt_Carpenters.text = [NSString stringWithFormat:@"%@",Carpenters];
                        _txt_CementMasons.text = [NSString stringWithFormat:@"%@",CementMasons];
                        _txt_Electricians.text = [NSString stringWithFormat:@"%@",Electricians];
                        _txt_IronWorkers.text = [NSString stringWithFormat:@"%@",IronWorkers];
                        _txt_Laborers.text = [NSString stringWithFormat:@"%@",Laborers];
                        _txt_OperatingEng.text = [NSString stringWithFormat:@"%@",OperatingEng];
                        _txt_Plumbers.text = [NSString stringWithFormat:@"%@",Plumbers];
                        _txt_PipeFilters.text = [NSString stringWithFormat:@"%@",PipeFilters];
                        _txt_sheetmatles.text = [NSString stringWithFormat:@"%@",SheetMetal];
                        _txt_truckdrivers.text = [NSString stringWithFormat:@"%@",TruckDrivers];
                        _txt_miscworkers.text = [NSString stringWithFormat:@"%@",Miscellaneous];
                        _txt_Total.text = [NSString stringWithFormat:@"%@",Total];
                        _txt_EquipmentRental.text = [NSString stringWithFormat:@"%@",EquipmentRental];
                        _txt_drywall.text=[NSString stringWithFormat:@"%@",Drywall];
                        _txt_insulators.text=[NSString stringWithFormat:@"%@",Drywall];
                        _txt_painters.text=[NSString stringWithFormat:@"%@",Drywall];
                        _txt_roofers.text=[NSString stringWithFormat:@"%@",Drywall];
                        _txt_florring.text=[NSString stringWithFormat:@"%@",Flooring];
                        
                    
                    
                        
                       
                        
                        
                    }
                }
            }
        }
        [ApiUtillity dissmissSVProgressHUDView:self.view];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
        NSInteger statusCode = error.code;
        NSLog(@"%d",(int)statusCode);
        
        if(statusCode == -1001) {
            // request timed out
            NSLog(@"request timed out");
            [ApiUtillity setSVProgressHUDInfoView:self.view];
        }
        else if (statusCode == -1005){
            [ApiUtillity setSVProgressHUDInfoView:self.view];
        }
        else if (statusCode == -1009 || statusCode == -1004) {
            // no internet connectivity
            NSLog(@"No Internet Connection");
            UIAlertView *alert = [[UIAlertView alloc]init];
            alert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"No Internet Connection" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
            [alert show];
        }
            [ApiUtillity dissmissSVProgressHUDView:self.view];
        
    }];
    
}

-(void)calculateData{
    NSUInteger MainCalculation = 0;
    
    if (![_txt_Super.text isEqualToString:@""]) {
        MainCalculation = MainCalculation + [_txt_Super.text integerValue];
    }
    if (![_txt_Clerk.text isEqualToString:@""]) {
        MainCalculation = MainCalculation + [_txt_Clerk.text integerValue];
    }
    if (![_txt_Bricklayer.text isEqualToString:@""]) {
        MainCalculation = MainCalculation + [_txt_Bricklayer.text integerValue];
    }
    if (![_txt_Carpenters.text isEqualToString:@""]) {
        MainCalculation = MainCalculation + [_txt_Carpenters.text integerValue];
    }
    if (![_txt_CementMasons.text isEqualToString:@""]) {
        MainCalculation = MainCalculation + [_txt_CementMasons.text integerValue];
    }
    if (![_txt_Electricians.text isEqualToString:@""]) {
        MainCalculation = MainCalculation + [_txt_Electricians.text integerValue];
    }
    if (![_txt_IronWorkers.text isEqualToString:@""]) {
        MainCalculation = MainCalculation + [_txt_IronWorkers.text integerValue];
    }
    if (![_txt_Laborers.text isEqualToString:@""]) {
        MainCalculation = MainCalculation + [_txt_Laborers.text integerValue];
    }
    if (![_txt_OperatingEng.text isEqualToString:@""]) {
        MainCalculation = MainCalculation + [_txt_OperatingEng.text integerValue];
    }
    if (![_txt_Plumbers.text isEqualToString:@""]) {
        MainCalculation = MainCalculation + [_txt_Plumbers.text integerValue];
    }
    if (![_txt_PipeFilters.text isEqualToString:@""]) {
        MainCalculation = MainCalculation + [_txt_PipeFilters.text integerValue];
    }
    if (![_txt_sheetmatles.text isEqualToString:@""]) {
        MainCalculation = MainCalculation + [_txt_sheetmatles.text integerValue];
    }
    if (![_txt_truckdrivers.text isEqualToString:@""]) {
        MainCalculation = MainCalculation + [_txt_truckdrivers.text integerValue];
    }
    if (![_txt_miscworkers.text isEqualToString:@""]) {
        MainCalculation = MainCalculation + [_txt_miscworkers.text integerValue];
    }
    if (![_txt_drywall.text isEqualToString:@""]) {
        MainCalculation = MainCalculation + [_txt_drywall.text integerValue];
    }
    if (![_txt_florring.text isEqualToString:@""]) {
        MainCalculation = MainCalculation + [_txt_florring.text integerValue];
    }
    if (![_txt_insulators.text isEqualToString:@""]) {
        MainCalculation = MainCalculation + [_txt_insulators.text integerValue];
    }
    if (![_txt_painters.text isEqualToString:@""]) {
        MainCalculation = MainCalculation + [_txt_painters.text integerValue];
    }
    if (![_txt_roofers.text isEqualToString:@""]) {
        MainCalculation = MainCalculation + [_txt_roofers.text integerValue];
    }
    
    _txt_Total.text = [NSString stringWithFormat:@"%ld",MainCalculation];
}

//Encoding :--
- (NSString *)encodeToBase64String:(UIImage *)image {
    NSData* data = UIImageJPEGRepresentation(image, 1.0f);
    return [Base64 encode:data];
}

//Decoding :--
- (UIImage *)decodeBase64ToImage:(NSString *)strEncodeData {
    NSData *data = [[NSData alloc]initWithBase64EncodedString:strEncodeData options:NSDataBase64DecodingIgnoreUnknownCharacters];
    return [UIImage imageWithData:data];
}

#pragma mark- Methode For Signature
- (void)setSignature{
    NSData *encodeImage;
    encodeImage = UIImagePNGRepresentation(_signatureView.image);
    _imageview_Sign.image = [UIImage imageWithData:encodeImage];
}

-(void)updateSlider
{
    NSString *str = [[NSUserDefaults standardUserDefaults] objectForKey:@"IsScollEnable"];
    if ([str  isEqual: @"YES"]) {
        _scrollview_WorkFlow.scrollEnabled = true;
        [[NSUserDefaults standardUserDefaults] setObject:@"NO" forKey:@"IsScollEnable"];
    }
}

#pragma mark- ButtonAction
- (IBAction)btn_bar_Back:(id)sender
{
//    GilliatteVC *vc=[self.storyboard instantiateViewControllerWithIdentifier:@"GilliatteVC"];
//    [self.navigationController pushViewController:vc animated:YES];
    
    [self.navigationController popViewControllerAnimated:true];
}

- (IBAction)btn_Handler_ImageClear:(id)sender {
    [_signatureView clearWithColor:[UIColor whiteColor]];
    [_signatureView clearWithColor:[UIColor clearColor]];
}

- (IBAction)btn_Handler_Tap_Sign:(id)sender {
    _btn_Tap_Sign.hidden = true;
    [_signatureView clearWithColor:[UIColor whiteColor]];
    [_signatureView clearWithColor:[UIColor clearColor]];
    _view_SignatureView.hidden = false;
}

- (IBAction)btn_Handler_Cancel:(id)sender {
    _btn_Tap_Sign.hidden = false;
    _view_SignatureView.hidden = true;
}


- (IBAction)btn_Handler_PopUpSave:(id)sender {
    _btn_Tap_Sign.hidden = false;
    _view_SignatureView.hidden = true;
    [self setSignature];
}

- (IBAction)btn_Handler_Save:(id)sender {
    if (isEdited == 0) {
        [self SendWorkFlowData];
    }
    else{
        [self SendEditedWorkFlowData];
    }
}


#pragma mark- TextView Action
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    if ([_textview_Write.text  isEqual: @"Write Here..."]) {
        _textview_Write.text = @"";
        _textview_Write.textColor = [UIColor blackColor];
    }
    _textview_Write.textColor = [UIColor blackColor];
    return YES;
}

-(void)textViewDidChange:(UITextView *)textView{
    if (_textview_Write.text.length == 0) {
        _textview_Write.textColor = [UIColor lightGrayColor];
        _textview_Write.text = @"Write Here...";
        [_textview_Write resignFirstResponder];
    }
}


#pragma mark- TextField Action
-(BOOL)textFieldShouldReturn:(UITextField*)textField
{
    [textField resignFirstResponder];
    return NO;
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    return true;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField{
    tag = textField.tag;
}

-(void) SetPrevNextButton{
    
    [_txt_WorkForce addPreviousNextDoneOnKeyboardWithTarget:self previousAction:@selector(previousAction:) nextAction:@selector(nextAction:) doneAction:@selector(doneAction:)];
    [_txt_Super addPreviousNextDoneOnKeyboardWithTarget:self previousAction:@selector(previousAction:) nextAction:@selector(nextAction:) doneAction:@selector(doneAction:)];
    [_txt_Clerk addPreviousNextDoneOnKeyboardWithTarget:self previousAction:@selector(previousAction:) nextAction:@selector(nextAction:) doneAction:@selector(doneAction:)];
    [_txt_Bricklayer addPreviousNextDoneOnKeyboardWithTarget:self previousAction:@selector(previousAction:) nextAction:@selector(nextAction:) doneAction:@selector(doneAction:)];
    [_txt_Carpenters addPreviousNextDoneOnKeyboardWithTarget:self previousAction:@selector(previousAction:) nextAction:@selector(nextAction:) doneAction:@selector(doneAction:)];
    [_txt_CementMasons addPreviousNextDoneOnKeyboardWithTarget:self previousAction:@selector(previousAction:) nextAction:@selector(nextAction:) doneAction:@selector(doneAction:)];
    [_txt_Electricians addPreviousNextDoneOnKeyboardWithTarget:self previousAction:@selector(previousAction:) nextAction:@selector(nextAction:) doneAction:@selector(doneAction:)];
    [_txt_IronWorkers addPreviousNextDoneOnKeyboardWithTarget:self previousAction:@selector(previousAction:) nextAction:@selector(nextAction:) doneAction:@selector(doneAction:)];
    [_txt_Laborers addPreviousNextDoneOnKeyboardWithTarget:self previousAction:@selector(previousAction:) nextAction:@selector(nextAction:) doneAction:@selector(doneAction:)];
    [_txt_OperatingEng addPreviousNextDoneOnKeyboardWithTarget:self previousAction:@selector(previousAction:) nextAction:@selector(nextAction:) doneAction:@selector(doneAction:)];
    [_txt_Plumbers addPreviousNextDoneOnKeyboardWithTarget:self previousAction:@selector(previousAction:) nextAction:@selector(nextAction:) doneAction:@selector(doneAction:)];
    [_txt_PipeFilters addPreviousNextDoneOnKeyboardWithTarget:self previousAction:@selector(previousAction:) nextAction:@selector(nextAction:) doneAction:@selector(doneAction:)];
    [_txt_sheetmatles addPreviousNextDoneOnKeyboardWithTarget:self previousAction:@selector(previousAction:) nextAction:@selector(nextAction:) doneAction:@selector(doneAction:)];
    [_txt_truckdrivers addPreviousNextDoneOnKeyboardWithTarget:self previousAction:@selector(previousAction:) nextAction:@selector(nextAction:) doneAction:@selector(doneAction:)];
    [_txt_miscworkers addPreviousNextDoneOnKeyboardWithTarget:self previousAction:@selector(previousAction:) nextAction:@selector(nextAction:) doneAction:@selector(doneAction:)];
    [_txt_Total addPreviousNextDoneOnKeyboardWithTarget:self previousAction:@selector(previousAction:) nextAction:@selector(nextAction:) doneAction:@selector(doneAction:)];
    [_txt_EquipmentRental addPreviousNextDoneOnKeyboardWithTarget:self previousAction:@selector(previousAction:) nextAction:@selector(nextAction:) doneAction:@selector(doneAction:)];
    [_txt_drywall addPreviousNextDoneOnKeyboardWithTarget:self previousAction:@selector(previousAction:) nextAction:@selector(nextAction:) doneAction:@selector(doneAction:)];
    [_txt_florring addPreviousNextDoneOnKeyboardWithTarget:self previousAction:@selector(previousAction:) nextAction:@selector(nextAction:) doneAction:@selector(doneAction:)];
    [_txt_insulators addPreviousNextDoneOnKeyboardWithTarget:self previousAction:@selector(previousAction:) nextAction:@selector(nextAction:) doneAction:@selector(doneAction:)];
    [_txt_painters addPreviousNextDoneOnKeyboardWithTarget:self previousAction:@selector(previousAction:) nextAction:@selector(nextAction:) doneAction:@selector(doneAction:)];
    [_txt_roofers addPreviousNextDoneOnKeyboardWithTarget:self previousAction:@selector(previousAction:) nextAction:@selector(nextAction:) doneAction:@selector(doneAction:)];
    
}

/*! previousAction. */
-(void)previousAction:(id)button
{
    //previousAction
    //nextAction
    if (_txt_WorkForce.tag == tag) {
        [_txt_WorkForce resignFirstResponder];
        return;
    }
    if (_txt_Super.tag == tag) {
        [_txt_WorkForce becomeFirstResponder];
        [_txt_Super resignFirstResponder];
        return;
    }
    if (_txt_Clerk.tag == tag) {
        [_txt_Super becomeFirstResponder];
        [_txt_Clerk resignFirstResponder];
        return;
    }
    if (_txt_Bricklayer.tag == tag) {
        [_txt_Clerk becomeFirstResponder];
        [_txt_Bricklayer resignFirstResponder];
        return;
    }
    if (_txt_Carpenters.tag == tag) {
        [_txt_Bricklayer becomeFirstResponder];
        [_txt_Carpenters resignFirstResponder];
        return;
    }
    if (_txt_CementMasons.tag == tag) {
        [_txt_Carpenters becomeFirstResponder];
        [_txt_CementMasons resignFirstResponder];
        return;
    }
    if (_txt_Electricians.tag == tag) {
        [_txt_CementMasons becomeFirstResponder];
        [_txt_Electricians resignFirstResponder];
        return;
    }
    if (_txt_IronWorkers.tag == tag) {
        [_txt_Electricians becomeFirstResponder];
        [_txt_IronWorkers resignFirstResponder];
        return;
    }
    if (_txt_Laborers.tag == tag) {
        [_txt_IronWorkers becomeFirstResponder];
        [_txt_Laborers resignFirstResponder];
        return;
    }
    if (_txt_OperatingEng.tag == tag) {
        [_txt_Laborers becomeFirstResponder];
        [_txt_OperatingEng resignFirstResponder];
        return;
    }
    if (_txt_Plumbers.tag == tag) {
        [_txt_OperatingEng becomeFirstResponder];
        [_txt_Plumbers resignFirstResponder];
        return;
    }
    if (_txt_PipeFilters.tag == tag) {
        [_txt_Plumbers becomeFirstResponder];
        [_txt_PipeFilters resignFirstResponder];
        return;
    }
    if (_txt_sheetmatles.tag == tag) {
        [_txt_PipeFilters becomeFirstResponder];
        [_txt_sheetmatles resignFirstResponder];
        return;
    }
    if (_txt_truckdrivers.tag == tag) {
        [_txt_sheetmatles becomeFirstResponder];
        [_txt_truckdrivers resignFirstResponder];
        return;
    }
    if (_txt_miscworkers.tag == tag) {
        [_txt_truckdrivers becomeFirstResponder];
        [_txt_miscworkers resignFirstResponder];
        return;
    }
    if (_txt_Total.tag == tag) {
        [_txt_miscworkers becomeFirstResponder];
        [_txt_Total resignFirstResponder];
        return;
    }
    if (_txt_EquipmentRental.tag == tag) {
        [_txt_Total becomeFirstResponder];
        return;
    }
    if (_txt_drywall.tag == tag) {
        [_txt_drywall resignFirstResponder];
        return;
    }
    if (_txt_florring.tag == tag) {
        [_txt_florring resignFirstResponder];
        return;
    }
    if (_txt_insulators.tag == tag) {
        [_txt_insulators resignFirstResponder];
        return;
        
    }if (_txt_painters.tag == tag) {
        [_txt_painters resignFirstResponder];
        return;
    }if (_txt_roofers.tag == tag) {
        [_txt_roofers resignFirstResponder];
        return;
    }
    
}

/*! nextAction. */
-(void)nextAction:(id)button
{
    NSLog(@"%@",button);
    //nextAction
    if (_txt_WorkForce.tag == tag) {
        [_txt_WorkForce resignFirstResponder];
        [_txt_Super becomeFirstResponder];
        return;
    }
    if (_txt_Super.tag == tag) {
        [_txt_Super resignFirstResponder];
        [_txt_Clerk becomeFirstResponder];
        return;
    }
    if (_txt_Clerk.tag == tag) {
        [_txt_Clerk resignFirstResponder];
        [_txt_Bricklayer becomeFirstResponder];
        return;
    }
    if (_txt_Bricklayer.tag == tag) {
        [_txt_Bricklayer resignFirstResponder];
        [_txt_Carpenters becomeFirstResponder];
        return;
    }
    if (_txt_Carpenters.tag == tag) {
        [_txt_Carpenters resignFirstResponder];
        [_txt_CementMasons becomeFirstResponder];
        return;
    }
    if (_txt_CementMasons.tag == tag) {
        [_txt_CementMasons resignFirstResponder];
        [_txt_drywall becomeFirstResponder];
        return;
    }
    if (_txt_Electricians.tag == tag) {
        [_txt_Electricians resignFirstResponder];
        [_txt_florring becomeFirstResponder];
        return;
    }
    if (_txt_IronWorkers.tag == tag) {
        [_txt_IronWorkers resignFirstResponder];
        [_txt_Laborers becomeFirstResponder];
        return;
    }
    if (_txt_Laborers.tag == tag) {
        [_txt_Laborers resignFirstResponder];
        [_txt_OperatingEng becomeFirstResponder];
        return;
    }
    if (_txt_OperatingEng.tag == tag) {
        [_txt_OperatingEng resignFirstResponder];
        [_txt_painters becomeFirstResponder];
        return;
    }
    if (_txt_Plumbers.tag == tag) {
        [_txt_Plumbers resignFirstResponder];
        [_txt_PipeFilters becomeFirstResponder];
        return;
    }
    if (_txt_PipeFilters.tag == tag) {
        [_txt_PipeFilters resignFirstResponder];
        [_txt_roofers becomeFirstResponder];
        return;
    }
    if (_txt_sheetmatles.tag == tag) {
        [_txt_sheetmatles resignFirstResponder];
        [_txt_truckdrivers becomeFirstResponder];
        return;
    }
    if (_txt_truckdrivers.tag == tag) {
        [_txt_truckdrivers resignFirstResponder];
        [_txt_miscworkers becomeFirstResponder];
        return;
    }
    if (_txt_miscworkers.tag == tag) {
        [_txt_miscworkers resignFirstResponder];
        [_txt_Total becomeFirstResponder];
        return;
    }
    if (_txt_Total.tag == tag) {
        [_txt_Total resignFirstResponder];
        [_txt_EquipmentRental becomeFirstResponder];
        return;
    }
    if (_txt_EquipmentRental.tag == tag) {
        [_txt_EquipmentRental resignFirstResponder];
        return;
    }
    if (_txt_drywall.tag == tag) {
        [_txt_EquipmentRental resignFirstResponder];
        [_txt_Electricians becomeFirstResponder];

        return;
    }
    if (_txt_florring.tag == tag) {
        [_txt_florring resignFirstResponder];
        [_txt_insulators becomeFirstResponder];

        return;
    }
    if (_txt_insulators.tag == tag) {
        [_txt_insulators resignFirstResponder];
        [_txt_IronWorkers becomeFirstResponder];

        return;
        
    }if (_txt_painters.tag == tag) {
        [_txt_painters resignFirstResponder];
        [_txt_Plumbers becomeFirstResponder];

        return;
    }if (_txt_roofers.tag == tag) {
        [_txt_roofers resignFirstResponder];
        [_txt_sheetmatles becomeFirstResponder];

        return;
    }
    
    
}

/*! doneAction. */
-(void)doneAction:(UIBarButtonItem*)barButton
{
    //doneAction
    [self.view endEditing:true];
}
@end
