<!-- BEGIN {$smarty.template} -->
<script type="text/javascript">
{literal}
    $(document).ready(function() {

        $('.cbxsel').selectable( { 
            distance: 5,
            selected: function(event, ui) { $(ui.selected).find(":checkbox").attr('checked', true); },
            unselected: function (event, ui) { $(ui.unselected).find(":checkbox").attr('checked', false); }
        });

        var dates = $('#bill_datefrom, #bill_dateto').datepicker({
            changeMonth: true,
            changeYear: true,
            numberOfMonths: 2,
            dateFormat: 'yy-mm-dd',
            showOn: 'both',
            buttonImage: '../img/phpaga/cal.png',
            buttonImageOnly: true,
            onSelect: function(selectedDate) {
                var option = this.id == "bill_datefrom" ? "minDate" : "maxDate";
                var instance = $(this).data("datepicker");
                var date = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings);
                dates.not(this).datepicker("option", option, date);
            }

        });

        /* Print dialog */
        $("#printbill").dialog({
                autoOpen: false,
                modal: true,
                buttons: {
                    "{/literal}{t}Open{/t}{literal}": function() {
                       $('#pdf_download').val('0');
                       $('#bill_print_form').submit(); 
                        $(this).dialog( "close" );
                    },
                    "{/literal}{t}Download{/t}{literal}": function() {
                       $('#pdf_download').val('1');
                       $('#bill_print_form').submit(); 
                        $(this).dialog( "close" );
                    },
                    Cancel: function() {
                        $(this).dialog( "close" );
                    }
                },
                close: function() {
                }
        });
        $("#printbill").parent().appendTo($('#bill_print_form')); // This is necessary for the dialog's form fields to be used on submit.

        $( "#select-invoices-message" ).dialog({
                modal: true,
                autoOpen: false,
                buttons: {
                    Ok: function() {
                        $( this ).dialog( "close" );
                    }
                }
        });

        $('#export_pdf').click(function() { 
            if ($('.idcbx').filter(':checked').length==0)
                $( "#select-invoices-message" ).dialog( "open" ); 
            else
                $( "#printbill" ).dialog( "open" ); 
        });

    });
{/literal}
</script>

<div id="select-invoices-message" title="{t}Select invoices{/t}">
    <p>
    <span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 50px 0;"> </span>
    {t}Please select the invoices you would like to print.{/t}
    </p>
</div>

<form action="{$FORM_ACTION}" method="get">
    <fieldset class="search">
        <legend>{t}Search invoices{/t}</legend>

        <label for="bill_number">{t}Invoice nr.{/t}:</label>
        <input type="text" class="tinput" id="bill_number" name="bill_number" value="{$billInfo.bill_number|wash}" size="10" maxlength="10">
        <br>

        <label for="bill_paid">{t}Payment status{/t}:</label>
        <select id="bill_paid" name="bill_paid">
            {html_options options=$select_paid selected=$billInfo.bill_paid}
        </select>
        <br>

        <label for="cpn_id">{t}Recipient (company){/t}:</label>
        <select id="cpn_id" name="cpn_id">
            {html_options options=$select_cpn selected=$billInfo.cpn_id}
        </select>
        <br>

        <label for="bill_additional_line">{t}Additional information{/t}:</label>
        <input type="text" class="tinput" id="bill_additional_line" name="bill_additional_line" value="{$billInfo.bill_additional_line|wash}" size="40" maxlength="255">
        <br>

        <label for="bill_datefrom">{t}Between{/t}:</label>
        <input type="text" class="tinput" id="bill_datefrom" name="bill_datefrom" value="{$billInfo.bill_datefrom|wash}" size="10" maxlength="10" title="yyy-mm-dd">
        <br>

        <label for="bill_dateto">{t}And{/t}:</label>
        <input type="text" class="tinput" id="bill_dateto" name="bill_dateto" value="{$billInfo.bill_dateto|wash}" size="10" maxlength="10" title="yyyy-mm-dd">
        <br>

        <input type="submit" name="submit" value="{t}Search{/t}">
    </fieldset>
</form>

<form action="bill_pdf.php" method="GET" id="bill_print_form">
    <input type="hidden" name="download" value="0" id="pdf_download" />
    <div id="printbill" title="{t}Print selected invoices to PDF{/t}">
        <h2>{t}Print options{/t}</h2>
        <input type="checkbox" class="cbox" name="letterhead" id="letterhead" value="1" {if $smarty.const.PHPAGA_PDF_PRINT_LETTERHEAD_DEFAULT}checked="checked"{/if}><label for="letterhead">{t}Print letterhead{/t}</label><br />
        <input type="checkbox" class="cbox" name="oplist" id="oplist" value="1"><label for="oplist">{t}Append task list{/t}</label><br />
        <input type="checkbox" class="cbox" name="timesheet" id="timesheet" value="1"><label for="timesheet">{t}Append time sheet{/t}</label>
    </div>

    <table cellspacing="0">
        <caption><em>{t}Invoices{/t}</em>
            {if $perm_invoices}[ <a href="add_bill.php">{t}Add invoice{/t}</a>
            | <a href="operations.php?op_billed={$smarty.const.PHPAGA_BILL_NOTBILLED}">{t}Add invoice with tasks{/t}</a>
            | <a href="bills_csv.php?{$search_args}">{t}CSV Export{/t}</a>
            | <a id="export_pdf" href="#">{t}Print selected to PDF{/t}</a> ]
            {/if}
        </caption>
        {if (count($rows) > 0)}
        <thead>
            <tr>
                <th class="number"> {t}Invoice nr.{/t} </th>
                <th class="number"> {t}Recurring invoice nr.{/t} </th>
                <th class="time"> {t}Invoice date{/t} </th>
                <th> {t}Recipient (company){/t} </th>
                <th> {t}Amount{/t} </th>
                <th> {t}Amount missing{/t} </th>
                <th class="time"> {t}Date due{/t} </th>
                <th class="time"> {t}Date paid{/t} </th>
                <th> <img src="img/phpaga/page_white_acrobat.png" height="16" width="16" border="0" alt="{t}PDF{/t}" title="{t}PDF{/t}"> </th>
                <th> {t}Actions{/t} </th>
            </tr>
        </thead>
        <tbody class="cbxsel">
            {section name=invoices loop=$rows}
            <tr>
                <td class="number"> <a href="bill.php?id={$rows[invoices].bill_id}">{$rows[invoices].bill_number|wash}</a> </td>
                <td class="number">{$rows[invoices].rbill_id|wash|default:"&nbsp;"}</td>
                <td class="time"> <a href="bill.php?id={$rows[invoices].bill_id}">{$rows[invoices].bill_date|wash|date_format:$smarty.const.PHPAGA_DATEFORMAT_SHORT}</a> </td>
                <td> <a href="company.php?id={$rows[invoices].cpn_id}">{$rows[invoices].cpn_name|wash}</a> </td>
                <td class="number"> {$config.MONETARY_SYMBOL|wash} {$rows[invoices].bill_endsum|wash|phpaga_formatamount} </td>
                <td class="number"> {$config.MONETARY_SYMBOL|wash}
                    {if $rows[invoices].missing_pmt}{$rows[invoices].missing_pmt|wash|phpaga_formatamount}
                    {else}{$rows[invoices].bill_endsum|wash|phpaga_formatamount}
                    {/if}
                    {if $config.SHOW_CURRENCY|wash eq 'true'}{$rows[invoices].curr_name|default:"&nbsp;"}{/if}
                </td>

                <td class="time{if $rows[invoices].bill_overdue and !strlen($rows[invoices].bill_paydate)} warn{/if}"> {$rows[invoices].bill_date_due|wash|date_format:$smarty.const.PHPAGA_DATEFORMAT_SHORT} </td>
                <td class="time"> {$rows[invoices].bill_paydate|wash|date_format:$smarty.const.PHPAGA_DATEFORMAT_SHORT} </td>
                <td> {if !strlen($rows[invoices].bill_paydate)}<input class="idcbx" type="checkbox" name="id[]" value="{$rows[invoices].bill_id}">{else}&nbsp;{/if} </td>
                <td class="actions">
                    <a href="bill.php?id={$rows[invoices].bill_id}"><img src="img/phpaga/view.png" height="16" width="16" border="0" alt="{t}View{/t}" title="{t}View{/t}"></a>
                </td>
            </tr>
            {/section}
        </tbody>
        {else}
        <tr>
            <td class="last" colspan="10"> {t}No bills matching your search criteria were found.{/t} </td>
        </tr>
        {/if}
        {if strlen($BROWSE)}
        <tfoot>
            <tr>
                <td class="browse" colspan="10"> {$BROWSE} </td>
            </tr>
        </tfoot>
        {/if}
    </table>
</form>

<!-- END {$smarty.template} -->
