<template>
    <div class="col-md-12">
        <div class="card">
            <div class="card-header">
                <div class="md:flex md:justify-between">
                    <div class="md:flex">
                        <div class="sm:w-auto flex justify-center">
                            <div class="grid grid-rows-2">
                                {{ chdatere }}
                                <label>ເລືອກວັນທີ່, ເດືອນ, ງວດ, ປີ.</label>
                                <div
                                    class="btn-group h-9 w-auto mb-2"
                                    role="group"
                                    aria-label="Basic"
                                >
                                    <button
                                        type="button"
                                        class="btn btn-info btn-outline btn-sm"
                                        :class="sem"
                                        @click="selectdtype('m')"
                                    >ເດືອນ</button>
                                    <button
                                        type="button"
                                        class="btn btn-info btn-outline btn-sm"
                                        :class="se3"
                                        @click="selectdtype('m1-3')"
                                    >ງວດ1</button>
                                    <button
                                        type="button"
                                        class="btn btn-info btn-outline btn-sm"
                                        :class="se6"
                                        @click="selectdtype('m4-6')"
                                    >ງວດ2</button>
                                    <button
                                        type="button"
                                        class="btn btn-info btn-outline btn-sm"
                                        :class="se9"
                                        @click="selectdtype('m7-9')"
                                    >ງວດ3</button>
                                    <button
                                        type="button"
                                        class="btn btn-info btn-outline btn-sm"
                                        :class="se12"
                                        @click="selectdtype('m10-12')"
                                    >ງວດ4</button>
                                    <button
                                        type="button"
                                        class="btn btn-info btn-outline btn-sm"
                                        :class="sey"
                                        @click="selectdtype('y')"
                                    >ປີ</button>
                                </div>
                                <flat-pickr
                                    :config="config"
                                    v-model="dmy"
                                    class="form-control form-control-sm w-auto mb-2"
                                    placeholder="ເລືອກວັນທີ່"
                                />
                            </div>
                        </div>
                    </div>
                    <div class="h-auto flex justify-center">
                        <div class>
                            <button
                                type="button"
                                class="btn btn-success btn-sm"
                                @click="CreateReport()"
                            >
                                <i class="fas fa-file-alt"></i> ສ້າງໃບລາຍງານ
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
    </div>
</template>

<script>
import chartline from './ChartLineReport.vue';
import moment from "moment";
import monthchart from './LineChart.vue'



export default {
    name: 'Reportcomponent',
    props: {
        report: {
            type: Object,
            required: true,
        },
    },
    components: { chartline, moment, monthchart },

    data() {
        return {
            AllProductStore:[],
            ProductLost:[],
            Sell_Pro_Ser:[],
            Import_Pro:[],
            SellG:[],
            /// old
            re: null,
            ex: null,
            ShChart: false,
            key: 0,
            charh_label: null,
            update_chart: null,
            chdata: null,
            chdataop: {
                responsive: true,
                maintainAspectRatio: false,
                scales: {
                    yAxes: [{
                        ticks: {
                            display: true,
                            beginAtZero: false,
                            callback: function (value, index, values) {
                                return Number(value).toFixed(0).replace(/./g, function (c, i, a) { return i > 0 && c !== "," && (a.length - i) % 3 === 0 ? "," + c : c; }) + " ກີບ";
                            }
                        },
                        gridLines: {
                            show: true,
                        }
                    }]
                },
                tooltips: {
                    callbacks: {
                        label: function (tooltipItem, data) {
                            return Number(tooltipItem.yLabel).toFixed(0).replace(/./g, function (c, i, a) { return i > 0 && c !== "," && (a.length - i) % 3 === 0 ? "," + c : c; }) + " ກີບ";
                        }
                    },
                    mode: 'index',
                    intersect: false,

                }
            },

            isLoggedIn: false,
            loading: false,
            dmy: '',
            reportCode: 'racc',
            datere: 'm',

            currentDate() {
                const current = new Date();
                const date = current.getFullYear() + '-' + (current.getMonth() + 1) + '-' + current.getDate();
                const time = current.getHours() + ":" + current.getMinutes() + ":" + current.getSeconds();
                const dateTime = date + ' ' + time;
                return date;
            },
            config: {
                wrap: true, // set wrap to true only when using 'input-group'
                altFormat: 'd/m/Y',
                altInput: true,
                dateFormat: 'Y-m-d',
                allowInput: true,
            },
            datareport: [],
            dataInEx: [],
            dataInExB: [],
            dataInExSB: [],
            data_chart: null,
            // pdf config
            pdfOptions: {
                margin: 15,
                image: {
                    type: 'jpeg',
                    quality: 1,
                },
                html2canvas: { scale: 3 },
                jsPDF: {
                    unit: 'mm',
                    format: 'a4',
                    orientation: 'p',
                },
            },
            exportFilename: 'ບົດລາຍງານຜົນໄດ້ຮັບ.pdf',
        };
    },

    mounted() {

    },
    watch:{
        'AllProductStore'(){
            
            for(const item of this.AllProductStore){
                if(item.in_store<5){
                    //console.log(item.in_store)
                    this.ProductLost.push({
                        product_id: item.product_id,
                        product_name: item.product_name,
                        s_name: item.s_name,
                        in_store: item.in_store,
                        product_price_buy: item.product_price_buy,
                        unit_name: item.unit_name
                    });
                }
            }
            console.log(this.ProductLost)
        }
    },
    computed: {
        AllSumStore() {
            return this.AllProductStore.reduce((num, item) => num + parseInt(item.product_price_buy*item.in_store), 0);
        },
        AllAmountStore() {
            return this.AllProductStore.reduce((num, item) => num + parseInt(item.in_store), 0);
        },
        AllSumStorelost() {
            return this.ProductLost.reduce((num, item) => num + parseInt(item.product_price_buy*item.in_store), 0);
        },
        AllAmountStorelost() {
            return this.ProductLost.reduce((num, item) => num + parseInt(item.in_store), 0);
        },
        AllSum() {
            return this.datareport.reduce((num, item) => num + parseInt(item.sum), 0);
        },
        SumIncome() {
            let db = this.dataInEx.filter((e) => e.trn_type == 'income')
            return db.reduce((num, item) => num + parseInt(item.sum), 0);
        },
        SumExpense() {
            let db = this.dataInEx.filter((e) => e.trn_type == 'expense')
            return db.reduce((num, item) => num + parseInt(item.sum), 0);
        },
        SumIncomeB() {
            let db = this.dataInExB.filter((e) => e.trn_type == 'income')
            return db.reduce((num, item) => num + parseInt(item.sum), 0);
        },
        SumExpenseB() {
            let db = this.dataInExB.filter((e) => e.trn_type == 'expense')
            return db.reduce((num, item) => num + parseInt(item.sum), 0);
        },
        SumIncomeSB() {
            let db = this.dataInExSB.filter((e) => e.trn_type == 'income')
            return db.reduce((num, item) => num + parseInt(item.sum), 0);
        },
        SumExpenseSB() {
            let db = this.dataInExSB.filter((e) => e.trn_type == 'expense')
            return db.reduce((num, item) => num + parseInt(item.sum), 0);
        },
        chdatere() {
            //if(this.datere=='d'){ this.sed = 'active'; this.sem=''; this.se3=''; this.se6=''; this.se9=''; this.sey=''; } else {  
            if (this.datere == 'm') { this.sem = 'active'; this.se3 = ''; this.se6 = ''; this.se9 = ''; this.se12 = ''; this.sey = ''; } else {
                if (this.datere == 'm1-3') { this.sem = ''; this.se3 = 'active'; this.se6 = ''; this.se9 = ''; this.se12 = ''; this.sey = ''; } else {
                    if (this.datere == 'm4-6') { this.sem = ''; this.se3 = ''; this.se6 = 'active'; this.se9 = ''; this.se12 = ''; this.sey = ''; } else {
                        if (this.datere == 'm7-9') { this.sem = ''; this.se3 = ''; this.se6 = ''; this.se9 = 'active'; this.se12 = ''; this.sey = ''; } else {
                            if (this.datere == 'm10-12') { this.sem = ''; this.se3 = ''; this.se6 = ''; this.se9 = ''; this.se12 = 'active'; this.sey = ''; } else {
                                if (this.datere == 'y') { this.sem = ''; this.se3 = ''; this.se6 = ''; this.se9 = ''; this.se12 = ''; this.sey = 'active'; }
                            }
                        }
                    }
                }
            }
            // }

            return null


        }
    },
    methods: {
        DownloadPDF(type) {
            if (type == 'doc') {
                this.$refs.doc_report_pdf.download()
            } else if (type == 'grap') {
                console.log('Grap')
                this.$refs.report_grap_pdf.download()
            }

        },
        shuffleData() {
            dataValues.value = shuffle(dataValues.value);
            console.log(doughnutChartRef.value.chartInstance);
        },
        switchLegend() {
            toggleLegend.value = !toggleLegend.value;
        },
        date(value) {
            return moment(value).format("DD/MM/YYYY");
        },
        Getday(value) {
            return moment(value).format("DD");
        },
        Getmonth(value) {
            return moment(value).format("MM");
        },
        Getyear(value) {
            return moment(value).format("YYYY");
        },
        formatPrice(value) {
            let val = (value / 1).toFixed(0).replace(".", ",");
            return val.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
        },
        selectdtype(type) {
            this.datere = type
        },
        CreateReport() {
            this.ShChart = false
          
                this.loading = true
                this.$Progress.start();
                this.$axios.get("/sanctum/csrf-cookie").then((response) => {
                    axios.post("/api/create_report/report_store", {
                            datere: this.datere,
                            dmy: this.dmy
                        }).then((response) => {
                            if (response.data.success) {
                                this.AllProductStore = response.data.AllProductStore
                                //this.ProductLost = response.data.ProductLost
                               // this.Sell_Pro_Ser = response.data.Sell_Pro_Ser
                                this.Import_Pro = response.data.Import_Pro
                                this.SellG = response.data.SellG
                                this.$Progress.finish();
                            } else {
                                this.loading = false
                                this.$notify({ group: "error", title: "ບໍ່ສຳເລັດ!", text: "ຜິດຜາດ: " + response.data.message, }, 4000);
                                this.$Progress.fail();
                            }
                        })
                        .catch((error) => {
                            this.loading = false
                            this.$Progress.fail();
                            this.$notify({ group: "error", title: "ບໍ່ສຳເລັດ!", text: "ຜິດຜາດ: " + error.message, }, 4000);
                        });
                });

           
           

        },
        GenGrap_acc() {
            this.ShChart = true
            this.key++
            if (this.reportCode == 'racc') {
                if (this.datere == 'm') {
                    let y = this.dmy.split('-')[0]
                    let m = this.dmy.split('-')[1]
                    let lastday = this.Getlastday(y, m)

                    let db_income = this.GetIncome(lastday, this.datareport)
                    let db_cincome = this.GetCIncome(lastday, this.datareport)
                    let db_expense = this.GetExpense(lastday, this.datareport)

                    console.log(db_cincome)
                    // console.log(db_expense)
                }
            }
        },
        GenGrap() {
            this.ShChart = true
            this.key++
            if (this.reportCode == 'racc') {
                // console.log(this.Getlastday(2021,10))
                this.chdata = null
                let db_income = this.datareport.filter((i) => i.trn_type == 'income')
                let db_expense = this.datareport.filter((i) => i.trn_type == 'expense')
                let db_cexpense = this.datareport.filter((i) => i.trn_type == 'cexpense')
                let db_cincome = this.datareport.filter((i) => i.trn_type == 'cincome')
                let db_capital = this.datareport.filter((i) => i.trn_type == 'capital')

                let re_income = []
                let re_expense = []
                let re_cincome = []
                let re_cexpense = []
                let re_capital = []

                if (this.datere == 'm') {
                    //console.log(this.chdata)
                    let y = this.dmy.split('-')[0]
                    let m = this.dmy.split('-')[1]
                    let lastday = this.Getlastday(y, m)
                    let chart_label = []
                    re_income = this.Get_data_chart(lastday, db_income) || 0
                    re_expense = this.Get_data_chart(lastday, db_expense) || 0
                    re_cincome = this.Get_data_chart(lastday, db_cincome) || 0
                    re_cexpense = this.Get_data_chart(lastday, db_cexpense) || 0
                    re_capital = this.Get_data_chart(lastday, db_capital) || 0

                    for (let i = 0; i < lastday; i++) { chart_label.push('ວັນທີ່ ' + (i + 1)) }
                    // Blue #3366CC
                    // Red #DC3912
                    // Yellow #FF9900
                    // Green #109618
                    // Purple #990099
                    // Sea blue #0099C6
                    // Pastel pink #DD4477
                    this.chdata = {
                        labels: chart_label,
                        datasets: [
                            {
                                label: 'ລາຍຮັບ',
                                fill: false,
                                borderColor: '#3366CC',
                                data: re_income
                            },
                            {
                                label: 'ລາຍຈ່າຍ',
                                fill: false,
                                borderColor: '#DC3912',
                                data: re_expense
                            },
                            {
                                label: 'ໜີ້ຕ້ອງຮັບ',
                                fill: false,
                                borderColor: '#FF9900',
                                data: re_cincome
                            },
                            {
                                label: 'ໜີ້ຕ້ອງສົ່ງ',
                                fill: false,
                                borderColor: '#990099',
                                data: re_cexpense
                            },
                            {
                                label: 'ລົງທຶນ',
                                fill: false,
                                borderColor: '#109618',
                                data: re_capital
                            }
                        ]
                    }
                    this.update_chart = Math.floor(Math.random() * 100);

                } else if (this.datere == 'm1-3' || this.datere == 'm1-6' || this.datere == 'm1-9' || this.datere == 'y') {
                    let y = this.dmy.split('-')[0]
                    let m = this.dmy.split('-')[1]
                    let lastday = this.Getlastday(y, m)
                    let chart_label = null
                    let minth_chart = null
                    if (this.datere == 'm1-3') { chart_label = ['ເດືອນ 1', 'ເດືອນ 2', 'ເດືອນ 3']; minth_chart = 3 }
                    else if (this.datere == 'm1-6') { chart_label = ['ເດືອນ 1', 'ເດືອນ 2', 'ເດືອນ 3', 'ເດືອນ 4', 'ເດືອນ 5', 'ເດືອນ 6']; minth_chart = 6 }
                    else if (this.datere == 'm1-9') { chart_label = ['ເດືອນ 1', 'ເດືອນ 2', 'ເດືອນ 3', 'ເດືອນ 4', 'ເດືອນ 5', 'ເດືອນ 6', 'ເດືອນ 7', 'ເດືອນ 8', 'ເດືອນ 9']; minth_chart = 9 }
                    else if (this.datere == 'y') { chart_label = ['ເດືອນ 1', 'ເດືອນ 2', 'ເດືອນ 3', 'ເດືອນ 4', 'ເດືອນ 5', 'ເດືອນ 6', 'ເດືອນ 7', 'ເດືອນ 8', 'ເດືອນ 9', 'ເດືອນ 10', 'ເດືອນ 11', 'ເດືອນ 12']; minth_chart = 12 }


                    re_income = this.Get_data_chart_m3(minth_chart, db_income) || 0
                    re_expense = this.Get_data_chart_m3(minth_chart, db_expense) || 0
                    re_cincome = this.Get_data_chart_m3(minth_chart, db_cincome) || 0
                    re_cexpense = this.Get_data_chart_m3(minth_chart, db_cexpense) || 0
                    re_capital = this.Get_data_chart_m3(minth_chart, db_capital) || 0

                    //for (let i = 0; i < lastday; i++) { chart_label.push('ວັນທີ່ '+(i+1)) }

                    this.chdata = {
                        labels: chart_label,
                        datasets: [
                            {
                                label: 'ລາຍຮັບ',
                                fill: false,
                                borderColor: '#3366CC',
                                data: re_income
                            },
                            {
                                label: 'ລາຍຈ່າຍ',
                                fill: false,
                                borderColor: '#DC3912',
                                data: re_expense
                            },
                            {
                                label: 'ໜີ້ຕ້ອງຮັບ',
                                fill: false,
                                borderColor: '#FF9900',
                                data: re_cincome
                            },
                            {
                                label: 'ໜີ້ຕ້ອງສົ່ງ',
                                fill: false,
                                borderColor: '#990099',
                                data: re_cexpense
                            },
                            {
                                label: 'ລົງທຶນ',
                                fill: false,
                                borderColor: '#109618',
                                data: re_capital
                            }
                        ]
                    }
                    this.update_chart = Math.floor(Math.random() * 100);

                }

            } else if (this.reportCode == 'raccs' || this.reportCode == 'raccb' || this.reportCode == 'raccsb') {
                // console.log(this.Getlastday(2021,10))
                let alldb = null
                if (this.reportCode == 'raccs') { alldb = this.dataInEx }
                else if (this.reportCode == 'raccb') { alldb = this.dataInExB }
                else if (this.reportCode == 'raccsb') { alldb = this.dataInExSB }
                this.chdata = null
                let db_income = alldb.filter((i) => i.trn_type == 'income')
                let db_expense = alldb.filter((i) => i.trn_type == 'expense')
                // let db_cexpense = this.datareport.filter((i)=>i.trn_type == 'cexpense')
                // let db_cincome = this.datareport.filter((i)=>i.trn_type == 'cincome')
                // let db_capital = this.datareport.filter((i)=>i.trn_type == 'capital')

                let re_income = []
                let re_expense = []
                // let re_cincome = []
                // let re_cexpense = []
                // let re_capital = []

                if (this.datere == 'm') {
                    //console.log(this.chdata)
                    let y = this.dmy.split('-')[0]
                    let m = this.dmy.split('-')[1]
                    let lastday = this.Getlastday(y, m)
                    let chart_label = []
                    re_income = this.Get_data_chart(lastday, db_income) || 0
                    re_expense = this.Get_data_chart(lastday, db_expense) || 0
                    // re_cincome = this.Get_data_chart(lastday,db_cincome) || 0
                    // re_cexpense = this.Get_data_chart(lastday,db_cexpense) || 0
                    // re_capital = this.Get_data_chart(lastday,db_capital) || 0

                    for (let i = 0; i < lastday; i++) { chart_label.push('ວັນທີ່ ' + (i + 1)) }
                    // Blue #3366CC
                    // Red #DC3912
                    // Yellow #FF9900
                    // Green #109618
                    // Purple #990099
                    // Sea blue #0099C6
                    // Pastel pink #DD4477
                    this.chdata = {
                        labels: chart_label,
                        datasets: [
                            {
                                label: 'ລາຍຮັບ',
                                fill: false,
                                borderColor: '#3366CC',
                                data: re_income
                            },
                            {
                                label: 'ລາຍຈ່າຍ',
                                fill: false,
                                borderColor: '#DC3912',
                                data: re_expense
                            },
                            // {
                            //     label: 'ໜີ້ຕ້ອງຮັບ',
                            //     fill: false,
                            //     borderColor: '#FF9900',
                            //     data: re_cincome
                            // },
                            // {
                            //     label: 'ໜີ້ຕ້ອງສົ່ງ',
                            //     fill: false,
                            //     borderColor: '#990099',
                            //     data: re_cexpense
                            // },
                            // {
                            //     label: 'ລົງທຶນ',
                            //     fill: false,
                            //     borderColor: '#109618',
                            //     data: re_capital
                            // }
                        ]
                    }
                    this.update_chart = Math.floor(Math.random() * 100);

                } else if (this.datere == 'm1-3' || this.datere == 'm1-6' || this.datere == 'm1-9' || this.datere == 'y') {
                    let y = this.dmy.split('-')[0]
                    let m = this.dmy.split('-')[1]
                    let lastday = this.Getlastday(y, m)
                    let chart_label = null
                    let minth_chart = null
                    if (this.datere == 'm1-3') { chart_label = ['ເດືອນ 1', 'ເດືອນ 2', 'ເດືອນ 3']; minth_chart = 3 }
                    else if (this.datere == 'm1-6') { chart_label = ['ເດືອນ 1', 'ເດືອນ 2', 'ເດືອນ 3', 'ເດືອນ 4', 'ເດືອນ 5', 'ເດືອນ 6']; minth_chart = 6 }
                    else if (this.datere == 'm1-9') { chart_label = ['ເດືອນ 1', 'ເດືອນ 2', 'ເດືອນ 3', 'ເດືອນ 4', 'ເດືອນ 5', 'ເດືອນ 6', 'ເດືອນ 7', 'ເດືອນ 8', 'ເດືອນ 9']; minth_chart = 9 }
                    else if (this.datere == 'y') { chart_label = ['ເດືອນ 1', 'ເດືອນ 2', 'ເດືອນ 3', 'ເດືອນ 4', 'ເດືອນ 5', 'ເດືອນ 6', 'ເດືອນ 7', 'ເດືອນ 8', 'ເດືອນ 9', 'ເດືອນ 10', 'ເດືອນ 11', 'ເດືອນ 12']; minth_chart = 12 }


                    re_income = this.Get_data_chart_m3(minth_chart, db_income) || 0
                    re_expense = this.Get_data_chart_m3(minth_chart, db_expense) || 0
                    // re_cincome = this.Get_data_chart_m3(minth_chart,db_cincome) || 0
                    // re_cexpense = this.Get_data_chart_m3(minth_chart,db_cexpense) || 0
                    // re_capital = this.Get_data_chart_m3(minth_chart,db_capital) || 0

                    //for (let i = 0; i < lastday; i++) { chart_label.push('ວັນທີ່ '+(i+1)) }

                    this.chdata = {
                        labels: chart_label,
                        datasets: [
                            {
                                label: 'ລາຍຮັບ',
                                fill: false,
                                borderColor: '#3366CC',
                                data: re_income
                            },
                            {
                                label: 'ລາຍຈ່າຍ',
                                fill: false,
                                borderColor: '#DC3912',
                                data: re_expense
                            },
                            // {
                            //     label: 'ໜີ້ຕ້ອງຮັບ',
                            //     fill: false,
                            //     borderColor: '#FF9900',
                            //     data: re_cincome
                            // },
                            // {
                            //     label: 'ໜີ້ຕ້ອງສົ່ງ',
                            //     fill: false,
                            //     borderColor: '#990099',
                            //     data: re_cexpense
                            // },
                            // {
                            //     label: 'ລົງທຶນ',
                            //     fill: false,
                            //     borderColor: '#109618',
                            //     data: re_capital
                            // }
                        ]
                    }
                    this.update_chart = Math.floor(Math.random() * 100);

                }

            }
        },
        GetIncome(day, data) {
            //let day = 31
            let db_income_cr = []
            let re_income = []
            let db = data
            let re = this.re
            for (let i = 0; i < db.length; i++) {
                if (re.includes(db[i].cr) && db[i].dr != '411') {
                    db_income_cr.push({ 'day': this.Getday(db[i].created_at), 'cr': db[i].cr, 'price': db[i].sum })
                }
            }

            let update_db_in = db_income_cr.reduce((a, c) => {
                let filtered = a.filter(el => el.day === c.day);
                if (filtered.length > 0) {
                    a[a.indexOf(filtered[0])].price = parseInt(a[a.indexOf(filtered[0])].price) + parseInt(c.price);
                } else {
                    a.push(c);
                }
                return a;
            }, []);

            for (let i = 0; i < day; i++) {
                let type = true
                for (let k = 0; k < update_db_in.length; k++) {
                    if (update_db_in[k].day == i + 1) {
                        if (type) {
                            re_income.push(update_db_in[k].price)
                            type = false
                        }
                    }
                }
                if (type) { re_income.push(0); type = false }
            }
            return re_income
        },
        GetCIncome(day, data) {
            let db_income_cr = []
            let re_income = []
            let db = data
            let ex = this.ex
            for (let i = 0; i < db.length; i++) {
                if (db[i].cr == '411' || db[i].dr == '411') {   /// ຍັງໜີ້ຕ້ອງສົ່ງ
                    // let dr =null
                    // let cr =null
                    // if(db[i].cr=='411'){ cr = db[i].sum }
                    // if(db[i].dr=='411'){ dr = db[i].sum }

                    db_income_cr.push({ 'day': this.Getday(db[i].created_at), 'dr': db[i].dr, 'price': dr - cr })
                }
            }

            let update_db_in = db_income_cr.reduce((a, c) => {
                let filtered = a.filter(el => el.day === c.day);
                if (filtered.length > 0) {
                    a[a.indexOf(filtered[0])].price = parseInt(a[a.indexOf(filtered[0])].price) + parseInt(c.price);
                } else {
                    a.push(c);
                }
                return a;
            }, []);

            for (let i = 0; i < day; i++) {
                let type = true
                for (let k = 0; k < update_db_in.length; k++) {
                    if (update_db_in[k].day == i + 1) {
                        if (type) {
                            re_income.push(update_db_in[k].price)
                            type = false
                        }
                    }
                }
                if (type) { re_income.push(0); type = false }
            }
            return re_income
        },
        GetExpense(day, data) {
            let db_income_cr = []
            let re_income = []
            let db = data
            let ex = this.ex
            for (let i = 0; i < db.length; i++) {
                if (ex.includes(db[i].dr)) {   /// ຍັງໜີ້ຕ້ອງສົ່ງ
                    db_income_cr.push({ 'day': this.Getday(db[i].created_at), 'dr': db[i].dr, 'price': db[i].sum })
                }
            }

            let update_db_in = db_income_cr.reduce((a, c) => {
                let filtered = a.filter(el => el.day === c.day);
                if (filtered.length > 0) {
                    a[a.indexOf(filtered[0])].price = parseInt(a[a.indexOf(filtered[0])].price) + parseInt(c.price);
                } else {
                    a.push(c);
                }
                return a;
            }, []);

            for (let i = 0; i < day; i++) {
                let type = true
                for (let k = 0; k < update_db_in.length; k++) {
                    if (update_db_in[k].day == i + 1) {
                        if (type) {
                            re_income.push(update_db_in[k].price)
                            type = false
                        }
                    }
                }
                if (type) { re_income.push(0); type = false }
            }
            return re_income
        },
        Get_data_chart(day, data) {

            let new_db_in = []
            let re_income = []
            for (let y = 0; y < data.length; y++) {
                if (data[y] != '') {
                    let day = this.Getday(data[y].created_at)
                    new_db_in.push({ 'price': data[y].sum, 'day': day })
                }
            }
            let update_db_in = new_db_in.reduce((a, c) => {
                let filtered = a.filter(el => el.day === c.day);
                if (filtered.length > 0) {
                    a[a.indexOf(filtered[0])].price = parseInt(a[a.indexOf(filtered[0])].price) + parseInt(c.price);
                } else {
                    a.push(c);
                }
                return a;
            }, []);
            for (let i = 0; i < day; i++) {
                let type = true
                for (let k = 0; k < update_db_in.length; k++) {
                    if (update_db_in[k].day == i + 1) {
                        if (type) {
                            re_income.push(update_db_in[k].price)
                            type = false
                        }
                    }
                }
                if (type) { re_income.push(0); type = false }
            }
            return re_income
        },
        Get_data_chart_m3(minth_chart, data) {
            let new_db_in = []
            let re_income = []
            for (let y = 0; y < data.length; y++) {
                if (data[y] != '') {
                    let month = this.Getmonth(data[y].created_at)
                    new_db_in.push({ 'price': data[y].sum, 'month': month })
                }
            }

            let update_db_in = new_db_in.reduce((a, c) => {
                let filtered = a.filter(el => el.month == c.month);
                if (filtered.length > 0) {
                    a[a.indexOf(filtered[0])].price = parseInt(a[a.indexOf(filtered[0])].price) + parseInt(c.price);
                } else {
                    a.push(c);
                }
                return a;
            }, []);
            // for (let i = 0; i < update_db_in.length; i++) {
            //    if(update_db_in[i]!=''){
            //        re_income.push(update_db_in[i].price)
            //    }
            // }

            for (let i = 0; i < minth_chart; i++) {
                let type = true
                for (let k = 0; k < update_db_in.length; k++) {
                    if (update_db_in[k].month == i + 1) {
                        if (type) {
                            re_income.push(update_db_in[k].price)
                            type = false
                        }
                    }
                }
                if (type) { re_income.push(0); type = false }
            }

            return re_income
        },

        Getlastday(y, m) {
            return new Date(y, m, 0).getDate();
        }
    },
    created() {
        this.dmy = this.currentDate()
    },


    //     mounted() {
    //     new Chart(this.$refs.myChart, {
    //       type: 'line',
    //       data: {
    //       labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
    //       datasets: [
    //         {
    //           label: 'Data One',
    //           backgroundColor: '#f87979',
    //           data: [40, 39, 10, 40, 39, 80, 40]
    //         }
    //       ]
    //     }
    //     });
    //   }
};
</script>

<style lang="scss" scoped>
table td,
.table th {
    padding: 4px;
}
.table680 {
    max-width: 820px;
    margin: auto;
}
table thead,
table tr td {
    padding-bottom: 15px;
}

// table,
// th,
// td {
//     border: 1px solid rgb(121, 120, 120);
// }
</style>