system.controller("GelatoReportController", GelatoReportController);

function GelatoReportController($scope, $http, $rootScope, $window, $timeout) {

    this.__proto__ = new BaseController($scope, $http, $rootScope);

    const TOKEN_STORAGE = 'gelatoToken';

    $scope.wallets = [
        {code: 'a1737f53-db23-4986-91cc-0e74474bf732', name: 'USD'},
        {code: 'c20bf927-0a91-44a2-8809-b6397e9978c5', name: 'EUR'},
        {code: 'de19c5a9-3033-4e32-8c8c-bcbb6a08fd9d', name: 'AUD'},
        {code: '97f3f865-189d-41bb-b137-8369b2f7cdf9', name: 'CAD'},
        {code: 'fb099691-d0b2-4e27-8854-d25d00692b63', name: 'GBP'},
        {code: '07a6e456-7fb4-4c8b-a248-d6682e1e88b9', name: 'JPY'},
        {code: '96050696-753d-49a2-9804-164ff1d0d28d', name: 'BRL'},
        {code: '474aa313-dc32-418f-b361-a9894c02fb17', name: 'CHF'},
        {code: '08a732f2-7c2c-48f2-9d15-190fc6d8d976', name: 'DKK'},
        {code: '36ceaaba-e641-4599-90e6-34d55de3091d', name: 'INR'},
        {code: '7855e7b9-59b7-4fbe-9724-628552afcfdc', name: 'NOK'},
        {code: '818d0d7d-1a3e-4796-8088-142d3e96a5b6', name: 'NZD'},
        {code: '6f07b30a-3de0-4b00-9852-c7d471253c25', name: 'PLN'},
        {code: '61f3bdfe-6200-4180-bcd8-ab7a691cfbcd', name: 'SEK'}
    ];

    $scope.report = {
        wallet: $scope.wallets[0].code
    };

    $scope.initialize = function () {
        $timeout(function () {
            $("#date-from").datepicker({
                dateFormat: "yy-mm-dd",
            }).datepicker("setDate", "-1w");

            $("#date-to").datepicker({
                dateFormat: "yy-mm-dd",
            }).datepicker("setDate", "+0");
        });
        // var gelatoToken = localStorage.getItem(TOKEN_STORAGE);
        // if (gelatoToken && gelatoToken != '') {
        //     $scope.report.token = gelatoToken;
        // }
    };

    $scope.exportExcel = function () {
        if (!$scope.report.token || $scope.report.token == '') {
            toastr.error('Xin mời nhập Access Token.');
            return;
        } else {
            localStorage.setItem(TOKEN_STORAGE, $scope.report.token);
        }
            var fromTime = $('#date-from').val();
            var toTime = $("#date-to").val();

        if (!fromTime || fromTime == '') {
            toastr.error('Mời chọn Ngày bắt đầu.');
            return;
        }

        if (!toTime || toTime == '') {
            toastr.error('Mời chọn Ngày kết thúc.');
            return;
        }

        var query = {
            createdAtAfter: fromTime,
            createdAtBefore: toTime,
            token: $scope.report.token,
            wallet: $scope.report.wallet
        };
        var exportUrl = '/print-suppliers/transaction/gelato?' + $.param(query);
        $window.open(exportUrl, '_blank');
    }

    $scope.initialize();
}