
$(document).ready(function () {
    var mainsetting = JSON.parse(localStorage.getItem("mainsetting"));
    var WarehouseItemMasterUpdate = mainsetting.map(obj => obj.WarehouseItemMasterUpdate)[0];
    var datalist1 = [];


    $("#cmbcompany").on('select', function (event) {
        var args = event.args;
        if (args) {
            // index represents the item's index.                
            var index = args.index;
            var item = args.item;
            // get item's label and value.
            var label = item.label;
            var value = item.value;
            if (value != null) {
                var winWidth = $(window).width();
                $.ajax({
                    url: "/Master/DashBoard",
                    type: "POST",
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    data: '{"ComidNew":' + value + '}',
                    success: function (data) {
                        var getdata = data.data;
                        var DaySale = data.DaySale;
                        var YesterdaySale = data.YesterdaySale;
                        var WeekSale = data.WeekSale;
                        var MonthSale = data.MonthSale;
                        var TurnOver = data.TurnOver;
                        var TurnOverW = (data.TurnOver*7);
                        var TurnOverM = (data.TurnOver * 30);
                        var MaxTurnOver = ValNum(TurnOver).toFixed(0);
                        var MinTurnOver = ValNum(MaxTurnOver / 10).toFixed(0);
                        MinTurnOver = parseInt(MinTurnOver);
                        var metrics =
                            [
                                {
                                    name: 'Today Sales',
                                    value: DaySale,
                                    max: TurnOver
                                },
                                {
                                    name: 'Yesterday Sales',
                                    value: YesterdaySale,
                                    max: TurnOver
                                },
                                {
                                    name: 'Last 7Days Sales',
                                    value: WeekSale,
                                    max: TurnOverW
                                },
                                {
                                    name: 'Last 30Days Sales',
                                    value: MonthSale,
                                    max: TurnOverM
                                }
                            ];

                        for (var i = 0; i < metrics.length; i++) {

                            var data = [];

                            data.push({ text: 'Sale Amount', value: metrics[i].value }); // current
                            data.push({ text: 'Target Amount', value: metrics[i].max - metrics[i].value }); // remaining

                            var settings = {
                                title: metrics[i].name,
                                description: '',
                                enableAnimations: true,
                                showLegend: false,
                                showBorderLine: true,
                                backgroundColor: '#FFFFFF',
                                padding: { left: 5, top: 5, right: 5, bottom: 5 },
                                titlePadding: { left: 5, top: 5, right: 5, bottom: 5 },
                                source: data,
                                showToolTips: true,
                                seriesGroups:
                                    [
                                        {
                                            type: 'donut',
                                            useGradientColors: false,
                                            series:
                                                [
                                                    {
                                                        showLabels: false,
                                                        enableSelection: true,
                                                        displayText: 'text',
                                                        dataField: 'value',
                                                        labelRadius: 120,
                                                        initialAngle: 90,
                                                        radius: 60,
                                                        innerRadius: 50,
                                                        centerOffset: 0
                                                    }
                                                ]
                                        }
                                    ]
                            };

                            var selector = '#chartContainer' + (i + 1).toString();
                            var element = document.getElementById("chartContainer" + (i + 1).toString());
                            if (winWidth <= 1199) {
                                $(selector).addClass("chartwidthmd1");
                                // element.classList.add("chart-widthmd2");
                            }
                            else {
                                //element.classList.add("chart-widthlg2");
                                $(selector).addClass("chartwidthlg1");

                            }

                            var valueText = metrics[i].value.toString();

                            settings.drawBefore = function (renderer, rect) {
                                sz = renderer.measureText(valueText, 0, { 'class': 'chart-inner-text' });

                                renderer.text(
                                    valueText,
                                    rect.x + (rect.width - sz.width) / 2,
                                    rect.y + rect.height / 2,
                                    0,
                                    0,
                                    0,
                                    { 'class': 'chart-inner-text' }
                                );
                            }

                            $(selector).jqxChart(settings);
                            $(selector).jqxChart('addColorScheme', 'customColorScheme', ['#2A85F1', '#EDE6E7']);
                            $(selector).jqxChart({ colorScheme: 'customColorScheme' });
                        }

                        var element1 = document.getElementById("chartContainer5");
                        if (winWidth <= 1199) {
                            element1.classList.add("chartwidthmd2");
                            // element.classList.add("chart-widthmd2");
                        }
                        else {
                            element1.classList.add("chartwidthlg2");

                        }
                        //   var datanew = getdata;
                        const array = datalist1;
                        if (array.length != 0) {
                            datalist1.splice(0)
                        }
                        for (var i = 0; i < getdata.length; i++) {
                            var Date = (jsondatesystem(getdata[i]["Date"]));
                            var Amount = getdata[i]["Amount"];

                            //if (findcolumnindex(SerialNodatalist, JComboName, 'ComboName') == null) {
                            datalist1.push({ Date1: Date, Amount: Amount })
                            //}

                        }



                        var settings1 = {
                            title: "Last 7Days Sales Amount Bar Chart",
                            description: "",
                            enableAnimations: true,
                            showLegend: true,
                            backgroundColor: '#FFFFFF',
                            showBorderLine: true,
                            padding: { left: 10, top: 10, right: 10, bottom: 10 },
                            titlePadding: { left: 10, top: 0, right: 0, bottom: 10 },
                            source: datalist1,
                            xAxis:
                            {
                                dataField: 'Date1',
                                baseUnit: 'day',
                                gridLines: { visible: true },
                                type: 'date',

                            },
                            colorScheme: 'scheme01',
                            seriesGroups:
                                [
                                    {
                                        type: 'column',
                                        columnsGapPercent: 0,
                                        columnsMaxWidth: 25,
                                        seriesGapPercent: 0,

                                        valueAxis:
                                        {
                                            unitInterval: MinTurnOver,
                                            minValue: 0,
                                            maxValue: MaxTurnOver,
                                            displayValueAxis: true,
                                            description: 'Amount',
                                            axisSize: 'auto',
                                            tickMarksColor: '#888888'
                                        },
                                        series: [
                                            { dataField: 'Amount', displayText: 'Amount' }

                                        ]
                                    }
                                ]
                        };

                        $('#chartContainer5').jqxChart(settings1);


                    },
                    error: function (error) {
                        $('#jqxLoader').jqxLoader('close');
                        MsgBox('Technical Fault Contact Software Vendor  !!!.');
                    }

                });
            }

        }
    });
  
     methods = (function () {
        return {
            init: function () {
                $("#cmbcompany").jqxComboBox({ width: '80%', height: '22px' });
                $.ajax({
            
                    url: "/Master/SelectCompany",
                    type: "POST",
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(objlist),
                    success: function (data) {
                        $("#cmbcompany").jqxComboBox({ source: data, displayMember: "CName", valueMember: "Id" });
                        setTimeout(function () {                     
                        if (WarehouseItemMasterUpdate == true) {
                            $("#cmbcompany").jqxComboBox('selectIndex', 1);
                        }
                        else {
                            $("#cmbcompany").jqxComboBox('selectIndex', 0);
                            }
                        }, 100);
                    },
                    error: function (error) {
                        MsgBox('Technical Fault Contact Software Vendor  !!!.');
                        return [];
                    }

                });

              
                //if (winWidth < 768) {
                //    console.log('Window Width: ' + winWidth + 'class used: col-xs');
                //} else if (winWidth <= 991) {
                //    console.log('Window Width: ' + winWidth + 'class used: col-sm');
                //} else if (winWidth <= 1199) {
                //    console.log('Window Width: ' + winWidth + 'class used: col-md');
                //} else {
                //     console.log('Window Width: ' + winWidth + 'class used: col-lg');
                //}
             
            
            }
            
        }

    })();
    window.methods = methods;
    methods.init();
});