import 'package:flutter/material.dart';
import 'package:liftmanager/res/gaps.dart';
import 'package:liftmanager/net/api_service.dart';
import 'package:liftmanager/utils/toast.dart';
import 'package:liftmanager/widgets/app_bar.dart';
import 'package:liftmanager/internal/search/search_router.dart';
import 'package:liftmanager/widgets/app_search_bar.dart';
import 'package:liftmanager/res/resources.dart';
import 'package:liftmanager/routers/fluro_navigator.dart';
import 'package:liftmanager/widgets/load_image.dart';
import 'package:liftmanager/widgets/selected_image.dart';
import 'package:image_picker/image_picker.dart';
import 'package:liftmanager/internal/bbs/bbs_router.dart';
import 'dart:io';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:liftmanager/widgets/my_card.dart';
// import 'package:liftmanager/internal/bbs/model/question_detail.dart';
import 'package:liftmanager/internal/bbs/model/position_model.dart';
import 'package:liftmanager/utils/time_format.dart';
import 'package:liftmanager/utils/url.dart';
import 'dart:convert';
import 'package:liftmanager/utils/fast_notification.dart';
import 'dart:math';
import 'package:flustars/flustars.dart' as FlutterStars;
import 'package:liftmanager/common/common.dart';
import 'package:liftmanager/internal/account/account_router.dart';
import 'package:fluwx/fluwx.dart' as fluwx;
import 'package:liftmanager/utils/theme_utils.dart';

class PositionDetail extends StatefulWidget {
  PositionDetail(this.id);

  final String id;

  @override
  State<StatefulWidget> createState() {
    return PositionDetailState();
  }
}

class PositionDetailState extends State<PositionDetail> {
  @override
  void initState() {
    super.initState();
    getPositionDetail();
    FastNotification.addListener("apply",(isApply){
      print(isApply);
      print(456456);
      if(isApply == true){
        getPositionDetail();
      }
      setState(() {});
      
    });
  }

  bool _hasData = false;

  PositionDetailModel detailObj;

  Future getPositionDetail() async {
    await NewApiService().getPositionDetail(int.parse(widget.id),
        onSuccess: (res) {
      if (res != null) {
        print(123456);
        print(res);
        detailObj = res;
        _hasData = true;
        setState(() {});
      }
    }, onError: (code, msg) {
      toasts(msg);
    });
  }

  Future changeFavorite() async {
    showLoading(context);
    print(detailObj.isFavorite);
    if (detailObj.isFavorite == 0) {
      await NewApiService().insertRecruitmentOperates(
        detailObj.id,
        3,
        onSuccess: (res) {
          print(res);
          dismissLoading(context);
          initCollect();
          this.getPositionDetail();
        },
        onError: (code, msg) {
          dismissLoading(context);
          toasts(msg);
        },
      );
    } else {
      await NewApiService().deleteRecruitmentOperates(
        detailObj.favoriteId,
        onSuccess: (res) {
          print(res);
          dismissLoading(context);
          initCollect();
          this.getPositionDetail();
        },
        onError: (code, msg) {
          dismissLoading(context);
          toasts(msg);
        },
      );
    }
  }

  showAlertEvent(){
    showAlert(
                              context,
                              "提示",
                              "确定登录？",
                              "确定",
                              () {
                                NavigatorUtils.push(context, AccountRouter.loginPage, clearStack: true);
                              },
                              txt2: "取消",
                              onPre2: () {
                                NavigatorUtils.goBack(context);
                              },
                            );
  }

  initCollect(){
    String collectInit = randomInt(1111,9999).toString() + DateTime.now().millisecondsSinceEpoch.toString();
    FastNotification.push("collectAction",collectInit);
  }

  randomInt(int min, int max) {
    return new Random().nextInt(max) % (max - min + 1) + min;
  }

  @override
  Widget build(BuildContext context) {
    print(widget.id);

    double width = MediaQuery.of(context).size.width;

    return Scaffold(
        resizeToAvoidBottomPadding: false, //不让键盘弹上去
        appBar: MyAppBar(
          centerTitle: "职位详情",
        ),
        body: _hasData
            ? Stack(
                children: <Widget>[
                  Container(
                      child: ListView(children: <Widget>[
                    Column(
                      mainAxisAlignment: MainAxisAlignment.start,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Container(
                          padding: EdgeInsets.only(
                              left: ScreenUtil().setWidth(15),
                              right: ScreenUtil().setWidth(15),
                              top: ScreenUtil().setWidth(10),
                              bottom: ScreenUtil().setWidth(10)),
                          decoration: BoxDecoration(
                            border: Border(
                              bottom:
                                  BorderSide(width: 0.5, color: Colours.line),
                            ),
                          ),
                          child: Column(
                              mainAxisAlignment: MainAxisAlignment.start,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: <Widget>[
                                MyCard(
                                  child: Container(
                                    color:ThemeUtils.getDialogTextFieldColor(context),
                                      padding: EdgeInsets.only(
                                          left: ScreenUtil().setWidth(10),
                                          right: ScreenUtil().setWidth(10),
                                          top: ScreenUtil().setWidth(20),
                                          bottom: ScreenUtil().setWidth(20)),
                                      width: width,
                                      child: Column(
                                          crossAxisAlignment:
                                              CrossAxisAlignment.start,
                                          children: <Widget>[
                                            Container(
                                              padding: EdgeInsets.only(
                                                  bottom: ScreenUtil()
                                                      .setWidth(10)),
                                              child: Row(
                                                crossAxisAlignment: CrossAxisAlignment.start,
                                                mainAxisAlignment:
                                                    MainAxisAlignment
                                                        .spaceBetween,
                                                children: <Widget>[
                                                  Expanded(child: Text(
                                                    // detailObj.job,
                                                    detailObj.job ?? '',
                                                    style: TextStyle(
                                                        fontSize: ScreenUtil()
                                                            .setSp(16)),
                                                    textAlign: TextAlign.start,
                                                  ),),
                                                  Container(
                                                    padding:EdgeInsets.only(top: 5),
                                                    child:Text(
                                                    DateUtils.instance
                                                        .getFormartData(
                                                            timeSamp: detailObj
                                                                .createTime,
                                                            format:
                                                                "yyyy/MM/dd"),
                                                    style: TextStyle(
                                                        color:
                                                            Color(0xff999999),
                                                        fontSize: ScreenUtil()
                                                            .setSp(14)),
                                                    textAlign: TextAlign.start,
                                                  ),
                                                  )
                                                ],
                                              ),
                                            ),
                                            Container(
                                              child: Text(
                                                "￥" +
                                                    detailObj.lowerSalary
                                                        .toString() +
                                                    "-" +
                                                    detailObj.upperSalary
                                                        .toString(),
                                                style: TextStyle(
                                                    color: Colors.red,
                                                    fontSize:
                                                        ScreenUtil().setSp(16)),
                                                textAlign: TextAlign.start,
                                              ),
                                            ),
                                            Container(
                                              child: Text(
                                                // (detailObj.typeName ?? '') +
                                                //     "|" +
                                                    ((detailObj.eduName != '' && detailObj.eduName != null ) ? detailObj.eduName : "不限学历") +
                                                    " | " +
                                                    (detailObj.upperWorking !=
                                                            null&&detailObj.lowerWorking != null
                                                        ? detailObj.lowerWorking.toString()+ "-" + detailObj.upperWorking
                                                            .toString()
                                                        : '0') +
                                                    "年",
                                                style: TextStyle(
                                                    color: Color(0xff999999),
                                                    fontSize:
                                                        ScreenUtil().setSp(14)),
                                                textAlign: TextAlign.start,
                                              ),
                                            ),
                                            Container(
                                                child: Row(
                                                  crossAxisAlignment: CrossAxisAlignment.start,
                                                  children: <Widget>[
                                              Text(
                                                "地址：",
                                                style: TextStyle(
                                                  color: Color(0xff999999),
                                                  fontSize:
                                                      ScreenUtil().setSp(14),
                                                ),
                                                textAlign: TextAlign.left,
                                              ),
                                              Container(
                                                width: width*0.7,
                                                child: Text(
                                                (detailObj.provinceName ?? '') + (detailObj.cityName ?? '') + (detailObj.address ?? ''),
                                                style: TextStyle(
                                                  color: Color(0xff999999),
                                                  fontSize:
                                                      ScreenUtil().setSp(14),
                                                ),
                                                textAlign: TextAlign.left,
                                              ),
                                              )
                                            ])),
                                          ])),
                                ),
                                SizedBox(
                                  height: ScreenUtil().setWidth(20),
                                ),
                                Text(
                                  "公司信息",
                                  style: TextStyle(
                                      fontSize: ScreenUtil().setSp(16)),
                                  textAlign: TextAlign.start,
                                ),
                                SizedBox(
                                  height: 5,
                                ),
                              ]),
                        ),
                        Container(
                          padding: EdgeInsets.only(
                              left: ScreenUtil().setWidth(15),
                              top: ScreenUtil().setWidth(20),
                              bottom: ScreenUtil().setWidth(20)),
                          width: width,
                          decoration: BoxDecoration(
                            border: Border(
                              bottom: BorderSide(
                                  width: 5, color: ThemeUtils.getDialogTextFieldColor(context)),
                            ),
                          ),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: <Widget>[
                              Container(
                                child: Text(
                                  detailObj.company.name == null ? '' : detailObj.company.name,
                                  style: TextStyle(
                                    color: Color(0xff666666),
                                    fontSize: ScreenUtil().setSp(14),
                                  ),
                                  textAlign: TextAlign.left,
                                ),
                              ),
                              SizedBox(
                                height: ScreenUtil().setWidth(10),
                              ),
                              // Container(
                              //   child: Text(
                              //     (detailObj.typeName ?? '') +
                              //         "|" +
                              //         (detailObj.company.companySize == null
                              //             ? ''
                              //             : detailObj.company.companySize) +
                              //         "|" +
                              //         (detailObj.company.mainbusiness == null
                              //             ? ''
                              //             : detailObj.company.mainbusiness),
                              //     style: TextStyle(
                              //       color: Color(0xff666666),
                              //       fontSize: ScreenUtil().setSp(14),
                              //     ),
                              //     textAlign: TextAlign.left,
                              //   ),
                              // ),
                              // SizedBox(
                              //   height: ScreenUtil().setWidth(10),
                              // ),
                              Container(
                                  child: Row(children: <Widget>[
                                Text(
                                  "联系人：",
                                  style: TextStyle(
                                    color: Color(0xff666666),
                                    fontSize: ScreenUtil().setSp(14),
                                  ),
                                  textAlign: TextAlign.left,
                                ),
                                Text(
                                  detailObj.company.corporator == null ? '' : detailObj.company.corporator,
                                  style: TextStyle(
                                    color: Color(0xff666666),
                                    fontSize: ScreenUtil().setSp(14),
                                  ),
                                  textAlign: TextAlign.left,
                                ),
                              ])),
                              SizedBox(
                                height: ScreenUtil().setWidth(10),
                              ),
                              Container(
                                  child: Row(children: <Widget>[
                                Text(
                                  "联系电话：",
                                  style: TextStyle(
                                    color: Color(0xff666666),
                                    fontSize: ScreenUtil().setSp(14),
                                  ),
                                  textAlign: TextAlign.left,
                                ),
                                Text(
                                  detailObj.company.telephone == null ? '' : detailObj.company.telephone,
                                  style: TextStyle(
                                    color: Color(0xff666666),
                                    fontSize: ScreenUtil().setSp(14),
                                  ),
                                  textAlign: TextAlign.left,
                                ),
                              ])),
                            ],
                          ),
                        ),
                        Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisAlignment: MainAxisAlignment.start,
                          children: <Widget>[
                            Container(
                              padding: EdgeInsets.only(
                                  left: ScreenUtil().setWidth(15),
                                  top: ScreenUtil().setWidth(15),
                                  bottom: ScreenUtil().setWidth(15)),
                              width: width,
                              decoration: BoxDecoration(
                                border: Border(
                                  bottom: BorderSide(
                                      width: 0.5, color: ThemeUtils.getDialogTextFieldColor(context)),
                                ),
                              ),
                              child: Text(
                                "职位信息",
                                style: TextStyle(
                                  fontSize: ScreenUtil().setSp(16),
                                ),
                                textAlign: TextAlign.left,
                              ),
                            ),
                            Container(
                              padding: EdgeInsets.only(
                                  left: ScreenUtil().setWidth(15),
                                  right: ScreenUtil().setWidth(15),
                                  top: ScreenUtil().setWidth(10),
                                  bottom: ScreenUtil().setWidth(90)),
                              child: Text(
                                detailObj.info ?? '',
                                style: TextStyle(
                                  color: Color(0xff666666),
                                  fontSize: ScreenUtil().setSp(14),
                                ),
                                textAlign: TextAlign.left,
                              ),
                            ),
                          ],
                        ),
                      ],
                    ),
                  ])),
                  Positioned(
                      bottom: 0,
                      left: 0,
                      child: Container(
                          width: width,
                          child: Row(children: <Widget>[
                            Container(
                                height: ScreenUtil().setWidth(70),
                                width: width / 2,
                                color: Colors.white,
                                padding: EdgeInsets.only(
                                    left: ScreenUtil().setWidth(10),
                                    right: ScreenUtil().setWidth(10),
                                    top: ScreenUtil().setWidth(10)),
                                child: Row(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceAround,
                                  children: <Widget>[
                                    GestureDetector(
                                      onTap: () {
                                        print('分享');
                                        print(jsonEncode(detailObj));
                                        if(FlutterStars.SpUtil.getString(Constant.userId) == "-1"){
                                          showAlertEvent();
                                        }else {
                                          String initThis = randomInt(1111,9999).toString() + DateTime.now().millisecondsSinceEpoch.toString();

                                          fluwx.shareToWeChat(fluwx.WeChatShareWebPageModel(
                                            scene: fluwx.WeChatScene.SESSION,
                                            webPage: "$jumpUrl/h5/index.html?num=$initThis&page=/bbs/positionDetail&id=${widget.id}",
                                            title: detailObj.job,
                                            description: detailObj.info,
                                            thumbnail: "",
                                          )).then((result){

                                          },
                                          onError: (msg){
                                            // print(msg);
                                          });
                                        }
                                      },
                                      child: Container(
                                          // padding: EdgeInsets.only(left:25,right:30),
                                          child: Column(children: <Widget>[
                                        Icon(
                                          IconData(0xe6f4,
                                              fontFamily: "myfont"),
                                          size: 26.0,
                                          color: Color(0xff000000),
                                        ),
                                        Text(
                                          "分享",
                                          style: TextStyle(
                                              color: Color(0xff000000),
                                              fontSize: ScreenUtil().setSp(14)),
                                          textAlign: TextAlign.start,
                                        ),
                                      ])),
                                    ),
                                    GestureDetector(
                                      onTap: () {
                                        print(jsonEncode(detailObj));
                                        if(FlutterStars.SpUtil.getString(Constant.userId) == "-1"){
                                          showAlertEvent();
                                        }else {
                                        changeFavorite();}
                                      },
                                      child: Container(
                                          // padding: EdgeInsets.only(left:25,right:30),
                                          child: Column(children: <Widget>[
                                        Icon(
                                          IconData(
                                              detailObj.isFavorite == 1
                                                  ? 0xe654
                                                  : 0xe604,
                                              fontFamily: "myfont"),
                                          size: 26.0,
                                          color: detailObj.isFavorite == 1
                                              ? Color(0xff0388FD)
                                              : Color(0xff333333),
                                        ),
                                        Text(
                                          "收藏",
                                          style: TextStyle(
                                              color: detailObj.isFavorite == 1
                                                  ? Color(0xff0388FD)
                                                  : Color(0xff333333),
                                              fontSize: ScreenUtil().setSp(14)),
                                          textAlign: TextAlign.start,
                                        ),
                                      ])),
                                    ),
                                  ],
                                )),
                            Container(
                              height: ScreenUtil().setWidth(70),
                              width: width / 2,
                              // color: Color(0xff0388FD),
                              color: detailObj.isApply == 1
                                  ? Color(0xff999999)
                                  : Color(0xff0388FD),
                              child: FlatButton(
                                // padding: EdgeInsets.all(15.0),
                                child: Text(
                                  detailObj.isApply == 1 ? "已申请" : "申请职位",
                                  style: TextStyle(
                                      fontSize: ScreenUtil().setSp(16)),
                                ),
                                textColor: Colors.white,
                                onPressed: () {
                                  if(FlutterStars.SpUtil.getString(Constant.userId) == "-1"){
                                          showAlertEvent();
                                        }else {
                                  if(detailObj.isApply == 1){
                                    toasts("您已经提交申请了哦");
                                  }else {
                                    NavigatorUtils.push(context,
                                      "${BbsRouter.positionApply}?id=${detailObj.id.toString()}");
                                  }
                                        }
                                },
                              ),
                            ),
                          ])))
                ],
              )
            : Center(
                child: Text("正在加载..."),
              ));
  }
}
