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

class YSDoctor extends StatefulWidget {
  final bool isPolicy;
  const YSDoctor({Key key, this.isPolicy}) : super(key: key);
  @override
  _YSDoctorState createState() => _YSDoctorState();
}

class _YSDoctorState extends State<YSDoctor> {
  List _dataArray = [];
  @override
  void initState() {
    Future.delayed(Duration(seconds: 0)).then((value){
     widget.isPolicy==false?_getDoctorData():_getPolicyDoctorData();
    });
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return YSBase(
      ystitle: '选择专家',
      yschild: Container(
        height: MediaQuery.of(context).size.height,
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.only(topRight: Radius.circular(20),topLeft: Radius.circular(20))
        ),
        child: ListView.builder(
          itemBuilder: (context,index){
            return GestureDetector(
              behavior: HitTestBehavior.opaque,
              onTap: (){
                Navigator.of(context).push(
                  CupertinoPageRoute(
                    builder: (context){
                      return YSAskQuestion(info: _dataArray[index],isPolicy: widget.isPolicy,);
                    }
                  )
                );
              },
              child: Container(
                height: 150,
                child: Column(
                  children: [
                    Container(
                      height: 148.5,
                      child: Row(
                        children: [
                          Container(
                            height: 120,
                            width: 100,
                            decoration: BoxDecoration(
                              color: Color(0xFF41898C),
                              borderRadius: BorderRadius.all(Radius.circular(5)),
                              image: DecorationImage(image: NetworkImage('${_dataArray[index]['avatar']}'),fit: BoxFit.cover)
                            ),
                          ),
                          Container(
                            height: 120,
                            margin: EdgeInsets.only(left: 10),
                            child: Column(
                              children: [
                                Container(
                                  child: Text('${_dataArray[index]['name']}',style: TextStyle(fontSize: 16,color: Color(0xFF292929),fontWeight: FontWeight.w600),overflow: TextOverflow.ellipsis,),
                                  width: MediaQuery.of(context).size.width-140,
                                  margin: EdgeInsets.only(bottom: 5,top: 5),
                                ),
                                Container(
                                  child: Text('${_dataArray[index]['introduction']}',
                                    style: TextStyle(fontSize: 12,color: Color(0xFF808080),fontWeight: FontWeight.normal),overflow: TextOverflow.ellipsis,maxLines: 5,),
                                  width: MediaQuery.of(context).size.width-140,
                                )
                              ],
                            ),
                          )
                        ],
                      ),
                    ),
                    Container(
                      width: MediaQuery.of(context).size.width,
                      height: 1.5,
                      child: Image.asset('lib/images/line.png'),
                    )
                  ],
                ),
              ),
            );
          },
          itemCount: _dataArray.length,
          padding: EdgeInsets.only(left: 15,right: 15,top: 5,bottom: 5),
        ),
      ),
    );
  }

  _getDoctorData() async{
    SharedPreferences prefer = await SharedPreferences.getInstance();
    Map dict = await ysRequestHttp(context, requestType.post, 'doctor/list', {'category_id':prefer.getInt('chapters')+1});
    if(dict!=null){
      setState(() {
        _dataArray = dict['data'];
      });
    }
  }

  _getPolicyDoctorData() async{
    SharedPreferences prefer = await SharedPreferences.getInstance();
    var dict = await ysRequestHttp(context, requestType.get, 'policy/doctor', {});
    if(dict!=null){
      setState(() {
        _dataArray = dict['data']??[];
      });
    }
  }
}
