import axios from 'axios'
export const saveProject = async (title,codeHTML,codeCSS,codeJS,user,email) => {
    try {
        const response = await axios.post(`http://localhost:4000/api/project/save`, {title,codeHTML,codeCSS,codeJS,user,email})
        alert(response.data.message)
    } catch (e) {
        alert(e.response.data.message)
    }
}
export const showProject = async (email) => {
    try {
        const response = await axios.post(`http://localhost:4000/api/project/userProject`, {email})
        let araay  = response.data.project
        let table =  document.getElementById("tableUserProj")
        let linksProj = new Array
        for(let i = 0, l = araay.length; i < l; i++) {
            let prId = araay[i].prId
            let prTitle = araay[i].prTitle
            linksProj.push(`<li className="creatorProjectLeft" ><a class="projectList" href="/id/${prId}" >${[i+1]}...${prTitle}</a></li>`)
        }
        linksProj.push(`<li className="creatorProject" ><a class="projectList" href="/">++++++++++CREATE+A+NEW+PROJECT++++++++++</a></li>`)
        let newLinks = linksProj.join('')
        table.innerHTML= newLinks
        linksProj = []
    } catch (e) {
        console.log(e)
    }
}
export const showAllProject = async () => {
    try {
        const response = await axios.post(`http://localhost:4000/api/project/allProject`, {})
        let araay  = response.data.project
        let table =  document.getElementById("worldProject")
        let linksProj = new Array
        for(let i = 0, l = araay.length; i < l; i++) {
            let prId = araay[i].prId
            let prTitle = araay[i].prTitle
            let email = araay[i].email
            linksProj.push(`<li className="creatorProjectLeft" ><a class="projectList" href="/id/${prId}" >${[i+1]}|.....${prTitle}.....by.....${email}</a></li>`)
        }
        linksProj.push(`<li className="creatorProject" ><a class="projectList" href="/">++++++++++CREATE+A+NEW+PROJECT++++++++++</a></li>`)
        let newLinks = linksProj.join('')
        table.innerHTML= newLinks
        
    } catch (e) {
        console.log(e)
    }
}
export const userProject = async (idProject) => {
    try {
        let id = idProject.pathname.slice(4)
        const response = await axios.post(`http://localhost:4000/api/project/findProject`, {id})
        let project = response.data
        return [project.codeHTML,project.codeCSS,project.codeJS, project.title, project.user]
    } catch (e) {
        console.log(e)
    }
}
export const reSaveProject = async (ids,title,codeHTML,codeCSS,codeJS) => {
    try {
        let id = ids.pathname.slice(4)
        console.log(id,title,codeHTML,codeCSS,codeJS)
        const response = await axios.post(`http://localhost:4000/api/project/reSave`, {id,title,codeHTML,codeCSS,codeJS})
        alert(response.data.message)
    } catch (e) {
        alert(e.response.data.message)
    }
}
export const deleteProject = async (ids) => {
    try {
        let id = ids.pathname.slice(4)
        console.log(id)
        const response = await axios.post(`http://localhost:4000/api/project/deleteProject`, {id})
        alert(response.data.message)
    } catch (e) {
        alert(e.response.data.message)
    }
}