var condition = null;

$('.selectWrap').find('select').on('change', function() {
	condition = $('.selOrder').val() + $('.selCondition').val();

	$.ajax({
        url: '/measurement/stats/size_status/sort/',
        type: 'POST',
        cache: false,
        data: {
            'order_id': order_id,
            'condition': condition
        },
        async: false,
        success: function (response) {
      		$('tbody').remove();
            $('thead').after(response);
        }
    });
});

$('.pdfBtn').on('click', function() {
    var pdfFile;
    var $elements = $('thead, tbody'),
        content = [],
        count = 0;
        docDefinition = {
            content: content,
            styles: {
                header: {
                    fontSize: 18,
                    bold: true,
                    margin: [5,10]
                }
            },
            defaultStyle: {
                font: 'font'
            }
        };

    content.push({
        text: '사이즈 제출 현황',
        style: 'header'
    });

    for (i = $elements.length - 1; i >= 0; i--) {
        makeCanvas(i); 
    } 

    function makeCanvas(idx) {
        count += 1;
        html2canvas($elements[idx], {
            onrendered: function(canvas) {
                var data = canvas.toDataURL();
                content[idx+1] = {
                    image:data, 
                    width:520,
                    margin: [0,0]
                };
                count -= 1;
                if (count === 0) {
                    pdfMake.fonts = {
                        font: {
                            normal: 'malgun.ttf',
                            bold: 'malgunbd.ttf'
                        }
                    };
                    pdfMake.createPdf(docDefinition).download('사이즈_제출현황.pdf');
                }
            }
        })
    }

});
    


