
//
//
//  Open
//
//  Created by 看看智能 on 2017/3/8.
//  Copyright © 2017年 黄江涛 . All rights reserved.
//

#import "postMessgaeTotal.h"
#import "DOPDropDownMenu.h"
#import "postMessageFirstCatViewController.h"
#import "postMessageCategorySecondMessage.h"
#import "PostCell.h"
typedef void(^CompletionHandle)(NSError *error);
@interface postMessgaeTotal ()<DOPDropDownMenuDataSource,DOPDropDownMenuDelegate,UITableViewDelegate,UITableViewDataSource>{

    
    
}
@property (weak, nonatomic) IBOutlet UIBarButtonItem *barright;
@property (nonatomic, strong) NSArray *classifys;
@property (nonatomic, strong) NSArray *cates;
@property (nonatomic, strong) NSArray *movices;
@property (nonatomic, strong) NSArray *hostels;
@property (nonatomic, strong) NSArray *areas;

@property (nonatomic, strong) NSArray *sorts;
@property (nonatomic, weak) DOPDropDownMenu *menu;
//获取大类的数据
@property (weak, nonatomic) IBOutlet UITableView *tableView_post;
@property(nonatomic,strong)PostMessageViewModel *postVM;
//table的hight
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableViewTopHeight;
//删除的id
@property(nonatomic,strong)NSString *idStr;
@property(nonatomic,strong)NSMutableArray *superDataNsmut;

//大类的字符串
@property(nonatomic,strong)NSString *parentStr;

//小类的字符串
@property(nonatomic,strong)NSString *childStr;


@end

@implementation postMessgaeTotal
-(NSArray *)superData{
    if (!_superData) {
        _superData =[NSArray new];
    }
    return _superData;
}
-(NSMutableArray *)superDataNsmut
{
    if (!_superDataNsmut) {
        _superDataNsmut =[NSMutableArray new];
    }
    return _superDataNsmut;
}



-(PostMessageViewModel *)postVM
{
    if (!_postVM) {
        _postVM =[PostMessageViewModel new];
    }
    return _postVM;


}
-(void)naviga{


}
- (IBAction)uitabbaritem:(UIBarButtonItem *)sender {
         self.navigationController.toolbarHidden=YES;
    UIStoryboard *sb  =[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    trafficViewController*vc=[sb instantiateViewControllerWithIdentifier:@"postMessageCategorySecondMessage"];
    [self.navigationController pushViewController:vc animated:YES];
    
}
//修改
-(void)titleAction
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
   
    }];
//    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"修改" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//     [self xiugai];
//    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
          [self shanchu:self.tableView_post];
    }];     UIAlertAction *action4 = [UIAlertAction actionWithTitle:@"查看" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self chakan];
    }];
        [alert addAction:action2];
    [alert addAction:action3];
//    [alert addAction:action1];
 [alert addAction:action4];
    
    [self presentViewController:alert animated:YES completion:nil];
}
-(void)xiugai
{

}
-(void)chakan{
   [self performSegueWithIdentifier:@"postMessageFirstCatViewController" sender:self];
    
}
-(void)shanchu:(UITableView*)table
{

[self.postVM userId:[getPeopleID PeopleID] andIdString:self.idStr Handle:^(NSError *error) {
     [_tableView_post reloadData];
     [self.tableView_post.mj_header beginRefreshing];
}];

}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    self.navigationController.toolbarHidden=YES;

    [self.navigationController setNavigationBarHidden:NO animated:YES];
    [self.navigationController.navigationBar navBarBackGroundColor:K_MainColor image:nil isOpaque:YES];//颜色
      
    [self.navigationController.navigationBar navBarBottomLineHidden:YES];
    
    //导航栏颜色
   self.navigationController.navigationBar.barTintColor = K_MainColor;
   [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor],
                                                                     NSFontAttributeName : [UIFont fontWithName:@"Helvetica-Bold" size:17]}];
   
   [self.transitionCoordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
       self.navigationController.navigationBar.barTintColor = K_MainColor;
   } completion:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
   }];
    
}
//JAnalytics
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
     [JANALYTICSService startLogPageView:@"免费发布 - 发布列表"];
    [MobClick beginLogPageView:@"免费发布 - 发布列表"];
}
-(void)viewDidDisappear:(BOOL)animated{
    
    [super viewDidDisappear:animated];
     [JANALYTICSService stopLogPageView:@"免费发布 - 发布列表"];
     [MobClick endLogPageView:@"免费发布 - 发布列表"];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self   naviga];
    self.title=@"发布";
    //我的发布
    [self.superDataNsmut addObjectsFromArray:self.superData];
    [self.superDataNsmut addObject:@"全部"];
        self.automaticallyAdjustsScrollViewInsets = NO;
    //头
    self.tableView_post.mj_header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.postVM.dataArr removeAllObjects];
        if (_isMyPublish == YES && self.tuijian == NO) {
            self.page =1;
            self.state =0;
            [self.postVM getDataFromNetCompleteHandle:[getPeopleID PeopleID] PageW:self.page andState:self.state handle:^(NSError *error) {
                if (error) {
                    NSLog(@"error");
                }else{
                    [_tableView_post reloadData];
                    //   重置脚步 没有更多数据
                    self.tableViewTopHeight.constant =0;
                 
                    if (self.postVM.hasMore == YES) {
                           [_tableView_post.mj_footer resetNoMoreData];
                                           [_tableView_post.mj_footer endRefreshingWithNoMoreData];
                    }
                }
                [_tableView_post.mj_header endRefreshing];
            }];
        }else if(_isMyPublish == NO && self.tuijian == NO)
        {
   self.tableViewTopHeight.constant =44;

            [self.postVM getOneParent:self.parentStr andTwoChild:self.childStr Page:1 Handle:^(NSError *error) {
                    self.page =1;
                    if (error) {
                        NSLog(@"error");
                    }else{
                        [_tableView_post reloadData];
                        //   重置脚步 没有更多数据
                        if (self.postVM.hasMore ==YES) {
                                                    [_tableView_post reloadData];
                                      [_tableView_post.mj_footer resetNoMoreData];
                        }
              
                    }
                    [_tableView_post.mj_header endRefreshing];
                }];
        }else if(self.tuijian == YES){
        //推荐的功能
         [self tuijian:1 Handle:^(NSError *error) {
             if (self.postVM.hasMore ==YES) {
                                         [_tableView_post reloadData];
                           [_tableView_post.mj_header endRefreshing];
                 
             }else{
                                         [_tableView_post reloadData];
                 [_tableView_post.mj_header endRefreshing];
                           [_tableView_post.mj_header endRefreshing];
             }
         }];
        }
          }];
    

    //脚的数据
    if (_isMyPublish == YES) {
 
        self.tableView_post.mj_footer =[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
                   self.page ++;
            [self.postVM getDataFromNetCompleteHandle:[getPeopleID PeopleID] PageW:self.page andState:self.state handle:^(NSError *error) {
                if (self.postVM.hasMore == YES) {
                    [_tableView_post reloadData];
                     [_tableView_post.mj_footer endRefreshingWithNoMoreData];
                }else{
                    self.tableView_post.tableFooterView=[[UIView alloc]initWithFrame:CGRectZero];
                    [self.tableView_post reloadData];
                    [_tableView_post.mj_footer endRefreshing];
                }
            }];
        }];
         [self.tableView_post.mj_header beginRefreshing];
    }else if(_isMyPublish == NO){
        self.tableView_post.mj_footer =[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            self.page ++;
            if(self.tuijian == YES){
                [self tuijian:self.page Handle:^(NSError *error) {
                    if (self.postVM.hasMore==YES) {
                        [_tableView_post.mj_footer endRefreshingWithNoMoreData];
                        [self.tableView_post reloadData];
                    }else{
                        [self.tableView_post reloadData];
                        [_tableView_post.mj_footer endRefreshing];
                    }
                }];
            }else{
            [self.postVM getOneParent:self.parentStr andTwoChild:self.childStr Page:self.page Handle:^(NSError *error) {
                if (self.postVM.hasMore==YES) {
                      [_tableView_post.mj_footer endRefreshingWithNoMoreData];
                        [self.tableView_post reloadData];
                }else{
                    [self.tableView_post reloadData];
                    [_tableView_post.mj_footer endRefreshing];
                }
            
            }];
            }
        }];
        [self.tableView_post.mj_header beginRefreshing];
        
        CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
        CGRect rectNav = self.navigationController.navigationBar.frame;
        DOPDropDownMenu *menu = [[DOPDropDownMenu alloc] initWithOrigin:CGPointMake(0, rectStatus.size.height +rectNav.size.height) andHeight:44];
        menu.delegate = self;
        menu.dataSource = self;
        [self.view addSubview:menu];
        _menu = menu;
        
        // 创建menu 第一次显示 不会调用点击代理，可以用这个手动调用
        [menu selectDefalutIndexPath];
    
    }else {
        NSLog(@"asdas");
    
    
    
    }
    self.tableView_post.tableFooterView=[[UIView alloc]initWithFrame:CGRectZero];
}
- (void)menuReloadData
{
    self.classifys = @[@"美食",@"今日新单",@"电影"];
    [_menu reloadData];
}
//获取大类的数据
//- (NSString *)menu:(DOPDropDownMenu *)menu imageNameForRowAtIndexPath:(DOPIndexPath *)indexPath
//{
//    if (indexPath.column == 0 || indexPath.column == 1) {
//        return [NSString stringWithFormat:@"ic_filter_category_%ld",indexPath.row];
//    }
//    return nil;
//}
//
//- (NSString *)menu:(DOPDropDownMenu *)menu imageNameForItemsInRowAtIndexPath:(DOPIndexPath *)indexPath
//{
//    if (indexPath.column == 0 && indexPath.item >= 0) {
//        return [NSString stringWithFormat:@"ic_filter_category_%ld",indexPath.item];
//    }
//    return nil;
//}

// new datasource

//- (NSString *)menu:(DOPDropDownMenu *)menu detailTextForRowAtIndexPath:(DOPIndexPath *)indexPath
//{
//    if (indexPath.column < 2) {
//        return [@(arc4random()%1000) stringValue];
//    }
//    return nil;
//}
//// 新增
//- (NSString *)menu:(DOPDropDownMenu *)menu detailTextForItemsInRowAtIndexPath:(DOPIndexPath *)indexPath
//{
//    return [@(arc4random()%1000) stringValue];
//}
/** 新增
 *  当有column列 row 行 返回有多少个item ，如果>0，说明有二级列表 ，=0 没有二级列表
 *  如果都没有可以不实现该协议
 */


/** 新增
 *  当有column列 row 行 返回有多少个item ，如果>0，说明有二级列表 ，=0 没有二级列表
 *  如果都没有可以不实现该协议
 */
- (NSInteger)menu:(DOPDropDownMenu *)menu numberOfItemsInRow:(NSInteger)row column:(NSInteger)column
{
    if (column == 0) {
        if (row == 0) {
            return self.superChildData_1.count;
        } else if (row == 1){
            return self.superChildData_2.count;
        } else if (row == 2){
            return self.superChildData_3.count;
        }else if (row == 3){
            return self.superChildData_4.count;
        }else if(row == 4){
            return self.superChildData_5.count;
        }else
        {
            return 0;
        }
    }
    return 0;
    
    
    
    
    
}
/** 新增
 *  当有column列 row 行 item项 title
 *  如果都没有可以不实现该协议
 */
- (NSString *)menu:(DOPDropDownMenu *)menu titleForItemsInRowAtIndexPath:(DOPIndexPath *)indexPath
{
     //这里访问小类

    if (indexPath.column == 0) {
        if (indexPath.row == 0) {
            return self.superChildData_1[indexPath.item];
        } else if (indexPath.row == 1){
            return self.superChildData_2[indexPath.item];
        } else if (indexPath.row == 2){
            return self.superChildData_3[indexPath.item];
        }else if (indexPath.row == 3){
            return self.superChildData_4[indexPath.item];
        }else if (indexPath.row == 4){
      return self.superChildData_5[indexPath.item];
        }else
        {
            return 0;
        }
    }
    return nil;
}
//点击了按钮之后
- (void)menu:(DOPDropDownMenu *)menu didSelectRowAtIndexPath:(DOPIndexPath *)indexPath
{
    self.tuijian = NO;
   self.page =1;
    //点击了全部按钮
    if (indexPath.row == 5) {
        self.parentStr =@"";
        self.childStr =@"";
        [self senderOne:self.parentStr senderTwo:self.childStr];
  
    }
    
    
    
    if (indexPath.column == 0 && indexPath.item >=0) {
        NSDictionary *dict =@{self.superData[0]:self.superChildData_1,
                              self.superData[1]:self.superChildData_2,
                              self.superData[2]:self.superChildData_3,
                              self.superData[3]:self.superChildData_4,
                              self.superData[4]:self.superChildData_5,};
                              
        
        
        NSArray *newArray =dict[_superData[indexPath.row]];
        NSString *str1 =newArray[indexPath.item];
        
        self.parentStr =self.superData[indexPath.row];
        self.childStr =str1;
        [self senderOne:_superData[indexPath.row] senderTwo:str1];
        
    
        
        
        
    }else if (indexPath.column == 1) {
   self.tuijian =YES;
        [self.postVM.dataArr removeAllObjects];
    
       [self tuijian:1 Handle:^(NSError *error) {
                 [self.tableView_post reloadData];
           
       }];
        
    }
    
    
    
}
-(void)tuijian:(NSInteger)page Handle:(CompletionHandle)completionHandle{
    
    [self.postVM parent:@"" andCity:@"" andCountry:@"" andPage:page Handle:^(NSError *error) {
        
        
        
        completionHandle(error);
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//有几个column
- (NSInteger)numberOfColumnsInMenu:(DOPDropDownMenu *)menu
{
    return 2;
}
// *  返回 menu 第column列有多少行
- (NSInteger)menu:(DOPDropDownMenu *)menu numberOfRowsInColumn:(NSInteger)column
{

    
    
    if (column == 0) {
        return self.superData.count+1;
    }else if (column == 1){
        return 1;
    }
    return 0;
}
// *  返回 menu 第column列 每行title
- (NSString *)menu:(DOPDropDownMenu *)menu titleForRowAtIndexPath:(DOPIndexPath *)indexPath
{
    if (indexPath.column == 0) {
        return self.superDataNsmut[indexPath.row];
    } else if (indexPath.column == 1){
        return @"推荐";
    }
    return @" ";
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.postVM.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PostCell *cell = [tableView dequeueReusableCellWithIdentifier:@"postCell" forIndexPath:indexPath];
    postMessageResultModel *model =self.postVM.dataArr[indexPath.row];
    cell.Mess =model;

    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    
    if (_isMyPublish == YES) {
        postMessageResultModel *model =self.postVM.dataArr[indexPath.row];
        self.idStr =model.ID;
        [self titleAction];
    }else
    {
        [self performSegueWithIdentifier:@"postMessageFirstCatViewController" sender:self];
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    
 


//    [self.navigationController pushViewController:tc animated:YES];

}
#pragma mark -取分类的值
-(void)senderOne:(NSString*)str1 senderTwo:(NSString*)str2
{
    [self.postVM.dataArr removeAllObjects];
    [self.postVM getOneParent:str1 andTwoChild:str2 Page:1 Handle:^(NSError *error) {
        [self.tableView_post reloadData];
        [self.view reloadInputViews];
      [self.tableView_post.mj_footer beginRefreshing];
    }];
};
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"postMessageFirstCatViewController"]) {
        postMessageFirstCatViewController *tc  =segue.destinationViewController;
   
    postMessageResultModel *models = self.postVM.dataArr[self.tableView_post.indexPathForSelectedRow.row];
        tc.modelSB=models;

        
    }
}





@end
