import 'package:flutter/material.dart';
import 'package:fun_selfie_app/pages/home/setting.dart';
import 'package:fun_selfie_app/pages/home/search_bar.dart';
import 'package:fun_selfie_app/utils/api.dart';
import 'package:fun_selfie_app/widgets/my_refresh_list.dart';

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  bool isUp = false;
  int currentPage = 1;
  int totalPage = 0;
  final ScrollController _controller = ScrollController();
  // List listData = [];

  @override
  void initState() {
    super.initState();
    // 初始化的时候开启倒计时
    getList();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('趣味自拍'), actions: <Widget>[
        TextButton.icon(
          icon: Icon(
            isUp ? Icons.vertical_align_top : Icons.vertical_align_bottom,
            color: Colors.white,
            size: 32,
          ),
          label: const Text(
            '排序 活动时间',
            style: TextStyle(color: Colors.white, fontSize: 20),
          ),
          onPressed: () {
            setState(() {
              isUp = !isUp;
            });
          },
        ),
        Padding(
            padding: const EdgeInsets.only(left: 30, right: 30),
            child: TextButton(
                child: const Text(
                  '创建',
                  style: TextStyle(color: Colors.white, fontSize: 20),
                ),
                onPressed: () {
                  Navigator.pushNamed(context, "/addNews");
                })),
        Padding(
            padding: const EdgeInsets.only(right: 30),
            child: IconButton(
              icon: const Icon(Icons.search),
              onPressed: () {
                showSearch(context: context, delegate: SeachBarDelegate());
              },
            )),
      ]),
      body: Scrollbar(
        // thumbVisibility: true,
        controller: _controller,
        child: DeerListView(
          controller: _controller,
          onRefresh: onRefresh,
          hasMore: currentPage < totalPage,
          loadMore: onRefresh,
          itemCount: listData.length, //条目个数：获取数据的个数
          itemBuilder: (context, index) {
            return GestureDetector(
              onTap: () {
                Navigator.pushNamed(
                    context, "/newsDetail/${listData[index]["id"]}");
              },
              child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                        padding:
                            const EdgeInsets.only(left: 12, right: 12, top: 10),
                        child: Text(listData[index]["createTime"],
                            style: const TextStyle(
                              fontSize: 16,
                              overflow: TextOverflow.ellipsis,
                            ))),
                    ListTile(
                      title: Text(listData[index]["title"]),
                      subtitle: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          const SizedBox(height: 12),
                          Text(listData[index]["subtitle"],
                              style: const TextStyle(
                                  fontSize: 12, color: Colors.grey)),
                        ],
                      ), //小标题
                      // trailing: Image.network(listData[index]["image"],
                      //     fit: BoxFit.fill, width: 110, height: 90),
                      // visualDensity:
                      //     const VisualDensity(vertical: 4), //增加item高度,优化布局，便于图片展示
                      minVerticalPadding: 8,
                    )
                  ]),
            );
          },
        ),
      ),
      drawer: const SizedBox(
          width: 500,
          child: Drawer(
            child: DrawerModalPage(),
          )),
    );
  }

  List listData = [
    {
      'id': '0001',
      "title": "斗门希尔顿「林俭华&魏映雪」婚礼直播",
      "subtitle": "珠海",
      "image":
          "https://img-blog.csdnimg.cn/c6dfd375abf1433fa3a42951cc186a2b.jpeg",
      'createTime': '2023年6月02日',
    },
    {
      'id': '0002',
      "title": "嘉德诺家庭日",
      "subtitle": "上海",
      "image":
          "https://img-blog.csdnimg.cn/678c0686dc694b65ad6b20693dbc35f1.jpeg",
      'createTime': '2023年5月16日',
    },
    {
      'id': '0003',
      "title": "祥琪酒店「梁培安&丘凤」婚礼直播",
      "subtitle": "上海",
      "image":
          "https://img-blog.csdnimg.cn/63efe7acbac74e7ebce85e3801f948e3.jpeg",
      'createTime': '2023年5月02日',
    },
    {
      'id': '0004',
      "title": "黄府寿宴--广州华南理工大学西湖苑宾馆",
      "subtitle": "广州",
      "image":
          "https://img-blog.csdnimg.cn/c6dfd375abf1433fa3a42951cc186a2b.jpeg",
      'createTime': '2023年4月30日',
    },
    {
      'id': '0005',
      "title": "西卡德高广州总部大厦乔迁庆典",
      "subtitle": "广州",
      "image":
          "https://img-blog.csdnimg.cn/678c0686dc694b65ad6b20693dbc35f1.jpeg",
      'createTime': '2023年4月15日',
    },
    {
      'id': '0006',
      "title": "郭鹏&欧阳思昐 新婚之喜",
      "subtitle": "广州",
      "image":
          "https://img-blog.csdnimg.cn/63efe7acbac74e7ebce85e3801f948e3.jpeg",
      'createTime': '2023年4月02日',
    },
    {
      'id': '0007',
      "title": "深圳宝安格兰云天",
      "subtitle": "广州",
      "image":
          "https://img-blog.csdnimg.cn/c6dfd375abf1433fa3a42951cc186a2b.jpeg",
      'createTime': '20201232',
    },
    {
      'id': '0008',
      "title": "祥琪酒店「梁培安&丘凤」婚礼直播",
      "subtitle": "上海",
      "image":
          "https://img-blog.csdnimg.cn/c6dfd375abf1433fa3a42951cc186a2b.jpeg",
      'createTime': '2023年5月24日',
    },
    {
      'id': '0009',
      "title": "西卡德高广州总部大厦乔迁庆典",
      "subtitle": "上海",
      "image":
          "https://img-blog.csdnimg.cn/c6dfd375abf1433fa3a42951cc186a2b.jpeg",
      'createTime': '2023年5月12日',
    },
  ];
  Future<dynamic> getList() async {
    try {
      // var params = {
      //   "pageNo": currentPage,
      //   "pageSize": 20,
      //   "status": 'DONE', //READY
      //   "saleOrderNo": ''
      // };
      // var res = await UtilHelper.updateList(params);
      var params = {
        "pageNo": currentPage,
        "pageSize": 30,
        "department": 'PACKING',
        "worklineId": 'workline-1-packing',
        "end": false
      };
      var res = await UtilHelper.updatePackingList(params);
      setState(() {
        // if (currentPage > 1) {
        //   listData.addAll(res['records']);
        // } else {
        //   listData = res['records'];
        // }
        // totalPage = res['pages'];
      });
    } catch (err) {
      debugPrint(err.toString());
    }
  }

  /// 处理刷新操作
  Future onRefresh() async {
    await Future.delayed(const Duration(seconds: 1), () {
      ++currentPage;
      getList();
    });
  }
}
