crmApp.service('OutboundService', ['GeneralService', function (GeneralService) {
    var outBound = {};
    var ansuranList = [];
    var tarikhKenaBayar = '';

    var getUsiaData = function () {
        var dtNow = new Date();
        var dtStart = outBound.ImportedDateTime || '';
        return GeneralService.daysBetween(dtStart, dtNow);
    }

    var getUsiaDebit = function (item) {
        var dtNow = new Date();
        var dtStart = outBound.ASSESSMENT_DATE || '';
        return GeneralService.daysBetween(dtStart, dtNow);
    }

    var handleAnsuranDates = function () {
        //var dtAnsuran = [];
        var i = 0;
        for (var key in outBound) {
            var matches = key.match(/^DATE(\d+)/);
            if (matches && matches.length > 1)
                if (outBound[key]) {
                    i++;
                    //dtAnsuran.push(new Date(outBound[key]));
                    ansuranList.push({ Key: key, Bil: i, Date: outBound[key], AMT: outBound['AMT' + matches[1]], Status: (outBound['AMT' + matches[1]] ? 'Gagal' : 'Bayar') });

                }
        }
        //tarikhKenaBayar = new Date(Math.max.apply(null, dtAnsuran));
    }

    this.init = function (item) {
        outBound = item || {};
        handleAnsuranDates();
    }

    this.Tunggakan = {
        get UsiaData() {
            return getUsiaData();
        },
        get UsiaDebit() {
            return getUsiaDebit();
        }

    }
    this.Ansuran = {
        get TarikhKenaBayar() {
            return tarikhKenaBayar;
        },
        get AnsuranList() {
            return ansuranList;
        }
    }

}]);