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

export default {
    async get(serial){
        try{
            const response = await apolloProvider.defaultClient.query({
                query: gql`
                    query aoiSerial($serial:String!){
                        aoiSerial(serial:$serial){              
                            uid                  
                            name                
                            state                
                            serial               
                            projectUid
                        }
                    }
                `,
                fetchPolicy: 'no-cache',
                variables: { serial: serial }
            })

            let aoi =  response.data.aoiSerial

            if (!aoi) return ''

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

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

    async update(input){
        try{
            const response = await apolloProvider.defaultClient.mutate({
                mutation: gql`
                    mutation updateAoi( $name: String! ,$state: Boolean! , $serial:String!  ,$updateDate:String! ,$uid:Int! , $projectUid:Int ){
                        updateAoi(input:{
                            name:$name
                            state:$state
                            serial:$serial
                            updateDate:$updateDate
                            projectUid:$projectUid
                            uid:$uid
                        })
                        {
                            aoi{
                                uid                  
                                name                
                                state                
                                serial               
                                updateDate
                                projectUid
                            }
                        }
                    }
                `,
                variables: {
                    uid:input.uid,              
                    name:input.name,            
                    state:input.state,                
                    serial:input.serial,                                           
                    updateDate:new Date(),    
                    projectUid:0
                }
            })

        let aoi = response.data.updateAoi.aoi

        if (!aoi) return ''

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

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