var peristalsis = false;
var baseDate = new Date();
var app = {
    // Application Constructor
    initialize: function() {
        document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
    },

    // deviceready Event Handler
    //
    // Bind any cordova events here. Common events are:
    // 'pause', 'resume', etc.
    onDeviceReady: function() {
        this.itemInit();
        this.eventInit();
        this.otherInit();
    },
    itemInit:function() {
        app.setToMonth();
    },
    eventInit:function(){
        //다음날 버튼
        $('.last').on("click", function () {
            //var date = $('#picker').datepicker('getDate');
            console.log("last 1 = "+baseDate);
            baseDate.setMonth(baseDate.getMonth() + 1);
            console.log("last 2 = "+baseDate);
            $("#picker").datepicker('setDate', baseDate);

            app.getMonthTotal($("#picker").val());
            app.initMonthButton();
        });
        //전날 버튼
        $('.first').on("click", function () {
            //var date = $('#picker').datepicker('getDate');
            console.log("first 1 = "+baseDate);
            baseDate.setMonth(baseDate.getMonth() - 1);
            console.log("first 2 = "+baseDate);
            $("#picker").datepicker('setDate', baseDate);

            app.getMonthTotal($("#picker").val());
            app.initMonthButton();
        });
        $('#monthTotal').on("click", function(){
            console.log("monthTotal 1 = "+date);
            baseDate = new Date();
            $("#picker").datepicker({
                dateFormat: "yy년 mm월"
            });
            $("#picker").datepicker('setDate',baseDate);
            app.getMonthTotal($("#picker").val());
            app.initMonthButton();
        });
    },
    otherInit:function(){
        if(isIphone()){

        }else {
            document.addEventListener("backbutton", app.backbuttonAction, false);
        }
    },
    backbuttonAction:function(){
        if(isIphone()) {
           cordova.plugins.finPlugin.backButton("", null, null);
       }else {
            cordova.plugins.finPlugin.moveStack(MAIN.DATA,null, null);
       }
    },
    getMonthTotal:function(today){
        
        if(isIphone())
        {
            
            var dateVal = {"calender" : today};
            cordova.plugins.finPlugin.getMonthTotal(dateVal, function(data){
                       app.setPaymentData(data);
                   }, null);
        }else
        {
            cordova.plugins.finPlugin.getMonthTotal(today, function(data){
                       app.setPaymentData(data);
                   }, null);
        }
        
        
       
    },
    
    setToday:function(){
        var today = new Date();
        $("#picker").datepicker({
            dateFormat: "yy년 mm월 dd일"
        });
        $("#picker").datepicker("setDate",today);
        app.getMonthTotal($("#picker").val());
        
    },
    
    setPaymentData:function(data) {
        
        if(data == null) return;
        data = JSON.parse(data);
        var html_data = "<ul class='SalesWrap01'> <li>합계</li>";
        
        if(isIphone())
        {
            var dataArray = data['monthlysList'];
            var total = data['monthlyTotal'];
            
            
            for (var i = 1; i <= 31; i++) {
                html_data = html_data + "<li>" + i + "일</li>";
            }
            html_data = html_data + "</ul > <ul class='SalesWrap02'> <li>" + total +"</li>";
            while (dataArray.length > 0)
             {
                 var dataObject = JSON.parse(dataArray.shift());
                 var valuer = dataObject['day'];
                 
                 html_data = html_data + "<li>" + valuer + "</li>";
             }
            
        }else
        {
            
            var dataObj = data.DATA;
            
            for (var i = 1; i <= 31; i++) {
                for(key in dataObj) {
                    var value = parseInt(key.substring(0, 2));
                    if (i == value) {
                        html_data = html_data + "<li>" + key + "</li>";
                    }
                }
            }
            html_data = html_data + "</ul > <ul class='SalesWrap02'> <li>" + data.TOTAL +"</li>";
            for (var i = 1; i <= 31; i++) {
                for(key in dataObj) {
                    var value = parseInt(key.substring(0, 2));
                    if (i == value) {
                        html_data = html_data + "<li>" + dataObj[key] + "</li>";
                        break;
                    }
                }
            }
            
        }
        html_data = html_data + "</ul >";
        $("#dv_list").html(html_data);
        
        
        
        
    },
    initMonthButton:function(){
        $('#thisMonth').addClass('selected');
        $('#prevMonth').removeClass('selected');
    },
    setToMonth:function(){
        baseDate = new Date();
        $("#picker").datepicker({
            dateFormat: "yy년 mm월"
        });
        $("#picker").datepicker("setDate",baseDate);
        app.getMonthTotal($("#picker").val());
        app.initMonthButton();
    },
    setThisMonth:function(){
        $('#thisMonth').addClass('selected');
        $('#prevMonth').removeClass('selected');
        app.getMonthTotal($("#picker").val());
    },
    setPrevMonth:function(){
        $('#thisMonth').removeClass('selected');
        $('#prevMonth').addClass('selected');
        app.getMonthTotal(app.getPrevMonth(baseDate));
    },
    getPrevMonth:function(today){
        var preMonth = new Date(today);
        preMonth.setMonth(preMonth.getMonth() - 1);
        var year = preMonth.getYear().toString();
        var month = preMonth.getMonth() + 1;
        return year.substring(1) + '년 ' + month + '월';
    }
};
app.initialize();