import axios from '../plugins/axios'
import { defineStore } from 'pinia'

export const useUploaderStore = defineStore('uploader', {
    state: () => ({

    }),


    actions: {
        async uploader(file) {
            
            const form = new FormData()

            form.append('directoryName', 'directoryName')
            form.append('file',  file)
            // for (var pair of form.entries()) {
            //     console.log('pairs :',pair[0]+ ', ' + pair[1]); 
            // }
            let config = {
                headers: {
                    "Content-Type": "multipart/form-data",
                },
                
            };
            try {
                const result = await axios.post('/uploader',
                    form,
                    config
                );
                console.log('result', result);
          
                return result;
            } catch (error) {
                console.log('err', error);
                return error;
            }


            //  const result = await axios.post('/uploader', form, 
            //     {
            //         headers: { "Content-Type": "multipart/form-data" }
            //     }
            // )
            // console.log(result);
            // return result
            //    const result =  await axios({
            //         method: "post",
            //         url: "/uploader",
            //         data: form,
            //         headers: { "Content-Type": "multipart/form-data" },
            //       })









            // .then(function (response) {

            //   console.log(response);
            // })
            // .catch(function (response) {

            //   console.log(response);
            // });
            //  console.log('result', result);
            //  return result;
        },



        async bulk(files) {
            const form = new FormData()
            files.forEach(file => form.append('images', file))

            const params = { type: 1 }

            const result = await axios.post('/images/admin/upload', form, { params })

            return result;
        }
    }
})