// $('#pdf-test').click(function (evt){
//     pdfMake.fonts = {
//         Roboto: {
//                 normal: 'Roboto-Regular.ttf',
//                 bold: 'Roboto-Medium.ttf',
//                 italics: 'Roboto-Italic.ttf',
//                 bolditalics: 'Roboto-MediumItalic.ttf'
//         }
//     };

//     let dd = {
//         header: 'simple text',

//         footer: function(page, pages) { 
//             return { 
//                 columns: [
//                     { 
//                         alignment: 'center',
//                         text: [
//                             { text: page.toString(), italics: true },
//                             ' de ',
//                             { text: pages.toString(), italics: true }
//                         ]
//                     }
//                 ],
//                 margin: [10, 0]
//             };
//         },
//         content: [
//             {
//                 text: 'This paragraph uses header style and extends the alignment property',
//                 style: 'header',
//                 alignment: 'center'
//             },
//             {
//                 text: [
//                     'This paragraph uses header style and overrides bold value setting it back to false.\n',
//                     'Header style in this example sets alignment to justify, so this paragraph should be rendered \n',
//                     'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Malit profecta versatur nomine ocurreret multavit, officiis viveremus aeternum superstitio suspicor alia nostram, quando nostros congressus susceperant concederetur leguntur iam, vigiliae democritea tantopere causae, atilii plerumque ipsas potitur pertineant multis rem quaeri pro, legendum didicisse credere ex maluisset per videtis. Cur discordans praetereat aliae ruinae dirigentur orestem eodem, praetermittenda divinum. Collegisti, deteriora malint loquuntur officii cotidie finitas referri doleamus ambigua acute. Adhaesiones ratione beate arbitraretur detractis perdiscere, constituant hostis polyaeno. Diu concederetur.'
//                     ],
//                 style: 'header',
//                 bold: false
//             }
//         ],
//         styles: {
//             header: {
//                 fontSize: 18,
//                 bold: true,
//                 alignment: 'justify'
//             }
//         }
//     }
//     pdfMake.createPdf(dd).open();
// });
// how to convert image into base64 tring using javascript
function toDataURL(src, callback, outputFormat) {
    var img = new Image();
    img.crossOrigin = 'Anonymous';
    img.onload = function() {
      var canvas = document.createElement('CANVAS');
      var ctx = canvas.getContext('2d');
      var dataURL;
      canvas.height = this.naturalHeight;
      canvas.width = this.naturalWidth;
      ctx.drawImage(this, 0, 0);
      console.log('output format')
      dataURL = canvas.toDataURL(outputFormat);
      console.log(dataURL)
      callback(dataURL);
    };
    img.src = src;
    if (img.complete || img.complete === undefined) {
      img.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
      img.src = src;
    }
  }
  
  
function getCanvas2()
{
    toDataURL(
        'http://localhost:8000/storage/images/personal/user.png',
        function(dataUrl) {
            result = dataUrl
            console.log('RESULT:', dataUrl)
        }
    );
}
function getCanvas(img)
{
    let strImg = img.src.split('.')
    let extension
    if (strImg)
    {
        extension = strImg[strImg.length - 1]
    }
    extension = extension || 'png'
    let canvas = document.createElement("canvas");
    canvas.width = img.width;
    canvas.height = img.height;
    let ctx = canvas.getContext("2d");
    ctx.drawImage(img,0,0);
    return canvas.toDataURL("image/" + extension);
}
function getBase64Image(img) {
    let canvas
    try
    {
        canvas = getCanvas(img)
    }
    catch
    {
        console.log('en el catch')
        canvas = getCanvas(document.getElementById("img-default"))
    }
    return canvas;
}
$('#pdf-test-consulta').click(function (evt){
    pdfMake.fonts = {
        Roboto: {
                normal: 'Roboto-Regular.ttf',
                bold: 'Roboto-Medium.ttf',
                italics: 'Roboto-Italic.ttf',
                bolditalics: 'Roboto-MediumItalic.ttf'
        }
    };
    
    let header = 
    {
        columns: [
            {
                type: 'none',
                ul: [
                        {text: 
                            [
                                {text: 'Codigo de consulta: ', bold: true},
                                {text: 'Ninguna'},
                            ]
                        },
                        {text: 
                            [
                                {text: 'Fecha de consulta: ', bold: true},
                                {text: 'Ninguna'},
                            ]
                        },
                        {text: 
                            [
                                {text: 'Fecha de impresion: ', bold: true},
                                {text: 'Ninguna'},
                            ]
                        },
                        {text: 
                            [
                                {text: 'Usuario de impresion: ', bold: true},
                                {text: 'Ninguna'},
                            ]
                        }
                    ]
            },
            {
                alignment: 'right',
                image: '',//getCanvas2(),//getBase64Image(document.getElementById("img")),
                link: 'http://google.com',
                fit: [60, 60]
            }
        ]
    };
    // let header2 = 
    // {
    //     columns: [
    //         {
    //             type: 'none',
    //             ul: [
    //                     {text: 
    //                         [
    //                             {text: 'Codigo de consulta: ', bold: true},
    //                             {text: 'Ninguna'},
    //                         ]
    //                     },
    //                     {text: 
    //                         [
    //                             {text: 'Fecha de consulta: ', bold: true},
    //                             {text: 'Ninguna'},
    //                         ]
    //                     },
    //                     {text: 
    //                         [
    //                             {text: 'Fecha de impresion: ', bold: true},
    //                             {text: 'Ninguna'},
    //                         ]
    //                     },
    //                     {text: 
    //                         [
    //                             {text: 'Usuario de impresion: ', bold: true},
    //                             {text: 'Ninguna'},
    //                         ]
    //                     }
    //                 ]
    //         },
    //         {
    //             alignment: 'right',
    //             image: getBase64Image(document.getElementById("img")),
    //             link: 'http://google.com',
    //             fit: [60, 60]
    //         }
    //     ]
    // };
    let title = 
    {
        text: 'Evaluacion Fisioterapeutica',
        style: 'header',
        alignment: 'center'
    };
    let informacion1 = 
    {
        style: 'littleTable',
        table: {
            widths: ['*', '*', '*'],
            body: [
                [{text: 'Nombres', style: 'tableHeader'}, {text: 'Cedula', style: 'tableHeader'}, {text: 'Edad', style: 'tableHeader'}],
                ['Cristian', '1313722355', '78']
            ]
        }
    };
    let informacion2 = 
    {
        style: 'littleTable',
        table: {
            widths: ['*', '*', '*'],
            body: [
                [{text: 'Estado', style: 'tableHeader'}, {text: 'Telefono', style: 'tableHeader'}, {text: 'Ocupacion', style: 'tableHeader'}],
                ['Soltero', '0982835748', 'Progaramador']
            ]
        }
    };
    let informacion3 = 
    {
        style: 'littleTable',
        table: {
            widths: ['*', '*'],
            body: [
                [{text: 'Medico Tratante', style: 'tableHeader'}, {text: 'Direccion', style: 'tableHeader'}],
                ['Cristian Pisco', 'Samanes 5']
            ]
        }
    };
    let informacion4 = 
    {
        style: 'littleTable',
        table: {
            widths: ['auto', 'auto'],
            body: [
                [{text: 'Antecedentes Personales', style: 'tableHeader'}, 'Ninguno'],
                [{text: 'Antecedentes Familiares', style: 'tableHeader'}, 'Ninguno'],
                [{text: 'Antecedentes Quirurgicos', style: 'tableHeader'}, 'Ninguno'],
                [{text: 'Antecedentes Farmacologicos', style: 'tableHeader'}, 'Ninguno'],
            ]
        }
    };
    let informacion5 =
    {
        style: 'littleTable',
        table: {
            widths: ['auto', 'auto'],
            body: [
                [{text: 'Motivo de consulta', style: 'tableHeader'}, 'Ninguno'],
                [{text: 'Diagnostico Fisioterapeuta', style: 'tableHeader'}, 'Ninguno'],
                [{text: 'Objetivo', style: 'tableHeader'}, 'Ninguno'],
                [{text: 'Plan de tratamiento', style: 'tableHeader'}, 'Ninguno'],
            ]
        }
    };
    let titleEvaluaciones = 
    {
        text: 'Evaluaciones',
        style: 'header',
        alignment: 'center'
    };
    let tableEvaluaciones = 
    {
        style: 'tableExample',
        table: {
            widths: ['auto', 'auto', 'auto'],
            body: [
                ['2019-12-11 00:00:00', 'Lorem ipsum dolor, sit amet consectetur adipisicing elit. Consequatur debitis doloribus, libero excepturi tenetur quaerat natus numquam molestiae officiis beatae quae deserunt officia dolorem cupiditate corrupti esse ea maxime cum.', 
                    {
                        alignment: 'center',
                        image: getBase64Image(document.getElementById("img")),
                        link: 'http://google.com',
                        fit: [60, 60]
                    }
                ],
                ['2019-12-11 00:00:00', 'Lorem ipsum dolor, sit amet consectetur adipisicing elit. Consequatur debitis doloribus, libero excepturi tenetur quaerat natus numquam molestiae officiis beatae quae deserunt officia dolorem cupiditate corrupti esse ea maxime cum.', 
                    {
                        alignment: 'center',
                        image: getBase64Image(document.getElementById("img")),
                        link: 'http://google.com',
                        fit: [60, 60]
                    }
                ],
                ['2019-12-11 00:00:00', 'Lorem ipsum dolor, sit amet consectetur adipisicing elit. Consequatur debitis doloribus, libero excepturi tenetur quaerat natus numquam molestiae officiis beatae quae deserunt officia dolorem cupiditate corrupti esse ea maxime cum.', 
                    {
                        alignment: 'center',
                        image: getBase64Image(document.getElementById("img")),
                        link: 'http://google.com',
                        fit: [60, 60]
                    }
                ],
                ['2019-12-11 00:00:00', 'Lorem ipsum dolor, sit amet consectetur adipisicing elit. Consequatur debitis doloribus, libero excepturi tenetur quaerat natus numquam molestiae officiis beatae quae deserunt officia dolorem cupiditate corrupti esse ea maxime cum.', 
                    {
                        alignment: 'center',
                        image: getBase64Image(document.getElementById("img")),
                        link: 'http://google.com',
                        fit: [60, 60]
                    }
                ],
            ]
        }
    };

    let dd = {
        header: function(currentPage, pageCount, pageSize) {
            return [
              { text: 'Consultas', alignment: 'center', fontSize: 20, margin: [0, 5] }
              //{ canvas: [ { type: 'rect', x: 170, y: 32, w: pageSize.width - 170, h: 40 } ] }
            ]
        },
        footer: function(page, pages) { 
            return { 
                columns: [
                    { 
                        alignment: 'center',
                        text: [
                            { text: page.toString(), italics: true },
                            ' de ',
                            { text: pages.toString(), italics: true }
                        ]
                    }
                ],
                margin: [10, 0]
            };
        },
        content: [
            header,
            title,
            informacion1,
            informacion2,
            informacion3,
            informacion4,
            informacion5,
            titleEvaluaciones,
            tableEvaluaciones
        ],
        styles: {
            header: {
                fontSize: 15,
                bold: true,
                alignment: 'center',
                margin: [0, 10]
            },
            tableExample: {
                margin: [0, 5, 0, 5]
            },
            littleTable: {
                margin: [0, 5, 0, 5]
            },
            tableHeader: {
                bold: true,
                fontSize: 13,
                color: 'black'
            }
        }
    }
    pdfMake.createPdf(dd).open();
});