@extends('admin.layouts.master')

@section('title')
    Projects
@stop

@section('style')

<style>
    .modal-content{
      z-index:2000 !important;
    }

    .modal-backdrop {
        z-index: 1040 !important;
    }
    .modal-dialog {
        margin: 2px auto;
        z-index: 1100 !important;
    }
</style>

@stop

@section('content')
    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <section class="content-header">
            <h1>
                <span class="fa fa-users"></span> Projects
            </h1>
            <ol class="breadcrumb">
                <li><a href="#"><i class="fa fa-users"></i> Admin Panel</a></li>
                <li class="active">Projects</li>
            </ol>
        </section>

        <!-- Main content -->
        <section class="content" style=" padding-bottom: 100%;">
            <div class="page-header">
            {{--{{route('project.new')}}--}}
                <a href="#" id="btnAdd" name="btnAdd" class="btn btn-primary"><i class="fa fa-plus-circle"></i> New Project</a>
            </div>

            <table class="table table-hover" id="user_table" width="100%">
                <thead>
                <tr style="background: grey ;color:#fff; font-weight: bold">
                    <td>ID</td>
                    <td>Code</td>
                    <td>Project Name</td>
                    <td>Developer</td>
                    <td>Created At</td>
                    <td>Updated At</td>
                    <td>Actions</td>
                </tr>
                </thead>
                @foreach($prjs as $prj)
                    <tr >
                        <td>{{$prj->id}}</td>
                        <td>{{$prj->code ?? '-'}}</td>
                        <td>{{$prj->name ?? '-'}}</td>
                        <td>{{$prj->developer->name ?? '-' }}</td>
                        <td>{{date("d-M-Y", strtotime($prj->created_at))}}</td>
                        <td>@if ($prj->updated_at == null)
                                NULL
                            @else
                                {{date("d-M-Y", strtotime($prj->updated_at))}}
                            @endif
                        </td>
                        <td class="btn btn-default ">
                            <a href="#" data-toggle="modal" data-target="#e{{$prj->id}}" class="btn btn-primary btn-xs"><i class="fa fa-edit"></i></a>

                            <!-- Edit Modal -->
                            <div class="modal fade" id="e{{$prj->id}}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                                <div class="modal-dialog" role="document">
                                    <form method="post" action="{{route('project.update')}}">
                                        <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="myModalLabel">Edit Project info for <b>"{{$prj->name}}"</b></h4>
                                            </div>
                                            <div class="modal-body text-left">
                                                <input type="hidden" name="id" value="{{$prj->id}}">
                                                <div class="form-group has-feedback @if($errors->has('code')) has-error @endif">
                                                    <label for="code" class="control-label">Project Code</label>
                                                    <input value="{{$prj->code}}" type="text" name="code" id="code" class="form-control">
                                                    <span class="glyphicon glyphicon-book form-control-feedback"></span>
                                                    @if($errors->has('code')) <span class="help-block">{{$errors->first('code')}}</span> @endif
                                                </div>
                                                <div class="form-group has-feedback @if($errors->has('name')) has-error @endif">
                                                    <label for="name" class="control-label">Project Name</label>
                                                    <input value="{{$prj->name}}" type="text" name="name" id="name" class="form-control">
                                                    <span class="glyphicon glyphicon-user form-control-feedback"></span>
                                                    @if($errors->has('name')) <span class="help-block">{{$errors->first('name')}}</span> @endif
                                                </div>
                                                <div class="form-group has-feedback @if($errors->has('developer_id')) has-error @endif">
                                                    <label for="developer_id" class="control-label">Developer</label>
                                                    <select name="developer_id" id="developer_id" class="form-control">
                                                        <option value="">Select Developer</option>
                                                        @foreach($devs as $dev)
                                                            @if ($prj->developer_id == $dev->id)
                                                                <option value="{{$dev->id}}" selected="selected">{{$dev->name}}</option>
                                                            @else
                                                                <option value="{{$dev->id}}">{{$dev->name}}</option>
                                                            @endif
                                                        @endforeach
                                                    </select>
                                                    <span class="fa fa-user-md form-control-feedback"></span>
                                                    @if($errors->has('developer_id')) <span class="help-block">{{$errors->first('developer_id')}}</span> @endif
                                                </div>

                                            </div>
                                            <div class="modal-footer">
                                                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                                                <button type="submit" class="btn btn-primary">Confirm</button>
                                            </div>
                                        </div>
                                        {{csrf_field()}}
                                    </form>
                                </div>
                            </div>

                            <a href="#" data-toggle="modal" data-target="#d{{$prj->id}}" class="text-danger btn btn-danger btn-xs"><i class="fa fa-trash"></i></a>
                            <!-- Delete Modal -->
                            <div class="modal fade" id="d{{$prj->id}}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                                <div class="modal-dialog" role="document">
                                    <form method="post" action="{{route('project.delete')}}">
                                    <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="myModalLabel"><i class="fa fa-warning"></i> confirm deletion</h4>
                                        </div>
                                        <div class="modal-body text-danger">
                                            <input type="hidden" name="id" value="{{$prj->id}}">
                                            Are you sure want to delete project <b>"{{$prj->name}}"</b>
                                        </div>
                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                                            <button type="submit" class="btn btn-primary">Confirm</button>
                                        </div>

                                    </div>
                                        {{csrf_field()}}
                                    </form>
                                </div>
                            </div>
                        </td>
                    </tr>
                    @endforeach
            </table>
            @if(Session('info'))
                <div class="row">
                    <div class="col-md-6 col-md-offset-3 text-center">
                        <div class="tem alert alert-success navbar-fixed-bottom"><span class="glyphicon glyphicon-ok-circle"></span> {{Session('info')}}</div>
                    </div>
                </div>
            @endif
        </section>

        <div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
            <div class="modal-dialog" role="document">
                <form method="post" action="{{route('project.new')}}">
                    <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="myModalLabel">Create New Project</h4>
                        </div>
                        <div class="modal-body text-left">
                            <input type="hidden" name="id" value="">
                            <div class="form-group has-feedback @if($errors->has('code')) has-error @endif">
                                <label for="code" class="control-label">Project Code</label>
                                <input value="" type="text" name="code" id="code" class="form-control" required>
                                <span class="glyphicon glyphicon-book form-control-feedback"></span>
                                @if($errors->has('code')) <span class="help-block">{{$errors->first('code')}}</span> @endif
                            </div>
                            <div class="form-group has-feedback @if($errors->has('name')) has-error @endif">
                                <label for="name" class="control-label">Project Name</label>
                                <input value="" type="text" name="name" id="name" class="form-control" required>
                                <span class="glyphicon glyphicon-user form-control-feedback"></span>
                                @if($errors->has('name')) <span class="help-block">{{$errors->first('name')}}</span> @endif
                            </div>
                            <div class="form-group has-feedback @if($errors->has('developer_id')) has-error @endif">
                                <label for="developer_id" class="control-label">Developer *:</label>
                                <select name="developer_id" id="developer_id" class="form-control" >
                                    <option value="">Select Developer</option>
                                    @foreach($devs as $dev)
                                        <option value="{{$dev->id}}">{{$dev->name}}</option>
                                    @endforeach
                                </select>
                                <span class="fa form-control-feedback"></span>
                                @if($errors->has('developer_id')) <span class="help-block">{{$errors->first('developer_id')}}</span> @endif
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                            <button type="submit" class="btn btn-primary">Save</button>
                        </div>

                    </div>
                    {{csrf_field()}}
                </form>
            </div>
        </div>

    </div>
@stop

@section('script')

    <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>

    <script>
        $(document).ready(function() {
            $('#user_table').DataTable( {
                "bDestroy": true,
                /*searching: true,
                paging: true,
                lengthChange: true,
                pageLength: 10,
                info: true,
                orderable: true,*/
                "order": [[ 0, "desc" ]]
            } );
        } );

        $("#btnAdd").on("click", function(){
            //alert('ready');
            $('#addModal').modal('toggle');
            $("#addModal").appendTo("body");
        });
    </script>
@stop