import axios from '../plugins/axios'
import { defineStore } from 'pinia'

export const useAdminsStore = defineStore('admins', {

    state: () => ({
        items: [],
        fetching: true,
        total: 0,
        token: null,
        permissions: []
    }),
    getters: {
        isLoggedIn: (state) => !!state.token
    },
    actions: {
        async login(number) {
            const { status, data, error } = await axios.post('/auth/login', {
                phone: number.value,
                type: 1
            })

            if (status == 200) {

                return { status, data, error }
            }

        }, async sendOtp(number, code) {
            const { status, data, error } = await axios.post('/auth/login-step-two', {
                phone: number.value,
                code: code.value
            })
            if (status == 200) {
                if (data.roles[0] === "Admin" || data.roles[1] === "Admin") {
                    this.token = data.token
                }
                else {
                    this.token = ''
                }

                return { status, data, error }
            }

        },
    }


})