import 'package:flutter/material.dart';
import 'package:qhd_prevention/customWidget/ItemWidgetFactory.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/tools.dart';
import 'package:url_launcher/url_launcher.dart';

class DefendRecordDetailPage extends StatefulWidget {
  const DefendRecordDetailPage({super.key, required this.INSPECTION_ID});

  final String INSPECTION_ID;

  @override
  State<DefendRecordDetailPage> createState() => _DefendRecordDetailPageState();
}

class _DefendRecordDetailPageState extends State<DefendRecordDetailPage> {
  late List _list = [];

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

  Future<void> _getData() async {
    LoadingDialogHelper.show();
    try {
      final result = await ApiService.safeCheckDefendRecordList(
        widget.INSPECTION_ID,
      );
      if (result['result'] == 'success') {
        _list = result['list'];
      }
      LoadingDialogHelper.hide();
    } catch (e) {
      LoadingDialogHelper.hide();
      ToastUtil.showNormal(context, '接口请求失败');
    }
  }

  Widget _itemWidget(Map<String, dynamic> item) {
    return Container(
      color: Colors.white,
      child: Column(
        children: [
          ItemListWidget.itemContainer(
            ListItemFactory.headerTitle(
              '申辩意见：${item['INSPECTED_EXPLAIN'] ?? ''}',
            ),
          ),
          ItemListWidget.singleLineTitleText(
            label: '申辩时间',
            isEditable: false,
            text: item['CREATTIME'] ?? '',
          ),
          ItemListWidget.singleLineTitleText(
            label: '申辩状态',
            isEditable: false,
            text:
                item['ISPASS'] == '0'
                    ? '待审核'
                    : (item['ISPASS'] == '1'
                        ? '申辩成功'
                        : (item['ISPASS'] == '2' ? '申辩驳回' : '历史数据')),
          ),
          ItemListWidget.OneRowImageTitle(
            label: '签字图片：',
            imgPath: item['INSPECTED_SITEUSER_SIGN_IMG'] ?? '',
            onTapCallBack: (imgUrl) {
              presentOpaque(SingleImageViewer(imageUrl: imgUrl), context);
            },
          ),
          if (FormUtils.hasValue(item, 'INSPECTED_EXPLAIN_FILENAME'))
            ItemListWidget.OneRowButtonTitleText(
              label: '申辩附件名称',
              buttonText: '申辩附件下载',
              text: item['INSPECTED_EXPLAIN_FILENAME'] ?? '',
              onTap: () {
                launchUrl(Uri.parse(item['INSPECTED_EXPLAIN_FILENAME'] ?? ''));
              },
            ),

          if (item['ISPASS'] == '2')
            Column(
              children: [
                ItemListWidget.OneRowImageTitle(
                  label: '检查人签字：',
                  imgPath: item['INSPECTION_ORIGINATOR_SIGN_IMG'] ?? '',
                  onTapCallBack: (imgUrl) {
                    presentOpaque(SingleImageViewer(imageUrl: imgUrl), context);
                  },
                ),
                ItemListWidget.singleLineTitleText(
                  label: '检查人意见',
                  isEditable: false,
                  text: item['INSPECTED_EXPLAIN_REFUSE'] ?? '',
                ),
                ItemListWidget.singleLineTitleText(
                  label: '审批时间',
                  isEditable: false,
                  text: item['INSPECTION_ORIGINATOR_SIGN_TIME'] ?? '',
                ),
              ],
            ),
          if (item['ISPASS'] == '1')
            ItemListWidget.singleLineTitleText(
              label: '申辩时间',
              isEditable: false,
              text: item['OPERATTIME'] ?? '',
            ),
          SizedBox(height: 10),
          const Divider(),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppbar(title: '申辩记录'),
      body: SafeArea(
        child: Expanded(
          child: ListView.builder(
            itemCount: _list.length,
            itemBuilder: (context, index) {
              final item = _list[index];
              return _itemWidget(item);
            },
          ),
        ),
      ),
    );
  }
}
