@extends(branchMenu())
@section('content')
    <?php Fun::lang()?>

    <?php
    $province = \App\Model\ProvinceModel::where('province_id', $lead->province)->first();
    if ($province) {
        if (if_kh()) {
            $province = $province->province_km;
        } else {
            $province = $province->province_en;
        }
    }
    ?>
    <div class="container-fluid">
        <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
            <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
                <div class="pull-right">
                    <button onclick="window.history.back();" class="btn btn-warning"><i class="fa fa-arrow-circle-left" title="{{__('administrator.back')}}"> {{__('administrator.back')}}</i></button>
                    @if ($lead->convert_status !== 1)
                        <a class="btn btn-success add_task"><i class="fa fa-tasks" title="{{__('administrator.task')}}"> {{__('administrator.task')}}</i></a>
                    @endif
                    @if($lead->convert_status !== 1)
                        <a href="javascript:;" data-url="{{ route('prospect.close_deal', $lead->id) }}" class="btn btn-primary close_deal"><i class="fa fa-paper-plane" title="{{__('administrator.close_deal')}}"> {{__('administrator.grade')}}</i></a>
                    @endif
                    @if($lead->convert_status === 1)
                        @php
                            $customer = \App\Model\people\CustomerModel::where('lead_id', $lead->id)->first();
                            $customer = $customer ? $customer->id : '';
                        @endphp
                        <button type="button" class="btn btn-info" onclick="window.location.replace('{{route("pos.create")}}?postype=rldf8e7sdf&sale_req=kl98sdf77&hellowajitnomotocambo=hibbbyerequestbyeloveyou&customer={{(Crypt::encrypt($customer))}}')">
                            <i href="javascript:;" class="fa fa-shopping-cart " title="View"></i></button>
                    @endif
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
                <h4>{{__('administrator.lead_info')}}</h4>
                <hr>
                <div class="form-group col-xs-6">
                    <div class="col-xs-4" style="text-align: right;">
                        <label>{{__('administrator.date')}} :</label>
                    </div>
                    <div class="col-xs-8">{{ myDate($lead->date) }}</div>
                </div>
                <div class="form-group col-xs-6">
                    <div class="col-xs-4" style="text-align: right;">
                        <label> {{__('administrator.agent')}} :</label>
                    </div>
                    <div class="col-xs-8">{{ $lead->agent_name() }}</div>
                </div>
                <div class="form-group col-xs-6">
                    <div class="col-xs-4" style="text-align: right;">
                        <label> {{__('administrator.prospect')}} :</label>
                    </div>
                    <div class="col-xs-8">{{ $lead->name }}</div>
                </div>
                <div class="form-group col-xs-6">
                    <div class="col-xs-4" style="text-align: right;">
                        <label> {{__('administrator.gender')}} :</label>
                    </div>
                    <div class="col-xs-8">{{ ucfirst($lead->sex) }}</div>
                </div>
                <div class="form-group col-xs-6">
                    <div class="col-xs-4" style="text-align: right;">
                        <label> {{__('administrator.contact')}} :</label>
                    </div>
                    <div class="col-xs-8">{{ $lead->contact }}</div>
                </div>
                <div class="form-group col-xs-6">
                    <div class="col-xs-4" style="text-align: right;">
                        <label> {{__('administrator.age')}} :</label>
                    </div>
                    <div class="col-xs-8">{{ $lead->age?($lead->age.' '.__('administrator.year')):'' }}</div>
                </div>
                <div class="form-group col-xs-6">
                    <div class="col-xs-4" style="text-align: right;">
                        <label> {{__('administrator.province')}} :</label>
                    </div>
                    <div class="col-xs-8">{{$province?:''}}</div>
                </div>
                <div class="form-group col-xs-6">
                    <div class="col-xs-4" style="text-align: right;">
                        <label> {{__('administrator.address')}} :</label>
                    </div>
                    <div class="col-xs-8">{{ $lead->address }}</div>
                </div>
                <div class="form-group col-xs-6">
                    <div class="col-xs-4" style="text-align: right;">
                        <label> {{__('administrator.known_product_from')}} :</label>
                    </div>
                    <div class="col-xs-8">{{ $lead->source() }}</div>
                </div>
                <div class="form-group col-xs-6">
                    <div class="col-xs-4" style="text-align: right;">
                        <label> {{__('administrator.grade')}} :</label>
                    </div>
                    <div class="col-xs-8"><?php
                        if ($lead->convert_status != 0)
                            echo bgDealStatus($lead->convert_status);
                        ?>
                    </div>
                </div>
                <div class="form-group col-xs-6">
                    <div class="col-xs-4" style="text-align: right;">
                        <label> {{__('administrator.result_note')}} :</label>
                    </div>
                    <div class="col-xs-8">{{$lead->close_deal_note}}</div>
                </div>
                <div class="form-group col-xs-6">
                    <div class="col-xs-4" style="text-align: right;">
                        <label> {{__('administrator.customer_note')}} :</label>
                    </div>
                    <div class="col-xs-8">{{$lead->customer_note}}</div>
                </div>
                <div class="form-group col-xs-6">
                    <div class="col-xs-4" style="text-align: right;">
                        <label> {{__('administrator.other')}} :</label>
                    </div>
                    <div class="col-xs-8">{{$lead->other}}</div>
                </div>
                <div class="form-group col-xs-6">
                    <div class="col-xs-4" style="text-align: right;">
                        <label> {{__('administrator.note')}} :</label>
                    </div>
                    <div class="col-xs-8">{{$lead->note}}</div>
                </div>
            </div>
            @if (count($task->where('task_type', 'appointment')))
                <div class="col-xs-12 table-responsive">
                    <h4>{{__('administrator.appointment_log')}}</h4>
                    <table class="table table-bordered">
                        <thead>
                        <tr>
                            <th> #</th>
                            <th> {{__('administrator.date')}} </th>
                            <th> {{__('administrator.subject')}} </th>
                            <th> {{__('administrator.place')}} </th>
                            <th> {{__('administrator.status')}} </th>
                            <th> {{__('administrator.task_note')}} </th>
                            <th> {{__('administrator.out_come')}} </th>
                            <th> {{__('administrator.result_percentage')}} </th>
                            <th> {{__('administrator.result_note')}} </th>
                            <th width="10%"> {{__('administrator.action')}} </th>
                        </tr>
                        </thead>
                        <tbody>

                        @php
                            $i = 1;
                        @endphp
                        @foreach ($task->where('task_type', 'appointment') as $appointment)
                            <tr>
                                <td>{{ $i ++ }}</td>
                                <td>{{ myDate($appointment->date) }}</td>
                                <td>{{ $appointment->subject }}</td>
                                <td>{{ $appointment->where }}</td>
                                <td>{!! $appointment->complete_status == 0 ?'<span class="label label-warning">'.__('administrator.pending').'</span>':'<span class="label label-success">'.__('administrator.complete').'</span>' !!}</td>
                                <td>{!! Form::textarea('body', $appointment->note, ['class' => ' no-resize no-bg no-border','readonly','rows'=>3]) !!}</td>
                                <td>{{ $appointment->out_come }}</td>
                                <td>{{ $appointment->result_percentage?($appointment->result_percentage.'%'):'' }}</td>
                                <td>{{ $appointment->result }}</td>
                                <td>
                                    @if ($appointment->complete_status == 0 &&$lead->convert_status !=1)
                                        <span data-href="{{route('task.edit',$appointment->id)}}"
                                              class="fa fa-pencil-square-o btn btn-sm btn-warning btn_edit"
                                              title="Edit"></span>
                                        <span data-url="{{ route('task.complete', $appointment->id) }}"
                                              class="fa fa-check btn btn-sm btn-success" title="Complete"></span>
                                    @endif
                                </td>
                            </tr>
                        @endforeach

                        </tbody>
                    </table>
                </div>
            @endif
            @if(count($task->where('task_type', 'meeting')))
                <div class="col-xs-12 table-responsive">
                    <h4>{{__('administrator.meeting_log')}}</h4>
                    <hr>
                    <table class="table table-bordered" width="100%">
                        <thead>
                        <tr>
                            <th>#</th>
                            <th>{{__('administrator.date')}} </th>
                            <th>{{__('administrator.subject')}} </th>
                            <th>{{__('administrator.place')}} </th>
                            <th>{{__('administrator.status')}} </th>
                            <th>{{__('administrator.task_note')}} </th>
                            <th>{{__('administrator.out_come')}}   </th>
                            <th>{{__('administrator.result_percentage')}}  </th>
                            <th>{{__('administrator.result_note')}}   </th>
                            <th width="10%">{{__('administrator.action')}} </th>
                        </tr>
                        </thead>
                        <tbody>
                        @php
                            $i = 1;
                        @endphp
                        @foreach ($task->where('task_type', 'meeting') as $meeting)

                            <tr class="no-bg">
                                <td>{{ $i ++ }}</td>
                                <td>{{ Carbon\Carbon::parse($meeting->date)->format('d-m-Y') }}</td>
                                <td>{{ $meeting->subject }}</td>
                                <td>{{ $meeting->where }}</td>
                                <td>{!! $meeting->complete_status == 0 ?'<span class="label label-warning">'.__('administrator.pending').'</span>':'<span class="label label-success">'.__('administrator.complete').'</span>' !!}</td>
                                <td>{!! Form::textarea('body', $meeting->note, ['class' => ' no-resize no-bg no-border','readonly','rows'=>3]) !!}</td>
                                <td>{{ $meeting->out_come }}</td>
                                <td>{{ $meeting->result_percentage?($meeting->result_percentage.'%'):'' }}</td>
                                <td>{{ $meeting->result }}</td>
                                <td>
                                    @if ($meeting->complete_status == 0  && $lead->convert_status !=1)
                                        <span data-href="{{route('task.edit',$meeting->id)}}" class="fa fa-pencil-square-o btn btn-sm btn-warning btn_edit" title="Edit"></span>
                                        <span data-url="{{ route('task.complete', $meeting->id) }}" class="fa fa-check btn btn-sm btn-success" title="Complete"></span>
                                    @endif
                                </td>
                            </tr>
                        @endforeach
                        </tbody>
                    </table>
                </div>
            @endif
            @if (count($task->where('task_type', 'call_log')))
                <div class="col-xs-12 table-responsive">
                    <h4>{{__('administrator.call_log')}}</h4>
                    <hr>
                    <table class="table table-bordered">
                        <thead>
                        <tr>
                            <th>#</th>
                            <th>{{__('administrator.date')}}</th>
                            <th>{{__('administrator.subject')}}</th>
                            <th>{{__('administrator.status')}}</th>
                            <th>{{__('administrator.call_type')}}</th>
                            <th>{{__('administrator.call_duration')}}</th>
                            <th>{{__('administrator.task_note')}}</th>
                            <th>{{__('administrator.out_come')}}</th>
                            <th>{{__('administrator.result_percentage')}}</th>
                            <th>{{__('administrator.result_note')}}</th>
                            <th width="10%">{{__('administrator.action')}}</th>
                        </tr>
                        </thead>
                        <tbody>
                        @foreach ($task->where('task_type', 'call_log') as $key => $call_log)
                            <tr>
                                <td>{{ $key + 1 }}</td>
                                <td>{{ myDate($call_log->date)}}</td>
                                <td>{!! Form::textarea('body', $call_log->subject, ['class' => ' no-resize no-bg no-border','readonly','rows'=>3]) !!}</td>
                                <td>{!! $call_log->complete_status == 0 ?'<span class="label label-warning">'.__('administrator.pending').'</span>':'<span class="label label-success">'.__('administrator.complete').'</span>' !!}</td>
                                <td>{{ $call_log->call_type }}</td>
                                <td>N/A</td>
                                <td>{!! Form::textarea('body', $call_log->note, ['class' => ' no-resize no-bg no-border','readonly','rows'=>3]) !!}</td>
                                <td>{{ $call_log->out_come }}</td>
                                <td>{{ $call_log->result_percentage?($call_log->result_percentage.'%'):'' }}</td>
                                <td>{{ $call_log->result }}</td>
                                <td>
                                    @if ($call_log->complete_status == 0 && $lead->convert_status !=1)
                                        <span data-href="{{route('task.edit',$call_log->id)}}"
                                              class="fa fa-pencil-square-o btn btn-sm btn-warning btn_edit"
                                              title="Edit"></span>
                                        <span data-url="{{ route('task.complete', $call_log->id) }}"
                                              class="fa fa-check btn btn-sm btn-success" title="Complete"></span>
                                    @endif
                                </td>
                            </tr>
                        @endforeach
                        </tbody>
                    </table>
                </div>
            @endif
            @if (count($task->where('task_type', 'chart')))
                <div class="col-xs-12 table-responsive">
                    <h4>{{__('administrator.chart')}}</h4>
                    <hr>
                    <table class="table table-bordered">
                        <thead>
                        <tr>
                            <th>#</th>
                            <th>{{__('administrator.date')}}</th>
                            <th>{{__('administrator.subject')}}</th>
                            <th>{{__('administrator.status')}}</th>
                            <th>{{__('administrator.call_type')}}</th>
                            <th>{{__('administrator.call_duration')}}</th>
                            <th>{{__('administrator.task_note')}}</th>
                            <th>{{__('administrator.out_come')}}</th>
                            <th>{{__('administrator.result_percentage')}}</th>
                            <th>{{__('administrator.result_note')}}</th>
                            <th width="10%">{{__('administrator.action')}}</th>
                        </tr>
                        </thead>
                        <tbody>
                        @foreach ($task->where('task_type', 'chart') as $key => $call_log)
                            <tr>
                                <td>{{ $key + 1 }}</td>
                                <td>{{ myDate($call_log->date)}}</td>
                                <td>{!! Form::textarea('body', $call_log->subject, ['class' => ' no-resize no-bg no-border','readonly','rows'=>3]) !!}</td>
                                <td>{!! $call_log->complete_status == 0 ?'<span class="label label-warning">'.__('administrator.pending').'</span>':'<span class="label label-success">'.__('administrator.complete').'</span>' !!}</td>
                                <td>{{ $call_log->call_type }}</td>
                                <td>N/A</td>
                                <td>{!! Form::textarea('body', $call_log->note, ['class' => ' no-resize no-bg no-border','readonly','rows'=>3]) !!}</td>
                                <td>{{ $call_log->out_come }}</td>
                                <td>{{ $call_log->result_percentage?($call_log->result_percentage.'%'):'' }}</td>
                                <td>{{ $call_log->result }}</td>
                                <td>
                                    @if ($call_log->complete_status == 0 && $lead->convert_status !=1)
                                        <span data-href="{{route('task.edit',$call_log->id)}}"
                                              class="fa fa-pencil-square-o btn btn-sm btn-warning btn_edit"
                                              title="Edit"></span>
                                        <span data-url="{{ route('task.complete', $call_log->id) }}"
                                              class="fa fa-check btn btn-sm btn-success" title="Complete"></span>
                                    @endif
                                </td>
                            </tr>
                        @endforeach
                        </tbody>
                    </table>
                </div>
            @endif
        </div>
    </div>
@endsection
@section('script')
    <script type="text/javascript">
        // new task

        $(document).ready(function () {
            $('.add_task').on('click', function () {
                getHtmlForm('{{route('task.create')}}?lead_id={{$lead->id}}', '{{__('administrator.create').' '.__('administrator.task')}}', '700px');
            });
            $('.btn_edit').on('click', function () {
                getHtmlForm($(this).data('href'), '{{__('administrator.edit').' '.__('administrator.task')}}', '700px');
            });
            // new complete
            $(document).on('click', '.fa-check', function () {
                var url = $(this).data('url');
                $.confirm({
                    useBootstrap: false,
                    boxWidth: '700px',
                    title: '{{__('administrator.complete_task')}}!',
                    content: `
                    <div class="container-fluid text-left">
                    <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6 col-xl-6 form-group">
                        <label class="required">{{__('administrator.outcome')}}</label>
                        {!! Form::select('outcome', getOutCome(), null, ['class' => 'outcome form-control', 'placeholder' => __('administrator.please_select')])  !!}
                        </div>
                        <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6 col-xl-6 form-group">
                            <label class="required text-left">{{__('administrator.result_percentage')}}</label>
                        <input type="text" class="form-control numeric" placeholder="{{ __('administrator.result_percentage') }}" maxlength="25"/>
                    </div>
                    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 col-xl-12 form-group">
                        <label class="text-left">{{ __('administrator.note') }}</label>
                        <textarea class="form-control note" rows="5"></textarea>
                    </div></div>`,
                    buttons: {
                        '{{__('administrator.update')}}': {
                            btnClass: 'btn-blue',
                            keys: ['enter'],
                            action: function () {
                                var outcome = this.$content.find('.outcome').val();
                                var percentage = this.$content.find('.numeric').val();
                                var note = this.$content.find('.note').val();

                                if (!outcome) {
                                    this.$content.find('.outcome').closest('.col-md-6').find('span.select2-selection').addClass('formError');
                                    return false;
                                } else {
                                    this.$content.find('.outcome').closest('.col-md-6').find('span.select2-selection').removeClass('formError');
                                }

                                if (!percentage) {
                                    this.$content.find('.numeric').addClass('formError');
                                    return false;
                                } else {
                                    this.$content.find('.numeric').removeClass('formError');
                                }

                                $.ajax({
                                    url: url,
                                    data: {
                                        '_token': '{{ csrf_token() }}',
                                        outcome,
                                        percentage,
                                        note
                                    },
                                    type: 'PATCH',
                                    async: false,
                                    success: function (data) {
                                        if (data[1] == 401) {
                                            showWarningToast(data[0]);
                                        } else {
                                            page = 1;
                                            showSuccessToast('Task updated successfully!');
                                            $('.jconfirm-buttons .btn-default').trigger('click');
                                            location.reload();
                                        }
                                    }
                                });
                                return false;
                            }
                        },
                        '{{__('administrator.cancel')}}': function () {}
                    },
                });
            });
            // close deal
            $(document).on('click', '.close_deal', function () {
                var url = $(this).data('url');
                $.confirm({
                    title: '{{ __('administrator.close_deal') }}',
                    columnClass: 'col-md-6 col-md-offset-3',
                    content: `
                     <div class="container-fluid text-left">
                    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 col-xl-12 form-group">
                        <label class="required">{{__('administrator.result')}}</label>
                        {!! Form::select('result',getDealResult(), null, ['class' => 'form-control result', 'placeholder' => __('administrator.please_select')])  !!}
                        </div>
                        <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 col-xl-12 form-group">
                            <label class="text-left">{{__('administrator.note')}}</label>
                         <textarea class="form-control note" rows="5"></textarea>
                    </div>
                    </div>`,
                    buttons: {
                        '{{__('administrator.update')}}': {
                            btnClass: 'btn-blue',
                            keys: ['enter'],
                            action: function () {
                                var result = this.$content.find('.result').val();
                                var note = this.$content.find('.note').val();

                                if (!result) {
                                    this.$content.find('.result').closest('.col-md-12').find('span.select2-selection').addClass('formError');
                                    return false;
                                } else {
                                    this.$content.find('.result').closest('.col-md-12').find('span.select2-selection').removeClass('formError');
                                }
                                $.ajax({
                                    url: url,
                                    data: {
                                        '_token': '{{ csrf_token() }}',
                                        result,
                                        note
                                    },
                                    type: 'PATCH',
                                    async: false,
                                    success: function (data) {
                                        if (data[1] == 401) {
                                            showWarningToast(data[0]);
                                        } else {
                                            page = 1;
                                            showSuccessToast('Lead updated successfully!');
                                            location.reload();
                                        }
                                    }
                                });
                                return false;
                            }
                        },
                        '{{__('administrator.cancel')}}': function () {}
                    },
                });
            });

        });


    </script>
@endsection
