{% extends "admin/base_site.html" %}

{% block title %}Bulk upload{% endblock %}


{% block extrahead %}
<script type="text/javascript">
    var data = Array();
    data[0] = 'Email\nbill@example.com\nbob@example.com\nfred@example.com\n';
    data[1] = 'Email\tRole\tLanguage\tCountry\tTitle\tFirst_name\tMiddle_name\tLast_name\tCompany\n';
    data[1] += 'bill@example.com\tBUSINESS\tEN\tSC\tMr\tBill\t\tWardell\t\n';
    data[1] += 'fred@example.com\tBUSINESS\tEN\tSC\t\tFred\tJacob\tHaydon\tRed Restaurant\n';
    data[1] += 'sharon@example.com\tBUSINESS\tEN\tSC\tMiss\tSharon\tBruce\t\n';
    data[2] = '';

    function sample_data(i) {
        var text_area = document.getElementById('pasted_content');
        text_area.value = data[i];
    }
</script>
{% endblock %}


{% block breadcrumbs %}
<div class="breadcrumbs"><a href="/admin/">Home</a>
&rsaquo; <a href="/admin/crm/">Crm</a>
&rsaquo; <a href="/admin/crm/lead/">Leads</a>
&rsaquo; <a href="/admin/crm/lead/bulk_upload/">Bulk upload</a>
&rsaquo; 
{% ifequal step 1 %}Paste in data{% endifequal %}
{% ifequal step 2 %}Data review{% endifequal %}</div>
{% endblock %}

{% block content %}
<h1>Bulk upload</h1>

{% if status %}
    <p><strong>{{ status }}</strong></p>
{% endif %}

{% ifequal step 2 %}

    {% if early_problems %}
    <p>There were problems:</p>
    <ul>
        {% for prob in early_problems %}
        <li>{{ prob }}</li>
        {% endfor %}
    </ul>
    {% endif %}


    {% if late_success %}
        {{data_table|safe}}
        <form action="." method="post">
            <div class="submit-row">
                <input type="hidden" name="pasted_content" value="{{pasted_content}}"/>
                <input type="submit" name="confirmed" value="Confirm" class="default"/>
            </div>
        </form>
    {% else %}
        <p>Please press your browser's 'Back' button and correct your data.</p>
        {% for lead_form in lead_forms %}
            <!--{% if lead_form.errors %}{{ lead_form.errors }}<br/>{% endif %}-->
        {% endfor %}
        {{data_table|safe}}
    {% endif %}


{% else %}
<p>
Please copy the data from your spreadsheet and paste it into the text area below. Further instructions provided at the bottom of the page
</p>

<form action="." method="post">
<textarea name="pasted_content" id="pasted_content" style="width: 100%; height: 350px"></textarea>
<div class="submit-row">
    <input type="submit" value="Submit" class="default"/>
</div>
</form>

<div>
<input type="button" value="Pre-fill minimal sample data" onclick="sample_data(0)" />
<input type="button" value="Pre-fill larger sample data" onclick="sample_data(1)" />
<input type="button" value="Clear data" onclick="sample_data(2)" />
</div>

<h2>Instructions</h2>
<table style="border: 1px solid #ccc; margin-bottom: 20px;">
    <tr>
        <th></th><th>Role</th><th>Language</th><th>Country</th><th>Dates (last_purchased)</th><th>Boolean (active)</th>
    </tr>
    <tr>
        <th>Accepted values</th>
        <td>{% for role in roles %}{{role.id}} {% endfor %}</td>
        <td>{% for language in languages %}{{language.id}} {% endfor %}</td>
        <td>GB SC FR DE IT ES etc.</td>
        <td>2009-07-20 (YYYY-MM-DD)</td>
        <td>1 or 0</td>
    </tr>
</table>
<p>
If this is your first time, try clicking the buttons above to see what data will look like when you paste it in.
</p>
<p>
Data needs to be enterred in tab-separated value form with headings for each of the fields you are supplying. We recommend you use a spreadsheet application initially such as OpenOffice Calc or Microsoft Excel. All your data should be grouped into columns for each field and a row for each lead (potential customer). The first row will just contain the column headings. 
</p>
<img src="/media/spreadsheet.png">
<p>
The only required field is 'email', although it is best to collect and store as much data as possible. The other available fields are:<br/>

<strong>Role, country, language, tags, first_name, middle_name, last_name, department, job_title, company, business_street, business_city, business_country, other_street, other_city, other_country, business_fax, business_phone, mobile_phone, other_phone, email_display_name, email_2_address, email_2_display_name, has_bought, last_purchased, active.</strong>
</p>
<p>
Once you have layed out your data like the screenshot, click the top-left cell and drag all the way to the bottom-right cell with content. Copy this data (Ctrl-C) and paste it into the text area on this page. Don't worry if the data doesn't look as clean once you have pasted it into this page - this is caused by the tab characters.
<p>
<p>
When you have pasted the data, click the 'Submit' button. If there are any fields with invalid data you will be shown an error and will not be allowed to procede. If this happens the invalid lines will be shown in red and you will have to go back and correct the data. You can either correct the data directly in the textfield on this page or go back to you spreadsheet, copy, then paste it in this page over the old data.
<p>
{% endifequal %}
{% endblock %}
