/*
 * @Author: WuWei
 * @Date: 2020-05-14 17:55:00
 * @LastEditors  : WuWei
 * @LastEditTime : 2023-02-07 10:10:45
 * @Description:
 */

import 'package:get/get.dart';
import 'package:app_package/app_package.dart';
import '../../src/service/impl/notice_service_impl.dart';
import '../../src/service/notice_service.dart';
import '../../src/models/notice_model.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
import 'package:intl/intl.dart';

class NoticeDetailStore extends GetxController {
  final NoticeService _noticeService = NoticeServiceImpl();
  final communityGlobalStore = GetIt.I.get<CommunityGlobalStore>();

  ///从储存中取出的当前小区 拿出来可能是空
  CommunityModel? get currentCommunity => communityGlobalStore.data;

  dateNow() {
    var now = DateTime.now();
    DateFormat outputFormat = DateFormat("yyyy-MM-dd HH:mm:ss");
    return outputFormat.format(now);
  }

  routeBack() async {
    Get.back();
  }

  ///详情
  var notice = NoticeModel().obs;
  var content = ''.obs;
  var status = ''.obs;
  var theme = ''.obs;
  Future getDetail() async {
    var res = await _noticeService.getNoticeDetail(Get.arguments['id']);
    res.data!.content?.replaceAll(RegExp('\n'), '<br>');
    res.data!.content?.replaceAll(RegExp(' '), '&nbsp;');
    res.data!.content?.replaceAllMapped(RegExp(r'<img src="/\w+/'), (match) {
      return '<img width="100%" src="${APPConfig.baseURL}';
    });
    res.data!.content?.replaceAllMapped(RegExp(r'&nbsp;'), (match) => ' ');
    notice.value = res.data!;
    content.value = res.data!.content!;
    status.value = res.data!.status!;
    theme.value = res.data!.theme!;
    notice.refresh();
    content.refresh();
    status.refresh();
    theme.refresh();
  }

  Future publishNotice(id) async {
    String? noticeId = await _noticeService.publishNotice(id);
    if (noticeId != null) {
      WToastUtil.show('发布成功');
      //成功后返回到通知列表
      // notice.value.status = 'statusPublished';
      // DateFormat outputFormat = DateFormat("yyyy-MM-dd HH:mm:ss");
      // notice.value.publishAt = outputFormat.format(DateTime.now());
      // Navigator.of(context)..pop()..pop(notice);
      Get.back();
      Get.back();
      Get.back();
      Get.toNamed('notice/index', arguments: {'appBarTitle': '通知列表'});
    } else {
      WToastUtil.show('发布失败');
    }
  }
}
