//#import Util.js
//#import pigeon.js
//#import $owl_user_invoice:services/modelService.jsx

var InvoiceService = (function () {

    var f = {
        getInvoiceList: function (userId) {
            var searchArgs = {};
            searchArgs.user_id = userId;

            var searchResult = owl_user_invoiceService.search(null, searchArgs, null, 0, 1000, [{"owl_createTime": {order: "desc"}}]);
            if (searchResult.state !== 'ok') {
                return [];
            }
            return searchResult.list;
        },
        removeAllDefaultInvoice: function (userId, invoiceId) {
            var searchArgs = {};
            searchArgs.user_id = userId;
            searchArgs.default = "1";

            var searchResult = owl_user_invoiceService.search(null, searchArgs, null, 0, 1000, null);
            if (searchResult.state !== 'ok') {
                return;
            }
            var listData = searchResult.list;
            if (listData) {
                for (var i = 0; i < listData.length; i++) {
                    var jRecord = listData[i];
                    if(jRecord.id == invoiceId) {
                        continue;
                    }

                    if (jRecord.default) {
                        jRecord = owl_user_invoiceService.get(jRecord.id);
                        jRecord.default = "0";
                        owl_user_invoiceService.update(jRecord);
                    }
                }
            }
        },
        getDefaultInvoice: function (userId) {
            var searchArgs = {};
            searchArgs.user_id = userId;
            searchArgs.default = "1";

            var searchResult = owl_user_invoiceService.search(null, searchArgs, null, 0, 1, null);
            if (searchResult.state !== 'ok') {
                return null;
            }
            return searchResult.list[0];
        },
        getInvoiceById: function (invoiceId) {
            return owl_user_invoiceService.get(invoiceId);
        }
    };
    return f;
})();