import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';

@Injectable({
  providedIn: 'root',
})
export class HttpService {
  constructor(public http: HttpClient) {}

  customSQL(sql: string) {
    return new Promise((resolve, reason) => {
      this.http
        .post('https://server.fmode.cn/api/novaql/select', {
          sql,
        })
        .subscribe((data) => {
          resolve(data);
        });
    });
  }

  // 定义请求方法
  httpPostRquest(url: string, params: any) : Promise<any> {
    return new Promise((resolve, reason) => {
      this.http
        .post(url, {
          params,
        })
        .subscribe((data) => {
          resolve(data);
        });
    });
  }

  postAuth(company: string, idCard: string, name: string) {
    return new Promise((resolve, reason) => {
      this.http
        .post('https://server.fmode.cn/api/apig/idcard', {
          company: company,
          cardNo: idCard,
          realName: name,
        })
        .subscribe((data) => {
          resolve(data);
        });
    });
  }
  async verifyCard(param:{
    company: string,
    bankcard: string,
    name: string,
    idcard: string,
    mobile: string,
  }) {
    return new Promise((resolve, reject) => {
      this.http
        .post('https://server.fmode.cn/api/apig/bankcard', param)
        .subscribe((res: any) => {
          console.log(res);
          resolve(res);
        });
    });
  }

  // 定义jsonp的方法
  getJsonpMsg(url: string, cb: string) {
    return this.http.jsonp(url, cb);
  }

  async httpRequst(url: string, reqBody: any, method: string = 'GET') {
    let queryString = '';
    if (method === 'GET' && reqBody) {
      queryString = new URLSearchParams(reqBody).toString();
      url += `?${queryString}`;
    }
    let result = await fetch(url, {
      method: method,
      headers: {
        'Content-Type': 'application/json',
      },
      body: method !== 'GET' ? JSON.stringify(reqBody) : undefined,
    });
    let data = await result.json();
    // console.log(data);
    return data;
  }
}
