@extends('layouts.app')

@include('tickets.modals')
@section('content')
<div class="row card text-right">
    <div class="my-3 mr-5">
        @if ($ticket->status == 'PENDING')
        <button class="btn btn-dark mx-3" data-toggle="modal"
        data-target="#editTicketModal">
            <i class="fas fa-pen"></i> Edit
        </button>
        {{-- <a href="{{ route('tickets.update',['ticket_id'=>$ticket->id]) }}" class="btn btn-dark mx-3">
            <i class="fas fa-pen"></i> Edit
        </a> --}}
            <a href="{{ route('tickets.open',['ticket_id'=>$ticket->id]) }}" class="btn btn-info">Open</a>
        @elseif ($ticket->status == 'OPENED')
            <a class="btn btn-success" href="{{ route('tickets.confirm.close',['ticket_id'=>$ticket->id]) }}" >Close</a>
            <a class="btn btn-warning" href="{{ route('tickets.confirm.escalate',['ticket_id'=>$ticket->id]) }}">Escalate</a>
        @elseif ($ticket->status == 'ESCALATED')
            <a class="btn btn-success" href="{{ route('tickets.confirm.close',['ticket_id'=>$ticket->id]) }}" >Close</a>
            <a class="btn btn-warning" href="{{ route('tickets.confirm.escalate',['ticket_id'=>$ticket->id]) }}">Escalate</a>
        @elseif ($ticket->status == 'CLOSED')
            <a class="btn btn-info" href="{{ route('tickets.confirm.validate',['ticket_id'=>$ticket->id]) }}">Validate</a>
        @else

        @endif
    </div>
</div>
<div class="row">
    <div class="col-xs-12 col-sm-12 col-md-4 col-lg-4">
      <div class="card">
        <div class="body">
          <div id="mail-nav">
            <button type="button" class="alert @if ($ticket->type=='ISSUE') alert-danger @else alert-info @endif waves-effect btn-compose m-b-15"><small>Type: </small> {{ $ticket->type }}</button>
            @if ($ticket->priority == 1)
                <button type="button" class="alert alert-danger waves-effect btn-compose m-b-15"><small>Priority: </small> CRITICAL</button>
            @elseif ($ticket->priority == 2)
                <button type="button" class="alert alert-warning waves-effect btn-compose m-b-15"><small>Priority: </small> MAJOR</button>
            @elseif ($ticket->priority == 3)
                <button type="button" class="alert alert-dark waves-effect btn-compose m-b-15"><small>Priority: </small> MINOR</button>
            @else
                  <button type="button" class="alert alert-info waves-effect btn-compose m-b-15"><small>Priority: </small> WARNING</button>
              @endif
            <ul class="" id="mail-folders">
              <li>
                <p>Created By: {{ $ticket->creator?->first_name.' '.$ticket->creator?->last_name ?? 'N/A' }}</p>
              </li>
              <li>
                <p>Created At: {{ date('M j, Y g:i A',strtotime($ticket->created_at)) }}</p>
              </li>
              <li>
                <p>Assigned To: {{ $ticket->assignedto?->first_name.' '.$ticket->assignedto?->last_name ?? 'N/A' }}</p>
              </li>
              <li>
                <p>Ecalated By: {{ $ticket->escalator?->first_name.' '.$ticket->escalator?->last_name ?? 'N/A' }}</p>
              </li>
              <li>
                <p>Ecalated To: {{ $ticket->escalatedto?->first_name.' '.$ticket->escalatedto?->last_name ?? 'N/A' }}</p>
              </li>
              <li>
                <p>Ecalated At: {{ $ticket->escalated_at?date('M j, Y g:i A',strtotime($ticket->escalated_at)):'N/A' }}</p>
              </li>
              <li>
                <p>Closed By: {{ $ticket->closedby?->first_name.' '.$ticket->closedby?->last_name ?? 'N/A' }}</p>
              </li>
              <li>
                <p>Closed At: {{ $ticket->closed_at?date('M j, Y g:i A',strtotime($ticket->closed_at)):'N/A' }}</p>
              </li>
              <li>
                <p>Validated By: {{ $ticket->validator?->first_name.' '.$ticket->validator?->last_name ?? 'N/A' }}</p>
              </li>
              <li>
                <p>Validated At: {{ $ticket->validated_at?date('M j, Y g:i A',strtotime($ticket->validated_at)):'N/A' }}</p>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </div>
    <div class="col-xs-12 col-sm-12 col-md-8 col-lg-8">
      <div class="card">
        <div class="boxs mail_listing">
          <div class="inbox-center table-responsive">
            <table class="table table-hover">
              <thead>
                <tr>
                  <th colspan="1">
                    <h5 class="ml-3 inbox-header">
                      {{ $ticket->title }}
                    </h5>
                  </th>
                  <th colspan="1">
                    <div class="inbox-header">
                       Status: <button type="button" class="btn btn-primary waves-effect btn-compose m-b-15 mt-3">{{ $ticket->status }}</button>
                    </div>
                  </th>
                </tr>
              </thead>
            </table>
          </div>
          <div class="row">
            <div class="col-12">
                <div class="bg-light mt-3 mb-3 mx-3 p-3 border border-secondary rounded">
                   <h6 class="md-3">Description</h6>
                    <p>{{ $ticket->description }}</p>
                </div>
            </div>
          </div>
          <div class="row">
            <div class="col-12">
                <div class="bg-light mt-3 mb-3 mx-3 p-3 border border-secondary rounded">
                   <h6 class="md-3">Ecalator's comment</h6>
                    <p>{{ $ticket->escalator_comment ?? 'N/A' }}</p>
                </div>
            </div>
          </div>
          <div class="row">
            <div class="col-12">
                <div class="bg-light mt-3 mb-3 mx-3 p-3 border border-secondary rounded">
                   <h6 class="md-3">Closure comment</h6>
                    <p>{{ $ticket->closure_comment ?? 'N/A' }}</p>
                </div>
            </div>
            <div class="col-12">
                <div class="bg-light mt-3 mb-3 mx-3 p-3 border border-secondary rounded">
                   <h6 class="md-3">Validator's comment</h6>
                    <p>{{ $ticket->validator_comment ?? 'N/A' }}</p>
                </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
@endsection

<div class="modal fade " id="editTicketModal" tabindex="-1" role="dialog"
aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
    <div class="modal-dialog modal-lg modal-dialog-centered" role="document">
        <div class="modal-content">
        <div class="modal-header mb-5 bg-secondary">
            <h5 class="modal-title text-dark" id="exampleModalCenterTitle">Update Ticket</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
            </button>
        </div>
        <form action="{{ route('tickets.update') }}" method="POST">
            @csrf
            <input name="ticket_id" value="{{ $ticket->id }}" hidden>
            <input name="user_id" value="{{ auth()->user()->id }}" hidden>
            <div class="form-group row mb-4">
              <label class="col-form-label text-md-right col-12 col-md-3 col-lg-3" for="title">Title</label>
              <div class="col-sm-12 col-md-7">
                <input type="text" name="title" value="{{ $ticket->title }}" class="form-control">
                @error('title')
                <span class="text-danger">{{ $message }}</span>
             @enderror
              </div>
            </div>
            <div class="form-group row mb-4">
              <label class="col-form-label text-md-right col-12 col-md-3 col-lg-3">Type</label>
              <div class="col-sm-12 col-md-7">
                <select name="type" class="form-control selectric">
                  <option value="ISSUE" @if ($ticket->type == 'ISSUE') selected @endif>Issue</option>
                  <option value="REQUEST" @if ($ticket->type == 'REQUEST') selected @endif>Request</option>
                  <option value="ALERT" @if ($ticket->type == 'ALERT') selected @endif>Alert</option>
                </select>
                @error('type')
                  <span class="text-danger">{{ $message }}</span>
                @enderror
              </div>
            </div>
            <div class="form-group row mb-4">
              <label class="col-form-label text-md-right col-12 col-md-3 col-lg-3">Description</label>
              <div class="col-sm-12 col-md-7">
                <textarea name="description" value="{{ $ticket->description }}" id="current_description" class="form-control" cols="58" rows="5">{{ $ticket->description }}</textarea>
                @error('description')
                  <span class="text-danger">{{ $message }}</span>
                @enderror
              </div>
            </div>
            <div class="form-group row mb-4">
              <label class="col-form-label text-md-right col-12 col-md-3 col-lg-3">Priority</label>
              <div class="col-sm-12 col-md-7">
                <select name="priority" class="form-control selectric">
                  <option value="1"  @if ($ticket->priority == 1) selected @endif>CRITICAL</option>
                  <option value="2" @if ($ticket->priority == 2) selected @endif>MAJOR</option>
                  <option value="3" @if ($ticket->priority == 3) selected @endif>MINOR</option>
                    <option value="3" @if ($ticket->priority == 3) selected @endif>WARNING</option>
                </select>
                @error('priority')
                  <span class="text-danger">{{ $message }}</span>
                @enderror
              </div>
            </div>
            <div class="form-group row mb-4">
              <label class="col-form-label text-md-right col-12 col-md-3 col-lg-3">Assign To</label>
              <div class="col-sm-12 col-md-7">
                <select name="assigned_to" class="form-control selectric">
                  <option value="">--select--</option>
                  @foreach ($users as $user)
                    <option value="{{ $user->id }}" @if ($ticket->assignedto?->id == $user->id) selected @endif>{{  $user->first_name.' - ('. $user->role->title .')' }}</option>
                  @endforeach
                </select>
                @error('assigned_to')
                  <span class="text-danger">{{ $message }}</span>
                @enderror
              </div>
            </div>
            <div class="modal-footer bg-whitesmoke br">
                <button type="submit" class="btn btn-primary">Submit</button>
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
            </div>
        </form>
        </div>
    </div>
</div>
<script>
    $('#current_description').val({{ $ticket->description }})
</script>
@if ($errors->has('title') || $errors->has('type') || $errors->has('description')|| $errors->has('priority')|| $errors->has('assigned_to'))
<script>
setTimeout(() => {
$('#newTicketModal').modal('show');
}, 500);
</script>
@endif
