{% extends "./template.html" %}
{% block content %}
{% load static %}


<script>
    function on_page_load() {
        //Initiate AJAX modules
        load_timesheet('{{ task_id }}','task');

        load_costs();
        load_task_customers();
        load_task_history();
        load_to_do();
        load_bug_list();
        load_bug_search();
        load_email_history();
        load_tag_results();

        load_document_tree_list({{ task_id }},'task');

        load_assigned_user_add();
        load_assigned_user_list();
        load_assigned_group_add();
        load_assigned_group_list();

        //Setup the datetime widgets
        $("#id_task_start_date").datetimepicker({
            scrollInput: false,
            format:'Y-m-d H:i:00',
            allowTimes: [
                '00:00','00:05','00:10','00:15','00:20','00:25','00:30','00:35','00:40','00:45','00:50','00:55',
                '01:00','01:05','01:10','01:15','01:20','01:25','01:30','01:35','01:40','01:45','01:50','01:55',
                '02:00','02:05','02:10','02:15','02:20','02:25','02:30','02:35','02:40','02:45','02:50','02:55',
                '03:00','03:05','03:10','03:15','03:20','03:25','03:30','03:35','03:40','03:45','03:50','03:55',
                '04:00','04:05','04:10','04:15','04:20','04:25','04:30','04:35','04:40','04:45','04:50','04:55',
                '05:00','05:05','05:10','05:15','05:20','05:25','05:30','05:35','05:40','05:45','05:50','05:55',
                '06:00','06:05','06:10','06:15','06:20','06:25','06:30','06:35','06:40','06:45','06:50','06:55',
                '07:00','07:05','07:10','07:15','07:20','07:25','07:30','07:35','07:40','07:45','07:50','07:55',
                '08:00','08:05','08:10','08:15','08:20','08:25','08:30','08:35','08:40','08:45','08:50','08:55',
                '09:00','09:05','09:10','09:15','09:20','09:25','09:30','09:35','09:40','09:45','09:50','09:55',
                '10:00','10:05','10:10','10:15','10:20','10:25','10:30','10:35','10:40','10:45','10:50','10:55',
                '11:00','11:05','11:10','11:15','11:20','11:25','11:30','11:35','11:40','11:45','11:50','11:55',
                '12:00','12:05','12:10','12:15','12:20','12:25','12:30','12:35','12:40','12:45','12:50','12:55',
                '13:00','13:05','13:10','13:15','13:20','13:25','13:30','13:35','13:40','13:45','13:50','13:55',
                '14:00','14:05','14:10','14:15','14:20','14:25','14:30','14:35','14:40','14:45','14:50','14:55',
                '15:00','15:05','15:10','15:15','15:20','15:25','15:30','15:35','15:40','15:45','15:50','15:55',
                '16:00','16:05','16:10','16:15','16:20','16:25','16:30','16:35','16:40','16:45','16:50','16:55',
                '17:00','17:05','17:10','17:15','17:20','17:25','17:30','17:35','17:40','17:45','17:50','17:55',
                '18:00','18:05','18:10','18:15','18:20','18:25','18:30','18:35','18:40','18:45','18:50','18:55',
                '19:00','19:05','19:10','19:15','19:20','19:25','19:30','19:35','19:40','19:45','19:50','19:55',
                '20:00','20:05','20:10','20:15','20:20','20:25','20:30','20:35','20:40','20:45','20:50','20:55',
                '21:00','21:05','21:10','21:15','21:20','21:25','21:30','21:35','21:40','21:45','21:50','21:55',
                '22:00','22:05','22:10','22:15','22:20','22:25','22:30','22:35','22:40','22:45','22:50','22:55',
                '23:00','23:05','23:10','23:15','23:20','23:25','23:30','23:35','23:40','23:45','23:50','23:55'
            ]
        });
        $("#id_task_end_date").datetimepicker({
            scrollInput: false,
            format:'Y-m-d H:i:00',
            allowTimes: [
                '00.00','00:05','00:10','00:15','00:20','00:25','00:30','00:35','00:40','00:45','00:50','00:55',
                '01:00','01:05','01:10','01:15','01:20','01:25','01:30','01:35','01:40','01:45','01:50','01:55',
                '02:00','02:05','02:10','02:15','02:20','02:25','02:30','02:35','02:40','02:45','02:50','02:55',
                '03:00','03:05','03:10','03:15','03:20','03:25','03:30','03:35','03:40','03:45','03:50','03:55',
                '04:00','04:05','04:10','04:15','04:20','04:25','04:30','04:35','04:40','04:45','04:50','04:55',
                '05:00','05:05','05:10','05:15','05:20','05:25','05:30','05:35','05:40','05:45','05:50','05:55',
                '06:00','06:05','06:10','06:15','06:20','06:25','06:30','06:35','06:40','06:45','06:50','06:55',
                '07:00','07:05','07:10','07:15','07:20','07:25','07:30','07:35','07:40','07:45','07:50','07:55',
                '08:00','08:05','08:10','08:15','08:20','08:25','08:30','08:35','08:40','08:45','08:50','08:55',
                '09:00','09:05','09:10','09:15','09:20','09:25','09:30','09:35','09:40','09:45','09:50','09:55',
                '10:00','10:05','10:10','10:15','10:20','10:25','10:30','10:35','10:40','10:45','10:50','10:55',
                '11:00','11:05','11:10','11:15','11:20','11:25','11:30','11:35','11:40','11:45','11:50','11:55',
                '12:00','12:05','12:10','12:15','12:20','12:25','12:30','12:35','12:40','12:45','12:50','12:55',
                '13:00','13:05','13:10','13:15','13:20','13:25','13:30','13:35','13:40','13:45','13:50','13:55',
                '14:00','14:05','14:10','14:15','14:20','14:25','14:30','14:35','14:40','14:45','14:50','14:55',
                '15:00','15:05','15:10','15:15','15:20','15:25','15:30','15:35','15:40','15:45','15:50','15:55',
                '16:00','16:05','16:10','16:15','16:20','16:25','16:30','16:35','16:40','16:45','16:50','16:55',
                '17:00','17:05','17:10','17:15','17:20','17:25','17:30','17:35','17:40','17:45','17:50','17:55',
                '18:00','18:05','18:10','18:15','18:20','18:25','18:30','18:35','18:40','18:45','18:50','18:55',
                '19:00','19:05','19:10','19:15','19:20','19:25','19:30','19:35','19:40','19:45','19:50','19:55',
                '20:00','20:05','20:10','20:15','20:20','20:25','20:30','20:35','20:40','20:45','20:50','20:55',
                '21:00','21:05','21:10','21:15','21:20','21:25','21:30','21:35','21:40','21:45','21:50','21:55',
                '22:00','22:05','22:10','22:15','22:20','22:25','22:30','22:35','22:40','22:45','22:50','22:55',
                '23:00','23:05','23:10','23:15','23:20','23:25','23:30','23:35','23:40','23:45','23:50','23:55'
            ]
        });
    }
</script>

<body onload="on_page_load()">
	<form id="task_information" name="task_information" method="POST" enctype="multipart/form-data">
        {% csrf_token %}
        <div class="card mt-4">
            <div class="card-header">
                <h2>Task information - TASK{{ task_results.task_id }}</h2>
            </div>
            <div class="card-body">
                <div id="save_state" class="alert-success"></div>
                {{ task_information_form.media }}
                <div class="form-group">
                    <label for="id_task_short_description">Short Description</label>
                    {{ task_information_form.task_short_description }}
                </div>
                <div class="form-group">
                    <label for="id_task_long_description">Task Long Description</label>
                    {{ task_information_form.task_long_description }}
                </div>
                <div class="form-group">
                    Current Task Status: {{ task_results.task_status }}
                </div>
                <div class="form-group">
                    {% if not task_results.organisations_id == Null %}
                        <p><a href="{% url 'organisation_information' task_results.organisations_id.organisations_id %}">{{ task_results.organisations_id }}</a></p>
                    {% endif %}
                    <p>Time Zone - {{ timezone }}</p>
                </div>
                <div class="form-row">
                    <div class="form-group col-md-4">
                        <label for="id_task_start_date">Task Start</label>
                        {{ task_information_form.task_start_date }}
                    </div>
                    <div class="form-group col-md-4">
                        <label for="id_task_end_date">Task Finish Date</label>
                        {{ task_information_form.task_end_date }}
                    </div>
                    <div class="form-group col-md-4">
                        <label for="id_story_point">Story Points</label>
                        <div id="id_story_point">
                            <strong>Min hours: </strong>{{ task_results.task_story_point_min }}
                            <strong>| Max hours: </strong>{{ task_results.task_story_point_max }}
                        </div>
                    </div>
                </div>
                {% if permission > 1 %}
                    <div class="form-row">
                        <input type="submit" name="update" value="Update" class="col-md-6 form-control btn btn-primary"/>
                        {% if not task_results.task_status == 'Closed' %}
                            <input type="submit" name="Resolve" value="Resolve Task" class="col-md-6 form-control btn btn-warning"/>
                        {% endif %}
                    </div>
                {% endif %}
            </div>
        </div>
    </form>

    <form method="post" class="timesheet mt-4" id="timesheet">
        <div class="card">
            <div class="card-header">
                <h2>Timesheet</h2>
            </div>
            <div class="card-body">
                Loading...
            </div>
        </div>
    </form>

    <br/>
    <form method="post" class="to_do" id="to_do">
        <h2>To Do</h2>
        Loading...
    </form>


    <form method="post" class="task_history" id="task_history">
        {% csrf_token %}
        <div class="card mt-4">
            <div class="card-header">
                <h2>Task History</h2>
            </div>
            <div class="card-body">
                {% if permission  > 1 %}
                    <div class="form-group">
                        {{ information_task_history.media }}
                        {{ information_task_history_form.task_history }}
                    </div>
                    <div class="form-row">
                        <div class="form-group col-md-6">
                            <input type="submit" name="task_history_submit" value="Submit" class="btn btn-primary form-control">
                        </div>
                        <div class="form-group col-md-6">
                            <a onclick="clear_history()" href="javascript:void(0);" class="btn btn-warning form-control">Clear</a>
                        </div>
                    </div>
                {% else %}
                    <label>Sorry, you do not have permission to add in any task history.</label>
                {% endif %}
            </div>
        </div>
    </form>

    <div id="task_history_results">
        Loading Task History Results...
    </div>

    <form action="POST" id="tag_results" class="tag_results">
        <div class="card mt-4">
            <div class="card-header">
                <h2>Tags</h2>
            </div>
            <div class="card-body">
                Loading...
            </div>
        </div>
    </form>

    {% if requirement_results %}
        <div class="card mt-4">
            <div class="card-header">
                <h2>Requirement Links</h2>
            </div>
            <div class="card-body">
                <table class="table table-striped table-hover">
                    <thead>
                        <tr>
                            <td width="20%">Requirement ID</td>
                            <td width="80%">Requirement Description</td>
                        </tr>
                    </thead>
                    {% for requirement in requirement_results %}
                        <tr>
                            <td><a href="{% url 'requirement_information' requirement.requirement_id %}">REQ{{ requirement.requirement_id }}</a></td>
                            <td>{{ requirement.requirement_title }}</td>
                        </tr>
                    {% endfor %}
                </table>
            </div>
        </div>
    {% endif %}

    {% if requirement_item_results %}
        <div class="card mt-4">
            <div class="card-header">
                <h2>Requirement Item Links</h2>
            </div>
            <div class="card-body">
                <table class="table table-striped table-hover">
                    <thead>
                        <tr>
                            <td width="20%">Requirement Item ID</td>
                            <td width="80%">Requirement Item Description</td>
                        </tr>
                    </thead>
                    {% for requirement_item in requirement_item_results %}
                        <tr>
                            <td><a href="{% url 'requirement_item_information' requirement_item.requirement_item_id %}">ITM{{ requirement_item.requirement_item_id }}</a></td>
                            <td>{{ requirement_item.requirement_item_title }}</td>
                        </tr>
                    {% endfor %}
                </table>
            </div>
        </div>
    {% endif %}

    <div class="card mt-4">
        <div class="card-header">
            <h2>Email History</h2>
        </div>
        <div id="email_history" class="card-body">
            Loading...
        </div>
    </div>


    <div class="card mt-4">
        <div class="card-header">
            <h2>Associated Projects</h2>
        </div>
        <div class="card-body">
            {% if permission > 1 %}
                {% if task_results.task_status in open_status %}
                    <a href="{% url 'associated_projects' task_results.task_id %}">Associate Project</a>
                {% endif %}
            {% endif %}

            {% if associated_project_results %}
                    <table class="table table-striped table-hover">
                        <thead>
                            <tr>
                                <td>Project Id</td>
                                <td>project Name</td>
                                <td>End Date</td>
                                <td>Status</td>
                            </tr>
                        </thead>
                        {% for associated_project_row in associated_project_results %}
                            <tr>
                                <td><a href="{% url 'project_information' associated_project_row.project_id %}">Project - {{ associated_project_row.project_id }}</a></td>
                                <td>{{ associated_project_row.project_name }}</td>
                                <td>{{ associated_project_row.project_end_date }}</td>
                                <td>{{ associated_project_row.project_status }}</td>
                            </tr>
                        {% endfor %}
                    </table>
            {% else %}
                <p>Currently no associated projects.</p>
            {% endif %}
        </div>
    </div>

    <div class="card mt-4">
        <div class="card-header">
            <h2>Task Customers</h2>
        </div>
        <div class="card-body">
            <form method="post" class="task_customers" id="task_customers" style="">
                Loading...
            </form>
        </div>
    </div>


    <form method="post" class="costs_form" id="costs_form" style="">
        <h2>Task Costs</h2>
        Loading...
    </form>


    <div class="card mt-4">
        <div class="card-header">
            <h2>Document Uploads</h2>
        </div>
        <div class="card-body">
            <div class="document_tree_list" id="document_tree_list">
                <h2>Document Tree List</h2>
                Loading...
            </div>
        </div>
    </div>

    <div class="card mt-4">
        <div class="card-header">
            <h2>Task Quotes</h2>
        </div>
        <div class="card-body">
            <a href="{% url 'new_quote' task_id 'task' %}">New Quote</a>
            {% if quote_results %}
                <table>
                    <tr>
                        <td width="50%">Quote Title</td>
                        <td width="25%">Valid Till</td>
                        <td width="25%">Quote Stage</td>
                    </tr>
                    {% for row in quote_results %}
                        <tr>
                            <td><a href="{% url 'quote_information' row.quote_id %}">{{ row.quote_title }}</a></td>
                            <td>{{ row.quote_valid_till }}</td>
                            <td>{{ row.quote_stage_id }}</td>
                        </tr>
                    {% endfor %}
                </table>
            {% endif %}
        </div>
    </div>




    <form method="post" class="bug_list" id="bug_list">
        <h2>Task Bug List</h2>
        Loading...
    </form>

    <form method="post" class="bug_search" id="bug_search">
        <h2>Bug Search</h2>
        Loading...
    </form>


    {% if permission >= 3 %}
        <div class="card mt-4">
            <div class="card-header">
                <h2>Assigned Users</h2>
            </div>
            <div class="card-body">
                <form method="post" class="assigned_users" id="assigned_users">
                    Loading...
                </form>
                <div id="assigned_user_list">Loading list...</div>
            </div>
        </div>

        <div class="card mt-4">
            <div class="card-header">
                <h2>Assigned Groups</h2>
            </div>
            <div class="card-body">
                <form method="post" class="assigned_groups" id="assigned_groups">
                    Loading add...
                </form>
                <div id="assigned_group_list">Loading list...</div>
            </div>
        </div>
    {% endif %}

<script>
    $("#assigned_users").on('submit', function(e) {
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: '/assigned_user_add/{{ task_id }}/task/',
            data: $(this).serialize(),
            success: function() {
                load_assigned_user_list();
                load_assigned_user_add();
            },
            error: function() {
                new_error("Sorry, project assigned users did not save. Please try again.");
            }
        })
    });


    $("#assigned_groups").on('submit', function(e) {
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: '/assigned_group_add/{{ task_id }}/task/',
            data: $(this).serialize(),
            success: function() {
                load_assigned_group_add();
                load_assigned_group_list();
            },
            error: function() {
                new_error("Sorry, project assigned users did not save. Please try again.");
            }
        })
    });


    $("#bug_search").on('submit', function(e) {
       e.preventDefault();

       //Change the submit button for user feedback
       $("#bug_search_submit").prop("disabled",true);
       $("#bug_search_submit").val("SEARCHING...");


       $.ajax({
           type: 'POST',
           url: '/bug_search/{{ task_id }}/task/',
           data: $(this).serialize(),
           success: function(data) {
               $("#bug_search").html(data);
           },
           error: function() {
               new_error("Sorry, submitting the bug search caused an error");
           }
       })
    });


    $("#document_tree_create_folder").on('submit', function(e) {
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: '/document_tree_create_folder/{{ task_id }}/T/',
            data: $(this).serialize(),
            success: function() {
                load_document_tree_create_folder();
                load_document_tree_list();
            },
            error: function() {
                new_error("Sorry, document tree create folder did not save. Please try again.");
            }
        })
    });

    $("#task_assigned_users").on('submit', function(e) {
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: '/information_task_assign_users/{{ task_id }}/',
            data: $(this).serialize(),
            success: function() {
                //Reload the task history :)
                load_task_assigned_users();
            },
            error: function() {
                new_error("Sorry, task assigned users did not save. Please try again.");
            }
        })
    });

    $("#costs_form").on('submit', function(e) {
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: '/cost_information/{{ task_id }}/task/',
            data: $(this).serialize(),
            success: function() {
                //Reload the task history :)
                load_costs();
            },
            error: function() {
                new_error("Sorry, task costs did not save. Please try again.");
            }
        })
    });

    //Code used to help send which button was clicked in POST
    $("form input[type=submit]").click(function() {
        $("input[type=submit]", $(this).parents("form")).removeAttr("clicked");
        $(this).attr("clicked", "true");
    });
    $("#task_information").on('submit', function(e) {
        //State the save_state is currently saving
        $("#save_state").html("Currently Saving Project");

        //Grab which button was pressed
        var which_button = $("input[type=submit][clicked=true]").val();

        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: '/task_information/{{ task_id }}/',
            data: $(this).serialize(),
            success: function() {
                //State that the function has saved
                var current_time = Date();
                $("#save_state").html("Saved last on " + current_time);

                //If we have the resolve button, we will need to navigate to the resolved task url.
                if (which_button == "Resolve Task") {
                    //Navigate to the resolve task URL
                    document.location = "{% url 'resolve_task' task_id %}"
                }
            },
            error: function() {
                new_error("Sorry, task information did not save. Please try again.");
            }
        })
    });

    $("#task_customers").on('submit', function(e) {
        e.preventDefault();
        $.ajax({
            url: '/information_task_customer/{{ task_id }}/',
            type: 'POST',
            data: $(this).serialize(),
            success: function(data) {
                $("#task_customers").html(data)
            },
            error: function() {
                new_error("Sorry, we can not seem to add this customer to this task")
            },
        })
    });

    $("#task_history").on('submit', function(e) {
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: '/information_task_history/{{ task_id }}/',
            data: $(this).serialize(),
            success: function() {
                //Reload the task history :)
                load_task_history();
            },
            error: function() {
                new_error("Sorry, task history did not save. Please try again.");
            }
        })
    });

    $("#timesheet").on('submit', function(e) {
        e.preventDefault();

        $.ajaxSetup({
            beforeSend: function (xhr, settings) {
                if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
                    xhr.setRequestHeader("X-CSRFToken", csrftoken);
                }
            }
        });

        $.ajax({
            type: 'POST',
            url: '/timesheet/{{ task_id }}/task/',
            data: $(this).serialize(),
            success: function(data) {
                $("#timesheet").html(data);

                //Reinitialise the timesheet
                timesheet_setup();
            },
            error: function() {
                new_error("Sorry - updating the timesheet went wrong");
            }
        });
    });

    $("#to_do").on('submit', function(e) {
        e.preventDefault();
        $.ajax({
            url: '/to_do/{{ task_id }}/task/',
            data: $(this).serialize(),
            type: 'POST',
            success: function(data) {
                $("#to_do").html(data);
            },
            error: function() {
                new_error("Submitting the TO DO had an issue!");
            }
        })
    });

    $("#tag_results").on('submit', function(e) {
       e.preventDefault();

       $.ajaxSetup({
            beforeSend: function (xhr, settings) {
                if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
                    xhr.setRequestHeader("X-CSRFToken", csrftoken);
                }
            }
        });

       $.ajax({
           url: '/tag_information/{{ task_id }}/task/',
           data: $(this).serialize(),
           dataType: 'HTML',
           type: 'POST',
           success: function(data) {
               $("#tag_results").html(data);
           },
           error: function() {
               new_error("Sorry, could not save tag.");
           },
       })
    });


    function load_bug_list() {
        $.ajax({
            url: '/bug_list/{{ task_id }}/task',
            data: {},
            dataType: 'HTML',
            type: 'GET',
            success: function(data) {
                $("#bug_list").html(data);
            },
            error: function() {
                new_error("There was an issue loading the bug list");
            }
        })
    }


    function load_bug_search() {
        $.ajax({
            url: '/bug_search/{{ task_id }}/task',
            data: {},
            dataType: 'HTML',
            type: 'GET',
            success: function(data) {
                $("#bug_search").html(data);
            },
            error: function() {
                new_error("There was an issues loading the bug search");
            }
        })

    }


    function complete_to_do(to_do_id) {
        $("#to_do_"+to_do_id).html("Completing...");

        //Send data to the database
        $.ajaxSetup({
            beforeSend: function (xhr, settings) {
                if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
                    xhr.setRequestHeader("X-CSRFToken", csrftoken);
                }
            }
        });

        $.ajax({
            url: '/to_do_complete/' + to_do_id + '/',
            data: {},
            type: "POST",
            success: function(data) {
                load_to_do();
            },
            error: function() {
                new_error("Can not complete To do :(");
                $("#to_do_"+to_do_id).html('<a href="javascript:void(0)" onclick="complete_to_do(' + to_do_id + '">Complete</a>')
            }
        });
    }


    function load_email_history() {
        $.ajax({
            url: '/email_history/{{ task_id }}/task',
            data: {},
            dataType: 'HTML',
            type: 'GET',
            success: function(data) {
                $("#email_history").html(data);
            },
            error: function() {
                new_error("Sorry, the email history could not load");
            }
        })
    };



    function load_costs() {
        //Load Project History
        $.ajax({
            url: '/cost_information/{{ task_id }}/task/',
            data: {},
            dataType: 'HTML',
            type: 'GET',
            success: function(data) {
                $('#costs_form').html(data);
            },
            error: function() {
                $('#costs_form').html('<h2>Task Costs</h2>Sorry, task costs encounted an error and did not load.');
            }
        });
    };

    function load_task_customers() {
        //Load Task Customers
        $.ajax({
            url: '/information_task_customer/{{ task_id }}/',
            data: {},
            dataType: 'HTML',
            type: 'GET',
            success: function(data) {
                $('#task_customers').html(data);
            },
            error: function() {
                $('#task_customers').html('<h2>Task Customers</h2>Sorry, task customers encounted an error and did not load.');
            }
        });
    };

    function load_task_history() {
        //Load Project History
        $.ajax({
            url: '/information_task_history/{{ task_id }}/',
            data: {},
            dataType: 'HTML',
            type: 'GET',
            success: function(data) {
                $('#task_history_results').html(data);

                //Initialise tinyMCE for task history
                tinyMCE.init({
                    selector: '.old_task_history',
                    readonly: true,
                    'menubar': false,
                    'toolbar': false,
                    'height': '300px',
                });
            },
            error: function() {
                $('#task_history_results').html('<h2>Project History</h2>Sorry, task history encounted an error and did not load.');
            }
        });
    };

    function load_to_do() {
        $.ajax({
            url: '/to_do/{{ task_id }}/task/',
            data: {},
            dataType: 'HTML',
            type: 'GET',
            success: function(data) {
                $('#to_do').html(data)
            },
            error: function() {
                new_error("Sorry, could not load the to do list");
            }
        })
    }

    function load_assigned_user_add() {
        $.ajax({
            url: '/assigned_user_add/{{ task_id }}/task/',
            data:  {},
            dataType: 'HTML',
            type: 'GET',
            success: function(data) {
                $("#assigned_users").html(data);
            },
            error: function() {
                $("#assigned_users").html("<h2>Assigned Users</h2>Sorry, users encounted an error and did not load");
            }
        })
    };

    function load_assigned_user_list() {
        $.ajax({
            url: '/assigned_user_list/{{ task_id }}/task/',
            data: {},
            dataType: 'HTML',
            type: 'GET',
            success: function(data) {
                $("#assigned_user_list").html(data);
            },
            error: function(data) {
                $("#assigned_user_list").html("<p>User list failed to load</p>")
            }
        })
    };

    function delete_group_item(group_item) {
        $.ajaxSetup({
            beforeSend: function (xhr, settings) {
                if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
                    xhr.setRequestHeader("X-CSRFToken", csrftoken);
                }
            }
        });

        $.ajax({
            url: '/assigned_group_delete/' + group_item + '/',
            data: {},
            dataType: 'HTML',
            type: 'POST',
            success: function(data) {
                load_assigned_group_add();
                load_assigned_group_list();
            },
            error: function() {
                new_error("Sorry, there was an issue deleting the Assigned Group")
            }
        });
    };


    function load_assigned_group_add() {
        $.ajax({
            url: '/assigned_group_add/{{ task_id }}/task/',
            data: {},
            dataType: 'HTML',
            type: 'GET',
            success: function(data) {
                $('#assigned_groups').html(data);
            },
            error: function() {
                $('#assigned_groups').html('<h2>Assigned Groups</h2>Sorry, assigned groups encounted an error and did not load.');
            }
        });
    };


    function load_assigned_group_list() {
        $.ajax({
            url: '/assigned_group_list/{{ task_id }}/task/',
            data: {},
            dataType: 'HTML',
            type: 'GET',
            success: function(data) {
                $('#assigned_group_list').html(data);
            },
            error: function() {
                $('#assigned_group_list').html('<h2>Assigned Group</h2>Sorry, task history encounted an error and did not load.');
            }
        });
    };

    function delete_cost(cost_id,task_id) {
        //Send data to the database
        $.ajaxSetup({
            beforeSend: function (xhr, settings) {
                if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
                    xhr.setRequestHeader("X-CSRFToken", csrftoken);
                }
            }
        });

        $.ajax({
            url: '/delete_cost/' + cost_id + '/' + task_id + '/T/',
            data: $(this).serialize(),
            type: "POST",
            success: function(data) {
                load_costs();
            },
            error: function() {
                new_error("Sorry, could not delete costs");
            }
        });
    }

    function remove_customer(task_customer_id) {
        //Send data to the database
        $.ajaxSetup({
            beforeSend: function (xhr, settings) {
                if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
                    xhr.setRequestHeader("X-CSRFToken", csrftoken);
                }
            }
        });

        $.ajax({
            url: '/task_remove_customer/' + task_customer_id + '/',
            data: {},
            type: "POST",
            success: function(data) {
                load_task_customers();
            },
            error: function() {
                new_error("Sorry - there is something wrong with removing this customer");
            }
        });
    }


    function load_tag_results() {
        $.ajax({
            url: '/tag_information/{{ task_id }}/task/',
            data: {},
            dataType: 'HTML',
            type: 'GET',
            success: function(data) {
                $("#tag_results").html(data);
            },
            error: function() {
                new_error("Sorry - tag results did not load");
            }
        });
    }

    function delete_tag(tag_id) {
        $.ajaxSetup({
            beforeSend: function (xhr, settings) {
                if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
                    xhr.setRequestHeader("X-CSRFToken", csrftoken);
                }
            }
        });

        $.ajax({
            url: '/delete_tag_from_object/' + tag_id + '/{{ task_id }}/task/',
            data: {},
            dataType: 'HTML',
            type: 'POST',
            success: function(data) {
                load_tag_results();
            },
            error: function() {
                new_error("Sorry - did not delete tag correctly");
            }
        });
    }
</script>

</body>
{% endblock %}