[% IF c.user.roles == "subscriber" || c.user.roles == "subscriberadmin" -%]
    [% site_config.title = c.loc('Customer Details') -%]
[% ELSE -%]
    [% site_config.title = c.loc('Customer Details for #[_1] ([_2])',contract.id,product.name) -%]
[% END -%]

[%
    lock_levels = [
        { level = 0, text = c.loc("none") },
        { level = 1, text = c.loc("foreign calls") },
        { level = 2, text = c.loc("all outgoing calls") },
        { level = 3, text = c.loc("incoming and outgoing") },
        { level = 4, text = c.loc("global (including web login)") },
    ];
-%]

<div class="row">
    <span>
        <a class="btn btn-primary btn-large" href="[% c.uri_for('/back') %]"><i class="icon-arrow-left"></i> [% c.loc('Back') %]</a>
        <a class="btn btn-primary btn-large" href="[% c.uri_for_action('/customer/preferences', c.req.captures) %]"><i class="icon-list"></i> [% c.loc('Preferences') %]</a>
    </span>
    [% IF !c.user.read_only && (c.user.roles == 'admin' || c.user.roles == 'reseller') -%]
    <span>
        <a class="btn btn-primary btn-large" href="[% c.uri_for_action('/customer/edit', [ contract.id ]) %]"><i class="icon-edit"></i> [% c.loc('Edit') %]</a>
    </span>
    [% END -%]
</div>
[% back_created = 1 -%]

<div class="row">
    [% FOREACH m IN messages -%]
        <div class="alert alert-[% m.type %]">[% m.text %]</div>
    [% END -%]
    [% IF contract.status != "active" -%]
        <div class="alert">Customer is <b>[% contract.status %]</b></div>
    [% END -%]
</div>

<div class="ngcp-separator"></div>

<div class="accordion" id="customer_details">

    [% IF c.user.is_superuser -%]
    <div class="accordion-group">
        <div class="accordion-heading">
            <a class="accordion-toggle" data-toggle="collapse" data-parent="#customer_details" href="#collapse_reseller">[% c.loc('Reseller') %]</a>
        </div>
        <div class="accordion-body collapse" id="collapse_reseller">
            <div class="accordion-inner">
                <table class="table table-bordered table-striped table-highlight table-hover">
                    <thead>
                        <tr>
                            <th>[% c.loc('Name') %]</th>
                            <th>[% c.loc('Value') %]</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td>[% c.loc('#') %]</td>
                            <td>[% contract.contact.reseller.id %]</td>
                        </tr>
                        <tr>
                            <td>[% c.loc('Name') %]</td>
                            <td>[% contract.contact.reseller.name %]</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    [% END -%]

    [% IF c.user.roles == 'admin' || c.user.roles == 'reseller' %]
    <div class="accordion-group">
        <div class="accordion-heading">
            <a class="accordion-toggle" data-toggle="collapse" data-parent="#customer_details" href="#collapse_contact">[% c.loc('Contact Details') %]</a>
        </div>
        <div class="accordion-body collapse" id="collapse_contact">
            <div class="accordion-inner">

                [% UNLESS c.user.read_only -%]
                <span>
                    <a class="btn btn-primary btn-large" href="[% c.uri_for('/contact', contact_hash.id, 'edit') %]"><i class="icon-edit"></i> [% c.loc('Edit Contact') %]</a>
                </span>
                <div class="ngcp-separator"></div>
                [% END -%]

                <table class="table table-bordered table-striped table-highlight table-hover">
                    <thead>
                        <tr>
                            <th>[% c.loc('Name') %]</th>
                            <th>[% c.loc('Value') %]</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td>[% c.loc('Email') %]</td>
                            <td>[% contact_hash.email%]</td>
                        </tr>
                        <tr>
                            <td>[% c.loc('Name') %]</td>
                            <td>[% contact_hash.firstname %] [% contact_hash.lastname %]</td>
                        </tr>
                        <tr>
                            <td>[% c.loc('Company') %]</td>
                            <td>[% contact_hash.company%]</td>
                        </tr>
                        <tr>
                            <td>[% c.loc('Address') %]</td>
                            <td>
                                [% contact_hash.street %]<br/>
                                [% contact_hash.postcode %] [% contact_hash.city %]<br/>
                                [% contact_hash.country %]
                            </td>
                        </tr>
                        <tr>
                            <td>[% c.loc('Phone/Fax') %]</td>
                            <td>
                                Mobile: [% contact_hash.mobilenumber %]<br/>
                                Fixed: [% contact_hash.phonenumber %]<br/>
                                Fax: [% contact_hash.faxnumber %]<br/>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <div class="accordion-group">
        <div class="accordion-heading">
            <a class="accordion-toggle" data-toggle="collapse" data-parent="#customer_details" href="#collapse_bilprofs">[% c.loc('Billing Profiles') %]</a>
        </div>
        <div class="accordion-body collapse" id="collapse_bilprofs">
            <div class="accordion-inner">
                <table class="table table-bordered table-striped table-highlight table-hover">
                    <thead>
                        <tr>
                            <th>[% c.loc('Date') %]</th>
                            <th>[% c.loc('Billing Profile Name') %]</th>
                        </tr>
                    </thead>
                    <tbody>
                        [% FOR mapping IN contract.billing_mappings.all -%]
                        <tr class="sw_action_row">
                            <td>
                                [% mapping.start_date ? mapping.start_date : 'NULL' %] - [% mapping.end_date.defined ? mapping.end_date : 'NULL' %]
                            </td>
                            <td>[% mapping.billing_profile.name %]</td>
                        </tr>
                        [% END -%]
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    [% END -%]

    <div class="accordion-group">
        <div class="accordion-heading">
            <a class="accordion-toggle" data-toggle="collapse" data-parent="#customer_details" href="#collapse_subs">[% c.loc('Subscribers') %]</a>
        </div>
        <div class="accordion-body collapse" id="collapse_subs">
            <div class="accordion-inner">
                [% IF ((c.user.roles == "subscriberadmin" && product.class == "pbxaccount") || c.user.roles == "admin" || c.user.roles == "reseller") && contract.max_subscribers.defined && subscriber_count < contract.max_subscribers -%]
                    <div class="alert alert-info">
                        [% c.loc('[_1] of maximum [_2] subscribers [_3] created',subscriber_count,contract.max_subscribers,
                                c.config.features.cloudpbx ? c.loc('(including PBX groups) ') : '') %]
                    </div>
                    <a class="btn btn-large btn-primary" href="[% c.uri_for_action('/customer/subscriber_create', [ c.req.captures.0 ]) %]">
                        <i class="icon-star"></i> [% c.loc('Create Subscriber') %]
                    </a>
                [% ELSIF contract.max_subscribers.defined -%]
                    <div class="alert">
                        [% c.loc('Maximum number of [_1] subscribers [_2] created',contract.max_subscribers,
                            c.config.features.cloudpbx ? c.loc('(including PBX groups) ') : '') %]
                    </div>
                [% ELSIF (c.user.roles == "subscriberadmin" && product.class == "pbxaccount") || c.user.roles == "admin" || c.user.roles == "reseller" -%]
                    <a class="btn btn-large btn-primary" href="[% c.uri_for_action('/customer/subscriber_create', [ c.req.captures.0 ]) %]">
                        <i class="icon-star"></i> [% c.loc('Create Subscriber') %]
                    </a>
                [% END -%]

[%
    helper.name = c.loc('Subscribers');
    helper.identifier = 'subscribers';
    helper.dt_columns = subscriber_dt_columns;
    helper.ajax_uri = c.uri_for_action('/customer/subscriber_ajax', [ c.req.captures.0 ]);

    UNLESS c.user.read_only;
        helper.dt_buttons = [
            { name = c.loc('Terminate'), uri = "/subscriber/'+full.id+'/terminate", class = 'btn-small btn-secondary', icon = 'icon-remove' },
            { name = c.loc('Details'), uri = "/subscriber/'+full.id+'/details", class = 'btn-small btn-primary', icon = 'icon-th-list' },
            { name = c.loc('Preferences'), uri = "/subscriber/'+full.id+'/preferences", class = 'btn-small btn-tertiary', icon = 'icon-list' },
        ];
    ELSE;
        helper.dt_buttons = [
            { name = c.loc('Details'), uri = "/subscriber/'+full.id+'/details", class = 'btn-small btn-primary', icon = 'icon-th-list' },
            { name = c.loc('Preferences'), uri = "/subscriber/'+full.id+'/preferences", class = 'btn-small btn-tertiary', icon = 'icon-list' },
        ];
    END;

    PROCESS 'helpers/datatables.tt';
%]

            </div>
        </div>
    </div>

    [% IF c.config.features.cloudpbx && product.class == 'pbxaccount' && subscribers.count -%]
    <div class="accordion-group">
        <div class="accordion-heading">
            <a class="accordion-toggle" data-toggle="collapse" data-parent="#customer_details" href="#collapse_pbxgroups">[% c.loc('PBX Groups') %]</a>
        </div>
        <div class="accordion-body collapse" id="collapse_pbxgroups">
            <div class="accordion-inner">
                [% IF contract.max_subscribers.defined && subscriber_count < contract.max_subscribers -%]
                    <div class="alert alert-info">
                        [% c.loc('[_1] of maximum [_2] subscribers [_3] created',subscriber_count, contract.max_subscribers,
                            c.config.features.cloudpbx ? c.loc('(including PBX groups) ') : '') %]
                    </div>
                    <a class="btn btn-large btn-primary" href="[% c.uri_for_action('/customer/pbx_group_create', [ c.req.captures.0 ]) %]">
                        <i class="icon-star"></i> [% c.loc('Create PBX Group') %]
                    </a>
                [% ELSIF contract.max_subscribers.defined -%]
                    <div class="alert">
                        [% c.loc('Maximum number of [_1] subscribers [_2] created',contract.max_subscribers,
                            c.config.features.cloudpbx ? c.loc('(including PBX groups) ') : '') %]
                    </div>
                [% ELSE %]
                    <a class="btn btn-large btn-primary" href="[% c.uri_for_action('/customer/pbx_group_create', [ c.req.captures.0 ]) %]">
                        <i class="icon-star"></i> [% c.loc('Create PBX Group') %]
                    </a>
                [% END -%]

[%
    helper.name = c.loc('PBX Groups');
    helper.identifier = 'pbx_groups';
    helper.dt_columns = pbxgroup_dt_columns;
    helper.ajax_uri = c.uri_for_action('/customer/pbx_group_ajax', [ c.req.captures.0 ]);

    UNLESS c.user.read_only;
        helper.dt_buttons = [
            { name = c.loc('Terminate'), uri = "/subscriber/'+full.id+'/terminate", class = 'btn-small btn-secondary', icon = 'icon-remove' },
            { name = c.loc('Details'), uri = "/subscriber/'+full.id+'/details", class = 'btn-small btn-primary', icon = 'icon-th-list' },
            { name = c.loc('Preferences'), uri = "/subscriber/'+full.id+'/preferences", class = 'btn-small btn-tertiary', icon = 'icon-list' },
        ];
    ELSE;
        helper.dt_buttons = [
            { name = c.loc('Details'), uri = "/subscriber/'+full.id+'/details", class = 'btn-small btn-primary', icon = 'icon-th-list' },
            { name = c.loc('Preferences'), uri = "/subscriber/'+full.id+'/preferences", class = 'btn-small btn-tertiary', icon = 'icon-list' },
        ];
    END;

    PROCESS 'helpers/datatables.tt';
%]


            </div>
        </div>
    </div>

    <div class="accordion-group">
        <div class="accordion-heading">
            <a class="accordion-toggle" data-toggle="collapse" data-parent="#customer_details" href="#collapse_pbxdevs">[% c.loc('PBX Devices') %]</a>
        </div>
        <div class="accordion-body collapse" id="collapse_pbxdevs">
            <div class="accordion-inner">
                <a class="btn btn-large btn-primary" href="[% c.uri_for_action('/customer/pbx_device_create', [ c.req.captures.0 ]) %]"><i class="icon-star"></i> [% c.loc('Create PBX Device') %]</a>
                <div class="ngcp-separator"></div>
                <table class="table table-bordered table-striped table-highlight table-hover" id="subscribers_table">
                    <thead>
                        <tr>
                            <th></th>
                            <th>[% c.loc('Station Name') %]</th>
                            <th>[% c.loc('Subscriber') %]</th>
                            <th>[% c.loc('MAC Address / Identifier') %]</th>
                            <th>[% c.loc('Device Profile') %]</th>
                            <th class="ngcp-actions-column"></th>
                        </tr>
                    </thead>
                    <tbody>
                        [% FOR dev IN pbx_devices -%]
                        <tr class="sw_action_row">
                            <td><img src="[% c.uri_for_action('/device/devmod_download_frontimage', [ dev.profile.config.device.id]) %]" style="height:40px"/></td>
                            <td>[% dev.station_name %]</td>
                            <td>
                                <ul style="list-style-type:none; margin-left:0">
                                [% FOR line IN dev.autoprov_field_device_lines.all -%]
                                    [% sub = line.provisioning_voip_subscriber.username _ '@' _ line.provisioning_voip_subscriber.domain.domain; -%]
                                    <li>[% line.autoprov_device_line_range.name %]/[% line.key_num %]: [% line.line_type %]<br/>[% sub %]</li>[% UNLESS loop.last -%]<li>&nbsp;</li>[% END -%]
                                [% END -%]
                                </ul>
                            </td>
                            <td>[% dev.identifier %]</td>
                            <td>[% dev.profile.name %]</td>
                            <td class="ngcp-actions-column">
                                <div class="sw_actions">
                                    [% UNLESS c.user.readonly -%]
                                    <a class="btn btn-secondary btn-small" href="[% c.uri_for_action("/customer/pbx_device_delete", [contract.id, dev.id])%]" data-confirm="Delete">
                                        <i class="icon-remove"></i> [% c.loc('Delete') %]
                                    </a>
                                    <a class="btn btn-primary btn-small" href="[% c.uri_for_action("/customer/pbx_device_edit", [contract.id, dev.id]) %]">
                                        <i class="icon-edit"></i> [% c.loc('Edit') %]
                                    </a>
                                    <a class="btn btn-primary btn-small" href="[% c.uri_for_action("/customer/pbx_device_sync", [contract.id, dev.id]) %]">
                                        <i class="icon-edit"></i> [% c.loc('Sync Device') %]
                                    </a>
                                    [% END -%]
                                </div>
                            </td>
                        </tr>
                        [% END -%]
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    [% END -%]

    [% IF 
        c.config.features.cloudpbx && 
        (c.user.roles == 'admin' || c.user.roles == 'reseller' || c.user.roles == 'subscriberadmin')
    -%]
    <div class="accordion-group">
        <div class="accordion-heading">
            <a class="accordion-toggle" data-toggle="collapse" data-parent="#customer_details" href="#collapse_soundsets">[% c.loc('Sound Sets') %]</a>
        </div>
        <div class="accordion-body collapse" id="collapse_soundsets">
            <div class="accordion-inner">
            
[%
    helper.name = c.loc('Sound Set');
    helper.identifier = 'sound_sets';
    helper.dt_columns = soundset_dt_columns;
    helper.ajax_uri = c.uri_for_action('/sound/contract_ajax', [ c.req.captures.0 ]);

    UNLESS c.user.read_only;
        helper.dt_buttons = [
            { name = c.loc('Edit'), uri = "/sound/'+full.id+'/edit", class = 'btn-small btn-primary', icon = 'icon-edit' },
            { name = c.loc('Delete'), uri = "/sound/'+full.id+'/delete", class = 'btn-small btn-secondary', icon = 'icon-trash' },
            { name = c.loc('Files'), uri = "/sound/'+full.id+'/handles", class = 'btn-small btn-tertiary', icon = 'icon-list' },
        ];
        helper.top_buttons = [
            { name = c.loc('Create Sound Set'), uri = c.uri_for_action('/sound/create', c.req.captures.0), icon = 'icon-star', accordion_button = 1 },
        ];
    ELSE;
        helper.dt_buttons = [
            { name = c.loc('Files'), uri = "/sound/'+full.id+'/handles", class = 'btn-small btn-tertiary', icon = 'icon-list' },
        ];
    END;

    PROCESS 'helpers/datatables.tt';
%]
            </div>
        </div>
    </div>
    [% END -%]

    <div class="accordion-group">
        <div class="accordion-heading">
            <a class="accordion-toggle" data-toggle="collapse" data-parent="#customer_details" href="#collapse_balance">[% c.loc('Contract Balance') %]</a>
        </div>
        <div class="accordion-body collapse" id="collapse_balance">
            <div class="accordion-inner">
                <table class="table table-bordered table-striped table-highlight table-hover">
                    <thead>
                        <tr>
                            <th></th>
                            <th>[% c.loc('Cash') %]</th>
                            <th>[% c.loc('Free time') %]</th>
                            <th class="ngcp-actions-column"></th>
                        </tr>
                    </thead>
                    <tbody>
                        [% USE format %]
                        [% money_format = format('%.2f') %]
                        <tr class="sw_action_row">
                            <td>[% c.loc('Current totals') %]</td>
                            <td>[% money_format(balance.cash_balance / 100) %]</td>
                            <td>[% balance.free_time_balance %]</td>
                            <td class="ngcp-actions-column">
                                <div class="sw_actions pull-right">
                                    <a class="btn btn-small btn-primary" 
                                       href="[% c.uri_for_action("/customer/edit_balance", [contract.id]) %]">
                                        <i class="icon-edit"></i> [% c.loc('Edit') %]
                                    </a>
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td>[% c.loc('Spent this interval') %]</td>
                            <td>[% money_format( balance.cash_balance_interval / 100 ) %]</td>
                            <td>[% balance.free_time_balance_interval %]</td>
                            <td></td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

   
    [% IF c.user.roles == 'admin' || c.user.roles == 'reseller' %]
    <div class="accordion-group">
        <div class="accordion-heading">
            <a class="accordion-toggle" data-toggle="collapse" data-parent="#customer_details" href="#collapse_fraud">[% c.loc('Fraud Limits') %]</a>
        </div>
        <div class="accordion-body collapse" id="collapse_fraud">
            <div class="accordion-inner">
                <table class="table table-bordered table-striped table-highlight table-hover">
                    <thead>
                        <tr>
                            <th></th>
                            <th>[% c.loc('Limit') %]</th>
                            <th>[% c.loc('Lock Level') %]</th>
                            <th>[% c.loc('Notify') %]</th>
                            <th class="ngcp-actions-column"></th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr class="sw_action_row">
                            <td>[% c.loc('Monthly Settings') %]</td>
                            [% fraud_def_message = (fraud.fraud_interval_limit.defined ? "none" : c.loc("billing profile default")) %]
                            <td>[% fraud.fraud_interval_limit.defined ? fraud.fraud_interval_limit : fraud_def_message %]</td>
                            <td>
                                [% IF fraud.fraud_interval_lock.defined -%]
                                    <select disabled="disabled">
                                       [% FOR l IN lock_levels -%]
                                       <option [% fraud.fraud_interval_lock == l.level ? 'selected="selected"' : '' %]>[% l.text %]</option>
                                       [% END -%]
                                    </select>
                                [% ELSE -%]
                                   [% fraud_def_message %]
                                [% END -%]
                            </td>
                            <td>[% fraud.fraud_interval_notify.defined ? fraud.fraud_interval_notify : fraud_def_message %]</td>
                            <td class="ngcp-actions-column">
                                <div class="sw_actions pull-right">
                                    <a class="btn btn-small btn-primary" 
                                       href="[% c.uri_for_action("/customer/edit_fraud", [c.req.captures.0], "month") %]">
                                        <i class="icon-edit"></i> [% c.loc('Edit') %]
                                    </a>
                                    <a class="btn btn-small btn-secondary" data-confirm="Delete" 
                                       href="[% c.uri_for_action("/customer/delete_fraud", [c.req.captures.0], "month") %]">
                                        <i class="icon-trash"></i> [% c.loc('Delete') %]
                                    </a>
                                </div>
                            </td>
                        </tr>
                        <tr class="sw_action_row">
                            <td>[% c.loc('Daily Settings') %]</td>
                            [% fraud_def_message = (fraud.fraud_daily_limit.defined ? "none" : c.loc("billing profile default")) %]
                            <td>[% fraud.fraud_daily_limit.defined ? fraud.fraud_daily_limit : fraud_def_message %]</td>
                            <td>
                                [% IF fraud.fraud_daily_lock.defined -%]
                                    <select disabled="disabled">
                                       [% FOR l IN lock_levels -%]
                                       <option [% fraud.fraud_daily_lock == l.level ? 'selected="selected"' : '' %]>[% l.text %]</option>
                                       [% END -%]
                                    </select>
                                [% ELSE -%]
                                   [% fraud_def_message %]
                                [% END -%]
                            </td>
                            <td>[% fraud.fraud_daily_notify.defined ? fraud.fraud_daily_notify : fraud_def_message %]</td>
                            <td class="ngcp-actions-column">
                                <div class="sw_actions pull-right">
                                    <a class="btn btn-small btn-primary" 
                                       href="[% c.uri_for_action("/customer/edit_fraud", [c.req.captures.0], "day") %]">
                                        <i class="icon-edit"></i> [% c.loc('Edit') %]
                                    </a>
                                    <a class="btn btn-small btn-secondary" data-confirm="Delete" 
                                       href="[% c.uri_for_action("/customer/delete_fraud", [c.req.captures.0], "day") %]">
                                        <i class="icon-trash"></i> [% c.loc('Delete') %]
                                    </a>
                                </div>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    [% END -%]

    <div class="accordion-group">
        <div class="accordion-heading">
            <a class="accordion-toggle" data-toggle="collapse" data-parent="#customer_details" href="#collapse_invoices">[% c.loc('Invoices') %]</a>
        </div>
        <div class="accordion-body collapse" id="collapse_invoices">
            <div class="accordion-inner">
            
[%
    helper.name = c.loc('Invoice');
    helper.identifier = 'invoices';
    helper.dt_columns = invoice_dt_columns;
    helper.ajax_uri = c.uri_for_action('/invoice/customer_ajax', [ c.req.captures.0 ]);

    helper.top_buttons = [];
    helper.dt_buttons = [
        { name = c.loc('Download'), uri = "/invoice/'+full.id+'/download", class = 'btn-small btn-tertiary', icon = 'icon-download-alt' },
    ];

    PROCESS 'helpers/datatables.tt';
%]
            </div>
        </div>
    </div>

</div>

[% IF create_flag == 1 -%]
[%
    IF form.has_for_js;
        form.render_repeatable_js;
    END;
    PROCESS "helpers/modal.tt";
    modal_header(m.create_flag = create_flag,
                 m.name = description.defined ? description : c.loc("Subscriber"));
    translate_form(form).render;
    modal_footer();
    modal_script(m.close_target = close_target);
-%]
[% ELSIF edit_flag == 1 -%]
[%
    IF form.has_for_js;
        form.render_repeatable_js;
    END;
    PROCESS "helpers/modal.tt";
    modal_header(m.create_flag=0,
                 m.name = c.loc("Settings"));
    translate_form(form).render;
    modal_footer();
    modal_script(m.close_target = close_target);
-%]
[% ELSIF devsync_flag == 1 -%]
[%
    PROCESS "helpers/modal.tt";
    modal_header(m.create_flag=0, m.edit_flag=0,
                 m.name = c.loc("Deployed Device"));
    translate_form(form).render;
    modal_footer();
    modal_script(m.close_target = close_target);
-%]
    <script>
        $('input#sync').click(function(e) {
            e.preventDefault();
            var full_params = "";
            var ip = $('form#devsyncform input#ip').val();
            if(!ip.length) {
                return 0;
            }
            $('form#devsyncform .form_messages').empty();

            var autoprov_uri = "[% autoprov_uri %]";
            autoprov_uri = autoprov_uri.replace('__NGCP_CLIENT_IP__', ip);
            [% IF autoprov_method == "POST" -%]
                $devform = $('form#devsyncform').clone(true);
                $devform.remove('input:hidden');
                $devform.find('input#ip').remove();
                [% FOR p IN autoprov_params -%]
                    var input = document.createElement("input");
                    input.type = "hidden";
                    input.name = "[% p.key %]";
                    input.value = "[% p.value %]";
                    $devform.append(input);
                [% END -%]
                $devform.attr("target", "_blank");
                $devform.attr("action", autoprov_uri);
                $devform.attr("method", "[% autoprov_method %]");
                $devform.submit();
            [% ELSE -%]
                [% FOR p IN autoprov_params -%]
                    if(full_params.length > 0)
                        full_params += '&';
                    full_params += "[% p.key %]";
                    full_params += '=' + "[% p.value %]";
                [% END -%]
                window.open(autoprov_uri + '?' + full_params, '_blank');
            [% END -%]

            return false;
        });
    </script>
[% END -%]
[% IF c.config.features.cloudpbx && device_flag -%]
    <style>
        .annotated
        {
            position: relative;
            margin: 20px 20px 0 20px;
        }
        .annotated img
        {
            display: block;
        }
        .annotated a
        {
            text-decoration: none;
            color: inherit;
        }
        .annotated .caption
        {
            white-space: nowrap;
        }
        .annotated select
        {
            height: 15px;
            line-height: 15px;
            padding: 0;
            margin: 0;
            font-size: 11px;
        }
        .annotated select.subselect
        {
            width: 180px;
        }
        .annotated select.modeselect
        {
            width: 60px;
        }
    </style>
    <link rel="stylesheet" href="/css/ngcp-annotate.css">
    <script src="/js/libs/ngcp-annotate.js"></script>
    <link rel="stylesheet" type="text/css" href="/font/font-awesome/css/font-awesome.min.css"/>
    <script>
        var aaData;
        function annotate_device() {
            $('.annotated').remove();
            var prof_id = $('div.controls #profile_id option:selected').first().attr('value');
            if(!prof_id) return;

            $.ajax({
                url: "/device/profile/" + prof_id + "/annolines/ajax",
            }).done(function(data) {
                aaData = data.aaData;
                console.log("got data", data);
                var markup = '<div class="annotated">' +
                    '<img src="/device/profile/' + prof_id + '/frontimage" />';
                var formcnt = -1;
                for(var i = 0; i < aaData.length; ++i) {
                    var range = aaData[i];
                    for(var j = 0; j < range.annotations.length; ++j) {
                        formcnt++;
                        $('#line\\.' + formcnt).remove();

                        var a = range.annotations[j];
                        [% IF create_flag == 1 -%]
                            var status = "unassigned";
                            var action = '<i class="fa fa-plus-square fa-fw"></i> ' +
                                  '<select class="subselect" name="line.' + formcnt + '.subscriber_id" id="line.' + formcnt + '.subscriber_id">' + 
                                    '<option value="0">[% c.loc("Subscriber") %]</option>' +
                                    [% subs = [] -%]
                                    [% FOR sub IN subs.merge(pbx_groups.all, subscribers.all) -%]
                                    '<option value="[% sub.provisioning_voip_subscriber.id %]">[% sub.provisioning_voip_subscriber.display_name ? sub.provisioning_voip_subscriber.display_name : sub.username %][% sub.provisioning_voip_subscriber.pbx_extension.defined ? " (" _ sub.provisioning_voip_subscriber.pbx_extension _ ")" : "" %]</option>' +
                                    [% END -%]
                                  '</select>' +
                                  '<select class="modeselect" name="line.' + formcnt + '.type" id="line.' + formcnt + '.type">' + 
                                    [% FOR opt IN ["private", "shared", "blf"] -%]
                                    '<option value="[% opt %]">[% opt %]</option>' +
                                    [% END -%]
                                  '</select>' + 
                                  '<input type="hidden" name="line.' + formcnt + '.line" id="line.' + formcnt + '.line" value="' + a.range_id + '.' + a.line_index + '"/>' + 
                                  '';
                        [% ELSIF edit_flag == 1 -%]
                            var linekeys = {
                            [% FOR line IN pbx_device.autoprov_field_device_lines.all -%]
                                '[% line.linerange_id %].[% line.key_num %]': { 
                                  'mode': '[% line.line_type %]',
                                  'sub': '[% line.provisioning_voip_subscriber.username %]@[% line.provisioning_voip_subscriber.domain.domain%]',
                                  'subid': [% line.provisioning_voip_subscriber.id %],
                                  'ext': '[% line.provisioning_voip_subscriber.pbx_extension%]'
                                }  [% line == pbx_device.autoprov_field_device_lines.all.last ? '' : ',' %]
                            [% END -%]
                            };

                            var status, action;
                            var idx = aaData[i].id + '.' + a.line_index;
                            if(linekeys[idx]) {
                                status = "assigned";
                                //action = '<i class="fa ' + mode + ' fa-fw"></i> ' + linekeys[idx].sub + ' (' + linekeys[idx].ext + ')';
                                var action = '<i class="fa fa-user fa-fw"></i> ' +
                                  '<select class="subselect" name="line.' + formcnt + '.subscriber_id" id="line.' + formcnt + '.subscriber_id">' + 
                                    '<option value="0">[% c.loc("None") %]</option>' +
                                    [% subs = [] -%]
                                    [% FOR sub IN subs.merge(pbx_groups.all, subscribers.all) -%]
                                    '<option value="[% sub.provisioning_voip_subscriber.id %]"' + (linekeys[idx].subid == [% sub.provisioning_voip_subscriber.id %] ? ' selected="selected"' : '') + '>[% sub.provisioning_voip_subscriber.display_name ? sub.provisioning_voip_subscriber.display_name : sub.username %][% sub.provisioning_voip_subscriber.pbx_extension.defined ? " (" _ sub.provisioning_voip_subscriber.pbx_extension _ ")" : "" %]</option>' +
                                    [% END -%]
                                  '</select>' +
                                  '<select class="modeselect" name="line.' + formcnt + '.type" id="line.' + formcnt + '.type">' + 
                                    [% FOR opt IN ["private", "shared", "blf"] -%]
                                    '<option value="[% opt %]"' + (linekeys[idx].mode == "[% opt %]" ? ' selected="selected"' : '') + '>[% opt %]</option>' +
                                    [% END -%]
                                  '</select>' + 
                                  '<input type="hidden" name="line.' + formcnt + '.line" id="line.' + formcnt + '.line" value="' + a.range_id + '.' + a.line_index + '"/>' + 
                                  '';
                            } else {
                                status = "unassigned";
                                var action = '<i class="fa fa-plus-square fa-fw"></i> ' +
                                      '<select class="subselect" name="line.' + formcnt + '.subscriber_id" id="line.' + formcnt + '.subscriber_id">' + 
                                        '<option value="0">[% c.loc("Subscriber") %]</option>' +
                                        [% subs = [] -%]
                                        [% FOR sub IN subs.merge(pbx_groups.all, subscribers.all) -%]
                                        '<option value="[% sub.provisioning_voip_subscriber.id %]">[% sub.provisioning_voip_subscriber.display_name ? sub.provisioning_voip_subscriber.display_name : sub.username %][% sub.provisioning_voip_subscriber.pbx_extension.defined ? " (" _ sub.provisioning_voip_subscriber.pbx_extension _ ")" : "" %]</option>' +
                                        [% END -%]
                                      '</select>' +
                                      '<select class="modeselect" name="line.' + formcnt + '.type" id="line.' + formcnt + '.type">' + 
                                        [% FOR opt IN ["private", "shared", "blf"] -%]
                                        '<option value="[% opt %]">[% opt %]</option>' +
                                        [% END -%]
                                      '</select>' + 
                                      '<input type="hidden" name="line.' + formcnt + '.line" id="line.' + formcnt + '.line" value="' + a.range_id + '.' + a.line_index + '"/>' + 
                                      '';
                            }
                        [% END -%]
                        markup += '<div class="caption ' + status + '" style="top:' + a.y + 'px; left:' + a.x + 'px;" data-pos="' + a.position + '">' + action + '</div>';
                    }
                }
                markup += '</div>';
                $('div.control-group').last().after(markup);
                $("div.annotated").each(function(){
                    $(this).find("div.caption").annotate(this);
                });
            });
        }
        $('div.controls #profile_id').change(annotate_device);
        annotate_device();

    </script>
[% END -%]

[% # vim: set tabstop=4 syntax=html expandtab: -%]
