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

export const useTomorrowOrderStore = defineStore('tomorrowOrder', {
    state: () => ({
        lists: [],
        page: {},
        pagination: [],
        show: {},
        orderItems: {},
        orderUser: {},
        orderAddress: {},
        orderDeliveryBoy: {}
    }),
    actions: {
        fetchLists: function (payload) {
            return new Promise((resolve, reject) => {
                let url = 'admin/tomorrow-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) => {
                let url = `admin/tomorrow-order/show/${payload.id}`;
                if (payload) {
                    url = url + appService.requestHandler(payload);
                }
                axios.get(url).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);
                });
            });
        },
        export: function (payload) {
            return new Promise((resolve, reject) => {
                let url = "admin/tomorrow-order/export";
                if (payload) {
                    url = url + appService.requestHandler(payload);
                }
                axios.get(url, { responseType: "blob" }).then((res) => {
                    resolve(res);
                }).catch((err) => {
                    reject(err);
                });
            });
        },
    },
})
