<script>
import moment from 'moment'
import axios from 'axios'
import Error from '@/types/ErrorType.vue'
export default {
    data() {
        return {
            customer_list: [],
            errors: {}
        }
    },
    methods:{
        formatDate(d,f) {
            return moment(String(d)).format(f)
        },
        formatNumber(num,chars) {
            return parseFloat(num).toFixed(chars)
        },
        getDateNow() {
            let now = new Date()
            return now.toLocaleString('en-GB', {
                day: '2-digit',
                month: 'long',
                year: 'numeric'
            })
        },
        getTimeNow() {
            let now = new Date()
            return now.toLocaleString('en-GB', {
                month: 'long',
                day: '2-digit',
                year: 'numeric',
                hour: '2-digit',
                minute: '2-digit',
                second: '2-digit'})
        },
        async error(msg) {
            let e = new Error()
            e.msg = msg
            e.id = crypto.randomUUID()
            this.errors[e.id] = e
            setTimeout(() => {
               delete this.errors[e.id]
            }, 10000)
        },
        async getCustomerList(name) {
            console.log("searching customers...")
            if (this.customer_list.length == 0) {
                console.log("getting new customers...")
                let url = this.$api_url + "/customers/full_list"
                axios.get(url)
                    .then(resp => {
                        this.customer_list = resp.data
                        console.log(this.customer_list)
                        return this.customer_list.filter(x => {
                            x.name.contains(name) || x.acc_no.contains(name)
                        })
                    })
                    .catch(err => {
                        console.log(err)
                    })
            } else {
                return this.customer_list
            }
        }
    }
}
</script>
