import {
    deleteCardById,
    getCardFolderId,
    listCardImages,
    listCards,
    listCardsByFilter,
    moveCardFileToFolder,
    saveCardItem,
    uploadCardImage,
} from "@/module/ose-card/service/card-service";

export default {
    namespaced: true,
    state: {
        timer_id: null,
        card_list: [],
        edit_card_data: JSON.parse(sessionStorage.getItem('edit_card_data')) || {},
        images: []
    },
    actions: {
        async getCardList({rootState, commit, dispatch}, corporate_customer_id) {
            const client = rootState.directus.client
            const result = await listCards(client, corporate_customer_id);
            commit("card_list", result)
        },
        async deleteCard({rootState, commit, dispatch}, id) {
            const client = rootState.directus.client
            await deleteCardById(client, id);
            await dispatch("getCardList", rootState.user.current_corporate_customer)
        },
        async saveCardFilter({rootState, commit, dispatch}, options) {
            let start_date = new Date(options.item.start_date)
            start_date.setHours(new Date(options.item.start_date).getHours()+8)
            let end_date = new Date(options.item.end_date)
            end_date.setHours(new Date(options.item.end_date).getHours()+8)
            let filter = {
                _and: [
                    {
                        origin: {
                            _eq: options.item.origin
                        }
                    },
                    {
                        card_code: {
                            _eq: options.item.card_code
                        }
                    },
                    {
                        _or: [
                            {
                                start_date: {
                                    _between: [
                                        start_date.toISOString(),
                                        end_date.toISOString()
                                    ]
                                }
                            },
                            {
                                end_date: {
                                    _between: [
                                        start_date.toISOString(),
                                        end_date.toISOString()
                                    ]
                                }
                            }
                        ]
                    }
                ]
            }
            if (options.mode == 'edit')
                filter._and.push({
                    id: {
                        _neq: options.id
                    }
                })
            const client = rootState.directus.client
            const result = await listCardsByFilter(client, filter);
            return result
        },
        async saveCard({rootState, commit, dispatch}, options) {
            const client = rootState.directus.client
            return await saveCardItem(client, options)
        },
        async getImages({rootState, commit, dispatch}) {
            const client = rootState.directus.client
            const result = await listCardImages(client)
            commit("images", result)
        },
        async getFolder({rootState, commit, dispatch}) {
            const client = rootState.directus.client
            return await getCardFolderId(client)
        },
        async changeFileFolder({rootState, commit, dispatch}, fileId) {
            const client = rootState.directus.client
            const folderId = await dispatch('getFolder')
            await moveCardFileToFolder(client, fileId, folderId)
        },
        async uploadImages({rootState, commit, dispatch}, file) {
            const client = rootState.directus.client
            const folderId = await dispatch('getFolder')
            const result = await uploadCardImage(client, file, folderId)
            commit("addNewImage", {url: result.url})
        }
    },
    mutations: {
        card_list(state, val) {
            state.card_list = val
        },
        edit_card_data(state, val) {
            state.edit_card_data = val
        },
        images(state, val) {
            state.images = val
        },
        addNewImage(state, val) {
            state.images.unshift(val)
        }

    },
    getters: {}
}
