import 'dart:io';

import 'package:flutter/material.dart';
import 'package:photo_view/photo_view.dart';
import 'package:photo_view/photo_view_gallery.dart';
import 'package:qhd_prevention/customWidget/custom_button.dart';
import 'package:qhd_prevention/customWidget/single_image_viewer.dart';
import 'package:qhd_prevention/pages/home/work/danger_wait_list_page.dart';
import 'package:qhd_prevention/pages/my_appbar.dart';
import 'package:qhd_prevention/tools/h_colors.dart';
import 'package:qhd_prevention/tools/tools.dart';

import '../../../customWidget/ItemWidgetFactory.dart';
import '../../../customWidget/date_picker_dialog.dart';
import '../../../customWidget/photo_picker_row.dart';

class DangerRepairPage extends StatefulWidget {
  const DangerRepairPage(this.dangerType, {Key? key}) : super(key: key);

  final DangerType dangerType;

  @override
  State<DangerRepairPage> createState() => _DangerRepairPageState();
}

class _DangerRepairPageState extends State<DangerRepairPage> {
  bool _accepted = false;
  var _selectData = DateTime.now();

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppbar(title: widget.dangerType.detailTitle),
      body: SafeArea(
        child: Column(
          children: [
            // 可滚动内容区域
            Expanded(
              child: SingleChildScrollView(
                padding: const EdgeInsets.all(15),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: [
                    // 第一部分：内嵌列表
                    _danner_detail(),

                    ListItemFactory.createYesNoSection(
                      title: '是否正常整改',
                      yesLabel: '是',
                      noLabel: '否',
                      groupValue: _accepted,
                      onChanged: (val) {
                        setState(() {
                          _accepted = val;
                        });
                      },
                    ),
                    // 整改选项
                    _accepted ? _danner_repair() : _noAccepet_repair(_accepted),

                    const SizedBox(height: 20),
                    CustomButton(text: "提交", backgroundColor: Colors.blue),
                  ],
                ),
              ),
            ),
            // 底部警示文字，固定在页面底部
            Container(
              padding: const EdgeInsets.all(15),
              color: Colors.white,
              child: Text(
                '        严禁在本互联网非涉密平台处理、传输国家秘密和工作秘密，请确认扫描、传输的文件资料不涉及国家秘密和工作秘密',
                style: TextStyle(fontSize: 14, color: Colors.red),
              ),
            ),
          ],
        ),
      ),
    );
  }

  /// 整改
  Widget _danner_repair() {
    return Container(
      padding: EdgeInsets.only(bottom: 10),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(5),
      ),
      child: Column(
        children: [
          ListItemFactory.createBuildSimpleSection("隐患整改"),
          Divider(height: 1),
          Container(
            height: 130,
            padding: EdgeInsets.all(15),
            child: Column(
              children: [
                Row(
                  children: [HhTextStyleUtils.mainTitle("隐患描述", fontSize: 15)],
                ),
                TextField(
                  keyboardType: TextInputType.multiline,
                  maxLines: null, // 不限制行数，输入多少文字就撑开多少行
                  style: TextStyle(fontSize: 15),
                  decoration: InputDecoration(
                    hintText: '请对隐患进行详细描述（必填项）',
                    border: InputBorder.none,
                  ),
                ),
              ],
            ),
          ),
          Divider(height: 1),
          GestureDetector(
            onTap: () {
              showDialog(
                context: context,
                builder:
                    (_) => HDatePickerDialog(
                      initialDate: DateTime.now(),
                      onCancel: () => Navigator.of(context).pop(),
                      onConfirm: (selected) {
                        print('选中日期: $selected');
                        Navigator.of(context).pop();
                        setState(() {
                          _selectData = selected;
                        });
                      },
                    ),
              );
            },
            child: Padding(
              padding: EdgeInsets.symmetric(horizontal: 10),
              child: ListItemFactory.createRowSpaceBetweenItem(
                leftText: "整改日期",
                rightText: "请选择",
                isRight: true,
              ),
            ),
          ),
          Divider(),
          Padding(
            padding: EdgeInsets.symmetric(horizontal: 10),
            child: ListItemFactory.createRowSpaceBetweenItem(
              leftText: "整改后照片",
              rightText: "0/4",
            ),
          ),
          Padding(
            padding: EdgeInsets.symmetric(horizontal: 10),
            child: PhotoPickerRow(
              maxCount: 4,
              onChanged: (List<File> images) {
                // images 列表更新
              },
            ),
          ),
          Divider(),
          Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              CustomButton(
                onPressed: () {},
                text: "添加",
                backgroundColor: Colors.blue,
                borderRadius: 17,
                height: 34,
                padding: EdgeInsets.symmetric(horizontal: 20),
              ),
            ],
          ),
          _departmentItem(1),
          _departmentItem(2),
          Divider(),
          // ListItemFactory.createYesNoSection(title: "是否有整改方案", yesLabel: "", noLabel: noLabel, groupValue: groupValue, onChanged: onChanged)
        ],
      ),
    );
  }

  Widget _departmentItem(int index) {
    return Padding(
      padding: const EdgeInsets.all(10),
      child: Stack(
        clipBehavior: Clip.none,
        children: [
          Container(
            decoration: BoxDecoration(
              border: Border.all(color: Colors.black12, width: 1),
            ),
            child: _noAccepet_repair(false),
          ),

          // 当 index > 1 时，左上角显示删除按钮
          if (index > 1)
            Positioned(
              top: -20,
              left: -20,
              child: IconButton(
                padding: EdgeInsets.zero,
                constraints: const BoxConstraints(),
                icon: const Icon(Icons.cancel, color: Colors.red, size: 25),
                onPressed: () {
                  // 这里处理删除逻辑，比如：
                  // setState(() => _items.removeAt(index));
                },
              ),
            ),
        ],
      ),
    );
  }


  /// 不整改
  Widget _noAccepet_repair(bool _accept) {
    return Column(
      children: [
        Container(
          padding: EdgeInsets.symmetric(horizontal: 10),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(5),
          ),
          child: ListItemFactory.createRowSpaceBetweenItem(
            leftText: "整改部门",
            rightText: "测试啊",
            isRight: true,
          ),
        ),
        Divider(height: 10,color: _accept ? Colors.black12 : Colors.transparent,),
        Container(
          padding: EdgeInsets.symmetric(horizontal: 10),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(5),
          ),
          child: ListItemFactory.createRowSpaceBetweenItem(
            leftText: "整改负责人",
            rightText: "测试啊",
            isRight: true,
          ),
        ),
      ],
    );
  }

  /// 隐患详情
  Widget _danner_detail() {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(5),
      ),
      padding: const EdgeInsets.all(12),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          ListView.builder(
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
            itemCount: 16,
            itemBuilder: (context, index) {
              if (index == 0) {
                return ListItemFactory.createAloneTextItem(
                  text: "地坪漆漆未分配全皮肤期漆未分配全皮肤期漆未分配全皮肤期未分配全皮肤期间哦飞机哦脾气金佛怕",
                );
              } else if ((index > 0 && index < 4) ||
                  index == 5 ||
                  (index > 6 && index < 15)) {
                return ListItemFactory.createRowSpaceBetweenItem(
                  leftText: "隐患来源",
                  rightText: "隐患排查",
                );
              } else if (index == 4 || index == 6) {
                return ListItemFactory.createColumnTextItem(
                  topText: "存在风险",
                  bottomText: "哦IQ好然后前后hi前后哦i",
                );
              }
              return ListItemFactory.createTextImageItem(
                text: "隐患照片",
                imageUrl:
                    "https://pic.rmb.bdstatic.com/bjh/news/100b8b78cbd136ede03249d9f3b3f5c42221.jpeg",
                onImageTapped: () {
                  pushPage(
                    SingleImageViewer(
                      imageUrl:
                          "https://pic.rmb.bdstatic.com/bjh/news/100b8b78cbd136ede03249d9f3b3f5c42221.jpeg",
                    ),
                    context,
                  );
                },
              );
            },
          ),
        ],
      ),
    );
  }
}
