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

class YSHospitalDetail extends StatefulWidget {
  final bool isEdit;
  final int healthId;
  const YSHospitalDetail({Key key, @required this.healthId, this.isEdit = true}) : super(key: key);
  @override
  _YSHospitalDetailState createState() => _YSHospitalDetailState();
}

class _YSHospitalDetailState extends State<YSHospitalDetail> {
  Map info;
  var regId;
  @override
  void initState() {
    Future.delayed(Duration(seconds: 0)).then((value){
      _getHospitalDetail(widget.healthId);
      _getUserData();
    });
    super.initState();
  }
  _getUserData() async{
    Map dict = await ysRequestHttpNoLoading(context, requestType.get, 'user/info', {});
    if(dict!=null){
      regId = dict['region_id'];
      if(dict['health_id']!=null&&widget.healthId==null){
        _getHospitalDetail(dict['health_id']);
      }
    }
  }
  @override
  Widget build(BuildContext context) {
    return YSBase(
      ystitle: '卫生院详情',
     ysright: widget.isEdit?CupertinoButton(
       padding: EdgeInsets.all(0),
       child: Container(
         child: Text('更改绑定',style: TextStyle(fontSize: 14,color: Colors.white,decoration: TextDecoration.none,fontWeight: FontWeight.normal),),
         alignment: Alignment.center,
         height: 30,
       ),
       onPressed: (){
         Navigator.of(context).push(
           CupertinoPageRoute(
             builder: (context){
               return YSHospital(areaId: regId,healthId: info['id'],);
             }
           )
         ).then((value) async{
           Map dict = await ysRequestHttpNoLoading(context, requestType.get, 'user/info', {});
           if(dict!=null){
             _getHospitalDetail(dict['health_id']);
           }
         });
       },
     ):null,
      yschild: SingleChildScrollView(
        child: info==null?Container():Column(
          children: [
            Container(
              height: 50,
              width: MediaQuery.of(context).size.width,
              padding: EdgeInsets.all(15),
              child: Text('${info['name']}',style: TextStyle(fontSize: 16,color: Color(0xFF202329),decoration: TextDecoration.none,fontWeight: FontWeight.normal),),
            ),
            Container(
              width: MediaQuery.of(context).size.width,
              height: 1.5,
              child: Image.asset('lib/images/line.png'),
            ),
            Container(
              width: MediaQuery.of(context).size.width,
              padding: EdgeInsets.all(15),
              child: Text('${info['jurisdiction']}',
                style: TextStyle(fontSize: 14,color: Color(0xFF202329),decoration: TextDecoration.none,fontWeight: FontWeight.normal),),
            ),
            Container(
              width: MediaQuery.of(context).size.width,
              padding: EdgeInsets.only(left: 15,right: 15),
              margin: EdgeInsets.only(top: 10,bottom: 10),
              child: Text('地址：${info['address']}',style: TextStyle(fontSize: 14,color: Color(0xFF202329),decoration: TextDecoration.none,fontWeight: FontWeight.normal),),
            ),
            Container(
              width: MediaQuery.of(context).size.width,
              padding: EdgeInsets.only(left: 15,right: 15,bottom: 15),
              child: Text('咨询电话：${info['contacts']}',style: TextStyle(fontSize: 14,color: Color(0xFF202329),decoration: TextDecoration.none,fontWeight: FontWeight.normal),),
            ),
          ],
        ),
      ),
    );
  }

  _getHospitalDetail (int healthId) async{
    Map dict = await ysRequestHttpNoLoading(context, requestType.post, 'common/healthCenter', {'id':healthId});
    if(dict!=null){
      setState(() {
        info = dict['data'];
      });
    }
  }

}
