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

export const useFaqStore = defineStore("faq", {
    state: () => ({
        lists: [],
        page: {},
        pagination: [],
        show: {},
        temp: {
            temp_id: null,
            isEditing: false,
        }
    }),
    actions: {
        fetchLists: function (payload) {
            return new Promise((resolve, reject) => {
                let url = "admin/setting/faq";
                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/setting/faq";
                if (this.temp.isEditing) {
                    method = axios.put;
                    url = `/admin/setting/faq/${this.temp.temp_id}`;
                }
                method(url, payload.form).then((res) => {
                    this.fetchLists(payload.search).then().catch();
                    this.temp.temp_id = null;
                    this.temp.isEditing = false;
                    resolve(res);
                }).catch((err) => {
                    reject(err);
                });
            });
        },
        edit: function (payload) {
            return new Promise((resolve, reject) => {
                this.temp.temp_id = payload;
                this.temp.isEditing = true;
                resolve();
            })
        },
        destroy: function (payload) {
            return new Promise((resolve, reject) => {
                axios.delete(`admin/setting/faq/${payload.id}`).then((res) => {
                    this.fetchLists(payload.search).then().catch();
                    resolve(res);
                }).catch((err) => {
                    reject(err);
                });
            });
        },
        fetchShow: function (payload) {
            return new Promise((resolve, reject) => {
                axios.get(`admin/setting/faq/show/${payload}`).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();
            });
        },
        sortFaq: function (payload) {
            return new Promise((resolve, reject) => {
                let method = axios.post;
                let url = '/admin/setting/faq/sort/sortFaq';
                method(url, payload.form).then(res => {
                    this.fetchLists(payload.search).then().catch();
                    this.temp.temp_id = null;
                    this.temp.isEditing = false;
                    resolve(res);
                }).catch((err) => {
                    reject(err);
                });
            });
        },
    }
});
