import { Component, OnInit ,ViewChild} from '@angular/core';
import * as moment from 'moment';
import { FormGroup, FormBuilder, Validators, FormControl } from '@angular/forms';
import { HyperService } from '../../core/services/http.service';
import { Router } from "@angular/router";
import { ActivatedRoute } from '@angular/router';
import { LocalStorage } from '../../core/services/local_storage.service';
import { BulletinDetailsComponent } from '../bulletin-details/bulletin-details.component';
import { Location } from '@angular/common';
declare var $: any;

@Component({
  selector: 'app-wallet-desc',
  templateUrl: './product_description.html',
  styleUrls: ['./product_description.scss']
})
export class WalletProductDescComponent implements OnInit {

  @ViewChild(BulletinDetailsComponent) content_modal: BulletinDetailsComponent;

  loader: boolean;
  order_history: any;
  sales_date: any;
  user_dashboard: any;
  percentage: any;
  otpform: FormGroup;
  mpin:string='';
  mpin_error:boolean=false;
  current_amount:string='';
  cat_list:any;
  catlist:boolean=true;
  productlist:boolean=false;
  product_list:any;
  prod_desc:any;
  amount:string='';
  sub: any;
  view_type: any;
  constructor(private server: HyperService, private router: Router,private formBuilder: FormBuilder,private route: ActivatedRoute, private _location: Location, ) {
    this.sub = this.route.params.subscribe(params => {
        this.view_type = params['id'];
    });
    // console.log(this.view_type,"view_type")
  }

  ngOnInit() { 
    this.catlist=true;
    this.onClickPurchase();
    
   
  } 
    
  getTrainingList() {
      this.loader = true
      this.server.get("dashboard/orderhistory?page=0&limit=10")
          .then((data) => {
              if (data.status == 200 && data.result.response != 'failed' ) {
                  this.loader = false;
                  this.order_history = data.result.data;
                  this.current_amount=data.result.current_amount;
                  console.log(this.order_history,"this.order_history")
                //   LocalStorage.setValue('bullet_in', this.bullet_in)
                 
              } else if (data.result.status == 401 && data.result.response == 'failed') {
                  this.router.navigateByUrl('');
              }
          });
  }

  onSelect(list) {
      this.content_modal.showcontentrModal('Result', list, (data) => { 
      })
  }
  
  formBuild() {
    this.otpform = this.formBuilder.group({
        mpin: [null, Validators.compose([Validators.required])],
        
    });
}
onSubmitRedeem(){
    // console.log("mpin",this.mpin.length)
    if(this.amount!='' ){
    this.mpin_error=false;
    this.loader = true
    this.server.post("redeem/order",{product_price:this.amount,product_id:this.view_type})
        .then((data) => {
            if (data.status == 200 && data.result.response != 'failed' && data.result.type==1) {
                this.loader = false;
                $('#pinSuceessModalCenter').modal('show');
                $('#pinSuceessModalCenter').modal({
                    backdrop: 'static',
                    keyboard: false
                })
                // this._location.back();
             }else if (data.status == 200 && data.result.response != 'failed' && data.result.type==2) {
                this.loader = false;
                $('#progressModalCenter').modal('show');
                $('#progressModalCenter').modal({
                    backdrop: 'static',
                    keyboard: false
                })
                // this._location.back();
             }  else if (data.result.status == 406 && data.result.response == 'failed') {
                this.loader = false;
                alert(data.result.message)
            }else{
                this.router.navigateByUrl('');
            }
        });
    }else{
        // this.mpin_error=true;
        alert("Please enter/select the amount")
    }
}
onSubmitProgress(){
    $('#progressModalCenter').modal('hide');
    this._location.back();
}
onSubmit(){
    $('#pinSuceessModalCenter').modal('hide');
    this._location.back();
}
is_selected: any = [];
slab_arr:any=[];
selectedItem(list,i){
    for (let i = 0; i <= this.slab_arr.length; i++) {
        this.is_selected[i] = ""
      }
      this.is_selected[i] = "selected";
      this.amount=list;
      console.log(this.amount,"amount")
      console.log(this.is_selected[i],"this.is_selected[i]")
}

onClickPurchase(){
    // this.router.navigateByUrl('');
    this.loader = true
    this.server.get("redeem/product/"+this.view_type)
        .then((data) => {
            if (data.status == 200 && data.result.response != 'failed' ) {
                this.loader = false;
                this.prod_desc =  data.result.data;
                if(this.prod_desc.custom_denominations!=null && this.prod_desc.price_type=='Slab'){
                    this.slab_arr=this.prod_desc.custom_denominations.split(',')
                    console.log(this.slab_arr,"this.slab_arr")
                }
                console.log( this.prod_desc ," this.prod_desc ")
               
            } else if (data.result.status == 401 && data.result.response == 'failed') {
                this.router.navigateByUrl('');
            }
        });
}
// onClickCategory(data){
//     this.catlist=false;
//     this.productlist=true;
//     this.loader = true
//     this.server.get("redeem/category/121" )
//         .then((data) => {
//             if (data.status == 200 && data.result.response != 'failed' ) {
//                 this.loader = false;
//                 this.product_list = data.result.data._embedded.product;
//                 console.log(this.product_list[0].images.base_image,"this.product_list")
               
//             } else if (data.result.status == 401 && data.result.response == 'failed') {
//                 this.router.navigateByUrl('');
//             }
//         });
// }
 
}