@extends('layouts.master')

@section('style')
    <style>
        .activity-btn:hover{
            color: #48C2A9;
            border-color: #48C2A9;
        }
        .remarks-div {
            height: 250px;
            /*background-color: #eee;*/
            overflow-y: auto;
        }
        .activity-div {
            height: 250px;
            overflow-y: auto;
        }
        .panel-service .panel-body{
            height:350px;
            overflow-y: auto;
        }
        .bg-grey{background: #ddd;}
        .table{font-size: 12px;}
        .table-company tr th,.table-company tr td{font-size: 12px;}
        .table-company tr th{ width: 25% !important; }
        .table-company tr td{ width:auto !important; }   
        .table-service tr th,.table-service tr td{font-size: 12px;vertical-align: middle !important;}

        @media screen and (min-width: 768px){
            #modalUpload .modal-content{ top:10%;width: 30%;margin: 0 auto; }
        }
        .remarks-footer{
        	padding-top: 8px;
        	font-size: 11px;
        	color: #616A75;
        }
    </style>
@endsection

@section('content')
<!-- Main content -->
<h3 class="title">{{ $title }}</h3>
<section class="content">
    <!-- Main row -->
    <div class="row">
        <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6">
            <div class="panel panel-default">
             <div class="panel-heading">Company Info</div>
               <table class="table table-company">
                     <tr>
                         <th>
                             Company Name
                         </th>
                         <td>
                             {{$all->name}}
                             <input type="hidden" value="{{$all->id}}" id="company_id">
                         </td>
                     </tr>
                     <?php 
                     $ary_industry = [
                         1=>"Recreation",2=>"Food",3=>"Training",
                         4=>"Professionals",5=>"Industry",6=>"Agriculture",7=>"Germents",8=>"Health",9=>"Apperal",10=>"E-Commerce"
                         ];
                     ?>
                     <tr>
                         <th>Industry</th>
                         <td>
                             @foreach($ary_industry as $key=>$indus)
                                 @if($key == $all->industry)
                                     {{$indus}}
                                 @endif
                             @endforeach
                         </td>
                     </tr>
                     <tr>
                         <th>Mobile</th>
                         <td>{{$all->mob1}}, {{$all->mob2}}</td>
                     </tr>
                     <tr>
                         <th>Email</th>
                         <td>{{$all->email1}}, {{$all->email2}}</td>
                         <input type="hidden" class="hdn-email1" value="{{$all->email1}}">
                         <input type="hidden" class="hdn-email2" value="{{$all->email2}}">
                     </tr>
                     <tr>
                         <th>Website</th>
                         <td>{{$all->web}}</td>
                     </tr>
                     <tr>
                         <th>Address</th>
                         <td>{{$all->address}}</td>
                     </tr>
                     <tr>
                         <th>Remarks</th>
                         <td>{{$all->remarks}}</td>
                     </tr>
               </table>
            </div>

            <div class="panel panel-default">
                <!-- Default panel contents -->
                <div class="panel-heading">Adress Book   <button class="btn btn-success btn-xs pull-right" data-toggle="modal" data-target=".add-modal">Add Contract Person</button></div>
                <table class="table table-bordered">
                    <tr>
                        <th>Name</th>
                        <th>Designation</th>
                        <th>Mobile</th>
                        <th>Email</th>
                    </tr>
                    @foreach($all->contact_persons as $cp)
                        <tr>
                            <td>{{$cp->name}}</td>
                            <td>{{$cp->degisnation}}</td>
                            <td>{{$cp->mob1}}<br>{{$cp->mob2}}</td>
                            <td>{{$cp->email1}}<br>{{$cp->email2}}</td>
                        </tr>
                    @endforeach
                </table>
            </div>
            

            <div class="panel panel-default">
                <div class="panel-heading">Remarks</div>
                <div class="panel-body">
                     <textarea class="form-control" name="" id="remarkss" placeholder="Write down remarks"></textarea>
                     <button class="btn btn-success btn-xs remarks-btn" style="margin-top:10px;margin-bottom: 20px;"><i class="fa fa-comment-o"></i> Remark</button>
                     <div class="remarks-div"></div>
                </div>
            </div>
            
        </div>
        

        <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6">
          
            <div class="panel panel-default panel-service">
                <!-- Default panel contents -->
                <div class="panel-heading">
                {{ $service->services?$service->services->name:'--' }}
                <a class="btn btn-primary btn-xs pull-right" data-toggle='modal' data-target='#modalUpload' href="{{ url('admin/company/service/upload') }}/{{ $service->id }}"><i class="fa fa-cloud-upload"></i> New Attachment</a>
                </div>
                <!-- Table -->
                <div class="panel-body">
                 
                    <table class="table table-bordered table-service">
                        <thead>
                            <tr>
                                <th>Deployment Unit</th>
                                <td>{{ $service->deployment_unit_details }}</td>
                            </tr>
                            <tr>
                                <th>Start Date</th>
                                <td>{{ date('d M, Y',strtotime($service->start_date)) }}</td>
                            </tr>
                            <tr>
                                <th style=''>Contact Info</th>
                                <td>
                                	<strong>Name : </strong>  {{ $service->contact_person }} <br>
                                	<strong>Phone : </strong>  {{ $service->contact_number }}
                                </td>
                            </tr>
                            <tr>
                            	<th>PO Details</th>
                            	<td>
                            		{{ $service->po_details }}
                            	</td>
                            </tr>
                            <tr>
                            	<th>Renew Cycle</th>
								<td><span class="label label-info">{{ $service->renewal_cycle }} Year</span></td>
                            </tr>
                            <tr>
                            	<th>Renew Session</th>
                            	<td><span class="label label-info">{{ $service->free_session }} Year</span></td>
                            </tr>
                            <tr>
                            	<th>Attachments</th>
                            	<td>
                            	@if($service->attachments)
                            		@foreach($service->attachments as $row)
                            		<a class='btn btn-default btn-xs' target="_blank" href="{{ asset('assets/uploads/service') }}/{{ $row->file_name }}">{{ !empty($row->title)?$row->title:$row->file_name }} <i class="fa fa-cloud-download"></i></a> <br>
                            		@endforeach
                            	@endif
                            	</td>
                            </tr>
                        </thead>
                    </table>
                </div>
              
            </div>
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title">Activity</h3>
                </div>
                <div class="panel-body">
                    <div style="min-height: 250px;" class="activity-div"></div>
                </div>
             
                <div class="panel-footer">
                    <button class="btn btn-xs activity-btn" data-fullText="Already called.">
                         Mobile 
                         <i class="fa fa-mobile" aria-hidden="true"></i>
                    </button>
                    <button class="btn btn-xs activity-btn" data-toggle="modal" data-target=".modal-email" data-fullText="mail">
                         Email 
                         <i class="fa fa-envelope-o" aria-hidden="true"></i>
                    </button>
                    <button class="btn btn-xs activity-btn" data-fullText="Already send a sms.">
                         SMS             
                         <i class="fa fa-comments-o" aria-hidden="true"></i>
                    </button>
                </div>
            </div>
        </div>


    </div>

</section>

<!-- Modal to add services -->
@include('admin.company_info.service_modal')
<!-- end modal to add services -->

<!-- Modal Add Contract person -->
@include('admin.company_info.add_contact_info')
<!-- /.modal -->

<!-- Modal Email -->
@include('admin.company_info.modal_email')
<!-- /.modal -->

<!-- Modal File Upload -->
<div class="modal fade" id="modalUpload">
  <div class="modal-dialog modal-md">
    
  </div>
</div>
<!-- /.modal -->
@endsection

@section('script')

<script src="{{URL::asset('assets/ckeditor/ckeditor.js')}}"></script>
<script type="text/javascript">

    $(document).ready(function(){

        $( ".date" ).datepicker({
             changeMonth: true,
             changeYear: true,
             dateFormat:"yy-mm-dd"
        });
        $('body').on('hidden.bs.modal', '.modal', function () {
          $(this).removeData('bs.modal');
        });

        $('#lnkAssignService').click(function(e){
            e.preventDefault();
            var com_id=$(this).data('company_id');
            var com_name=$(this).data('company_name');
            $('span#company_name').text(com_name);
            $('input[name="hdn_com_id"]').val(com_id);

            $('#modalService').modal('show');

        });

        $('#btn_assign_service').click(function(){
            var com_id=$('input[name="hdn_com_id"]').val();
            var service=$('#ddlService').val();
            var contact_person=$('#contact_person').val();
            var contact_number=$('#contact_number').val();
            var deployement_unit=$('#deployement_unit').val();
            var po_details=$('#po_details').val();
            var renewal_cycle=$('#renewal_cycle').val();
            var free_session=$('#free_session').val();
            var start_date=$('#start_date').val();
            var token='{{ csrf_token() }}';
            if(com_id.length==0 || service.length==0){
                $('#spn_msg').text('Service name must be selected !'); 
                return false;
            }

            $.ajax({
                url: '{{ url('admin/company/service/assign') }}',
                type: 'POST',
                dataType: 'json',
                data: {com_id:com_id,service:service,contact_person:contact_person,contact_number:contact_number,deployement_unit:deployement_unit,po_details:po_details,renewal_cycle:renewal_cycle,free_session:free_session,start_date:start_date,_token:token}
            })
            .done(function(res) {
                if(res.ok)
                {
                    alert(res.msg);
                    $('#modalService').modal('hide');

                }
            });
            
        });

        var company_id = $('#company_id').val();
        var com_service_id='{{ $service->id }}';
        //fetch remarks
        $.ajax({
            url: "{{url('admin/save_remarks')}}",
            type: 'POST',
            data: {remarks: '0', company_id: company_id,company_service_id:com_service_id},
        })
        .done(function(data) {
            
            $('.remarks-div').html(data);
        }); 

        //fetch activity
        $.ajax({
            url: "{{url('admin/save_activity')}}",
            type: 'POST',
            data: {val: '0', company_id: company_id},
        })
        .done(function(data) {
            
            $('.activity-div').html(data);
        });

        //ck editor
        CKEDITOR.replace( 'editor1' );
        CKEDITOR.replace( 'comment_txt' );

    });

    $('.activity-btn').click(function(event) {
        
        var email1 = $('.hdn-email1').val();
        var email2 = $('.hdn-email2').val();
        
        $('.to-email').val(email1+', '+email2);

        var val = $(this).attr("data-fullText");
        
        if(val != 'mail'){

            if(confirm('Want to add in activity list ?')){
                var company_id = $('#company_id').val();
                $.ajax({
                    url: "{{url('admin/save_activity')}}",
                    type: 'POST',
                    data: {val: val, company_id: company_id},
                })
                .done(function(data) {
                    
                    $('.activity-div').html(data);
                });
            }
        }
    });

    $('.email-send').click(function(event){

        var val = "An email sent.";
        var company_id = $('#company_id').val();
        var email1 = $('.hdn-email1').val();
        var email2 = $('.hdn-email2').val();
        var email_subj = $('.email_subj').val();
        var attachment_id = $('.attachment_id').val();
        var email_body = CKEDITOR.instances.editor1.getData();

        if(email1.length > 0){
            $.ajax({
                url: "{{url('admin/send_email_to_company')}}",
                type: 'POST',
                data: {email1: email1,email2: email2, subj: email_subj, email_body:email_body,attachment_id: attachment_id},
            })
            .done(function(data){
                
                if(data == 'success'){
                    $.ajax({
                        url: "{{url('admin/save_activity')}}",
                        type: 'POST',
                        data: {val: val, company_id: company_id},
                    })
                    .done(function(data) {
                        
                        $('.activity-div').html(data);
                    });

                    alert('Email Send successfully!!');
                }
                else{
                    alert('Sorry! email not send ...');
                }
            });
        }
        else{
            alert('Email 01 filed is mendatory. Email address not available!');
        }
    });

    $( ".activity-div" ).on( "click",".remove-activity",function() {
        
        var id = $(this).attr("data-id");
        var company_id = $('#company_id').val();

        if(confirm('Want to delete?'))
        {
            $.ajax({
                url: "{{url('admin/remove_activity')}}",
                type: 'POST',
                data: {id: id,company_id: company_id},
            })
            .done(function(data) {
                
                $('.activity-div').html(data);
                alert('Data removed successfully!');
            });
        }

    });
    
    $('.remarks-btn').click(function(event) {
        
        var remarks = $('#remarkss').val();
        var company_id = $('#company_id').val();
        var company_service_id='{{ $service->id }}';

        if(remarks.length > 0){

            $.ajax({
                url: "{{url('admin/save_remarks')}}",
                type: 'POST',
                data: {remarks: remarks, company_id: company_id,company_service_id:company_service_id},
            })
            .done(function(data) {
                
                $('.remarks-div').html(data);
                $('#remarkss').val('');
            });
        }
        else{
            alert('Please fill up remarks field');
        }
    });

    $( ".remarks-div" ).on( "click",".remove-remarks",function() {
        
        var id = $(this).attr("data-id");
        var company_id = $('#company_id').val();

            if(confirm('Want to delete?'))
            {
                $.ajax({
                    url: "{{url('admin/remove_remarks')}}",
                    type: 'POST',
                    data: {id: id,company_id: company_id},
                })
                .done(function(data) {
                    
                    $('.remarks-div').html(data);
                    alert('Data removed successfully!');
                });
            }

    });
</script>
@endsection
