import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:keyboard_actions/keyboard_actions.dart';
import 'package:liftmanager/internal/account/account_router.dart';
import 'package:liftmanager/internal/account/model/certificate_item.dart';
import 'package:liftmanager/net/api_service.dart';
import 'package:liftmanager/res/gaps.dart';
import 'package:liftmanager/res/resources.dart';
import 'package:liftmanager/routers/fluro_navigator.dart';
import 'package:liftmanager/utils/toast.dart';
import 'package:liftmanager/widgets/app_bar.dart';
import 'package:liftmanager/widgets/click_item.dart';

class CertificatePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return CertificatePageState();
  }
}

class CertificatePageState extends State<CertificatePage> {
  CertificateItem item = CertificateItem();

  @override
  void initState() {
    super.initState();
    getCertificateDetail();
  }

  getCertificateDetail() {
    ApiService(context: context).liftCertificateFindByUser(onSuccess: (res) {
      if (res != null) {
        item = res;
      }
      setState(() {});
    }, onError: (code, msg) {
      toasts(msg);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(
        centerTitle: "操作证",
        actions: <Widget>[
          FlatButton(
            child: Text("上传"),
            textColor: Colours.text,
            highlightColor: Colors.transparent,
            onPressed: () {
              NavigatorUtils.pushResult(
                  context, AccountRouter.certificateUploadPage, (res) {
                getCertificateDetail();
              });
            },
          )
        ],
      ),
      body: SafeArea(
        child: Column(
          children: <Widget>[
            Expanded(
              flex: 1,
              child: defaultTargetPlatform == TargetPlatform.iOS
                  ? FormKeyboardActions(child: _buildBody())
                  : SingleChildScrollView(child: _buildBody()),
            )
          ],
        ),
      ),
    );
  }

  _buildBody() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: _listWidgets(),
    );
  }

  _listWidgets() {
    if (item.status == 0)
      return [
        ClickItem(
          title: "操作证",
          content:
              "${item.status == 1 ? '待审核' : item.status == 2 ? '审核未通过' : item.status == 3 ? '合格' : item.status == 4 ? '超期' : '无证'}",
        )
      ];
    return [
      ClickItem(
        title: "操作证",
        content:
            "${item.status == 1 ? '待审核' : item.status == 2 ? '审核未通过' : item.status == 3 ? '合格' : item.status == 4 ? '超期' : '无证'}",
      ),
      ClickItem(
        title: "操作证类型",
        content: "${item.code == 1 ? '维保工操作证' : '其他'}",
      ),
      ClickItem(
        title: "证件有效期",
        content: "${item.expirationDate}",
      ),
      ClickItem(
        title: "发证单位",
        content: "${item.issuanceAgency}",
      ),
      Gaps.vGap10,
      ClickItem(
        title: "操作证图片",
        content: "",
        hideDiv: true,
      ),
      Row(
        children: <Widget>[
          Gaps.hGap10,
          Image.network(
            "${item.firstImgUrl}",
            width: 150,
            height: 150,
            fit: BoxFit.fill,
          ),
          Gaps.hGap10,
          Image.network(
            "${item.secondImgUrl}",
            width: 150,
            height: 150,
            fit: BoxFit.fill,
          )
        ],
      )
    ];
  }
}
