//
//  SLFeedbackVc.m
//  SLAiELTS
//
//  Created by Gusont on 2023/3/14.
//

#import "SLFeedbackVc.h"
#import "PlaceholderTextView.h"
#import "SLBaseCellView.h"
#import "SLPickView.h"
#import "ZLPhotoBrowerView.h"

@interface SLFeedbackVc ()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet SLBaseCellView *topView;

@property (weak, nonatomic) IBOutlet PlaceholderTextView *textView;

@property (weak, nonatomic) IBOutlet UILabel *textLabel;
@property (weak, nonatomic) IBOutlet UISwitch *feedBackSwitch;
@property (weak, nonatomic) IBOutlet ZLPhotoBrowerView *photoBrowerView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *photoHeightConst;

@property (nonatomic, strong)NSString *classify;

@property (nonatomic, strong) NSMutableArray *photoBrowerModels;

@end

@implementation SLFeedbackVc

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"意见反馈";
    self.classify = @"";
    self.textView.placeholder = @"想对我们说什么？";
    self.textView.font = [UIFont systemFontOfSize:16];
    self.textView.delegate = self;
    self.topView.titleLabel.text = @"选择问题分类";
    self.topView.titleLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.9];
    self.topView.subTitleLabel.text = @"问题类别";
    [self.topView updateConstraintsWith:SLBaseCellView1];
    self.topView.lineView.hidden = YES;
    WS(weakSelf);
    [self.topView addTapWithBlock:^{
        NSMutableArray *arr = [NSMutableArray array];
        [arr addObject:@[@"功能bug",@"提升建议",@"其他类型"]];
        SLPickView *pickView = [[SLPickView alloc] initWithPickerData:arr];
        pickView.resultBlock = ^(NSString * _Nullable str, NSArray *titlesValue) {
            weakSelf.classify = str;
            weakSelf.topView.subTitleLabel.text = str;
        };
        [pickView show];
    }];
    
    self.photoBrowerView.maxSelectCount = 4;
//    self.photoBrowerView.lineCount = 4;
    self.photoBrowerModels = [NSMutableArray array];
    self.photoBrowerView.dataArray = self.photoBrowerModels;
    self.photoBrowerView.selectedData = ^(NSMutableArray<ZLPhotoBrowerModel *> *photoBrowerModels) {
        weakSelf.photoBrowerModels = photoBrowerModels;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            float collectViewHeight = weakSelf.photoBrowerView.collectionView.collectionViewLayout.collectionViewContentSize.height;
            weakSelf.photoHeightConst.constant = collectViewHeight;
        });
    };
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    NSString *toBeString = [textView.text stringByReplacingCharactersInRange:range withString:text];
    if (toBeString.length > 500) {
        return NO;
    }
    self.textLabel.text = [NSString stringWithFormat:@"%ld/500",toBeString.length];
    return YES;
}

- (IBAction)sureBtnClick:(id)sender {
    if (!self.classify.length) {
        [ZFToast ShowWithMessage:@"请选择问题类别"];
        return;
    }
    if (!self.textView.text.length) {
        [ZFToast ShowWithMessage:@"请输入反馈内容"];
        return;
    }
    if (self.textView.text.length) {
        WS(weakSelf);
        void(^InsertFeedback)(NSString *imgUrls) = ^(NSString *imgUrls){
            NSDictionary *dict = @{@"classify" : self.classify, @"content" : self.textView.text, @"isContact" : @(self.feedBackSwitch.isOn), @"url" : imgUrls ?: @""};
            [[SLHttpCenter SharedInstance] postWithUrl:@"/api/User/InsertFeedback" parameter:dict success:^(id responseObject) {
                [ZFToast ShowWithMessage:@"反馈成功"];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.navigationController popToRootViewControllerAnimated:YES];
                });
            } failure:^(SPRequestError *error) {
                
            }];
        };
        if (self.photoBrowerModels.count) {
            [[SLHttpCenter SharedInstance] postWithSubmitUrl:@"/api/User/UploadFiles" parameter:@{} formDataHandel:^(id<AFMultipartFormData>  _Nonnull formData) {
                [self.photoBrowerModels enumerateObjectsUsingBlock:^(ZLPhotoBrowerModel * obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    [formData appendPartWithFileData:obj.imageData name:@"file" fileName:obj.imageName mimeType:@"image/png"];
                }];
            } success:^(id responseObject) {
                NSString *url = [responseObject objectForKey:@"data"];
                InsertFeedback(url);
            } failure:^(SPRequestError *error) {
                
            }];
        }else {
            InsertFeedback(@"");
        }
    }
}

@end
