import 'dart:async';
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

class Promotion {
  int promotionId;
   String promotionName;
   String startDate;
   String expirationDate;
   String shortDescription;
   String description;
   String imgUrl;
   bool isActive;

  Promotion(
      {this.promotionId,
      this.promotionName,
      this.startDate,
      this.expirationDate,
      this.shortDescription,
      this.description,
      this.imgUrl,
      this.isActive});

  factory Promotion.fromJson(Map<String, dynamic> json) {
    return Promotion(
      promotionId: json['promotionId'] as int,
      promotionName: json['promotionName'] as String,
      startDate: json['startDate'] as String,
      expirationDate: json['expirationDate'] as String,
      description: json['description'] as String,
      isActive: json['isActive'] as bool,
    );
  }
}

class RequestListView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return FutureBuilder<List<Promotion>>(
      future: _fetchJobs(),
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          List<Promotion> data = snapshot.data;
          return _jobsListView(data);
        } else if (snapshot.hasError) {
          return Text("${snapshot.error}");
        }
        return CircularProgressIndicator();
      },
    );
  }

  Future<List<Promotion>> _fetchJobs() async {
    Map<String, String> headers = {
      "Content-type": "application/json",
      "Authorization":
          "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJtcm5nb2NodXUiLCJVc2VybmFtZSI6Im1ybmdvY2h1dSIsIkVtYWlsIjoibmdvY2hodUBnbWFpbC5jb20iLCJGdWxsbmFtZSI6Ik5nb2NIdXUiLCJCcmFuZElkIjoiNjYiLCJTdG9yZUlkIjoiNSIsInJvbGUiOiJBZG1pbiIsImp0aSI6IjZiYzQ2YWY0LWU5YWUtNDBmOC1iZjQ4LWE2ZjIwYTQ1MTRmYiIsIm5iZiI6MTYwNTYyNjczOCwiZXhwIjoxNjA4MjE4NzM4LCJpYXQiOjE2MDU2MjY3MzgsImlzcyI6Imh0dHBzOi8vbG9jYWxob3N0OjQ0MzM2LyIsImF1ZCI6Imh0dHBzOi8vbG9jYWxob3N0OjQ0MzM2LyJ9.8uWNcrzvThhgVeaysRkm0b3S-zxJLbivK2HxS6OryQU"
    };
    final response = await http.get(
        'https://apivoucher-develop.azurewebsites.net/api/v1/stores/5/promotions?page=1&pageSize=10&isDesc=false',
        headers: headers);

    if (response.statusCode == 200) {
      var data = json.decode(response.body);
      List rs =  data as List ;
      List<Promotion> result = [];
      print(rs.length);
      rs.forEach((pr) {
        //print(pr['shortDescription']);
        Promotion p = new Promotion();
        p.promotionId = pr["promotionId"] as int;
        p.promotionName = pr["promotionName"] as String;
        p.startDate = pr["startDate"] as String;
        p.expirationDate = pr["expirationDate"] as String;
        p.shortDescription = pr["shortDescription"] as String;
        p.description = pr["description"] as String;
        p.imgUrl = pr["imgUrl"] as String;
        p.isActive = pr["isActive"] as bool;
        result.add(p);
      });
    return result;
     // return data.map((request) => new Promotion.fromJson(request)).toList();
    } else {
      throw Exception('Failed to load jobs from API');
    }
  }

  ListView _jobsListView(data) {
    return ListView.builder(
        itemCount: data.length,
        itemBuilder: (context, index) {
          return _tile(data[index]);
        });
  }

  Widget _tile(Promotion request) {
         return  ListTile(
             leading: const Icon(Icons.flight_land),
             title:  Text(request.shortDescription),
             subtitle: Text(request.startDate) ,
         );
  }
}