import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_wallet/base/YSBase.dart';
import 'package:flutter_wallet/generated/l10n.dart';
import 'package:flutter_wallet/setting/YSAlwayAddressAdd.dart';
import 'package:flutter_wallet/tools/YSColors.dart';
import 'package:flutter_wallet/tools/YSSqflite.dart';
import 'package:flutter_wallet/tools/YSTools.dart';

class YSAlwayAddress extends StatefulWidget {
  final bool isChoose;
  const YSAlwayAddress({Key? key, this.isChoose = false}) : super(key: key);

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

class YSAlwayAddressState extends State<YSAlwayAddress> {
  List _dataArray = [];
  @override
  void initState() {
    networkDelay((){
      _getDataArray();
    });
    super.initState();
  }
  _getDataArray() async{
    YSAlwayAddressTable table = YSAlwayAddressTable().init();
    _dataArray = await table.rawQuery();
    setState(() {});
  }
  @override
  Widget build(BuildContext context) {
    return YSBase(
      ysTitle: S.current.TIANJIACHANGYONGDIZHI,
      ysChild: SizedBox(
        width: ysWidth(context),
        child: Column(
          children: [
            Container(
              height: hsp(45),
              color: YSColors.containColor(context),
              padding: EdgeInsets.only(left: hsp(15),right: hsp(15)),
              child: Row(
                children: [
                  Expanded(child: Text(S.current.QUANBUWANGLUO,style: YSColors.contentStyle(context),)),
                  GestureDetector(
                    onTap: (){
                      Navigator.of(context).push(
                          CupertinoPageRoute(builder: (context){
                            return const YSAlwayAddressAdd();
                          })
                      ).then((value) {
                        _getDataArray();
                      });
                    },
                    child: Image.asset(YSColors.imageStyle(context, '添 加'),color: Colors.black,height: hsp(20),width: hsp(20),),
                  )
                ],
              ),
            ),
            SizedBox(
              height: ysHeight(context)-ysTOP(context)-hsp(115),
              child: _dataArray.isEmpty?Padding(
                padding: EdgeInsets.only(top: hsp(150)),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Image.asset(YSColors.imageStyle(context, '资源 2'),height: hsp(40),width: hsp(70),),
                    Padding(
                      padding: EdgeInsets.only(top: hsp(15)),
                      child: Text(S.current.NINXIANZAIHAIMEIYOULIANXIREN,style: YSColors.subStyle(context),)
                    ),
                    GestureDetector(
                      onTap: (){
                        Navigator.of(context).push(
                          CupertinoPageRoute(builder: (context){
                            return const YSAlwayAddressAdd();
                          })
                        ).then((value) {
                            _getDataArray();
                        });
                      },
                      behavior: HitTestBehavior.opaque,
                      child: Container(
                        margin: EdgeInsets.only(top: hsp(50)),
                        height: hsp(30),
                        width: hsp(150),
                        decoration: BoxDecoration(
                          color: YSColors.buttonColor(context),
                          borderRadius: const BorderRadius.all(Radius.circular(5))
                        ),
                        alignment: Alignment.center,
                        child: Text(S.current.XINJIANLIANXIREN,style: YSColors.sub2Style(context),),
                      ),
                    )
                  ],
                ),
              ):ListView.separated(
                itemBuilder: (context,index){
                  Map item = _dataArray[index];
                  return Container(
                    padding: EdgeInsets.only(top: hsp(10),bottom: hsp(10),left: hsp(15),right: hsp(15)),
                    color: Colors.white,
                    child: Row(
                      children: [
                        YSImage.network(item['icon'],height: hsp(40),width: hsp(40),),
                        Expanded(child: Padding(
                          padding: EdgeInsets.only(left: hsp(5),right: hsp(5)),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(item['name'],style: YSColors.contentStyle(context),),
                              Text(ysTextCut(item['address'],number: 8),style: YSColors.subStyle(context),)
                            ],
                          ),
                        ),),
                        widget.isChoose?GestureDetector(
                          onTap: () async{
                            Navigator.of(context).pop(item);
                          },
                          child: Icon(Icons.add_circle_outline,size: hsp(20),color: Colors.black,),
                        ):Icon(Icons.chevron_right,size: hsp(20),color: YSColors.shadowColor(context),)

                      ],
                    ),
                  );
                },
                separatorBuilder: (context,index){
                  return Divider(height: hsp(1),color: YSColors.lineColor(context),);
                },
                itemCount: _dataArray.length,
                padding: const EdgeInsets.all(0),
              ),
            )
          ],
        ),
      ),
    );
  }
}
