import 'dart:async';
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:sk_base_mobile/constants/global_url.dart';
import 'package:sk_base_mobile/models/base_response.dart';
import 'package:sk_base_mobile/models/dict_type.model.dart';
import 'package:sk_base_mobile/services/dio.service.dart';
import '../constants/constants.dart';

class Api {
// 登录
  static Future<Response> login(String username, String password) {
    return DioService.dio.post(
      Urls.login,
      data: {'username': username, 'password': password},
    );
  }

// 获取个人信息
  static Future<Response> getUserInfo() {
    return DioService.dio.get(
      Urls.getUserInfo,
    );
  }

// 分页获取项目列表
  static Future<Response<PaginationData>> getProjects(Map params) {
    return DioService.dio.get<PaginationData>(Urls.projects,
        queryParameters: {'page': 1, 'pageSize': 10, ...params});
  }

// 分页获取产品列表
  static Future<Response<PaginationData>> getProducts(Map params) {
    return DioService.dio.get<PaginationData>(Urls.products,
        queryParameters: {'page': 1, 'pageSize': 10, ...params});
  }

// 分页获取原材料出入库列表
  static Future<Response<PaginationData>> getInventoryInout(Map params) {
    return DioService.dio.get<PaginationData>(Urls.inventoryInout,
        queryParameters: {'page': 1, 'pageSize': 10, ...(params)});
  }

// 获取原材料出入库详情
  static Future<Response> getInventoryInOutInfo(int id) {
    return DioService.dio.get(
      '${Urls.inventoryInout}/$id',
    );
  }

// 创建出入库记录
  static Future<Response> createInventoryInout(Map params) {
    return DioService.dio.post(Urls.inventoryInout, data: params);
  }

// 更新出入库记录
  static Future<Response> updateInventoryInout(int id, Map params) {
    return DioService.dio.put('${Urls.inventoryInout}/$id', data: params);
  }

// 分页获取库存列表
  static Future<Response<PaginationData>> getInventory(Map params) {
    return DioService.dio.get<PaginationData>(Urls.inventory,
        queryParameters: {'page': 1, 'pageSize': 10, ...(params)});
  }

// 一次性获取所有的字典类型(不分页)
  static Future<Response> getDictTypeAll(Map params) {
    return DioService.dio.post(Urls.getDictType, data: params);
  }

  static Future<Response> logout() {
    return DioService.dio.post(
      Urls.logout,
    );
  }

  static Future<Response> deleteAccount() {
    return DioService.dio.post(
      Urls.deleteAccount,
    );
  }

  static Future<Response> getAppConfig() {
    Map<String, dynamic> query = {'ver': 0};
    return DioService.dio.get(Urls.getAppConfig, queryParameters: query);
  }

  static Future<Response> saveUserInfo(Map<String, dynamic> data) {
    return DioService.dio.post(Urls.saveUserInfo, data: data);
  }

  static Future<Response> uploadImg(File file,
      {String? bussinessModule, String? bussinessRecordId}) async {
    String fileName = file.path.split('/').last;
    final filePath =
        await MultipartFile.fromFile(file.path, filename: fileName);
    final formData = FormData.fromMap({
      'bussinessRecordId': bussinessRecordId,
      'bussinessModule': bussinessModule,
      'file': filePath,
    });
    return DioService.dio.post(Urls.uploadAttachemnt, data: formData);
  }

  static Future<Response> updateAvatar(String filename) {
    return DioService.dio
        .post(Urls.updateAvatar, data: {'avatarPath': filename});
  }
}
