import axios from 'axios'
import { defineStore } from "pinia";
import appService from "../services/appService";

export const UsePushNotificationStore = defineStore ("pushNotification", {
    state: () => ({
        lists: [],
        page: {},
        pagination: [],
        show: {},
        temp: {
            temp_id: null,
            isEditing: false,
        },
    }),
    actions: {
        fetchList: function (payload) {           
            return new Promise((resolve, reject) => {
                let url = 'admin/push-notification';
                if (payload) {
                    url = url + appService.requestHandler(payload);
                }
                axios
                    .get(url)
                    .then((res) => {
                        if (
                            typeof payload.pinia === "undefined" ||
                            payload.pinia === true
                        ) {
                            this.lists = res.data.data;
                            this.pagination = res.data;
                            if (
                                typeof res.data.meta !== "undefined" &&
                                res.data.meta !== null
                            ) {
                                this.page = {
                                    from: res.data.meta.from,
                                    to: res.data.meta.to,
                                    total: res.data.meta.total,
                                };
                            }
                        }
                        resolve(res);
                    })
                    .catch((err) => {
                        reject(err);
                    });
            });
        },
        save: function (payload) {
            return new Promise((resolve, reject) => {
                let method = axios.post;
                let url = "/admin/push-notification";
                if (this.temp.isEditing) {
                    method = axios.post;
                    url = `/admin/push-notification/${this.temp.temp_id}`;
                }
                method(url, payload.form)
                    .then((res) => {
                        this.fetchList(payload.search).then().catch();
                        this.temp.temp_id = null;
                        this.temp.isEditing = false;
                        resolve(res);
                    })
                    .catch((err) => {
                        reject(err);
                    });
            });
        },
        edit: function (payload) {
            this.temp.temp_id = payload;
            this.temp.isEditing = true;
        },
        destroy: function (payload) {
            return new Promise((resolve, reject) => {
                axios
                    .delete(`admin/push-notification/${payload.id}`)
                    .then((res) => {
                        this.fetchList(payload.search).then().catch();
                        resolve(res);
                    })
                    .catch((err) => {
                        reject(err);
                    });
            });
        },
        fetchShow: function (payload) {
            return new Promise((resolve, reject) => {
                axios
                    .get(`admin/push-notification/show/${payload.id}`)
                    .then((res) => {
                        this.show = res.data.data;
                        resolve(res);
                    })
                    .catch((err) => {
                        reject(err);
                    });
            });
        },
        changeImage: function (payload) {
            return new Promise((resolve, reject) => {
                axios
                    .post(
                        `admin/push-notification/change-image/${payload.id}`,
                        payload.form,
                        {
                            headers: {
                                "Content-Type": "multipart/form-data",
                            },
                        }
                    )
                    .then((res) => {
                        this.show = res.data.data;
                        resolve(res);
                    })
                    .catch((err) => {
                        reject(err);
                    });
            });
        },
        reset: function () {
            return new Promise((resolve, reject) => {
                this.temp.temp_id = null;
                this.temp.isEditing = false;
                resolve();
            });
        },
        export: function (payload) {
            return new Promise((resolve, reject) => {
                let url = "admin/push-notification/export";
                if (payload) {
                    url = url + appService.requestHandler(payload);
                }
                axios
                    .get(url, { responseType: "blob" })
                    .then((res) => {
                        resolve(res);
                    })
                    .catch((err) => {
                        reject(err);
                    });
            });
        },
        details: function (payload) {
            return new Promise((resolve, reject) => {
                let url = `admin/push-notification/details/${payload}`;
                axios
                    .get(url, payload)
                    .then((res) => {
                        resolve(res);
                    })
                    .catch((err) => {
                        reject(err);
                    });
            });
        },
    },
})
