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

export const useAllOrderStore = defineStore('allOrder', {
    state: () => ({
        lists: [],
        page: {},
        pagination: [],
        show: {},
        orderItems: {},
        orderUser: {},
        orderAddress: {},
        orderDeliveryBoy: {}
    }),
    actions: {
        fetchLists: function (payload) {
            return new Promise((resolve, reject) => {
                let url = 'admin/all-order';
                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);
                });
            });
        },
        fetchShow: function (payload) {
            return new Promise((resolve, reject) => {
                axios.get(`admin/all-order/show/${payload}`).then((res) => {
                    this.show = res.data.data;
                    this.orderItems = res.data.data.order_items;
                    this.orderUser = res.data.data.user;
                    this.orderAddress = res.data.data.order_address;
                    this.orderDeliveryBoy = res.data.data.delivery_boy;
                    resolve(res);
                }).catch((err) => {
                    reject(err);
                });
            });
        },
        changeStatus: function (payload) {
            return new Promise((resolve, reject) => {
                axios.post(`admin/all-order/change-status/${payload.id}`, payload).then((res) => {
                    this.show = res.data.data;
                    resolve(res);
                }).catch((err) => {
                    reject(err);
                });
            });
        },
        changePaymentStatus: function (payload) {
            return new Promise((resolve, reject) => {
                axios.post(`admin/all-order/change-payment-status/${payload.id}`, payload).then((res) => {
                    this.show = res.data.data;
                    resolve(res);
                }).catch((err) => {
                    reject(err);
                });
            });
        },
        selectDeliveryBoy: function (payload) {
            return new Promise((resolve, reject) => {
                axios.post(`admin/all-order/select-delivery-boy/${payload.id}`, payload).then((res) => {
                    this.show = res.data.data;
                    resolve(res);
                }).catch((err) => {
                    reject(err);
                });
            });
        },
        export: function (payload) {
            return new Promise((resolve, reject) => {
                let url = "admin/all-order/export";
                if (payload) {
                    url = url + appService.requestHandler(payload);
                }
                axios.get(url, { responseType: "blob" }).then((res) => {
                    resolve(res);
                }).catch((err) => {
                    reject(err);
                });
            });
        },
    },
})
