import 'dart:io';

import 'package:flutter/material.dart';
import 'package:qhdkfq_regulatory_flutter/Custom/custom_button.dart';
import 'package:qhdkfq_regulatory_flutter/Custom/dashed_line_text.dart';
import 'package:qhdkfq_regulatory_flutter/Custom/edit_list_items.dart';
import 'package:qhdkfq_regulatory_flutter/Custom/help_department_person_item.dart';
import 'package:qhdkfq_regulatory_flutter/Custom/read_only_table.dart';
import 'package:qhdkfq_regulatory_flutter/tools/h_colors.dart';
import 'package:qhdkfq_regulatory_flutter/tools/my_appbar.dart';

import '../../Custom/photo_picker_row.dart';

enum HelpEditType {
  edit,
  help

}

class DangerEditPage extends StatefulWidget {
  const DangerEditPage(this.type, {Key? key}) : super(key: key);
  final HelpEditType type;
  @override
  State<DangerEditPage> createState() => _DangerEditPageState();
}

class _DangerEditPageState extends State<DangerEditPage> {
  // 控制器列表
  final _companyController = TextEditingController(text: '公司');
  final _addressController = TextEditingController();
  final _nameController = TextEditingController(text: '韩双');
  final _phoneController = TextEditingController();
  final _codeController = TextEditingController();
  final _placeController = TextEditingController();

  // 单选选项
  final List<String> _radioTitles = ['日常检查', '专项检查', '总和检查', '执法检查', '举报检查'];
  int _selectedType = 0;
// 创建一个 GlobalKey 来拿到 State
  final _sectionKey = GlobalKey<HelpDeptPersonSectionState>();

  /// 帮扶时间
  final String _helpTimeStr = "请选择";

  @override
  void dispose() {
    _companyController.dispose();
    _addressController.dispose();
    _nameController.dispose();
    _phoneController.dispose();
    _codeController.dispose();
    _placeController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // 1. 基本信息字段
    final basicFields = <Widget>[
      EditListItems.createRowSpaceBetweenItem(
        leftText: '公司名称',
        rightText: _companyController.text,
        isImportent: true,
      ),
      Divider(height: .5),
      EditListItems.createBuildMultilineInput(
        label: '地址',
        hint: '',
        controller: _addressController,
        isImportent: true,
        isVal: true,
      ),
      Divider(height: .5),
      EditListItems.createBuildMultilineInput(
        label: '姓名',
        hint: '',
        controller: _nameController,
        isImportent: true,
      ),
      Divider(height: .5),
      EditListItems.createBuildMultilineInput(
        label: '联系电话',
        hint: '',
        controller: _phoneController,
        isImportent: true,
      ),
      Divider(height: .5),
      EditListItems.createBuildMultilineInput(
        label: '编号',
        hint: '',
        controller: _codeController,
        isImportent: true,
      ),
      Divider(height: .5),
      EditListItems.createBuildMultilineInput(
        label: '场所',
        hint: '',
        controller: _placeController,
        isImportent: true,
        isVal: true,
      ),
      Divider(height: .5),
      EditListItems.createRadioRow(
        label: '检查类型',
        options: _radioTitles,
        selectedValue: _selectedType,
        onChanged: (v) => setState(() => _selectedType = v),
        isImportent: true,
      ),
      Divider(height: .5),

      EditListItems.createRowSpaceBetweenItem(
        leftText: "帮扶时间",
        isImportent: true,
        rightText: _helpTimeStr,
        isRight: true,
        onTap: () {},
      ),
      Divider(height: .5),
      EditListItems.createYesNoSection(
        title: "邀请专家",
        yesLabel: "是",
        noLabel: "否",
        horizontalPadding: 0,
        verticalPadding: 0,
        groupValue: false,
        onChanged: (val) {},
      ),
      Divider(height: .5),

      EditListItems.createRowSpaceBetweenItem(
        leftText: "现场照片",
        isImportent: true,
        rightText: "",
        isRight: false,
      ),
      MediaPickerRow(
        maxCount: 16,
        onChanged: (List<File> images) {
          // images 列表更新
        },
      ),

    ];

    // 帮扶内容标题
    final helpHeader = Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        DashedLineText(
          text: '帮扶内容',
          lineColor: h_mainBlueColor(),
          textStyle: TextStyle(fontSize: 16, color: h_mainBlueColor()),
        ),
        SizedBox(height: 20),
      ],
    );

    //按钮组
    final buttons = Row(
      mainAxisAlignment: MainAxisAlignment.end,
      children: [
        CustomButton(
          text: '选择标准',
          height: 35,
          backgroundColor: Colors.blue,
          onPressed: () {},
        ),
        SizedBox(width: 8),
        CustomButton(
          text: '手动录入',
          height: 35,
          backgroundColor: Colors.blue,
          onPressed: () {},
        ),
      ],
    );
    final table = ReadOnlyTable(
      headers: ['序号', '内容来源', '隐患描述', '操作'],
      data: [
        ['1', '政府部门', '示例内容', '合格'],
        ['2', '社会组织', '示例', '不合格'],
      ],
      onTapAction: (row) => print('点击表格第$row 行'),
    );

    // 帮扶内容标题
    final helpPersonHeader = Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        DashedLineText(
          text: '帮扶人员',
          lineColor: h_mainBlueColor(),
          textStyle: TextStyle(fontSize: 16, color: h_mainBlueColor()),
        ),
        SizedBox(height: 20),
      ],
    );
    final helpPerson = Row(
      mainAxisAlignment: MainAxisAlignment.end,
      children: [
        CustomButton(
          text: '添加',
          height: 35,
          backgroundColor: Colors.blue,
          onPressed: () {
            _sectionKey.currentState?.addEntry();
          },
        ),
      ],
    );
    final dartPerson = HelpDeptPersonSection(key: _sectionKey);

    final danger = Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        DashedLineText(
          text: '隐患',
          lineColor: h_mainBlueColor(),
          textStyle: TextStyle(fontSize: 16, color: h_mainBlueColor()),
        ),
        SizedBox(height: 20),
      ],
    );

    final dangerTable = ReadOnlyTable(
      headers: ['序号', '所属帮扶', '隐患描述', '操作'],
      data: [],
    );
    final bottomButtons = Row(
      children: [
        Expanded(
          child: CustomButton(
            text: "保存",
            backgroundColor: Colors.blue,
            height: 40,
          ),
        ),
        const SizedBox(width: 12),
        Expanded(
          child: CustomButton(
            text: "暂存",
            backgroundColor: Colors.green,
            height: 40,
          ),
        ),
      ],
    );
    // 将所有部分按顺序拼起来
    final children = <Widget>[
      // 基本信息标题
      DashedLineText(
        text: '基本信息',
        lineColor: h_mainBlueColor(),
        textStyle: TextStyle(fontSize: 16, color: h_mainBlueColor()),
      ),
      SizedBox(height: 20),

      ...basicFields,
      SizedBox(height: 20),
      helpHeader,
      buttons,
      SizedBox(height: 10),
      table,
      SizedBox(height: 20),
      helpPersonHeader,
      helpPerson,
      SizedBox(height: 20),
      dartPerson,
      SizedBox(height: 20),
      danger,
      SizedBox(height: 20),
      dangerTable,
      SizedBox(height: 20),
      bottomButtons,
    ];

    return Scaffold(
      backgroundColor: Colors.white,
      appBar: MyAppbar(title: widget.type == HelpEditType.help ? '监管帮扶' : '编辑'),
      body: SafeArea(
        child: Column(
          children: [
            Expanded(
              child: ListView(
                padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 20),
                children: children,
              ),
            ),
            EditListItems.bottomTipWidget(),
          ],
        ),
      ),
    );
  }
}
