(function($) {
    $(function() {
        $('[data-action=download]').on('click', function() {
            $.ajax({
                url: '/phpexcel/excel.php',
                type: "POST",
                data: {
                    type: $(this).data('type'),
                    id: $(this).data('id')
                },
                dataType: 'json',
                success: function(data){
                    var link = document.createElement("a");
                    link.href = data.file;
                    link.download = data.name;
                    link.click();

                    console.log(data.msg);
                },
                error: function(data) {
                    console.error(data);
                }
            }).done(function(data) {
                $.ajax({
                    url: '/phpexcel/excel.php',
                    type: "POST",
                    data: {
                        delete: true,
                        file: data.file
                    },
                    success: function(data) {
                        console.log(data);
                    },
                    error: function(data) {
                        console.error(data);
                    }
                });
            });
        });
    });
})(jQuery);