import React from 'react'
import { AsyncStorage } from 'react-native'
import io from 'socket.io-client'

const SERVER_URL = 'http://192.168.31.144'
// const SERVER_URL = 'http://10.89.80.125'
// const SERVER_URL = 'http://10.89.151.157'

export const SOCKET_URL = `${SERVER_URL}:8081`
const API_URL = `${SERVER_URL}:8080`

const jsonHeader = {
  'Accept': 'application/json',
  'Content-Type': 'application/json'
}

// Login
export function loginUser (form) {
  return fetch(`${API_URL}/login`, {
    method: 'POST',
    headers: { ...jsonHeader },
    body: JSON.stringify(form)
  })
    .then(response => response.json())
    .catch(err => console.error(err))
}

export async function logoutUser () {
  let token = await AsyncStorage.getItem('Authorization')
  return fetch(`${API_URL}/login`, {
    method: 'POST',
    headers: { Authorization: token, ...jsonHeader },
  })
    .then(response => response.json())
    .catch(err => console.error(err))
}

export function registerUser (form) {
  return fetch(`${API_URL}/user`, {
    method: 'POST',
    headers: { ...jsonHeader },
    body: JSON.stringify(form)
  })
    .then(response => response.json())
    .catch(err => console.error(err))
}

// Task
export async function fetchTasks (page = 0) {
  let token = await AsyncStorage.getItem('Authorization')
  return fetch(`${API_URL}/task?page=${page}`, {
    headers: { Authorization: token, ...jsonHeader }
  })
    .then(response => response.json())
    .catch(err => console.error(err))
}

export async function fetchTaskByID (taskID) {
  let token = await AsyncStorage.getItem('Authorization')
  return fetch(`${API_URL}/task/${taskID}`, {
    headers: { Authorization: token, ...jsonHeader }
  })
    .then(response => response.json())
    .catch(err => console.error(err))
}

export async function fetchChats () {
  let token = await AsyncStorage.getItem('Authorization')
  let itsc = await AsyncStorage.getItem('itsc')
  return fetch(`${API_URL}/task?rfid=${itsc}`, {
    headers: { Authorization: token, ...jsonHeader }
  })
    .then(response => response.json())
    .catch(err => console.error(err))
}

export async function createTask (task) {
  let token = await AsyncStorage.getItem('Authorization')
  return fetch(`${API_URL}/task`, {
    method: 'POST',
    headers: { Authorization: token, ...jsonHeader },
    body: JSON.stringify(task)
  })
    .then(response => response.json())
    .catch(err => console.error(err))
}

export async function updateTask (task, change) {
  let token = await AsyncStorage.getItem('Authorization')
  return fetch(`${API_URL}/task/${task._id}`, {
    method: 'PUT',
    headers: { Authorization: token, ...jsonHeader },
    body: JSON.stringify(change)
  })
    .then(response => console.log(response))
    .catch(err => console.error(err))
}

@@ -698,33 +698,22 @@
  () %7B%0A  
-let token = await
+return
  AsyncSt
@@ -710,35 +710,38 @@
 rn AsyncStorage.
-get
+remove
 Item('Authorizat
@@ -750,163 +750,8 @@
 n')%0A
-  return fetch(%60$%7BAPI_URL%7D/login%60, %7B%0A    method: 'POST',%0A    headers: %7B Authorization: token, ...jsonHeader %7D,%0A  %7D)%0A    .then(response =%3E response.json())%0A
     
@@ -1075,16 +1075,22 @@
 ks (
+%7B
 page
- = 0
+, status%7D
 ) %7B%0A
@@ -1139,32 +1139,48 @@
 Authorization')%0A
+  console.log()%0A
   return fetch(%60
@@ -1199,21 +1199,79 @@
 ask?
-page=$%7Bpage%7D%60
+%60 + (page ? %60page=$%7Bpage%7D&%60 : '') + (status ? %60status=$%7Bstatus%7D&%60 : '')
 , %7B%0A
@@ -1841,16 +1841,16 @@
 'itsc')%0A
-
   return
@@ -1885,16 +1885,34 @@
 =$%7Bitsc%7D
+&nstatus=COMPLETED
 %60, %7B%0A   

fix logout user (previously copies login)