import { defineStore } from 'pinia';

/**
 * 运送清单表单数据缓存 Store
 * 支持新增模式和编辑模式的表单数据缓存
 */
export const useDeliveryFormStore = defineStore('deliveryForm', {
    state: () => {
        return {
            // 新增模式的缓存数据
            newFormData: {
                orderId: null as number | null,
                shipper: null as number | null,
                buyer: null as number | null,
                plateNumber: null as string | null,
                driverId: null as number | null,
                driverPhone: '' as string,
                serverId: null as number | null,
                serverDeviceId: '' as string,
                eartagIds: [] as number[],
                collarIds: [] as number[],
                eartagDeviceIds: [] as string[],
                collarDeviceIds: [] as string[],
                estimatedDepartureTime: '' as string,
                estimatedArrivalTime: '' as string,
                startLocation: '' as string,
                endLocation: '' as string,
                startLat: '' as string,
                startLon: '' as string,
                endLat: '' as string,
                endLon: '' as string,
                cattleCount: 1 as number,
                quarantineCertNo: '' as string,
                remark: '' as string,
                emptyWeight: null as number | null,
                entruckWeight: null as number | null,
                landingEntruckWeight: null as number | null,
                quarantineTickeyUrl: '' as string,
                poundListImg: '' as string,
                emptyVehicleFrontPhoto: '' as string,
                loadedVehicleFrontPhoto: '' as string,
                loadedVehicleWeightPhoto: '' as string,
                driverIdCardPhoto: '' as string,
                destinationPoundListImg: '' as string,
                destinationVehicleFrontPhoto: '' as string,
                entruckWeightVideo: '' as string,
                emptyWeightVideo: '' as string,
                cattleLoadingVideo: '' as string,
                controlSlotVideo: '' as string,
                cattleLoadingCircleVideo: '' as string,
                unloadCattleVideo: '' as string,
                destinationWeightVideo: '' as string,
            },
            // 编辑模式的缓存数据（key为editId）
            editFormData: {} as Record<number, {
                editId: number;
                orderId: number | null;
                shipper: number | null;
                buyer: number | null;
                plateNumber: string | null;
                driverId: number | null;
                driverPhone: string;
                serverId: number | null;
                serverDeviceId: string;
                eartagIds: number[];
                collarIds: number[];
                eartagDeviceIds: string[];
                collarDeviceIds: string[];
                estimatedDepartureTime: string;
                estimatedArrivalTime: string;
                startLocation: string;
                endLocation: string;
                startLat: string;
                startLon: string;
                endLat: string;
                endLon: string;
                cattleCount: number;
                quarantineCertNo: string;
                remark: string;
                emptyWeight: number | null;
                entruckWeight: number | null;
                landingEntruckWeight: number | null;
                quarantineTickeyUrl: string;
                poundListImg: string;
                emptyVehicleFrontPhoto: string;
                loadedVehicleFrontPhoto: string;
                loadedVehicleWeightPhoto: string;
                driverIdCardPhoto: string;
                destinationPoundListImg: string;
                destinationVehicleFrontPhoto: string;
                entruckWeightVideo: string;
                emptyWeightVideo: string;
                cattleLoadingVideo: string;
                controlSlotVideo: string;
                cattleLoadingCircleVideo: string;
                unloadCattleVideo: string;
                destinationWeightVideo: string;
            }>,
        };
    },
    persist: {
        enabled: true,
        strategies: [
            {
                key: 'deliveryFormCache',
                storage: localStorage,
            },
        ],
    },
    actions: {
        /**
         * 保存新增模式表单数据
         */
        saveNewFormData(data: Partial<typeof this.newFormData>) {
            // 深度合并，确保数组和对象正确更新
            Object.keys(data).forEach(key => {
                const value = data[key];
                if (Array.isArray(value)) {
                    // 数组类型，创建新数组
                    this.newFormData[key] = [...value] as any;
                } else {
                    // 其他类型，直接赋值
                    this.newFormData[key] = value as any;
                }
            });
        },

        /**
         * 获取新增模式表单数据
         */
        getNewFormData() {
            return { ...this.newFormData };
        },

        /**
         * 清除新增模式表单数据
         */
        clearNewFormData() {
            this.newFormData = {
                orderId: null,
                shipper: null,
                buyer: null,
                plateNumber: null,
                driverId: null,
                driverPhone: '',
                serverId: null,
                serverDeviceId: '',
                eartagIds: [],
                collarIds: [],
                eartagDeviceIds: [],
                collarDeviceIds: [],
                estimatedDepartureTime: '',
                estimatedArrivalTime: '',
                startLocation: '',
                endLocation: '',
                startLat: '',
                startLon: '',
                endLat: '',
                endLon: '',
                cattleCount: 1,
                quarantineCertNo: '',
                remark: '',
                emptyWeight: null,
                entruckWeight: null,
                landingEntruckWeight: null,
                quarantineTickeyUrl: '',
                poundListImg: '',
                emptyVehicleFrontPhoto: '',
                loadedVehicleFrontPhoto: '',
                loadedVehicleWeightPhoto: '',
                driverIdCardPhoto: '',
                destinationPoundListImg: '',
                destinationVehicleFrontPhoto: '',
                entruckWeightVideo: '',
                emptyWeightVideo: '',
                cattleLoadingVideo: '',
                controlSlotVideo: '',
                cattleLoadingCircleVideo: '',
                unloadCattleVideo: '',
                destinationWeightVideo: '',
            };
        },

        /**
         * 保存编辑模式表单数据
         */
        saveEditFormData(editId: number, data: Partial<typeof this.editFormData[number]>) {
            if (!this.editFormData[editId]) {
                this.editFormData[editId] = {
                    editId,
                    orderId: null,
                    shipper: null,
                    buyer: null,
                    plateNumber: null,
                    driverId: null,
                    driverPhone: '',
                    serverId: null,
                    serverDeviceId: '',
                    eartagIds: [],
                    collarIds: [],
                    eartagDeviceIds: [],
                    collarDeviceIds: [],
                    estimatedDepartureTime: '',
                    estimatedArrivalTime: '',
                    startLocation: '',
                    endLocation: '',
                    startLat: '',
                    startLon: '',
                    endLat: '',
                    endLon: '',
                    cattleCount: 1,
                    quarantineCertNo: '',
                    remark: '',
                    emptyWeight: null,
                    entruckWeight: null,
                    landingEntruckWeight: null,
                    quarantineTickeyUrl: '',
                    poundListImg: '',
                    emptyVehicleFrontPhoto: '',
                    loadedVehicleFrontPhoto: '',
                    loadedVehicleWeightPhoto: '',
                    driverIdCardPhoto: '',
                    destinationPoundListImg: '',
                    destinationVehicleFrontPhoto: '',
                    entruckWeightVideo: '',
                    emptyWeightVideo: '',
                    cattleLoadingVideo: '',
                    controlSlotVideo: '',
                    cattleLoadingCircleVideo: '',
                    unloadCattleVideo: '',
                    destinationWeightVideo: '',
                };
            }
            this.editFormData[editId] = { ...this.editFormData[editId], ...data, editId };
        },

        /**
         * 获取编辑模式表单数据
         */
        getEditFormData(editId: number) {
            return this.editFormData[editId] ? { ...this.editFormData[editId] } : null;
        },

        /**
         * 清除指定编辑模式表单数据
         */
        clearEditFormData(editId: number) {
            if (this.editFormData[editId]) {
                delete this.editFormData[editId];
            }
        },

        /**
         * 清除所有缓存
         */
        clearAllCache() {
            this.clearNewFormData();
            this.editFormData = {};
        },
    },
});

