@extends('Admin.layout')
<style>
    .page-title {
        font-weight: 700;
        color: var(--bs-body-color);
    }

    .generate-card {
        border: 1px solid rgba(255, 255, 255, .08);
        border-radius: 18px;
        background: var(--bs-body-bg);
        box-shadow: 0 10px 25px rgba(0, 0, 0, .08);
        transition: .3s;
        overflow: hidden;
    }

    .generate-card:hover {
        transform: translateY(-4px);
        box-shadow: 0 15px 30px rgba(0, 0, 0, .12);
    }

    .card-header-custom {
        padding: 16px 18px;
        font-weight: 700;
        display: flex;
        justify-content: space-between;
        align-items: center;
        flex-wrap: wrap;
        gap: 10px;
    }

    .roi-header {
        background: linear-gradient(135deg, #0d6efd, #0dcaf0);
        color: #fff;
    }

    .bdb-header {
        background: linear-gradient(135deg, #198754, #20c997);
        color: #fff;
    }

    .history-btn {
        border-radius: 10px;
        padding: 6px 14px;
        font-size: 14px;
        font-weight: 600;
        background: rgba(255, 255, 255, .18);
        color: #fff;
        border: 1px solid rgba(255, 255, 255, .25);
        text-decoration: none;
    }

    .history-btn:hover {
        background: rgba(255, 255, 255, .28);
        color: #fff;
    }

    .card-body-custom {
        padding: 22px;
    }

    .form-label {
        font-weight: 600;
    }

    .form-control {
        border-radius: 12px;
        padding: 10px 14px;
    }

    .btn-custom {
        border-radius: 12px;
        padding: 10px 24px;
        font-weight: 600;
    }
</style>
@section('content')
    <div class="row d-flex justify-content-between">
        <div class="col-auto">
            <h5><button type="button" class="btn btn-icon bg-white me-2 waves-effect waves-light"
                    style="box-shadow: 0px 9px 12px -2px #66328E1F;">
                    <a href="{{ route('admin.silver_payout') }}">
                        <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none"
                            stroke="#000" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"
                            class="icon icon-tabler">
                            <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
                            <path d="M15 6l-6 6l6 6"></path>
                        </svg>
                    </a>
                </button>Silver Payout Generate</h5>
        </div>

    </div>
    <div class="row d-flex justify-content-center">
        <div class="col-lg-6">
            <div class="generate-card">

                <div class="card-header-custom roi-header">
                    <span>
                        <i class="ti tabler-chart-line me-2"></i>
                        Generate
                    </span>

                    <!-- <a href="#" class="history-btn">
                        <i class="ti tabler-history me-1"></i> Report
                    </a> -->
                </div>

                <div class="card-body-custom">
                    <form action="#" method="POST">
                        @csrf

                         {{-- FROM DATE --}}
                            <div class="mb-4">

                                <label class="form-label fw-semibold mb-2">
                                    From Date (Saturday Only)
                                </label>

                                <div class="input-group">

                                    <span class="input-group-text bg-light">
                                        <i class="ti tabler-calendar-event text-primary"></i>
                                    </span>

                                    <input type="date" id="from_date" name="from_date" class="form-control form-control-lg"
                                        required>

                                </div>

                                <small class="text-muted">
                                    Only Saturdays are allowed
                                </small>

                            </div>

                            {{-- TO DATE --}}
                            <div class="mb-4">

                                <label class="form-label fw-semibold mb-2">
                                    To Date (Auto Friday)
                                </label>

                                <div class="input-group">

                                    <span class="input-group-text bg-light">
                                        <i class="ti tabler-calendar-event text-primary"></i>
                                    </span>

                                    <input type="date" id="to_date" name="to_date" class="form-control form-control-lg"
                                        readonly required>

                                </div>

                            </div>

                            <div class="d-grid gap-2 mt-5">

                                <button type="submit" id="submit_btn" class="btn btn-primary fw-bold">

                                    <i class="ti tabler-rocket"></i>
                                    Submit & Generate

                                </button>

                            </div>
                    </form>
                </div>

            </div>
        </div>
    </div>

      <script>

        const fromDateInput = document.getElementById('from_date');
        const toDateInput = document.getElementById('to_date');


        function getLastSaturday() {

            let today = new Date();

            let day = today.getDay();

            // Saturday = 6
            let diff = (day >= 6)
                ? day - 6
                : 7 - (6 - day);

            today.setDate(today.getDate() - diff);

            return today;
        }


        function formatDate(date) {

            let year = date.getFullYear();
            let month = String(date.getMonth() + 1).padStart(2, '0');
            let day = String(date.getDate()).padStart(2, '0');

            return `${year}-${month}-${day}`;
        }


        function setNextFriday(saturdayDate) {

            let friday = new Date(saturdayDate);

            friday.setDate(friday.getDate() + 6);

            toDateInput.value = formatDate(friday);
        }


        let lastSaturday = getLastSaturday();

        fromDateInput.value = formatDate(lastSaturday);

        setNextFriday(lastSaturday);


        fromDateInput.addEventListener('change', function () {

            let selectedDate = new Date(this.value);

            // Saturday = 6
            if (selectedDate.getDay() !== 6) {

                alert('Only Saturday can be selected');

                this.value = formatDate(lastSaturday);

                setNextFriday(lastSaturday);

                return;
            }

            setNextFriday(selectedDate);
        });


        document.getElementById('profitForm').addEventListener('submit', function () {

            const btn = document.getElementById('submit_btn');

            btn.disabled = true;

            btn.innerHTML = `
                <span class="spinner-border spinner-border-sm me-2"></span>
                Processing...
            `;
        });

    </script>
@endsection