//
//  GEContractFilterViewController.m
//  Granite Expo eSign
//
//  Created by Ray on 12/30/16.
//  Copyright © 2016 United Software Applications, Inc. All rights reserved.
//

#import "GEContractFilterViewController.h"
#import "DatePickerViewController.h"
#import "RAUtils.h"


@interface GEContractFilterViewController ()

@end

@implementation GEContractFilterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tfAgent.text = self.filterparams[@"agent_name"];
    self.tfJobZip.text = self.filterparams[@"job_zip"];
    self.tfJobAddr.text = self.filterparams[@"job_addr"];
    self.tfJobCity.text = self.filterparams[@"job_city"];
    self.tfCustomer.text = self.filterparams[@"customer_name"];
    self.tfDocument.text = self.filterparams[@"doc_number"];
    self.tfJobState.text = self.filterparams[@"job_state"];
    self.tfCustomerTel.text = self.filterparams[@"tel"];
    
    self.labelJobDate.text =self.filterparams[@"job_date"];
    self.labelSignDate.text =self.filterparams[@"signed_date"];
    

    // Do any additional setup after loading the
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)onSelectSignDate:(id)sender {
    
     __weak __typeof(self)weakSelf = self;
    NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"MM/dd/yyyy"];
    NSString* date = self.labelSignDate.text;

    NSDate* currentDate =nil;
    if(date.length==0)
        currentDate=[NSDate date];
    else
        currentDate=[formatter dateFromString:date];
    
    DatePickerViewController* dpvc =[ [UIStoryboard storyboardWithName:@"CommonEditor" bundle:nil] instantiateViewControllerWithIdentifier:@"DatePickerViewController"];

        dpvc.pickerMode = UIDatePickerModeDate;
 
    
    
    dpvc.date = currentDate;
    dpvc.formatter = formatter;
    dpvc.labelTime.text = [formatter stringFromDate:currentDate];
    
    dpvc.blk_Set = ^(NSString* strdate)
    {
        weakSelf.labelSignDate.text = strdate;
    };
    dpvc.title=@"Please choose sign date";
    [self.navigationController pushViewController:dpvc animated:true];
}

- (IBAction)onSelectJobDate:(id)sender {
    
     __weak __typeof(self)weakSelf = self;
    
    NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"MM/dd/yyyy"];
    NSString* date = self.labelJobDate.text;
    
    NSDate* currentDate =nil;
    if(date.length==0)
        currentDate=[NSDate date];
    else
        currentDate=[formatter dateFromString:date];
    
    DatePickerViewController* dpvc =[ [UIStoryboard storyboardWithName:@"CommonEditor" bundle:nil] instantiateViewControllerWithIdentifier:@"DatePickerViewController"];
    
    dpvc.pickerMode = UIDatePickerModeDate;
    
    
    
    dpvc.date = currentDate;
    dpvc.formatter = formatter;
    dpvc.labelTime.text = [formatter stringFromDate:currentDate];
    
    dpvc.blk_Set = ^(NSString* strdate)
    {
        weakSelf.labelJobDate.text = strdate;
    };
    dpvc.title=@"Please choose job date";
    [self.navigationController pushViewController:dpvc animated:true];
}

- (IBAction)onSearchClick:(id)sender {
    
    
    
    
    NSString* agent_name= self.tfAgent.text;
    NSString* job_zip= self.tfJobZip.text;
    NSString* job_addr= self.tfJobAddr.text;
    NSString* job_city= self.tfJobCity.text;
    NSString* customer_name= self.tfCustomer.text;
    NSString* doc_number= self.tfDocument.text;
    NSString* job_state= self.tfJobState.text;
    NSString* tel= self.tfCustomerTel.text;
    NSString* job_date= self.labelJobDate.text;
    NSString* signed_date= self.labelSignDate.text;

    if(agent_name.length==0)
       [self.filterparams removeObjectForKey:@"agent_name"];
    else
        self.filterparams[@"agent_name"]=agent_name;
    
    if(job_zip.length==0)
        [self.filterparams removeObjectForKey:@"job_zip"];
    else
        self.filterparams[@"job_zip"]=job_zip;
    
    if(job_addr.length==0)
        [self.filterparams removeObjectForKey:@"job_addr"];
    else
        self.filterparams[@"job_addr"]=job_addr;
    
    if(job_city.length==0)
        [self.filterparams removeObjectForKey:@"job_city"];
    else
        self.filterparams[@"job_city"]=job_city;
    
    if(customer_name.length==0)
        [self.filterparams removeObjectForKey:@"customer_name"];
    else
        self.filterparams[@"customer_name"]=customer_name;
    
    if(doc_number.length==0)
        [self.filterparams removeObjectForKey:@"doc_number"];
    else
        self.filterparams[@"doc_number"]=doc_number;
    
    if(job_state.length==0)
        [self.filterparams removeObjectForKey:@"job_state"];
    else
        self.filterparams[@"job_state"]=job_state;
    
    if(tel.length==0)
        [self.filterparams removeObjectForKey:@"tel"];
    else
        self.filterparams[@"tel"]=tel;
    
    if(job_date.length==0)
        [self.filterparams removeObjectForKey:@"job_date"];
    else
        self.filterparams[@"job_date"]=job_date;
    
    if(signed_date.length==0)
        [self.filterparams removeObjectForKey:@"signed_date"];
    else
        self.filterparams[@"signed_date"]=signed_date;
    
    
    [self dismissViewControllerAnimated:false completion:^{
    
        if(self.OnSearch)
            self.OnSearch(self.filterparams);
    }];
}
- (IBAction)onCancelClick:(id)sender {
    [self dismissViewControllerAnimated:false completion:nil];
}
- (IBAction)onResetClick:(id)sender {
    self.tfAgent.text = self.filterparams[@"agent_name"];
    self.tfJobZip.text = self.filterparams[@"job_zip"];
    self.tfJobAddr.text = self.filterparams[@"job_addr"];
    self.tfJobCity.text = self.filterparams[@"job_city"];
    self.tfCustomer.text = self.filterparams[@"customer_name"];
    self.tfDocument.text = self.filterparams[@"doc_number"];
    self.tfJobState.text = self.filterparams[@"job_state"];
    self.tfCustomerTel.text = self.filterparams[@"tel"];
    
    self.labelJobDate =self.filterparams[@"job_date"];
    self.labelSignDate =self.filterparams[@"signed_date"];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
