@extends('base.base')

@section('base')
    <div class="main-panel">
        <div class="content-wrapper">
            <div class="page-header">
                <h3 class="page-title">
                <span class="page-title-icon bg-gradient-primary text-white mr-2">
                    <i class="mdi mdi-tag-text-outline"></i>
                </span>
                    {{ __('auto.Reward Code Management') }}
                </h3>
            </div>

            <form class="form-inline mb-3" method="get" action="">
                <div class="form-group mr-2">
                    <label class="mr-1">{{ __('auto.Code') }}</label>
                    <input type="text" name="code" class="form-control" value="{{ $code ?? '' }}" placeholder="e.g. AB12">
                </div>
                <div class="form-group mr-2">
                    <label class="mr-1">{{ __('auto.Status') }}</label>
                    <select name="status" class="form-control">
                        <option value="">{{ __('auto.All') }}</option>
                        <option value="1" @if($status==='1') selected @endif>{{ __('auto.Enabled') }}</option>
                        <option value="0" @if($status==='0') selected @endif>{{ __('auto.Disabled') }}</option>
                    </select>
                </div>
                <div class="form-group mr-2">
                    <label class="mr-1">{{ __('auto.Expired') }}</label>
                    <select name="expired" class="form-control">
                        <option value="">{{ __('auto.All') }}</option>
                        <option value="0" @if($expired==='0') selected @endif>{{ __('auto.Active') }}</option>
                        <option value="1" @if($expired==='1') selected @endif>{{ __('auto.Expired') }}</option>
                    </select>
                </div>
                <button type="submit" class="btn btn-primary">{{ __('auto.Search') }}</button>
            </form>

            @if(session('success'))
                <div class="alert alert-success">{{ session('success') }}</div>
            @endif

            <div class="card mb-4">
                <div class="card-header">{{ __('auto.Create Reward Code') }}</div>
                <div class="card-body">
                    <form method="post" action="/admin/reward-code">
                        @csrf
                        <div class="form-row">
                            <div class="form-group col-md-2">
                                <label>{{ __('auto.Total Amount') }}</label>
                                <input type="number" name="total_amount" class="form-control" min="0.01" step="0.01" required>
                            </div>
                            <div class="form-group col-md-2">
                                <label>{{ __('auto.Min Amount') }}</label>
                                <input type="number" name="min_amount" class="form-control" min="0.01" step="0.01" required>
                            </div>
                            <div class="form-group col-md-2">
                                <label>{{ __('auto.Max Amount') }}</label>
                                <input type="number" name="max_amount" class="form-control" min="0.01" step="0.01" required>
                            </div>
                            <div class="form-group col-md-2">
                                <label>{{ __('auto.Total Count') }}</label>
                                <input type="number" name="total_count" class="form-control" min="1" required>
                            </div>
                            <div class="form-group col-md-2">
                                <label>{{ __('auto.Expire At') }}</label>
                                <input type="datetime-local" name="expire_at" class="form-control">
                            </div>
                        </div>
                        <div class="form-row">
                            <div class="form-group col-md-12">
                                <label>{{ __('auto.Remark') }}</label>
                                <input type="text" name="remark" class="form-control" maxlength="255">
                            </div>
                        </div>
                        <button type="submit" class="btn btn-success">{{ __('auto.Create One Code') }}</button>
                        <small class="text-muted ml-2">{{ __('auto.Total amount must be >= min * total count; min <= max.') }}</small>
                    </form>
                </div>
            </div>

            <div class="card">
                <div class="card-header">{{ __('auto.Reward Code List') }}</div>
                <div class="card-body table-responsive">
                    <table class="table table-bordered table-sm">
                        <thead>
                        <tr>
                            <th>{{ __('auto.ID') }}</th>
                            <th>{{ __('auto.Code') }}</th>
                            <th>{{ __('auto.Status') }}</th>
                            <th>{{ __('auto.Expire At') }}</th>
                            <th>{{ __('auto.Total Amount') }}</th>
                            <th>{{ __('auto.Claimed Amount') }}</th>
                            <th>{{ __('auto.Total Count') }}</th>
                            <th>{{ __('auto.Claimed Count') }}</th>
                            <th>{{ __('auto.Min') }}</th>
                            <th>{{ __('auto.Max') }}</th>
                            <th>{{ __('auto.Remark') }}</th>
                            <th>{{ __('auto.Created At') }}</th>
                            <th>{{ __('auto.Action') }}</th>
                        </tr>
                        </thead>
                        <tbody>
                        @foreach($list as $item)
                            <tr>
                                <td>{{ $item->id }}</td>
                                <td><strong><a href="/admin/reward-code/history?code={{ $item->code }}" class="text-primary">{{ $item->code }}</a></strong></td>
                                <td>{{ $item->status ? __('auto.Enabled') : __('auto.Disabled') }}</td>
                                <td>{{ $item->expire_at }}</td>
                                <td>{{ $item->total_amount }}</td>
                                <td>{{ $item->claimed_amount }}</td>
                                <td>{{ $item->total_count }}</td>
                                <td>{{ $item->claimed_count }}</td>
                                <td>{{ $item->min_amount }}</td>
                                <td>{{ $item->max_amount }}</td>
                                <td>{{ $item->remark }}</td>
                                <td>{{ $item->created_at }}</td>
                                <td>
                                    <form method="post" action="/admin/reward-code/{{ $item->id }}/status" style="display:inline-block;">
                                        @csrf
                                        <input type="hidden" name="status" value="{{ $item->status ? 0 : 1 }}">
                                        <button type="submit" class="btn btn-sm {{ $item->status ? 'btn-warning' : 'btn-success' }}">
                                            {{ $item->status ? __('auto.Disable') : __('auto.Enable') }}
                                        </button>
                                    </form>
                                </td>
                            </tr>
                        @endforeach
                        </tbody>
                    </table>
                    {{ $list->links() }}
                </div>
            </div>
        </div>
    </div>
@endsection