import 'dart:convert';

import 'package:EasyDartModule/EasyDartModule.dart';
import 'package:ef/ef.dart';
import 'package:vbvs_app/common/color/ServiceConstant.dart';
import 'package:vbvs_app/common/color/app_uri_status.dart';
import 'package:vbvs_app/common/util/DailyLogUtils.dart';
import 'package:vbvs_app/common/util/Dio.dart';
import 'package:vbvs_app/common/util/MyUtils.dart';
import 'package:vbvs_app/common/util/requestWithLog.dart';
import 'package:vbvs_app/component/tool/TopSlideNotification.dart';
import 'package:vbvs_app/controller/user_info_controller.dart';
import 'package:vbvs_app/model/api_response.dart';

class BookInfoModel {
  // DateTime? dateTime; //预约时间
  String? userName; //预约人
  String? userPhone; //预约电话
  List time_period = []; //预约时段
  // String? select_time; //选择时间
  int? select_time_index; //选择时间

  List datetimes = [];
  int? datetimes_index;
  Map dataT = {};

  BookInfoModel();
  BookInfoModel.fromJson(Map<String, dynamic> json) {
    // dateTime = json['dateTime'];
    userName = json['userName'];
    userPhone = json['userPhone'];
    time_period = json['time_period'];
    // select_time = json['select_time'];
    select_time_index = json['select_time_index'];
  }
}

class BookInfoController extends GetControllerEx<BookInfoModel> {
  BookInfoController() {
    attr = GetModel(BookInfoModel()).obs;
  }

  get userInfoController => Get.find<UserInfoController>();

  getData(id) {
    model.datetimes = [];
    model.datetimes_index = null;
    model.dataT = {};
    updateAll();
    ApiService.reservation
        .get("/agent/userBook/config/detailConfigByStore?storeId=$id")
        .then((d) {
      model.datetimes = d.data["dateList"];
      model.datetimes_index = 0;
      model.dataT = d.data;
      time_periodChange();
    });
  }

  time_periodChange() {
    if (model.datetimes_index == null) {
      return;
    }
    model.select_time_index = null;
    model.time_period =
        model.dataT[model.datetimes?[model.datetimes_index!]["day"]];
    updateAll();
  }

  submitData(id) {
    String phone = userInfoController.model?.user?.phone ?? "";
    if (phone.isEmpty) {
      showToast("用户未存在手机号");
      return;
    }
    return ApiService.reservation.post("/agent/userBook/submitBook", data: {
      "extUserId": phone,
      "storeId": id,
      "realName": model.userName,
      "userPhone": model.userPhone,
      "bookDateId": model.time_period[model.select_time_index!]["id"]
    });
  }

  messageAdd(Map data) {
    return ApiService.request.post("/api/message/info",
        data: {"type": 0, "status": 1, "read": 1, "data": jsonEncode(data)});
  }
}
