import gql from 'graphql-tag' // eslint-disable-line no-unused-vars
import apolloProvider from '../plugins/apollo'
// import store from '@/store'

export default {
    async get(uid){
        try{
            const response = await apolloProvider.defaultClient.query({
                query: gql`
                    query nProject($aoiUid:Int!) {
                        nProject(aoiUid:$aoiUid) {
                            name
                            admin
                            user
                            createDate
                            successDate
                            counter
                            aoiUid
                            uid
                            state
                            updateDate
                        }
                    }`,
                fetchPolicy: 'no-cache',

                variables: { aoiUid: uid }
            })

        let project = response.data.nProject

        if (!project) return ''

        if (project.errors)  throw new Error(project.errors[0])
        
        return project

        }catch (e) { 
            return false
        }
    },

    async update(input){
        try{
            const response = await apolloProvider.defaultClient.mutate({
                mutation: gql`
                    mutation updateNproject( 
                            $name: String! ,
                            $admin: String! ,
                            $user: String! ,
                            $createDate: String! ,
                            $successDate: String! , 
                            $counter:Int , 
                            $aoiUid:Int! ,
                            $uid:Int!,
                            $state:Boolean!, 
                            $updateDate:String,
                            $success:Int,
                            $fail:Int,
                            $total:Int,
                            $result:Boolean
                        ){
                        updateNproject(input:{
                            name:$name
                            admin:$admin
                            user:$user
                            createDate:$createDate
                            successDate:$successDate
                            counter:$counter
                            aoiUid:$aoiUid
                            uid:$uid
                            state:$state
                            updateDate:$updateDate,
                            success:$success,
                            fail:$fail,
                            total:$total,
                            result:$result
                        })
                        {
                            nProject{
                                name
                                admin
                                user
                                createDate
                                successDate
                                counter
                                aoiUid
                                uid
                                state
                                updateDate
                                success
                                fail
                                total
                                result
                            }
                        }
                    }
                `,
                variables: {
                    name: input.name,
                    admin: input.admin,
                    user: input.user,
                    createDate: input.createDate,
                    successDate: input.successDate,
                    counter: input.counter,
                    aoiUid: 0,
                    uid: input.uid,
                    state: input.state,
                    updateDate: new Date(),
                    success:input.success,
                    fail:input.fail,
                    total:input.total,
                    result:input.result,
                }
            })

        let project = response.data.updateNproject.nProject

        if (!project) return ''

        if (project.errors){
            throw new Error(project.errors[0])
        } 
        
        return true

        }catch (e) { 
            console.log(e)
            throw e 
        }
    },

}