@extends('layout.app')


@section('content')
    <div class="card card-default">
        <div class="card-body">

            <form action="{{ route('resellers.store') }}" method="POST" enctype="multipart/form-data" id="resellerForm">
                @csrf
                <div class="row">
                    <div class="form-group col-md-4">
                        <label for="name" class="block text-sm font-medium text-gray-700">
                            Manager Name*
                        </label>
                        <input type="text" name="name" id="name" value="{{ old('name') }}" class="form-control"
                            required placeholder="Manager Name" autocomplete="off">
                        @error('name')
                            <div class="alert alert-danger">{{ $message }}</div>
                        @enderror
                    </div>

                    <div class="form-group col-md-4">
                        <label for="name" class="block text-sm font-medium text-gray-700">
                            Manager Type*
                        </label>
                        <select name="reseller_type" id="reseller_type" class="select2 form-control" required>
                            <option value="">Select Type</option>
                            <option value="other">Other</option>
                            <option value="own">Own</option>
                        </select>
                        @error('reseller_type')
                            <div class="alert alert-danger">{{ $message }}</div>
                        @enderror
                    </div>

                    <div class="form-group col-md-4">
                        <label for="contact" class="block text-sm font-medium text-gray-700">
                            Contact*
                        </label>

                        <input type="text" name="contact" id="contact" class="form-control" placeholder="Contact"
                            required value="{{ old('contact') }}" autocomplete="off">
                        @error('contact')
                            <div class="alert alert-danger">
                                {{ $message }}
                            </div>
                        @enderror
                    </div>

                    <div class="form-group col-md-4">
                        <label for="address" class="block text-sm font-medium text-gray-700">
                            Address*
                        </label>

                        <input type="text" name="address" id="address" class="form-control" placeholder="Address"
                            required value="{{ old('address') }}" autocomplete="off">

                        @error('address')
                            <div class="alert alert-danger">{{ $message }}</div>
                        @enderror
                    </div>

                    <div class="form-group col-md-4">
                        <label for="remark" class="block text-sm font-medium text-gray-700">
                            Remark
                        </label>
                        <div class=" flex rounded-md shadow-sm">

                            <input type="text" name="remark" id="remark" class="form-control" placeholder="remark"
                                value="{{ old('remark') }}" autocomplete="off">
                        </div>
                        @error('remark')
                            <div class="alert alert-dangers">
                                {{ $message }}
                            </div>
                        @enderror
                    </div>

                    <div class="form-group d-none">
                        <label for="billable" class="block text-sm font-medium text-gray-700">
                            Billable
                        </label>
                        <div class="mt-1 flex rounded-md shadow-sm">

                            <select name="billable" id="billable">
                                <option value="yes">YES</option>
                                <option value="no">No</option>
                            </select>
                        </div>
                        @error('billable')
                            <div class="text-red-500 mt-2 text-sm">
                                {{ $message }}
                            </div>
                        @enderror
                    </div>
                    @if (checkSettings('reseller-image') == 'enable')
                        @can('reseller-image-upload')
                            <div class="form-group col-md-4">
                                <label for="name" class="block text-sm font-medium text-gray-700">
                                    Upload File
                                </label>
                                <input type="file" name="img_url" value="{{ old('img_url') }}" class="form-control"
                                    placeholder="image" autocomplete="off" accept="image/*">
                                @error('img_url')
                                    <div class="alert alert-danger">{{ $message }}</div>
                                @enderror
                            </div>
                        @endcan
                    @endif
                    @if (checkSettings('manager-commission-percentage') === 'enable')
                        @can('set-manager-commission')
                            <div class="form-group col-md-4">
                                <label for="remark" class="block text-sm font-medium text-gray-700">
                                    Manager Miscellaneous Expense
                                </label>
                                <div class=" flex rounded-md shadow-sm">

                                    <input type="number" step=".01" name="commission_percentage" id="commission_percentage"
                                        class="form-control" placeholder="Commission in percentage"
                                        value="{{ old('commission_percentage') }}" autocomplete="off">
                                </div>
                                @error('commission_percentage')
                                    <div class="alert alert-dangers">
                                        {{ $message }}
                                    </div>
                                @enderror
                            </div>
                        @endcan
                    @endif

                    @if (checkSettings('vat_deduct_from_payment_for_commission_calculation') === 'enable')
                        <div class="form-group col-md-4">
                            <label for="vat" class="block text-sm font-medium text-gray-700">
                                VAT (%)
                            </label>
                            <div class=" flex rounded-md shadow-sm">
                                <input type="number" step=".01" name="vat" id="vat" class="form-control"
                                    placeholder="VAT Percentage" value="{{ old('vat', 0) }}" autocomplete="off">
                            </div>
                            @error('vat')
                                <div class="alert alert-danger">
                                    {{ $message }}
                                </div>
                            @enderror
                        </div>
                    @endif
                    @if (checkSettings('manager_bank_info') === 'enable')
                        <div class="form-group col-md-4">
                            <label for="bank_name" class="block text-sm font-medium text-gray-700">
                                Bank Name
                            </label>
                            <div class=" flex rounded-md shadow-sm">
                                <input type="text" name="bank_name" id="bank_name" class="form-control"
                                    placeholder="Bank Name" value="{{ old('bank_name') }}" autocomplete="off">
                            </div>
                            @error('bank_name')
                                <div class="alert alert-danger">
                                    {{ $message }}
                                </div>
                            @enderror
                        </div>
                        <div class="form-group col-md-4">
                            <label for="branch_name" class="block text-sm font-medium text-gray-700">
                                Branch Name
                            </label>
                            <div class=" flex rounded-md shadow-sm">
                                <input type="text" name="branch_name" id="branch_name" class="form-control"
                                    placeholder="Branch Name" value="{{ old('branch_name') }}" autocomplete="off">
                            </div>
                            @error('branch_name')
                                <div class="alert alert-danger">
                                    {{ $message }}
                                </div>
                            @enderror
                        </div>
                        <div class="form-group col-md-4">
                            <label for="routing_number" class="block text-sm font-medium text-gray-700">
                                Routing Number
                            </label>
                            <div class=" flex rounded-md shadow-sm">
                                <input type="text" name="routing_number" id="routing_number" class="form-control"
                                    placeholder="Routing Number" value="{{ old('routing_number') }}" autocomplete="off">
                            </div>
                            @error('routing_number')
                                <div class="alert alert-danger">
                                    {{ $message }}
                                </div>
                            @enderror
                        </div>
                        <div class="form-group col-md-4">
                            <label for="bank_account_no" class="block text-sm font-medium text-gray-700">
                                Bank Account No
                            </label>
                            <div class=" flex rounded-md shadow-sm">
                                <input type="text" name="bank_account_no" id="bank_account_no" class="form-control"
                                    placeholder="Bank Account No" value="{{ old('bank_account_no') }}" autocomplete="off">
                            </div>
                            @error('bank_account_no')
                                <div class="alert alert-danger">
                                    {{ $message }}
                                </div>
                            @enderror
                        </div>
                        <div class="form-group col-md-4">
                            <label for="account_name" class="block text-sm font-medium text-gray-700">
                                Account Name
                            </label>
                            <div class=" flex rounded-md shadow-sm">
                                <input type="text" name="account_name" id="account_name" class="form-control"
                                    placeholder="Account Name" value="{{ old('account_name') }}" autocomplete="off">
                            </div>
                            @error('account_name')
                                <div class="alert alert-danger">
                                    {{ $message }}
                                </div>
                            @enderror
                        </div>
                    @endif


                </div>

                <hr>
                @if (checkSettings('reseller-online-recharge') == 'enable')
                    <div class="form-group col-md-4">
                        <label for="payment_charges">Gateway Charges In %</label>

                        <table class="table table-responsive table-condensed">
                            <tr>
                                <td>Bkash</td>
                                <td>
                                    <input type="number" step="any" name="bkash_charges" id="bkash_charges"
                                        class="form-control" placeholder="Bkash Charges"
                                        value="{{ old('bkash_charges') }}" autocomplete="off">
                                </td>
                            </tr>
                            <tr>
                                <td>Nagad</td>
                                <td>
                                    <input type="number" step="any" name="nagad_charges" id="nagad_charges"
                                        class="form-control" placeholder="Nagad Charges"
                                        value="{{ old('nagad_charges') }}" autocomplete="off">
                                </td>
                            </tr>
                            <tr>
                                <td>Upay</td>
                                <td>
                                    <input type="number" step="any" name="upay_charges" id="upay_charges"
                                        class="form-control" placeholder="Upay Charges"
                                        value="{{ old('upay_charges') }}" autocomplete="off">
                                </td>
                            </tr>
                        </table>
                    </div>
                @endif

                {{-- <div class="col-sm-6">
                    <label for="">Package List</label>
                    @foreach ($packages as $package)
                        <br>
                        <label for="package{{ $package->id }}">
                            <input type="checkbox" id="package{{ $package->id }}" name="package[]"
                                value="{{ $package->id }}"> {{ $package->package_name }}
                        </label>
                    @endforeach
                </div> --}}
                <div class="col-sm-12">


                    <div class="form-group float-right">
                        <button class="btn btn-primary" type="submit"><i class="fa fa-save"></i> Save</button>
                        <button class="btn btn-info" type="reset" id="reset">Reset</button>
                    </div>

                </div>


            </form>


        </div>


    </div>
@endsection
@section('ajax')
    <script>
        $(document).ready(function() {
            $('#resellerForm').on('submit', function(e) {
                e.preventDefault();
                var formData = new FormData(this);

                let resellerType = $('#reseller_type').val();

                // Validate commission if type is own (commented out - validation in controller)
                // if (resellerType === 'own' &&
                //     {{ auth()->user()->can('set-manager-commission') ? 'true' : 'false' }} &&
                //     {{ checkSettings('manager-commission-percentage') === 'enable' ? 'true' : 'false' }}) {
                //     let commission = $('#commission_percentage').val();
                //     if (!commission || commission <= 0) {
                //         $('#commission_percentage').focus();
                //         toastr.error('For Own type, Manager Miscellaneous Expense must be greater than 0.');
                //         return;
                //     }
                // }

                // submit the form if validation passes not using ajax
                this.submit();

            });
        });
    </script>
@endsection
