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

class YSChooseArea extends StatefulWidget {
  @override
  _YSChooseAreaState createState() => _YSChooseAreaState();
}

class _YSChooseAreaState extends State<YSChooseArea> {
  List _dataArray = [];
  bool _isStreet = false;
  Map _area;
  Map _street;
  @override
  void initState() {
    Future.delayed(Duration(seconds: 0)).then((value){
      _getAreaData();
    });
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return YSBase(
      ystitle: '区域选择',
      yschild: Container(
        height: MediaQuery.of(context).size.height,
        width: MediaQuery.of(context).size.width,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.only(topLeft: Radius.circular(20),topRight: Radius.circular(20)),
          color: Colors.white,
        ),
        child: ListView.builder(
            itemBuilder: (context,index){
              return Column(
                children: [
                  GestureDetector(
                    behavior: HitTestBehavior.opaque,
                    onTap: (){
                      if(_isStreet==false){
                        _isStreet = true;
                        _area = _dataArray[index];
                        _getStreetData(_dataArray[index]);
                      }else{
                        _street = _dataArray[index];
                        _getHealthData();
                      }
                    },
                    child: Container(
                        padding: EdgeInsets.all(15),
                        height: 50,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text('${_dataArray[index]['name']}',style: TextStyle(fontSize: 14,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.normal),),
                            Icon(Icons.keyboard_arrow_right,size: 20,color: Color(0xFFAEB3BD),),
                          ],
                        )
                    ),
                  ),
                  Container(
                    width: MediaQuery.of(context).size.width,
                    height: 1.5,
                    child: Image.asset('lib/images/line.png'),
                  )
                ],
              );
            },
            itemCount: _dataArray.length,
          padding: EdgeInsets.only(top: 10),
        ),
      ),
    );
  }

  _getAreaData() async{
    Map dict = await ysRequestHttpNoLoading(context, requestType.post, 'common/county', {});
    if(dict!=null){
      setState(() {
        _dataArray = dict['data'];
      });
    }
  }

  _getStreetData(Map area) async{
    Map dict = await ysRequestHttpNoLoading(context, requestType.post, 'common/street', {'id':area['id']});
    if(dict!=null){
      setState(() {
        _dataArray = dict['data'];
      });
    }
  }

  _getHealthData() async{
    Map dict = await ysRequestHttpNoLoading(context, requestType.post, 'common/countyHealth', {'region_id':_area['id']});
    if(dict!=null){
      // LogUtil.d(_area);
      // return;
      List datas = dict['data']['datas']??[];
      LogUtil.d(datas);
      if(datas.isNotEmpty){
        Navigator.of(context).pop({
          'healthId':datas[0]['id'],
          'health':'${datas[0]['name']}',
          'areaId':_area['id'],
          'area':'${_area['name']}',
          'streetId':_street['id'],
          'street':'${_street['name']}'
        });
      }else{
        Navigator.of(context).pop({
          'healthId':0,
          'health':'',
          'areaId':_area['id'],
          'area':'${_area['name']}',
          'streetId':_street['id'],
          'street':'${_street['name']}'
        });
      }

    }
  }
}


class YSChooseType extends StatelessWidget {
  final List dataArray = ['居民身份证','军人身份证','护照','港澳居民来往内地通行证','台湾居民来往内地通行证','中华人民共和国旅行证','其他'];
  @override
  Widget build(BuildContext context) {
    return YSBase(
      ystitle: '类型选择',
      yschild: Container(
        decoration: BoxDecoration(
          borderRadius: BorderRadius.only(topLeft: Radius.circular(20),topRight: Radius.circular(20)),
          color: Colors.white,
        ),
        child: ListView.builder(
            itemBuilder: (context,index){
              return GestureDetector(
                behavior: HitTestBehavior.opaque,
                onTap: (){
                  Navigator.of(context).pop(dataArray[index].toString());
                },
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                      padding: EdgeInsets.all(15),
                      height: 50,
                      child: Text(dataArray[index].toString(),style: TextStyle(fontSize: 14,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.normal),),
                    ),
                    Container(
                      width: MediaQuery.of(context).size.width,
                      height: 1.5,
                      child: Image.asset('lib/images/line.png'),
                    )
                  ],
                ),
              );
            },
            itemCount: dataArray.length
        ),
      ),
    );
  }
}
