@extends('layouts.master', ['title' => 'List -  Prospects - Pages'])

@section('workspace')

    <!-- Breadcrumb -->
    <section class="breadcrumb lg:flex items-start">
        <div>
            <h1>Prospects</h1>
            <ul>
                <li><a href="#no-link">Pages</a></li>
                <li class="divider la la-arrow-right"></li>
                <li><a href="#no-link">Prospect</a></li>
                <li class="divider la la-arrow-right"></li>
                <li>List</li>
            </ul>
        </div>

        <div class="flex flex-wrap gap-2 items-center ltr:ml-auto rtl:mr-auto mt-5 lg:mt-0">
            <!-- Search -->
            <form class="flex flex-auto">
                <label class="form-control-addon-within rounded-full">
                    <input class="form-control border-none" placeholder="Search">
                    <button
                        class="text-gray-300 dark:text-gray-700 text-xl leading-none la la-search ltr:mr-4 rtl:ml-4"></button>
                </label>
            </form>

            <div class="flex gap-x-2">

                <!-- Sort By -->
                <div class="dropdown">
                    <button class="btn btn_outlined btn_secondary uppercase" data-toggle="dropdown-menu">
                        Sort By
                        <span class="ltr:ml-3 rtl:mr-3 la la-caret-down text-xl leading-none"></span>
                    </button>
                    <div class="dropdown-menu">
                        <a href="#no-link">Ascending</a>
                        <a href="#no-link">Descending</a>
                    </div>
                </div>

                <!-- Add New -->
                {{--                <a href="{{ url('pages/blog/add') }}" class="btn btn_primary uppercase">Add New</a>--}}
            </div>
        </div>
    </section>

    <!-- List -->
    <div class="card p-5">
        <div class="overflow-x-auto">
            <table class="table table-auto table_hoverable w-full">
                <thead>
                <tr>
                    <th class="ltr:text-left rtl:text-right uppercase">Contact Person</th>
                    <th class="text-center uppercase">Contact Number</th>
                    <th class="text-center uppercase">Email</th>
                    <th class="text-center uppercase">State</th>
                    <th class="text-center uppercase">Note</th>
                    <th class="text-center uppercase">Has Contact</th>
                    <th class="text-center uppercase">Register Date</th>
                    <th class="uppercase"></th>
                </tr>
                </thead>
                <tbody>
                @if($trialCompanyDealers->count() > 0)
                    @foreach($trialCompanyDealers as $trialCompanyDealer)
                        <tr>
                            <td>{{$trialCompanyDealer->trialCompany->contact_name}}</td>
                            <td class="text-center">{{$trialCompanyDealer->trialCompany->contact_number}}</td>
                            <td class="text-center">{{$trialCompanyDealer->trialCompany->email}}</td>
                            <td class="text-center">{{$trialCompanyDealer->trialCompany->state}}</td>
                            <td class="text-center">{{$trialCompanyDealer->trialCompany->description}}</td>
                            <td class="text-center">
                                @if($trialCompanyDealer->trialCompany->is_contacted == 1)
                                    Yes
                                @else
                                    No
                                @endif
                            </td>
                            <td class="text-center">
                                {{$trialCompanyDealer->trialCompany->created_at->format('d M Y')}}
                            </td>
                            <td class="ltr:text-right rtl:text-left whitespace-nowrap">

                                <div class="inline-flex ltr:ml-auto rtl:mr-auto">
{{--                                    <button class="btn btn-icon btn_outlined btn_primary" data-toggle="modal"--}}
{{--                                            data-target="#exampleModalCentered"--}}
{{--                                            onclick="openModal('{{$trialCompanyDealer->trialCompany->company_id}}')"><span--}}
{{--                                            class="la la-exchange"></span></button>--}}

                                    @if($trialCompanyDealer->trialCompany->is_contacted != 1)
                                        <button class="btn btn-icon btn_outlined btn_primary" data-toggle="modal"
                                                data-target="#contactedModal" onclick="openModal('{{$trialCompanyDealer->trialCompany->company_id}}')"><span
                                                class="la la-pen-fancy"></span></button>
                                    @endif

{{--                                    <a href="#no-link" class="btn btn-icon btn_outlined btn_danger ltr:ml-2 rtl:mr-2">--}}
{{--                                        <span class="la la-trash-alt"></span>--}}
{{--                                    </a>--}}
                                </div>
                            </td>
                        </tr>
                    @endforeach
                @else
                    <tr>
                        <td colspan="8" class="text-center">No records found</td>
                    </tr>
                @endif

                </tbody>
            </table>
        </div>
    </div>

    <!-- Contacted -->
    <div id="contactedModal" class="modal" data-animations="fadeInDown, fadeOutUp" data-static-backdrop>
        <div class="modal-dialog modal-dialog_centered max-w-2xl">
            <div class="modal-content">
                <div class="modal-header">
                    <h2 class="modal-title">Prospect Contact</h2>
                    <button class="close la la-times" data-dismiss="modal" onclick="closeModal()"></button>
                </div>
                <div class="modal-body">
                    <p>Are you sure you want to mark this prospect as contacted?</p>
                </div>
                <div class="modal-footer">
                    <div class="flex ltr:ml-auto rtl:mr-auto">
                        <button class="btn btn_secondary uppercase" data-dismiss="modal" onclick="closeModal()">Close</button>
                        <button class="btn btn_primary ltr:ml-2 rtl:mr-2 uppercase" onclick="submitFunction()">Confirm</button>
                    </div>
                </div>
            </div>
        </div>
    </div>



{{--    <div class="mt-5">--}}
{{--        @include('partials._pagination')--}}
{{--    </div>--}}

@endsection


@section('scripts')
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <script>

        let selectedTrialCompanyId = '';

        function closeModal() {
            selectedTrialCompanyId = '';
        }

        function openModal(trialCompanyId) {
            selectedTrialCompanyId = trialCompanyId;
        }


        function submitFunction() {
            console.log('test');

            axios.post('{{route('sub-dealer.trial-customers.contacted')}}', {
                _token: '{{csrf_token()}}',
                trial_company_id: selectedTrialCompanyId,
            })
                .then(function (response) {
                    console.log(response);
                    if (response.status === 200) {
                        alert(response.data.message);
                        window.location.reload();
                    } else {
                        alert(response.data.message);
                    }
                })
                .catch(function (error) {
                    console.log(error);
                });
        }

    </script>
@endsection
