// import {MDCDialog} from '@material/dialog';
// import {MDCSelect} from '@material/select';
// import {MDCTextField} from '@material/textfield';
// const dialog = new MDCDialog(document.querySelector('.mdc-dialog'));
// const select = new MDCSelect(document.querySelector('.mdc-select'));
// const textfield = new MDCSelect(document.querySelector('.mdc-textfield'));

// var fonts = {
//     Roboto: {
//         normal: 'fonts/Roboto-Regular.ttf',
//         bold: 'fonts/Roboto-Medium.ttf',
//         italics: 'fonts/Roboto-Italic.ttf',
//         bolditalics: 'fonts/Roboto-.ttf',
//     }
// };

var pdfMake = require('pdfmake/build/pdfmake.js');
var pdfFonts = require('pdfmake/build/vfs_fonts.js');
pdfMake.vfs = pdfFonts.pdfMake.vfs;

function table(course) {
    return {
        widths: [100, 300],
        headerRows: 2,
        body: [
            [{text: `${course.code} ${course.title}`, bold: true, colSpan: 2, alignment: 'center'}, {}],
            ['Seccion', 'Profesor'],
        ].concat( course.sections.map( function(section) {
            if (section.hasOwnProperty('professors')) {
                return [section.code, section.professors.map( function (professor) {
                    return professor.first_name + ' ' + professor.last_name;
                })];
            } else {
                return [section.code, ''];
            }
        }))
    }
}

$(document).ready( function() {
    // $('#button-semester-load').on('click', function (event) {
    //     dialog.open();
    // });
    $('#button-load').on('show.bs.modal', function (event) {
        // const select = new MDCSelect(document.querySelector('.mdc-select'));
        var button = $(event.relatedTarget);
        var action = button.data('action');
        $('#select-semester').change( function() {
            $('#modal-semester-form').submit( function(event) {
                event.preventDefault();
                $.ajax({
                    url: `/dashboard/${action}/${select.value}`,
                    type: 'get',
                    success: function(data) {
                        var docDef = {
                            content: [
                                {text: `Horario BIOL ${data.alpha}`, style: 'header'},
                                {text: 'sujeto a cambios', style: 'header', fontSize: 18},
                            ].concat( data.courses.map( function(course) {
                                return {
                                    style: 'tableStyle',
                                    alignment: 'center',
                                    table: table(course)
                                }
                            })),
                            styles: {
                                header: {
                                    fontSize: 22,
                                    bold: true,
                                    alignment: 'center',
                                },
                                tableStyle: {
                                    margin: [50,10,0,15]
                                },
                            }

                        };

                        pdfMake.createPdf(docDef).download();
                    }
                })
            });
        });

    });
});

window.mdc.autoInit();
