import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutterappfuyou/code/base/YSTools.dart';
import 'base/YSBase.dart';
import 'base/YSNetWorking.dart';

class YSPhoto extends StatefulWidget {
  @override
  _YSPhotoState createState() => _YSPhotoState();
}
String timeSwitch(String timeStr){
  DateTime time = DateTime.parse(timeStr);
  return "${time.month.toString().padLeft(2,'0')}月";
}
class _YSPhotoState extends State<YSPhoto> {
  List dataArray = [];
  @override
  void initState() {
    Future.delayed(Duration(seconds: 0)).then((value){
      _getPhotoData();
    });
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
//    List dataArray = [{'time':'2020-07-08 15:32:41','photo':['0']},{'time':'2020-07-08 15:32:41','photo':['0','0']},{'time':'2020-07-08 15:32:41','photo':['0','0','0','0']},{'time':'2020-07-08 15:32:41','photo':['0']}
//      ,{'time':'2020-07-08 15:32:41','photo':['0','0','0','0','0','0','0','0','0','0']},{'time':'2020-07-08 15:32:41','photo':['0','0','0','0','0','0']}];
    return YSBase(
      ystitle: '相册',
      yschild: Container(
        height: MediaQuery.of(context).size.height-75,
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.only(topRight: Radius.circular(20),topLeft: Radius.circular(20))
        ),
        child: dataArray.length==0?Container(
          alignment: Alignment.center,
          child: Image.asset('lib/images/none.png',height: 200,width: 200,),
        ):ListView.builder(itemBuilder: (context,index){
          return Container(
            padding: EdgeInsets.only(left: 20,right: 20,top: 5,bottom: 5),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(timeSwitch(dataArray[index]['time']),style: TextStyle(fontSize: 18,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.w600),),
                Container(
                  width: MediaQuery.of(context).size.width-150,
                  height: dataArray[index]['covers'].length==0?0:dataArray[index]['covers'].length%3!=0?70.0*(((dataArray[index]['covers'].length)~/3)+1):70.0*((dataArray[index]['covers'].length)~/3),
                  margin: EdgeInsets.only(left: 30),
                  child: GridView.builder(
                    gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                      crossAxisCount: 3,
                    ),
                    itemBuilder: (context,indexSub){
                      return GestureDetector(
                        onTap: (){
                          Navigator.of(context).push(
                            CupertinoPageRoute(builder: (context){
                              return YSPhotoDetail(url: dataArray[index]['covers'][indexSub],);
                            })
                          );
                        },
                        behavior: HitTestBehavior.opaque,
                        child: Container(
                          height: 70,
                          width: 70,
                          margin: EdgeInsets.all(5),
                          color: Color(0xFFEFEFEF),
                          child: Image(image: NetworkImage('${dataArray[index]['covers'][indexSub]}'),fit: BoxFit.fill,height: double.infinity,width: double.infinity,),
                        ),
                      );
                    },
                    itemCount: dataArray[index]['covers'].length,
                    physics: NeverScrollableScrollPhysics(),
                    padding: EdgeInsets.all(0),
                  ),
                )
              ],
            ),
          );
        },itemCount: dataArray.length,padding: EdgeInsets.only(top: 15,bottom: 15),),
      )
      ,
    );
  }

  _getPhotoData() async{
    Map dict = await ysRequestHttp(context, requestType.get, 'diary/album', {});
    if(dict!=null){
      setState(() {
        dataArray = dict['data']['data']??[];
      });
    }
  }
}


class YSPhotoDetail extends StatefulWidget {
  final String url;
  const YSPhotoDetail({Key key, this.url}) : super(key: key);

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

class _YSPhotoDetailState extends State<YSPhotoDetail> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: GestureDetector(
        onTap: (){
          Navigator.pop(context);
        },
        child: Container(
          color: Colors.black,
          width: ysWidth(context),
          height: ysHeight(context),
          child: Image.network(widget.url),
        ),
      ),
    );
  }
}
