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

class KeyprojectDetail extends StatefulWidget {
  const KeyprojectDetail({super.key, required this.OUTSOURCED_ID});

  final String OUTSOURCED_ID;

  @override
  State<KeyprojectDetail> createState() => _KeyprojectDetailState();
}

class _KeyprojectDetailState extends State<KeyprojectDetail> {
  late Map<String, dynamic> info = {};

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

  Future<void> _getData() async {
    LoadingDialogHelper.show();
    final result = await ApiService.getKeyProjectDeatail(widget.OUTSOURCED_ID);
    try {
      setState(() {
        info = result['pd'] ?? {};
      });
    } catch (e) {
      print('加载数据失败：$e');
      ToastUtil.showNormal(context, '加载数据失败：$e');
    }
    LoadingDialogHelper.hide();
  }

  Future<void> onUpdateState(String state) async {
    // 提示文案
    String content = '';
    if (state == '1') {
      content = '确定同意开工吗?';
    } else if (state == '2') {
      content = '确定同意结束吗?';
    } else {
      content = '确定操作吗?';
    }
    final ok = await CustomAlertDialog.showConfirm(
      context,
      title: '温馨提示',
      content: content,
      cancelText: '取消',
    );
    if (!ok) {
      return;
    }
    LoadingDialogHelper.show();
    try {
      final response = await ApiService.sureKeyProjectState(
        widget.OUTSOURCED_ID,
        state,
      );
      LoadingDialogHelper.hide();
      if (response['result'] == 'success') {
        Navigator.of(context).pop();
      } else {
        ToastUtil.showNormal(context, '请求失败');
      }
    } catch (e) {
      ToastUtil.showNormal(context, '请求异常：$e');
    }
  }

  Widget lineItem({
    required String label,
    required bool isEditable,
    String? text,
  }) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        ItemListWidget.singleLineTitleText(
          label: label,
          isEditable: isEditable,
          text: text ?? '',
        ),
        const Divider(height: 1, thickness: 1),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppbar(title: "重点工程信息"),
      body: SafeArea(
        child: Padding(
          padding: EdgeInsets.symmetric(horizontal: 12, vertical: 12),
          child: Column(
            children: [
              ItemListWidget.itemContainer(
                Column(
                  children: [
                    ItemListWidget.singleLineTitleText(
                      label: '重点工程名称:',
                      isEditable: false,
                      text: info['OUTSOURCED_NAME'] ?? '',
                    ),
                    const Divider(),
                    ItemListWidget.singleLineTitleText(
                      label: '计划工期:',
                      isEditable: false,
                      text:
                          '${info['STARTTIME'] ?? ''}至${info['ENDTIME'] ?? ''}',
                    ),
                    const Divider(),
                    ItemListWidget.singleLineTitleText(
                      label: '合同号:',
                      isEditable: false,
                      text: info['CONTRACT_NUM'] ?? '',
                    ),
                    const Divider(),
                    ItemListWidget.singleLineTitleText(
                      label: '施工相关方:',
                      isEditable: false,
                      text: info['UNITS_NAME'] ?? '',
                    ),
                    const Divider(),
                    ItemListWidget.singleLineTitleText(
                      label: '相关方单位工程负责人:',
                      isEditable: false,
                      text: info['UNITS_PIC_NAME'] ?? '',
                    ),
                    const Divider(),
                    ItemListWidget.singleLineTitleText(
                      label: '相关方单位负责人手机:',
                      isEditable: false,
                      text: info['UNITS_PHONE'] ?? '',
                    ),
                    const Divider(),
                    ItemListWidget.singleLineTitleText(
                      label: '经度:',
                      isEditable: false,
                      text: info['WORK_LONGITUDE'] ?? '',
                    ),
                    const Divider(),
                    ItemListWidget.singleLineTitleText(
                      label: '纬度:',
                      isEditable: false,
                      text: info['WORK_LATITUDE'] ?? '',
                    ),
                    const Divider(),
                    ItemListWidget.singleLineTitleText(
                      label: '具体位置:',
                      isEditable: false,
                      text: info['LOCATION'] ?? '',
                    ),
                  ],
                ),
              ),
              SizedBox(height: 20),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  if (info['STATE'] == '-1') ...[
                    SizedBox(
                      width: 140,
                      child: CustomButton(
                        text: '工程开始',
                        backgroundColor: Colors.blue,
                        onPressed: () => onUpdateState('1'),
                      ),
                    ),
                    const SizedBox(width: 12),
                  ],
                  if (info['STATE'] == '-2') ...[
                    SizedBox(
                      width: 140,
                      child: CustomButton(
                        text: '工程结束',
                        backgroundColor: Colors.blue,
                        onPressed: () => onUpdateState('2'),
                      ),
                    ),
                    const SizedBox(width: 12),
                  ],
                  SizedBox(
                    width: 140,
                    child: CustomButton(
                      text: '返回',
                      backgroundColor: Colors.blue,
                      onPressed: () => Navigator.pop(context),
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}
