<div class="box-top-area">
  <div class="box-heading-row d-flex justify-content-between align-items-center">
    <p class="heading color-dark-green">Administrate</p>
    <div class="close-icon d-flex justify-content-center align-items-center"><img class="pointer"
        src="{{ asset('images/icons/Frame.png') }}" alt="Close"></div>
  </div>
  <div class="button-row d-flex justify-content-between  align-items-center">
    <button class="action-btn status-btn @if($ticket->status == 'open') active @endif" data-status="open">Open</button>
    <button class="action-btn status-btn @if($ticket->status == 'waiting') active @endif" data-status="waiting">Waiting</button>
    <button class="action-btn status-btn @if($ticket->status == 'done') active @endif" data-status="done">Done</button>
  </div>
</div>
<div class="box-body-area">
  <ul class="inbox-chat-options">
    <li class="single-option d-flex justify-content-between">
      <p>Assign <span>(Assign to me)</span></p>
      <p><img src="{{ asset('images/icons/Frame (1).png') }}" alt="Chevron Right"></p>
    </li>
    <li class="single-option d-flex justify-content-between">
      <p>Priority <span>(Choose Priority)</span></p>
      <p><img src="{{ asset('images/icons/Frame (1).png') }}" alt="Chevron Right"></p>
    </li>
    <li class="single-option d-flex justify-content-between">
      <p>Tags <span>(Choose Tags)</span></p>
      <p><img src="{{ asset('images/icons/+.png') }}" alt="Chevron Right"></p>
    </li>
    <li class="single-option d-flex justify-content-between">
      <div class="accordion w-100 border-0" id="accordionExample">
        <div class="accordion-item custom-accordion">
          <div data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true"
            aria-controls="collapseOne" class="custom-accordion-item d-flex justify-content-between">
            <p>Comments
              <span>(Write Comments)</span>
            </p>
            <p><img src="{{ asset('images/icons/Frame (1).png') }}" alt="Chevron Right"></p>
          </div>
          <div id="collapseOne" class="accordion-collapse collapse show" aria-labelledby="headingOne"
            data-bs-parent="#accordionExample">
            <div class="accordion-body accordion-body--custom">
              <div class="body-content">
                <textarea rows="7" placeholder="Your Message"
                  class="form-control input-reply-textarea input-comment-textarea"></textarea>
                <button class="ui button bg-light-green-color mt-4 add-comment-btn color-light">
                  Add Your Comment
                </button>
              </div>
              @foreach($ticket->comments as $comment)
              <div class="response-comment d-flex">
                <div class="response-comment-user-image">
                  <img src="{{ asset('images/Rectangle 4.png') }}" alt="">
                </div>
                <div class="response-content">
                  <div class="top-content-row d-flex justify-content-between align-items-center">
                    <div class="left-area">
                      <p class="response-comment-time">{{ \Carbon\Carbon::parse($comment->created_at)->format('h:i A') }}</p>
                      <p class="response-comment-user-name">{{ $comment->user->name }}</p>
                    </div>
                    <div class="right-area d-flex">
                      <button
                        class="ui button comment--btn bg-dark-green-color color-light comment-edit-btn ">
                        <i class="edit outline icon"></i>
                      </button>
                      <button
                        class="ui button comment--btn bg-light-green-color color-light comment-delete-btn">
                        <i class="trash alternate outline icon"></i>
                      </button>
                    </div>
                  </div>
                  <p>{{$comment->comment}}</p>
                </div>
              </div>
              @endforeach
            </div>
          </div>
        </div>


      </div>
    </li>
  </ul>
</div>

<!--store comment-->
<script>
    $(document).ready(function() {
      $('.add-comment-btn').on('click', function() {
        var ticketId = $('.chat-detail-item.active').data('ticket-id');
        var comment = $('.input-comment-textarea').val();
    
        if (comment === '') {
          alert('Comment cannot be empty');
          return;
        }
    
        $.ajax({
          url: '/store-comment',
          method: 'POST',
          data: {
            ticket_id: ticketId,
            comment: comment,
            _token: '{{ csrf_token() }}'
          },
          success: function(response) {
            $('.accordion-body--custom').prepend(response);
            toastr.success("Comment Added Successfully");
            $('.input-comment-textarea').val('');
          },
          error: function(error) {
            console.error('Error adding comment:', error);
          }
        });
      });
    });

</script>
<!--change ticket status-->
<script>
  $(document).ready(function() {
    // Click event for status buttons
    $('.status-btn').on('click', function() {
      var newStatus = $(this).data('status');

      // Update UI immediately
      $('.status-btn').removeClass('active');
      $(this).addClass('active');

      // Optionally, update backend via AJAX
      updateTicketStatus(newStatus);
    });

    // Function to update ticket status via AJAX
    function updateTicketStatus(newStatus) {
      // Example AJAX request
      $.ajax({
        url: 'update-ticket-status/{{ $ticket->id }}', // Replace with your route
        method: 'POST',
        data: {
          status: newStatus,
          _token: '{{ csrf_token() }}'
        },
        success: function(response) {
          // Handle success response if needed
          toastr.success(response.message);
        },
        error: function(error) {
          console.error('Error updating ticket status:', error);
          // Optionally handle error response
        }
      });
    }
  });
</script>