import request from '@/utils/request'
import { getToken, removeToken } from '@/utils/auth';
// #ifdef APP-ANDROID
import {selectTableData, insertTableData} from '@/utils/sqlite'
// #endif


export function offlineData(data:UTSJSONObject):Promise<UTSJSONObject> {
	return new Promise((resolve, reject) => {	
		let result = JSON.parseObject(JSON.stringify(data)) as UTSJSONObject 
		resolve(result)
	});	
}

// 登录方法
export function login(username:string, password:string, code:string, uuid:string):Promise<UTSJSONObject> {
  const data = {
    username,
    password,
    code,
    uuid
  } as UTSJSONObject
  
  let data2 = {
    username: "admin",
    password: "admin123",
    code: "",
    uuid: '1',
  	token: '123456', 
	roles:[] as string[]
  } as UTSJSONObject
  
    // #ifdef APP-ANDROID
    const result = selectTableData('app_user', 'username', username, 'password', password);
    console.log(result);
	return result;
	// #endif
   
  // #ifdef H5
   return offlineData(data2)
  // #endif
  
  
  /*
  return request({
    'url': '/login',
    headers: {
      isToken: false
    },
    'method': 'post',
    'data': data
  })*/ 
}
export type RegForm={
			username: string,
			password: string,
			token: string,
			confirmPassword: string,
			code: string,
			uuid: string
}
// 注册方法
export function register(data:RegForm):Promise<UTSJSONObject> {
  /*	
  return request({
    url: '/register',
    headers: {
      isToken: false
    },
    method: 'post',
    data
  }) */
  // #ifdef APP-ANDROID
	let condition = 'username,password,token,status,createtime,updatetime'
	let date = "strftime('%Y-%m-%d %H:%M:%S', 'now')"
	let dataStr = `'${data.username}','${data.password}','${data.token}',1, ${date}, ${date}`;  
	console.log(dataStr)
	const result = insertTableData('app_user', dataStr, condition);
	console.log(result);
	return result;
  // #endif
  
  // #ifdef H5
   return offlineData(data)
  // #endif  
}

// 获取用户详细信息
export function getInfo():Promise<UTSJSONObject> {
  /*	
  return request({
    'url': '/getInfo',
    'method': 'get'
  })*/
  let data = {
    username: "admin",
    password: "admin123",
    code: "",
    uuid: '1',
	token: '123456', 
	roles:[] as string[]
  } as UTSJSONObject
      
	// #ifdef APP-ANDROID
	const result = selectTableData('app_user', 'token', getToken(), null, null);
	console.log(result);
	return result;
  // #endif
  // #ifdef H5  
  return offlineData(data)
  // #endif
}

// 退出方法
export function logout(token?:any):Promise<UTSJSONObject> {
  /*
  return request({
    'url': '/logout',
    'method': 'post'
  }) */
  return new Promise((resolve, reject) => {  
	 let obj = {} as UTSJSONObject
  	 resolve(obj)
  });
}

// 获取验证码
export function getCodeImg():Promise<UTSJSONObject> {
  return request({
    'url': '/captchaImage',
    headers: {
      isToken: false
    },
    method: 'get',
    timeout: 20000
  })
}
