import 'package:flutter/material.dart';
import 'package:qhd_prevention/customWidget/remote_file_page.dart';
import 'package:qhd_prevention/customWidget/single_image_viewer.dart';
import 'package:qhd_prevention/customWidget/toast_util.dart';
import 'package:qhd_prevention/http/ApiService.dart';
import 'package:qhd_prevention/pages/home/tap/item_list_widget.dart';
import 'package:qhd_prevention/pages/my_appbar.dart';
import 'package:qhd_prevention/tools/h_colors.dart';
import 'package:qhd_prevention/tools/tools.dart';

class SxkjTzglDetail extends StatefulWidget {
  const SxkjTzglDetail({super.key, required this.LIMITSPACE_ID});

  final String LIMITSPACE_ID;

  @override
  State<SxkjTzglDetail> createState() => _SxkjTzglDetailState();
}

class _SxkjTzglDetailState extends State<SxkjTzglDetail> {
  late Map<String, dynamic> pd = {};

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

  Future<void> _getData() async {
    final result = await ApiService.getLimitspaceMsssage(widget.LIMITSPACE_ID);
    if (result['result'] == 'success') {
      setState(() {
        pd = result['pd'];
      });
    } else {
      ToastUtil.showNormal(context, '请求数据失败');
    }
  }

  Widget _card(Widget child) {
    return Container(
      padding: EdgeInsets.symmetric(vertical: 5),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(5),
      ),
      child: child,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: h_backGroundColor(),
      appBar: MyAppbar(title: '受限空间信息'),
      body: SafeArea(
        child: SingleChildScrollView(
          padding: EdgeInsets.all(12),
          child: Container(
            padding: const EdgeInsets.symmetric(vertical: 10),
            decoration: const BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.all(Radius.circular(5)),
            ),
            child: Column(
              children: [
                ItemListWidget.singleLineTitleText(
                  label: '受限空间编号',
                  isEditable: false,
                  text: pd['NUMBER'] ?? '',
                ),
                Divider(),
                ItemListWidget.singleLineTitleText(
                  label: '受限空间名称',
                  isEditable: false,
                  text: pd['NAME'] ?? '',
                ),

                Divider(),
                ItemListWidget.singleLineTitleText(
                  label: '设备名称/编号',
                  isEditable: false,
                  text: pd['DEVICENAME'] ?? '',
                ),
                Divider(),
                ItemListWidget.OneRowImageTitle(
                  label: '警示标志附件',
                  imgPath: pd['WARNINGSIGNS_PATH'] ?? '',
                  onTapCallBack: (path) {
                    presentOpaque(SingleImageViewer(imageUrl: path), context);
                  }
                ),
                Divider(),
                ItemListWidget.singleLineTitleText(
                  label: '位置及范围',
                  isEditable: false,
                  text: pd['POSITIONSCOPE'] ?? '',
                ),
                Divider(),
                ItemListWidget.singleLineTitleText(
                  label: '主要危险及有害因素',
                  isEditable: false,
                  text: pd['PRIMARYHAZARD'] ?? '',
                ),
                Divider(),
                ItemListWidget.singleLineTitleText(
                  label: '主要事故类型',
                  isEditable: false,
                  text: pd['ACCIDENTTYPE'] ?? '',
                ),
                Divider(),
                ItemListWidget.singleLineTitleText(
                  label: '最多作业人数',
                  isEditable: false,
                  text: pd['MAXPERSON'] ?? '',
                ),
                Divider(),
                ItemListWidget.singleLineTitleText(
                  label: '风险等级',
                  isEditable: false,
                  text: pd['RISKGRADENAME'] ?? '',
                ),
                Divider(),
                ItemListWidget.singleLineTitleText(
                  label: '本受限空间最多作业人数',
                  isEditable: false,
                  text: pd['MAXPERSON'] ?? '',
                ),
                Divider(),
                ItemListWidget.selectableLineTitleTextRightButton(
                  label: '应急指导书附件',
                  isEditable: true,
                  isRequired: false,
                  text: ' ',
                  onTap: () {
                    if (FormUtils.hasValue(pd, 'HASINSTRUCTOR_PATH')) {
                      pushPage(
                        RemoteFilePage(
                          fileUrl: ApiService.baseImgPath + pd['HASINSTRUCTOR_PATH'],
                          countdownSeconds: 10,
                        ),
                        context,
                      );
                    }else{
                      ToastUtil.showNormal(context, '无应急指导书附件');
                    }
                  }
                ),
                Divider(),
                ItemListWidget.twoRowSelectableTitleText(
                  label: '备注',
                  isEditable: false,
                  text: pd['DESCR'] ?? '',
                ),
                Divider(),
                ItemListWidget.singleLineTitleText(
                  label: '填报人',
                  isEditable: false,
                  text: pd['INFORMANTNAME'] ?? '',
                ),
                Divider(),
                ItemListWidget.singleLineTitleText(
                  label: '主要负责人',
                  isEditable: false,
                  text: pd['PRINCIPALNAME'] ?? '',
                ),
                Divider(),
                ItemListWidget.singleLineTitleText(
                  label: '填报时间',
                  isEditable: false,
                  text: pd['COMPILETIME'] ?? '',
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
