@extends('template.app')

@section('meta')
@endsection

@section('title')
    Cash Receipt Create
@endsection

@section('styles')
<link href="{{ asset('assets/css/dropify.min.css') }}" rel="stylesheet">
<link href="{{ asset('assets/css/sweetalert2.bundle.css') }}" rel="stylesheet">
@endsection

@section('content')
<div class="page-breadcrumb">
    <div class="row">
        <div class="col-7 align-self-center">
            <h4 class="page-title text-truncate text-dark font-weight-medium mb-1">Cash Receipt Master</h4>
            <div class="d-flex align-items-center">
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb m-0 p-0">
                        <li class="breadcrumb-item"><a href="{{ route('dashboard') }}" class="text-muted">Dashboard</a></li>
                        <li class="breadcrumb-item"><a href="{{ route('cash_receipt') }}" class="text-muted">Cash Receipt Master</a></li>
                        <li class="breadcrumb-item text-muted active" aria-current="page">Create</li>
                    </ol>
                </nav>
            </div>
        </div>
    </div>
</div>
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-body">
                    <form action="{{ route('cash_receipt.insert') }}" name="form" id="form" method="post" enctype="multipart/form-data">
                        @csrf
                        @method('POST')

                        <div class="row">
                            <input type="hidden" name="id" value="{{ $data->id ?? '' }}">
                            <input type="hidden" name="page" value="{{ $page ?? '' }}">
                            <input type="hidden" name="cash_receipt_data_id" id="cash_receipt_data_id" value="{{ $cash_receipt_data->id ?? '' }}" />
                            
                            <div class="form-group col-sm-3">
                                <label for="payment_type">Customer Name</label>
                                <select class="form-control" name="customer_nm" id="customer_nm">
                                    <option value="">Select Customer Name</option>
                                    @foreach($customer_options as $option)
                                        <option value="{{ $option }}" @if(isset($cash_receipt_data->customer_nm) && $option == $cash_receipt_data->customer_nm) selected @endif>{{ $option }}</option>
                                    @endforeach
                                </select>
                                <span class="kt-form__help error payment_type"></span>
                            </div>
                            
                            <div class="form-group col-sm-3">
                                <label for="amount">Receipt Type</label>
                                <select class="form-control" name="receipt_type" id="receipt_type">
                                    <option>Select Receipt type</option>
                                    
                                    <option value="booking_receipt" @if(isset($cash_receipt_data->receipt_type) && 'booking_receipt' == $cash_receipt_data->receipt_type) selected @endif>Booking Receipt</option>             
                                    
                                    <option value="downpayment_receipt" @if(isset($cash_receipt_data->receipt_type) && 'downpayment_receipt' == $cash_receipt_data->receipt_type) selected @endif>Down payment Receipt</option>
                                    
                                    <option value="disbusment_receipt" @if(isset($cash_receipt_data->receipt_type) && 'disbusment_receipt' == $cash_receipt_data->receipt_type) selected @endif>Disbusment Receipt</option> 
                                    
                                    <option value="access_sort_receipt" @if(isset($cash_receipt_data->receipt_type) && 'access_sort_receipt' == $cash_receipt_data->receipt_type) selected @endif>Access / Sort Receipt</option>
                                    
                                    <option value="other_receipt" @if(isset($cash_receipt_data->receipt_type) && 'other_receipt' == $cash_receipt_data->receipt_type) selected @endif>Other Receipt</option>
                                </select>
                                <span class="kt-form__help error amount"></span>
                            </div>
                            
                            <div class="form-group col-sm-3">
                                <label for="amount">Amount</label>
                                <input type="number" name="amount" id="amount" class="form-control" placeholder="Enter Amount" value="{{ $cash_receipt_data->amount ?? '' }}">
                                <span class="kt-form__help error amount"></span>
                            </div>
                            <div class="form-group col-sm-3">
                                <label for="payment_type">Payment Type</label>
                                <select class="form-control" name="payment_type" id="payment_type">
                                    <option>Select Payment type</option>
                                    <option value="online_transfer" @if(isset($cash_receipt_data->payment_type) && 'online_transfer' == $cash_receipt_data->payment_type) selected @endif>Online Transfer</option>                                    
                                    <option value="cash" @if(isset($cash_receipt_data->payment_type) && 'cash' == $cash_receipt_data->payment_type) selected @endif>Cash</option>                                    
                                    <option value="cheque" @if(isset($cash_receipt_data->payment_type) && 'cheque' == $cash_receipt_data->payment_type) selected @endif>Cheque</option>
                                    <option value="vallet_payment" @if(isset($cash_receipt_data->payment_type) && 'vallet_payment' == $cash_receipt_data->payment_type) selected @endif>Vallet Payment</option>  
                                </select>
                                <span class="kt-form__help error payment_type"></span>
                            </div>
                            
                            <div class="form-group col-sm-3 box cheque">
                                <label for="otr_number">Cheque Number </label>
                                <input type="text" name="chq_number" id="otr_number" class="form-control" placeholder="Enter Transaction Number" value="{{ $cash_receipt_data->transaction_no ?? '' }}" >
                                <span class="kt-form__help error otr_number"></span>
                            </div>

                            <div class="form-group col-sm-3 box online_transfer">
                                <label for="otr_number">Transaction Number </label>
                                <input type="text" name="trans_number" id="otr_number" class="form-control" placeholder="Enter Transaction Number" value="{{ $cash_receipt_data->transaction_no ?? ''}}" >
                                <span class="kt-form__help error otr_number"></span>
                            </div>
                            
                            
                            <div class="form-group col-sm-3 box bank_account_id">
                                <label for="bank_account_id">Bank Account Number </label>
                                <select class="form-control" name="bank_account_id">
                                    <option value="">Select Account Number</option>
                                    @if(isset($bank_account) && $bank_account->isNotEmpty())
                                        @foreach($bank_account as $row)
                                            @if(isset($cash_receipt_data) && !empty($cash_receipt_data))
                                                <option value="{{ $row->id }}" @if($cash_receipt_data['bank_account_id'] == $row->id) selected @endif> {{ $row->name }} </option>
                                            @else
                                                <option value="{{ $row->id }}"> {{ $row->name }} </option>
                                            @endif
                                        @endforeach
                                    @endif
                                </select>
                                <span class="kt-form__help error bank_account_id"></span>
                            </div>
                            
                            <div class="form-group col-sm-3 box vallet_payment">
                                <label for="div_vallet">Vallet Number </label>
                                <input type="text" name="vallet_number" id="otr_number" class="form-control" placeholder="Enter Vallet Number" value="{{ $cash_receipt_data->transaction_no ?? '' }}" >
                                <span class="kt-form__help error otr_number"></span>
                            </div>
                            
                            <div class="form-group col-sm-3">
                                <label for="remark">Remark </label>
                                <textarea name="cash_remark" id="cash_remark" class="form-control" placeholder="Enter Remarks" >{{ $cash_receipt_data->cash_remark ?? '' }}</textarea>
                                <span class="kt-form__help error"></span>
                            </div>
                            
                        </div>
                        <div class="form-group">
                            <button type="submit" onclick="confirmAction()" class="btn waves-effect waves-light btn-rounded btn-outline-primary">Save</button>
                            <a href="{{ route('cash_receipt.generate_cash_receipt') }}" class="btn waves-effect waves-light btn-rounded btn-outline-secondary">Save & Preview</a>
                            @if($page == 'cash_receipt')
                            <a href="{{ route('cash_receipt') }}" class="btn waves-effect waves-light btn-rounded btn-outline-secondary">Save & Exit</a>
                            @else
                            <a href="{{ route('order') }}" class="btn waves-effect waves-light btn-rounded btn-outline-secondary">Save & Exit</a>
                            @endif
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@section('scripts')
<script src="{{ asset('assets/js/promise.min.js') }}"></script>
<script src="{{ asset('assets/js/sweetalert2.bundle.js') }}"></script>
<script>
    function confirmAction() {
        // Prompt the user for confirmation
        var confirmation = confirm("Are you sure you want to proceed?");
        
        // If user confirms, perform the action
        if (confirmation) {
            // Perform your action here
            alert("Action performed!");
        } else {
            // Action canceled
            alert("Action canceled!");
        }
    }
</script>
  <script>
       $(document).ready(function(){

        //     $("select").change(function(){
        //     $(this).find("option:selected").each(function(){
        //         var optionValue = $(this).attr("value");
        //         if(optionValue){
        //             $(".box").not("." + optionValue).hide();
        //             $("." + optionValue).show();
        //         } else{
        //             $(".box").hide();
        //         }
        //     });
        // }).change();
        var initialType = $("#payment_type").val();

        if (initialType && initialType !== "cash") {
            $(".bank_account_id").show();
            $("." + initialType).show();
        } else {
            $(".bank_account_id").hide();
        }
        
        $("#payment_type").change(function () {
            var selectedType = $(this).val();
    
            // Hide all conditional boxes
            $(".box").hide();
    
            // Show the related box
            if (selectedType === "cash") {
                $(".bank_account_id").hide(); // Hide bank account for cash
            } else if (selectedType) {
                $("." + selectedType).show(); // Show specific payment type box
                $(".bank_account_id").show(); // Show bank account for other types
            }
        }).trigger('change');
         
    });
   </script>
@endsection