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

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

class Actiondetail {
  int id;
   int productCollectionId;
   bool isInclude;
  bool isActive;
   int actionGroupId;
   int discountAmount;
   int discountPercent;
   int discountUnitPercent;
   int actionGroup;
   int productCollection;

  factory Actiondetail.fromJson(Map<String, dynamic> json) {
    return Actiondetail(
      id: json['id'] as int,
      productCollectionId: json['productCollectionId'] as int,
      isActive: json['isActive'] as bool,
      actionGroupId: json['actionGroupId'] as int,
      discountAmount: json['discountAmount'] as int,
      discountPercent: json['discountPercent'] as int,
      discountUnitPercent: json['discountUnitPercent'] as int,
      actionGroup: json['actionGroup'] as int,
      productCollection: json['productCollection'] as int,
    );
  }
  Actiondetail(
  {
    this.id,
    this.productCollectionId,
    this.isInclude,
    this.isActive,
    this.actionGroupId,
    this.discountAmount,
    this.discountPercent,
    this.discountUnitPercent,
    this.actionGroup,
    this.productCollection
});


}



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

  Future<List<Actiondetail>> _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/5',
        headers: headers);

    if (response.statusCode == 200) {
      var data = json.decode(response.body);
      List rs =  data["actiondetail"] as List ;
      List<Actiondetail> result = [];
      print(rs.length);
      rs.forEach((pr) {
        //print(pr['shortDescription']);
        Actiondetail p = Actiondetail.fromJson(pr);
        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(Actiondetail request) {
         return  ListTile(
             leading: const Icon(Icons.flight_land),
             title:  Text(request.id.toString()), // còn 1 đóng info chị cứ theo vẽ mà ra nha
             subtitle: Text(request.productCollectionId.toString()) ,
         );
  }
}