import 'dart:async';

import 'package:flutter/material.dart';

import '../../bean/company_bean.dart';
import 'machine_model.dart';
import 'machine_room_page.dart';

class ReginPage extends StatefulWidget {
  String deptId;

  ReginPage({super.key, required this.deptId});

  @override
  State<ReginPage> createState() => _ReginPageState();
}

class _ReginPageState extends State<ReginPage> {
  late StreamSubscription subscription;
  final MachineModel _viewmodel = MachineModel();

  List<CompanyBean> regionList = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    subscription = _viewmodel.streamController.stream.listen((event) {
      String code = event['code'];
      if (code.isNotEmpty) {
        switch (code) {
          case "getRegion":
            regionList = event['data'];
            break;
        }
      }
      setState(() {});
    });

    _viewmodel.getRegion(widget.deptId);
  }

  @override
  void dispose() {
    // TODO: implement dispose
    subscription.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final double statusBarHeight = MediaQuery.of(context).padding.top;
    final size = MediaQuery.of(context).size;
    final t10 = size.width / 36;
    final w25 = size.width / 14.4;
    final p5 = size.width / 72;
    final w9 = size.width / 40;
    final s21 = size.width / 17.142857142857;
    final t20 = size.width / 18;

    return Scaffold(
      backgroundColor: const Color(0xFFD8D8D8),
      body: Container(
        decoration: const BoxDecoration(
            image: DecorationImage(
          image: AssetImage('assets/images/home_bg.png'),
          fit: BoxFit.cover,
        )),
        child: Column(
          children: [
            Container(
              margin: EdgeInsets.only(top: statusBarHeight + t10, left: t10, right: t10),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  GestureDetector(
                    onTap: () {
                      Navigator.pop(context);
                    },
                    child: Container(
                      width: w25,
                      height: w25,
                      padding: EdgeInsets.all(p5),
                      child: Image(
                        width: w9,
                        image: const AssetImage('assets/images/ic_back.png'),
                      ),
                    ),
                  ),
                  Container(
                    alignment: Alignment.center,
                    child: Text(
                      '地区列表',
                      style: TextStyle(fontSize: s21, fontWeight: FontWeight.w600),
                    ),
                  ),
                  Container(
                    width: w25,
                    height: w25,
                    padding: EdgeInsets.all(p5),
                  ),
                ],
              ),
            ),
            Expanded(
              child: Container(
                margin: EdgeInsets.only(top: t20, left: t10, right: t10),
                child: ListView.builder(
                    itemCount: regionList.length,
                    padding: const EdgeInsets.all(0),
                    itemBuilder: (BuildContext context, int index) {
                      return _item(regionList[index], t10, t20);
                    }),
              ),
            )
          ],
        ),
      ),
    );
  }

  _item(CompanyBean data, t10, t20) {
    return GestureDetector(
      onTap: () {
        Navigator.push(
          context,
          MaterialPageRoute(
            builder: (context) => MachineRoomPage(
              deptId: '${data.deptId}',
            ),
          ),
        );
      },
      child: Container(
        margin: EdgeInsets.only(bottom: t10),
        child: Card(
          color: Colors.white,
          child: Container(
            alignment: Alignment.centerLeft,
            padding: EdgeInsets.only(left: t20, top: t20, bottom: t20),
            child: Text("${data.deptName}"),
          ),
        ),
      ),
    );
  }
}
