@extends('layouts.master')

@section('content')
<section class="content">
	<div class="row">
		<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
		<div class="panel panel-default">
			<div class="panel-heading">
				<?php 
				$priority='';
				switch ($ticket->priority) {
					case '1':
						$priority="<span class='label label-default'>Low</span>";
						break;
					case '2':
						$priority="<span class='label label-info'>Medium</span>";
						break;

					case '3':
						$priority="<span class='label label-warning'>High</span>";
						break;

					case '4':
						$priority="<span class='label label-danger'>Urgent</span>";
						break;
					
					default:
						
						break;
				}

				?>
				<span class="label label-info" style="font-size: 14px !important;">{{ @$ticket->user->company_data?$ticket->user->company_data->name:'' }}</span>
				<h3 class="panel-title"  style="margin-top: 10px;">{{ $ticket->title }} {!! $priority !!}</h3>
				<h6>
					Opened by <strong>{{ $ticket->created_user?$ticket->created_user->name:'' }}</strong> on <strong>{{ date('m d, Y',strtotime($ticket->created_at)) }} </strong> at <strong>{{  date('h:m A',strtotime($ticket->created_at)) }}</strong> for <strong>{{ $ticket->service?$ticket->service->name:'' }}</strong>
					

					<div class='pull-right'>
					status : 
					<select id="status" {{ \Auth::user()->utype==3?'disabled':'' }}>
						<option {{ $ticket->status==0?'selected':'' }} value="0">Initiated</option>
						<option {{ $ticket->status==1?'selected':'' }} value="1">In Process</option>
						<option {{ $ticket->status==2?'selected':'' }} value="2">Onhold</option>
						<option {{ $ticket->status==3?'selected':'' }} value="3">Closed</option>
					</select>

					<input type="hidden" name="hdn_ticket_id" value="{{ $ticket->id }}">
					</div>
				</h6>
				<div class="clearfix"></div>
				@if( !empty( $ticket->ticket_document )) 
					<div class="form-group">
						Download document: <a target="_blank" href="{{ url('public/ticket_document/'.$ticket->ticket_document) }}">{{ $ticket->ticket_document }}</a>						
					</div>
				@endif

			</div>
			<div class="panel-body">
				{{ $ticket->details }}

			<div id="comment">
			<div id="comment-box">
				
			</div>
			<hr>
				<textarea name="comment" id="txt_comment" class='form-control'></textarea> <br>
				<button type="button" id='btnComment' data-ticket='{{ $ticket->id }}' class="btn btn-defaut"><i class="fa fa-comment"></i> Reply	</button>
			<hr>
			</div>
			</div>
		</div>
		</div>
	</div>
</section>

@stop

@section('style')
<style>
	#comment{
		margin-top: 15px;
	}
	.comment-item{
		padding: 10px;
	}
	.comment-head{
		font-size: 13px;
		padding: 15px;
		background: #f5f5f5;
	}
	.comment-body{
		padding: 10px;
	}

	.label{
	 padding: 0.2em 0.3em .2em !important;
	 font-size: 60% !important;
	}
</style>
@stop

@section('script')
<script type="text/javascript">
$(document).ready(function() {

	get_comments();

	$('#btnComment').click(function(){
		var ticket=$(this).data('ticket');
		var comment=$('#txt_comment').val();
		var token='{{ csrf_token() }}';

		if(comment.length>0){
			$.ajax({
				url: '{{ url('ticket/comment/save') }}',
				type: 'POST',
				data: {ticket: ticket,comment:comment,_token:token}
			})
			.done(function(res) {
				get_comments();
				$('#txt_comment').val('');
			});
		}
	});

	@if(\Auth::user()->utype!=3)
	$('#status').change(function(){

		var st=$(this).val();
		var token='{{ csrf_token() }}';

		var conf=confirm('Are you sure to change status ??');

		if(!conf) return false;

		if(st.length>0)
		{
			var ticket_id=$('input[name="hdn_ticket_id"]').val();
			$.ajax({
				url: '{{ url('ticket/status/change') }}',
				type: 'POST',
				data: {status: st,ticket_id:ticket_id,_token:token}
			})
			.done(function(res) {
				if(res=="ok")
				{
					alert("status changed successfully !");
				}
			});
		}

	});
	@endif

});

function get_comments()
{
	$.ajax({
		url: '{{ url('ticket/comments') }}/{{ $ticket->id }}',
		type: 'GET'
	})
	.done(function(res) {
		$('#comment-box').html(res);
	});
	
}
</script>
@stop