@extends('layouts.master')

@section('style')
    <link href="{{URL::asset('assets/css/jquery.datetimepicker.css')}}" rel="stylesheet" type="text/css"/>

    <style type="text/css">
        .cls-show-time{
        color: #000;
        font-style: italic;
        font-size: 12px;
        }
    </style>
@endsection

@section('content')
<section class="content">
    <div class="row">
        <div class="col-md-6">
        @if(!empty($pm_created_task))
            <section class="panel tasks-widget">
                <header class="panel-heading" style="color:#34BCA0;">
                    <b>PM # all created task</b>
                </header>
                <div class="cls-overflow table-responsive project-list">
                    <table class="table table-striped">
                        <thead>
                            <tr>
                                <th>
                                    Task
                                </th>
                                <th>
                                    Project
                                </th>
                                <th>
                                    Name
                                </th>
                                <th>
                                    Assigned time
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($pm_created_task as $pt)
                                <tr>
                                    <td>
                                        <a style="color: #333;" href="{{url("user/uproject_details/$pt->proj_id")}}">
                                        {{$pt->name}}
                                        </a>
                                    </td>
                                    <td>{{$pt->proj_name->name}}</td>
                                    <td>
                                        @if(!empty($pt->user->name))
                                            {{$pt->user->name}}
                                        @else
                                            <span style="color:#FF6000;">
                                            {{"Not assigned"}}
                                            </span>
                                        @endif
                                    </td>
                                    <td>{{date("d-m-Y H:i", strtotime($pt->assign_time))}}</td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                    <hr>
                    </hr>
                </div>
            </section>
        @endif
        </div>
        <div class="col-md-6">
        @if(!empty($pm_inprocess_task))
            <section class="panel tasks-widget">
                <header class="panel-heading" style="color:#34BCA0;">
                    <b>PM # all in-process task</b>
                </header>
                <div class="cls-overflow table-responsive project-list">
                    <table class="table table-striped">
                        <thead>
                            <tr>
                                <th>
                                    Task
                                </th>
                                <th>
                                    Project
                                </th>
                                <th>
                                    Name
                                </th>
                                <th>
                                    Start time
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($pm_inprocess_task as $pt)
                                <tr>
                                    <td>
                                        <a style="color: #333;" href="{{url("user/uproject_details/$pt->proj_id")}}">
                                        {{$pt->name}}
                                        </a>
                                    </td>
                                    <td>{{$pt->proj_name->name}}</td>
                                    <td>
                                        @if(!empty($pt->user->name))
                                            {{$pt->user->name}}
                                        @else
                                            <span style="color:#FF6000;">
                                            {{"Not assigned"}}
                                            </span>
                                        @endif
                                    </td>

                                    <td>{{date("d-m-Y H:i", strtotime($pt->start_time))}} # {{$pt->estimated_time}}</td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                    <hr>
                    </hr>
                </div>
            </section>
        @endif
        </div>
    </div>
    <div class="row">
        <div class="col-md-6">
            <section class="panel tasks-widget">
                <header class="panel-heading">
                    <b>Pending Tasks</b>
                </header>
                <div class="table-responsive project-list">
                    @if(count($tasks) > 0)
                    <table class="table table-striped">
                        @foreach($tasks as $tsk)
                            <tr>
                                <td></td>
                                <td>
                                    <span class="cls-assign" data-task_id="{{$tsk->id}}" data-toggle="modal" data-target=".bs-example-modal-lg" aria-hidden="true" style="cursor:pointer;">
                                        <b>{{ substr($tsk->name, 0, 30)}}</b><br>
                                        <b style="color:red"> 
                                            @if($tsk->proj_id != 0)
                                                {{ $tsk->proj_name->name }}
                                            @else
                                                {{ $tsk->company_name->name }}
                                            @endif 
                                        </b>&nbsp;&nbsp;&nbsp; <i class="fa fa-user" aria-hidden="true"></i> {{ $tsk->user->name }}&nbsp;&nbsp;&nbsp; <i class="fa fa-clock-o" aria-hidden="true"></i> {{ date("F jS", strtotime($tsk->dead_line)) }}
                                    </span>
                                </td>
                                <td>
                                    @if($tsk->status == 1)
                                        <a href="{{url("user/change_task_status/$tsk->id")}}">
                                            <i style="color:red;font-size: 18px;" class="fa fa-square-o" aria-hidden="true"></i>
                                        </a><br>
                                        <span style="color:red;font-size: 12px;">PENDING</span>
                                    @elseif($tsk->status == 6)
                                        <a href="{{url("user/change_task_status/$tsk->id")}}">
                                            <i style="color:#398439;font-size: 18px;" class="fa fa-check-square-o" aria-hidden="true"></i>
                                        </a><br>
                                        <span style="color:#398439;font-size: 12px;">DONE</span>
                                    @elseif($tsk->status == 2)
                                        <a href="{{url("user/change_task_status/$tsk->id")}}">
                                            <i style="color:#d58512;font-size: 18px;" class="fa fa-square-o" aria-hidden="true"></i>
                                        </a><br>
                                        <span style="color:#d58512;font-size: 12px;">IN PROGRESS</span>
                                    @endif
                                </td>
                            </tr>
                        @endforeach
                    </table>
                    @endif
                </div>
            </section>
        </div>
        <div class="col-md-6">
            <section class="panel tasks-widget">
                <header class="panel-heading">
                    Projects you assigned
                </header>
                <div class="table-responsive project-list">
                    <table class="table table-striped">
                        <thead>
                            <tr>
                                <th colspan="2">
                                    Project Name
                                </th>
                                <th>
                                    Assigned By
                                </th>
                                <th>
                                    Description
                                </th>
                                {{-- 
                                <th>
                                    Action
                                </th> 
                                --}}
                            </tr>
                        </thead>
                        <tbody>
                        @if (session('data'))
                            <div class="alert alert-success">
                                {{ session('data') }}
                            </div>
                        @endif
                        @if(!empty($projj))
                            @foreach($projj as $proj)
                                <tr>
                                    <td>
                                        <div class="task-checkbox">
                                            <input class="flat-grey" type="checkbox"/>
                                        </div>
                                    </td>
                                    <td>
                                        <a href="{{url("user/uproj_inprocess_pending_task/$proj->proj_id")}}">
                                            {{$proj->proj_name->name}}
                                        </a>
                                        <span class="cls-show-time">
                                            @if(count($proj->proj_name->tasks) > 0)
                                                <?php 
                                                    $pend = 0;
                                                    $donee = 0;
                                                    $not_assign = 0;
                                                ?>

                                                @foreach($proj->proj_name->tasks as $pt)
                                                    @if($pt->user_id == 0)
                                                        <?php $not_assign++; ?>
                                                    @elseif($pt->status < 6 && $pt->user_id == $login_user_id)
                                                        <?php $pend++; ?>
                                                    @elseif($pt->status == 6 && $pt->user_id == $login_user_id)
                                                        <?php $donee++; ?>
                                                    @endif
                                                @endforeach
                                                <br>
                                                Not Assigned: {{$not_assign}},
                                                Pending: {{$pend}},
                                                Done: {{$donee++}}
                                            @else
                                                {{"No tasks added"}}
                                            @endif
                                        </span>
                                        <br>
                                            <small>
                                                <i class="fa fa-clock-o">
                                                </i>
                                                Assigned in {{$proj->assign_time}}
                                            </small>
                                        </br>
                                    </td>
                                    <td>
                                        {{$proj->assigned->name}}
                                    </td>
                                    <td>
                                        {{str_limit($proj->proj_name->description, $limit = 30, $end = '...')}}
                                    </td>
                                    {{-- <td>
                                        <a href="" style="color: #7dcc93;">
                                            <i class="fa fa-pencil-square-o" aria-hidden="true"></i>
                                        </a>
                                        &nbsp;  &nbsp;
                                        <a onclick="return confirm ('want to delete?')" href="{{url("admin/remove_proj/$proj->id")}}" style="color:#FD4B39;">
                                            <i class="fa fa-times" aria-hidden="true"></i>
                                        </a>
                                    </td> --}}
                                </tr>   
                            @endforeach
                        @endif
                        </tbody>
                    </table>
                    <hr>
                    </hr>
                </div>
                
            </section>
        </div>
    </div>
    <div class="row">
        <div class="col-md-6">
            <section class="panel tasks-widget">
                <header class="panel-heading">
                    Important Notes

                    <button class="btn btn-success btn-xs pull-right" data-toggle="modal" data-target="#myModal2"><i class="fa fa-plus-circle" data-toggle="modal" data-target="#myModal2" aria-hidden="true"></i> Add Note</button>
                </header>
                <div class="table-responsive project-list">
                    <table class="table table-striped">
                        <thead>
                            <tr>
                                <th>
                                    Note
                                </th>
                                <th>
                                    Date Time
                                </th>
                                <th>
                                    Status
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                            @if(!empty($notes))
                            @foreach($notes as $nott)
                                <tr>
                                    <td>{{$nott->note}}</td>
                                    <td>{{$nott->time}}</td>
                                    <td style="cursor:pointer;">
                                        @if($nott->status > 0)
                                            <a href="{{url("user/change_note_status/$nott->id")}}">
                                                <i style="color:green;" class="fa fa-check-square-o" aria-hidden="true"></i>
                                            </a>
                                        @else
                                            <a href="{{url("user/change_note_status/$nott->id")}}">
                                                <i class="fa fa-square-o" aria-hidden="true"></i>
                                            </a>
                                        @endif
                                        &nbsp; &nbsp;
                                        <a onclick="return confirm ('want to delete?')" href="{{url("user/remove_note/$nott->id")}}">
                                            <i style="color:red" class="fa fa-times" aria-hidden="true"></i>
                                        </a>
                                    </td>
                                </tr>
                            @endforeach
                            @endif
                        </tbody>
                    </table>
                    <hr>
                    </hr>
                </div>
            </section>
        </div>
    </div>
</section>
<!-- row end -->
<!-- /.content -->

<!-- Add note modal -->
<div class="modal fade" id="myModal2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
     <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="gridSystemModalLabel">Add User</h4>
          </div>
          <div class="modal-body">

            <form role="form" action="{{url('user/add_note')}}" method="POST">
                {!! csrf_field() !!}
                <div class="form-group">
                    <label for="Add new Project">
                        Note
                    </label>
                    <textarea name="note" rows="5" class="form-control" id="new-project" placeholder="Write down your important notes..." ></textarea>
                </div>
                <div class="form-group">
                    <label for="Add new Project">
                        Select Date Time
                    </label>
                    <input name="date_time" class="form-control" id="datetimepicker" type="text">
                </div>
                <div class="form-group">
                    <button class="btn btn-info" type="submit">
                    Submit
                    </button>
                </div>
            </form> 
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          </div> 
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>
<!-- Modal assign task -->
<div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="gridSystemModalLabel">Assign Task</h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-md-7">
                        <b>Assinged users:</b> 
                        <div class="assinged-users">
                            
                        </div>
                    </div>
                    <div class="col-md-5">
                    

                        <input type="hidden" name="id_task_id" id="id_task_id" value="">
                        
                        <div class="form-group">
                            <label for="Add new Project">
                                User
                            </label>
                            <select name="user_id" id="id_user_id" class="form-control cls-proj-user">
                                
                            </select>
                        </div>

                        <div class="form-group">
                            <label for="Add new Project">
                                Estimated Time
                            </label>
                            <br>
                            <input type="text" id="id_estimated_time" name="estimated_time" class="form-control">
                            
                        </div>

                        <div class="form-group">
                            <button class="btn btn-primary assign-task-user">Assign</button>
                        </div>
                    
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-12">
                        <div style="margin-bottom:10px;">
                            <b>Details:</b> <span class="cls-task-details"></span>
                        </div>
                        <!-- Add Comments ... -->
                        <textarea class="form-control" name="comment_txt" id="comment_txt" cols="" rows="2" placeholder="Post comment here"></textarea>
                        <button class="btn btn-success add-task-comment" style="margin-top:5px;">Post Comment</button>
                        <button class="btn btn-success add-task-comment-doc" style="margin-top:5px;" data-toggle="modal" data-target="#add_doc_task_modal">Attach Documnet</button>
                        <br><br>
                        <b>Comments:</b><br>
                        <div class="comments-cls"  style="height:400px;overflow:scroll;">
                            
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-12">
                    <br/>
                    <b>Activity Logs:</b><br>
                        <div class="show-logs" style="height:150px;overflow:scroll;">
                            
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <div class="row">
                    <div class="col-md-10">
                        Change status to: 
                        <button data-id='2' class="btn btn-primary cls-change-created btn-xs">In-process</button>
                        <span class="show-change-btn">
                            <button data-id='3' class="btn btn-primary cls-change-created btn-xs">Completed</button>
                            <button data-id='4' class="btn btn-primary cls-change-created btn-xs">Checking</button>
                            <button data-id='5' class="btn btn-primary cls-change-created btn-xs">Bug-fixing</button>
                            <button data-id='6' class="btn btn-primary cls-change-created btn-xs">Done</button>    
                        </span>
                    </div>
                    <div class="col-md-2">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- Assign task finished -->
<!--  Assign modal -->
@endsection

@section('script')
  
    <script type="text/javascript">
        //jQuery('#datetimepicker').datetimepicker();

        
    </script>
<script src="{{URL::asset('assets/dropzone.js')}}" type="text/javascript"></script>
<script src="{{URL::asset('assets/ckeditor/ckeditor.js')}}"></script>
<link rel="stylesheet" href="{{ URL::asset('assets/css/jquery.datetimepicker.css') }}">
<script type="text/javascript" src="{{ URL::asset('assets/js/jquery.datetimepicker.full.min.js') }}"></script>
<script type="text/javascript">

    function datePicker1(){
        $( "#id_estimated_time" ).datetimepicker({
        });
    }$('.bs-example-modal-lg').on('shown.bs.modal', function () {
        datePicker1();
    });
    $(document).ready(function(){
        $( "#test5" ).datetimepicker({});
        $('.cls-group').hide();    
        $('.cls-user').hide();  
        $('.show-change-btn').hide();
        jQuery('#datetimepicker').datetimepicker({
          format:'Y-m-d H:i:s',
          lang:'ru'
        }); 

    });

    $('.cls-proj').change(function(event){
        
        var proj_id = $(this).val();
        
        $.ajax({
            url: "{{url("user/change_tast_by_proj")}}",
            type: 'POST',
            data: { proj_id:proj_id},
        })
        .done(function(data){
            $('.cls-tasks').html(data);
        });
        
    });

    $('#id-group-user').change(function(event)
    {    
        var chk_id = $(this).val();

        if(chk_id == 1){
            $('.cls-group').show();    
            $('.cls-user').hide();        
        }
        else if(chk_id == 2){
            $('.cls-group').hide();    
            $('.cls-user').show();
        }
        else{
            $('.cls-group').hide();    
            $('.cls-user').hide();
        }
    });

    $('.cls-assign').click(function(event)
    {   
        var proj_id = $('.cls-project-id').val();
        var task_id = $(this).data('task_id');
        $('#id_task_id').val(task_id);

        $.ajax({
                url: "{{url('user/user_list_for_task_assign')}}",
                type: 'POST',
                data: {proj_id: proj_id, task_id: task_id},
            })
            .done(function(data) {

                var status = data[4];
                if(status > 1)
                {
                    $('.show-change-btn').show();
                }
                else{
                    $('.show-change-btn').hide();
                }

                $('.cls-proj-user').html(data[0]);
                $('.assinged-users').html(data[1]);
                $('.show-logs').html(data[2]);
                $('.comments-cls').html(data[3]);
                $('.cls-task-details').html(data[5]);
            });
                
    });

    $('.assign-task-user').click(function(event)
    {
        var id_estimated_time = $('#id_estimated_time').val();
        
        var id_user_id = $('#id_user_id').val();
        var id_task_id = $('#id_task_id').val();
        
        if(id_estimated_time !=''){
            $.ajax({
                url: "{{url('user/task_assign_to_user')}}",
                type: 'POST',
                data: {id_estimated_time: id_estimated_time,id_user_id: id_user_id,id_task_id: id_task_id},
            })
            .done(function(data) {
                $('.assinged-users').html(data);
            });
        }
        else{
            alert('Please define time estimate!');
        }
        
    });


    $('.assinged-users').on('click','.remove-task-assign-user',function(){

        var id = $(this).prev('input').val();
        var task_id = $('#id_task_id').val();

        $.ajax({
            url: "{{url('user/remove_assign_task_user')}}",
            type: 'POST',
            data: {id: id,task_id: task_id},
        })
        .done(function(data) {
            $('.assinged-users').html(data);
        });

    });

    
    $('.add-task-comment').click(function(event) {
        
        var comment = $('#comment_txt').val();
        var task_id = $('#id_task_id').val();

        if(comment.length > 0){
            $.ajax({
                url: "{{url('user/add_task_comment')}}",
                type: 'POST',
                data: {comment: comment,task_id: task_id},
            })
            .done(function(data) {
                
                $('.comments-cls').html(data);
            });
        }
        else{
            alert('Please fill-up comment field.');
        }
        
    });

    $('.comments-cls').on('click','.edit_comment',function(){
        
        var id = $(this).data('id');
        
        $.ajax({
            url: '{{url('user/edit_comment_form')}}',
            type: 'POST',
            data: {id: id},
        })
        .done(function(data) {
            $('.modal-edit').modal('show');
            $('.update-comment').val(data['comment']);
            $('.update-comment-id').val(data['id']);
        });
        
    });

    $('.comments-cls').on('click','.remove_comment',function(){
        
        var id = $(this).data('id');
        var task_id = $('#id_task_id').val();

        $.ajax({
            url: "{{url('user/remove_comment')}}",
            type: 'POST',
            data: {id: id,task_id: task_id},
        })
        .done(function(data) {
            $('.comments-cls').html(data);
        }); 
    });

    $('.save-updated-comment').click(function(event) {
        
        var id = $('.update-comment-id').val();
        var comment = $('.update-comment').val();
        var task_id = $('#id_task_id').val();

        $.ajax({
            url: '{{url('user/save_update_comment')}}',
            type: 'POST',
            data: {id: id,comment: comment,task_id: task_id},
        })
        .done(function(data) {
            $('.modal-edit').modal('hide');
            $('.comments-cls').html(data);
            $('.update-comment').val('');
            alert('Data updated successfully!');
        });
        
    });

    $('.cls-change-created').click(function(event)
    {
        if(confirm("Are you sure you want to change status ?")) 
        {
            var task_id = $('#id_task_id').val();
            var status = $(this).data('id');
            
            $.ajax({
                url: "{{url('user/change_to_process')}}",
                type: 'POST',
                data: {task_id: task_id, status: status},
            })
            .done(function(data) {
                
                if(data == 'Yes'){
                    alert('Status Changed !!');
                    location.reload();
                }
                else{
                    alert('Status Not Changed !!');
                }
            });
            
        }
    });

    $('.upload-cls').click(function(event){

        var id = $('.cls-project-id').val();
        $('.cls-proj-id').val(id);
    });

    $('.add-task-comment-doc').click(function(event){

        var task_id = $('#id_task_id').val();
        $('.cls-task-doc-id').val(task_id);
    });

    $('.attatch-close-btn').click(function(event) {
        
        location.reload();
    });

    $('.attatchment-doc-comment').click(function(event) {
        
        //this event only for fetch the comments after close 
        //attatchemnt dropzone.
        var comment = '';
        var task_id = $('#id_task_id').val();

        $.ajax({
            url: "{{url('user/add_task_comment')}}",
            type: 'POST',
            data: {comment: comment,task_id: task_id},
        })
        .done(function(data){
            
            $('.comments-cls').html(data);
        });        
    });

</script>
@endsection
