import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:getx_scaffold/getx_scaffold.dart'; import 'package:intl/intl.dart'; import 'package:ln_jq_app/common/model/base_model.dart'; import 'package:ln_jq_app/pages/login/view.dart'; import '../../../common/styles/theme.dart'; import '../../../storage_service.dart'; class ReservationController extends GetxController with BaseControllerMixin { @override String get builderId => 'b_reservation'; final List operationStatusOptions = ["营运中", "维修中", "暂停营业", "站点关闭"]; String selectedOperationStatus = "营运中"; // --- 其它状态下的时间选择 --- DateTime? customStartTime; DateTime? customEndTime; String get customStartTimeStr => customStartTime != null ? DateFormat('yyyy-MM-dd HH:mm').format(customStartTime!) : '点击选择开始时间'; String get customEndTimeStr => customEndTime != null ? DateFormat('yyyy-MM-dd HH:mm').format(customEndTime!) : '点击选择结束时间'; // --- 站点广播相关 --- final TextEditingController broadcastTitleController = TextEditingController(); final TextEditingController broadcastContentController = TextEditingController(); final RxInt selectedTabIndex = 0.obs; @override void onInit() { super.onInit(); // 1. 初始化默认时间 customStartTime = DateTime.now(); customEndTime = customStartTime!.add(const Duration(days: 1)); renderData(); } String name = ""; String address = ""; String phone = ""; String costPrice = ""; String customerPrice = ""; String startBusiness = ""; String endBusiness = ""; String timeStr = ""; String operatingEnterprise = ""; String hydrogenId = ""; Future renderData() async { showLoading("加载中"); try { var responseData = await HttpService.to.get( 'appointment/station/getStationInfoById?hydrogenId=${StorageService.to.userId}', ); if (responseData == null && responseData!.data == null) { dismissLoading(); showToast('暂时无法获取站点信息'); return; } try { var result = BaseModel.fromJson(responseData.data); name = result.data["name"] ?? ""; hydrogenId = result.data["hydrogenId"].toString(); address = result.data["address"] ?? ""; var rawCostPrice = result.data["costPrice"]; costPrice = (rawCostPrice != null && rawCostPrice.toString().isNotEmpty) ? "¥$rawCostPrice" : "暂无价格"; var customerPriceTemp = result.data["customerPrice"]; customerPrice = (customerPriceTemp != null && customerPriceTemp.toString().isNotEmpty) ? "$customerPriceTemp" : "暂无价格"; phone = result.data["phone"] ?? ""; startBusiness = result.data["startBusiness"] ?? ""; endBusiness = result.data["endBusiness"] ?? ""; operatingEnterprise = result.data["operatingEnterprise"].toString(); String temp = result.data["siteStatusName"].toString(); selectedOperationStatus = (temp.isEmpty || temp == "null") ? "营运中" : temp; if (startBusiness.isNotEmpty && endBusiness.isNotEmpty) { if (startBusiness == "00:00:00" && endBusiness == "23:59:59") { timeStr = "24小时营业"; } else { timeStr = '${startBusiness.substring(0, 5)} - ${endBusiness.substring(0, 5)}'; } } else { timeStr = "时间未设置"; } operatingEnterprise = operatingEnterprise.isEmpty ? "暂未设置" : operatingEnterprise; updateUi(); dismissLoading(); } catch (e) { dismissLoading(); showToast('数据异常'); } } catch (e) { dismissLoading(); } } void onOperationStatusChanged(String? newValue) { if (newValue != null) { selectedOperationStatus = newValue; updateUi(); } } /// 使用底部滚轮形式选择时间(下拉框效果) void pickDateTime(BuildContext context, bool isStart) { DateTime now = DateTime.now(); DateTime initialDate = isStart ? (customStartTime ?? now) : (customEndTime ?? now); DateTime minLimit = isStart ? now.subtract(const Duration(minutes: 1)) : (customStartTime ?? now).subtract(const Duration(minutes: 1)); if (initialDate.isBefore(minLimit)) { initialDate = isStart ? now : (customStartTime ?? now); } DateTime tempDate = initialDate; Get.bottomSheet( Container( height: 300, decoration: const BoxDecoration( color: Colors.white, borderRadius: BorderRadius.only( topLeft: Radius.circular(16), topRight: Radius.circular(16), ), ), child: Column( children: [ Padding( padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ CupertinoButton( onPressed: () => Get.back(), child: const Text('取消', style: TextStyle(color: Colors.grey)), ), CupertinoButton( onPressed: () { if (isStart) { customStartTime = tempDate; if (customEndTime != null && customEndTime!.isBefore(customStartTime!)) { customEndTime = customStartTime!.add(const Duration(days: 1)); } } else { if (tempDate.isBefore(customStartTime ?? DateTime.now())) { showToast('结束时间不能早于开始时间'); return; } customEndTime = tempDate; } updateUi(); Get.back(); }, child: const Text( '确定', style: TextStyle( color: AppTheme.themeColor, fontWeight: FontWeight.bold, ), ), ), ], ), ), const Divider(height: 1), Expanded( child: CupertinoDatePicker( mode: CupertinoDatePickerMode.dateAndTime, initialDateTime: initialDate, minimumDate: minLimit, use24hFormat: true, onDateTimeChanged: (DateTime newDate) { tempDate = newDate; }, ), ), ], ), ), backgroundColor: Colors.transparent, ); } void saveInfo() async { if (selectedOperationStatus != "营运中") { if (customStartTime == null || customEndTime == null) { showToast("请选择开始和结束时间"); return; } } showLoading("保存中"); try { var responseData = await HttpService.to.post( 'appointment/station/updateStationStatus', data: { 'hydrogenId': hydrogenId, 'siteStatus': selectedOperationStatus == "营运中" ? "0" : selectedOperationStatus == "维修中" ? "1" : selectedOperationStatus == "站点关闭" ? "2" : selectedOperationStatus == "暂停营业" ? "3" : 0, 'beginTime': selectedOperationStatus == "营运中" ? null : DateFormat('yyyy-MM-dd HH:mm:ss').format(customStartTime!), 'endTime': selectedOperationStatus == "营运中" ? null : DateFormat('yyyy-MM-dd HH:mm:ss').format(customEndTime!), 'updateTime': getNowDateTimeString(), }, ); if (responseData == null || responseData!.data == null) { dismissLoading(); showToast('服务暂不可用,请稍后'); return; } var result = BaseModel.fromJson(responseData.data); if (result.code == 0) { showSuccessToast("保存成功"); } dismissLoading(); } catch (e) { dismissLoading(); } } /// 发送站点广播 void sendBroadcast() async { String title = broadcastTitleController.text.trim(); String content = broadcastContentController.text.trim(); if (title.isEmpty) { showToast("请输入通知标题"); return; } if (content.isEmpty) { showToast("请输入通知内容"); return; } showLoading("发送中..."); try { var responseData = await HttpService.to.post( 'appointment/notice/push/station/broadcast', data: { 'title': title, 'content': content, }, ); dismissLoading(); if (responseData != null) { var result = BaseModel.fromJson(responseData.data); if (result.code == 0) { showSuccessToast("广播发送成功"); } else { showErrorToast(result.error); } } } catch (e) { dismissLoading(); showToast("发送失败,请稍后重试"); } } void logout() async { await StorageService.to.clearLoginInfo(); Get.offAll(() => LoginPage()); } @override void onClose() { broadcastTitleController.dispose(); broadcastContentController.dispose(); super.onClose(); } }