import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_tzh/home/view/YSHomeListItemView.dart';
import 'package:flutter_tzh/tool/YSNetWork.dart';
import 'package:flutter_tzh/tool/YSRefrehLoad.dart';
import 'package:flutter_tzh/tool/YSTools.dart';

import '../mine/YSMineGatherPlan.dart';

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

  @override
  YSHomeState createState() => YSHomeState();
}

class YSHomeState extends State<YSHome> {
  List _dataArray = [];
  final List _headArray = [
    {'status':'全部','number':0,'key':'total'},
    {'status':'待采样','number':0,'key':'sam'},
    {'status':'进行中','number':0,'key':'ing'},
    {'status':'已完成','number':0,'key':'end'}
  ];
  @override
  void initState() {
    Future.delayed(const Duration(seconds: 0)).then((value) {
      _getHomeSampStatistics();
    });
    super.initState();
  }
  _getHomeSampStatistics() async{
    YSNetWork.ysRequestHttp(context, type: RequestType.get, api: '/samp/statistics', parameter: {}, successSetter: (dict){
      Map data = dict['data']??{};
      for (Map element in _headArray) {
        element['number'] = data[element['key']]??0;
      }
      setState(() {});
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.transparent,
      body: SingleChildScrollView(
        child: Stack(
          children: [
            // Image.asset('images/card-bg.png',height: hsp(200),width: ysWidth(context),),
            Container(
              height: hsp(200),
              decoration: const BoxDecoration(
                gradient: LinearGradient(colors: [Color(0xFF3E434E),Color(0xFF8A93A0)])
              ),
              alignment: Alignment.center,
              child: SizedBox(
                width: ysWidth(context)*0.6,
                height: hsp(120),
                child: GridView.builder(
                  gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 2,
                    childAspectRatio: 2,
                  ),
                  itemBuilder: (context,index){
                    Map item = _headArray[index];
                    return Container(
                      alignment: Alignment.center,
                      child: Column(
                        mainAxisSize: MainAxisSize.min,
                        // crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(item['status']??'',style: TextStyle(fontSize: zsp(13),color: const Color(0xFFACB5C5)),),
                          Text('${item['number']}',style: TextStyle(fontSize: zsp(22),color: Colors.white),maxLines: 1,)
                        ],
                      ),
                    );
                  },
                  itemCount: _headArray.length,
                  padding: const EdgeInsets.all(0),
                  physics: const NeverScrollableScrollPhysics(),
                ),
              ),
            ),
            Container(
              margin: EdgeInsets.only(top: hsp(170)),
              height: ysHeight(context)-114-hsp(170),
              decoration: const BoxDecoration(
                color: Color(0xFF23262B),
                borderRadius: BorderRadius.all(Radius.circular(24))
              ),
              child: Column(
                children: [
                  Container(
                    height: hsp(40),
                    padding: EdgeInsets.only(left: hsp(15),right: hsp(15)),
                    child: Row(
                      children: [
                        SizedBox(
                          width: ysWidth(context)*0.7-hsp(30),
                          child: Text('采样计划',style: TextStyle(fontSize: zsp(14),color: Colors.white),),
                        ),
                        GestureDetector(
                          onTap: (){
                            Navigator.of(context).push(
                                CupertinoPageRoute(builder: (context){
                                  return const YSMineGatherPlan();
                                })
                            );
                          },
                          behavior: HitTestBehavior.opaque,
                          child: Container(
                            alignment: Alignment.centerRight,
                            width: ysWidth(context)*0.3,
                            child: Text('查看更多',style: TextStyle(fontSize: zsp(11),color: const Color(0xFFACB5C5)),),
                          ),
                        )
                      ],
                    ),
                  ),
                  SizedBox(
                    height: ysHeight(context)-114-hsp(210),
                    child: YSRefreshLoad(
                      postData: (value) {
                        _dataArray = value;
                        setState(() {});
                      },
                      url: '/samp/index',
                      request: const {},
                      child: SingleChildScrollView(
                        child: ListView.separated(
                          itemBuilder: (context, index) {
                            Map item = _dataArray[index];
                            return YSHomeListItemView(item: item);
                          },
                          separatorBuilder: (context,index){
                            return Container(height: hsp(16),);
                          },
                          itemCount: _dataArray.length,
                          padding: const EdgeInsets.all(0),
                          shrinkWrap: true,
                          physics: const NeverScrollableScrollPhysics(),
                        ),
                      ),
                    ),
                  )
                ],
              ),
            )
          ],
        )
      ),
    );
  }
}
