@extends('template.app') 

@section('meta')
@endsection

@section('title')
Liqudation Process Form
@endsection

@section('styles')
    <link href="{{ asset('assets/css/dropify.min.css') }}" rel="stylesheet">
    <link href="{{ asset('assets/css/sweetalert2.bundle.css') }}" rel="stylesheet">
    <link href="{{ asset('assets/css/select2.css') }}" rel="stylesheet">
    <style type="text/css">
        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 100px;
        }
    
        table, th, td {
            border: 1px solid white;
        }
    
        th {
            font-weight: bold;
            background-color: #f2f2f2; /* Optional: light background for header */
            color: black;
        }
    
        td, th {
            padding: 8px;
            text-align: center;
        }
        
        .custom_margin{
            margin-top: 5rem;
            margin-bottom: 1rem;
        }
        
        .text-right {
            text-align: right;
        }
        
        .text-left {
            text-align: left;
        }
    </style>
@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">Liqudation Process Form</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('liquidation') }}" class="text-muted">Liquidation</a></li>
                      <li class="breadcrumb-item text-muted active" aria-current="page">Process Form</li>
                   </ol>
                </nav>
             </div>
          </div>
          <div class="col-5 align-self-center">
             <div class="customize-input float-right">
             </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('liquidation.process.insert') }}" name="form" id="form" method="post" enctype="multipart/form-data">
                            @csrf
                            @method('POST')
                            
                            @php
                                $net_customer_payable_amount = 0;
                            
                                foreach ($cash_receipt_data as $val) {
                                    if (in_array($val->receipt_type, ['booking_receipt','downpayment_receipt','access_sort_receipt','other_receipt'])) {
                                        $net_customer_payable_amount += $val->amount;
                                    }
                                }
                                
                                $net_bank_payable_amount = 0;
                            
                                foreach ($cash_receipt_data as $val) {
                                    if (in_array($val->receipt_type, ['disbusment_receipt'])) {
                                        $net_bank_payable_amount += $val->amount;
                                    }
                                }
                            @endphp
                            
                            <input type="hidden" name="id" value="{{ $data->id ?? '' }}">
                            <input type="hidden" name="total_received_amount" value="{{ $net_customer_payable_amount + $net_bank_payable_amount }}">
                            <input type="hidden" name="liquidation_cash_receipt_data_id" id="liquidation_cash_receipt_data_id" value="{{ $liquidation_cash_receipt_data->id ?? '' }}" />
                            
                            <h4 class="mb-3">Receipt Details</h4>
                            <table>
                                <thead>
                                    <tr>
                                        <th>No.</th>
                                        <th>Receipt Type</th>
                                        <th>Payment Type</th>
                                        <th>Transaction No.</th>
                                        <th>Amount</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach($cash_receipt_data as $key => $val)
                                        <tr>
                                            <td>{{ $key+1 }}</td>
                                            <td>{{ ucwords(str_replace('_',' ',$val->receipt_type)) }}</td>
                                            <td>{{ ucwords(str_replace('_',' ',$val->payment_type)) }}</td>
                                            <td>{{ $val->transaction_no }}</td>
                                            <td>{{ $val->amount }}</td>
                                        </tr>
                                    @endforeach
                                </tbody>
                                <tfoot>
                                    <th colspan="4">Total</th>
                                    <th colspan="4">{{ $net_customer_payable_amount + $net_bank_payable_amount }}</th>
                                </tfoot>
                            </table>
                                
                            <div class="row"  style="border-bottom:1px solid white">
                                <div class="col-sm-6">
                                    <h4 class="text-left">Customer Refund Payment</h4>
                                </div>
                                <div class="col-sm-6">
                                    <p class="text-right">( Net Payable Amount: {{ number_format($net_customer_payable_amount, 0) }} )<p>
                                </div>
                            </div>
                            
                            <div class="row mt-3 customer-refund-container">
                                <div class="form-group col-sm-3">
                                    <label for="liquidation_amount">Liquidation Amount</label>
                                    <input type="number" name="liquidation_amount" id="liquidation_amount" class="form-control" value="{{ $obf_data->refund_amount ?? $liquidation_cash_receipt_data->liquidation_amount }}" readonly>
                                    <span class="kt-form__help error liquidation_amount"></span>
                                </div>
                                
                                <div class="form-group col-sm-3">
                                    <label for="paying_amount">Refund Amount</label>
                                    <input type="number" name="paying_amount" id="paying_amount" class="form-control" placeholder="Enter Amount" value="{{ $liquidation_cash_receipt_data->paying_amount ?? '' }}">
                                    <span class="kt-form__help error paying_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($liquidation_cash_receipt_data->payment_type) && 'online_transfer' == $liquidation_cash_receipt_data->payment_type) selected @endif>Online Transfer</option>                                    
                                        <option value="cash" @if(isset($liquidation_cash_receipt_data->payment_type) && 'cash' == $liquidation_cash_receipt_data->payment_type) selected @endif>Cash</option>                                    
                                        <option value="cheque" @if(isset($liquidation_cash_receipt_data->payment_type) && 'cheque' == $liquidation_cash_receipt_data->payment_type) selected @endif>Cheque</option>
                                        <option value="vallet_payment" @if(isset($liquidation_cash_receipt_data->payment_type) && 'vallet_payment' == $liquidation_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="{{ $liquidation_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="{{ $liquidation_cash_receipt_data->transaction_no ?? ''}}" >
                                    <span class="kt-form__help error otr_number"></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="{{ $liquidation_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($liquidation_cash_receipt_data) && !empty($liquidation_cash_receipt_data))
                                                    <option value="{{ $row->id }}" @if($liquidation_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">
                                    <label for="remark">Remark </label>
                                    <textarea name="remark" id="remark" class="form-control" placeholder="Enter Remarks" >{{ $liquidation_cash_receipt_data->remark ?? '' }}</textarea>
                                    <span class="kt-form__help error"></span>
                                </div>
                            </div>
                            
                            
                            <div class="row custom_margin"  style="border-bottom:1px solid white">
                                <div class="col-sm-6">
                                    <h4 class="text-left">Finance (Bank) Refund Payment</h4>
                                </div>
                                <div class="col-sm-6">
                                    <p class="text-right">( Net Payable Amount: {{ number_format($net_bank_payable_amount, 0) }} )<p>
                                </div>
                            </div>    
                            
                            <div class="row finance-refund-container">
                                <div class="form-group col-sm-3">
                                    <label for="finance_liquidation_amount">Finance Liquidation Amount</label>
                                    <input type="number" name="finance_liquidation_amount" id="finance_liquidation_amount" class="form-control" value="{{ $net_bank_payable_amount ?? $liquidation_cash_receipt_data->finance_liquidation_amount }}" readonly>
                                    <span class="kt-form__help error finance_liquidation_amount"></span>
                                </div>
                                
                                <div class="form-group col-sm-3">
                                    <label for="finance_paying_amount">Finance Refund Amount</label>
                                    <input type="number" name="finance_paying_amount" id="finance_paying_amount" class="form-control" placeholder="Enter Amount" value="{{ $liquidation_cash_receipt_data->finance_paying_amount ?? '' }}">
                                    <span class="kt-form__help error finance_paying_amount"></span>
                                </div>
                                
                                <div class="form-group col-sm-3">
                                    <label for="finance_payment_type">Finance Payment Type</label>
                                    <select class="form-control" name="finance_payment_type" id="finance_payment_type">
                                        <option>Select Payment type</option>
                                        <option value="online_transfer" @if(isset($liquidation_cash_receipt_data->finance_payment_type) && 'online_transfer' == $liquidation_cash_receipt_data->finance_payment_type) selected @endif>Online Transfer</option>                                 
                                        <option value="cheque" @if(isset($liquidation_cash_receipt_data->finance_payment_type) && 'cheque' == $liquidation_cash_receipt_data->finance_payment_type) selected @endif>Cheque</option>
                                        <option value="vallet_payment" @if(isset($liquidation_cash_receipt_data->finance_payment_type) && 'vallet_payment' == $liquidation_cash_receipt_data->finance_payment_type) selected @endif>Vallet Payment</option>  
                                    </select>
                                    <span class="kt-form__help error finance_payment_type"></span>
                                </div>
                                
                                <div class="form-group col-sm-3 finance_box cheque">
                                    <label for="otr_number">Finance Cheque Number </label>
                                    <input type="text" name="finance_chq_number" id="otr_number" class="form-control" placeholder="Enter Transaction Number" value="{{ $liquidation_cash_receipt_data->finance_transaction_no ?? '' }}" >
                                    <span class="kt-form__help error otr_number"></span>
                                </div>
    
                                <div class="form-group col-sm-3 finance_box online_transfer">
                                    <label for="otr_number">Finance Transaction Number </label>
                                    <input type="text" name="finance_trans_number" id="otr_number" class="form-control" placeholder="Enter Transaction Number" value="{{ $liquidation_cash_receipt_data->finance_transaction_no ?? ''}}" >
                                    <span class="kt-form__help error otr_number"></span>
                                </div>
                                
                                <div class="form-group col-sm-3 finance_box vallet_payment">
                                    <label for="div_vallet">Finance Vallet Number </label>
                                    <input type="text" name="finance_vallet_number" id="otr_number" class="form-control" placeholder="Enter Vallet Number" value="{{ $liquidation_cash_receipt_data->finance_transaction_no ?? '' }}" >
                                    <span class="kt-form__help error otr_number"></span>
                                </div>
                                
                                <div class="form-group col-sm-3 finance_box finance_bank_account_id">
                                    <label for="finance_bank_account_id">Refund From Bank</label>
                                    <select class="form-control" name="finance_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))
                                                    @foreach($cash_receipt_data as $key => $val)
                                                        @if($val['bank_account_id'] == $row->id)
                                                            <option value="{{ $row->id }}" selected> {{ $row->name }} </option>
                                                        @endif
                                                    @endforeach
                                                @endif
                                            @endforeach
                                        @endif
                                    </select>
                                    <span class="kt-form__help error finance_bank_account_id"></span>
                                </div>
                                
                                <div class="form-group col-sm-3">
                                    <label for="refund_to_bank">Refund To Bank</label>
                                    <input type="text" name="refund_to_bank" class="form-control" placeholder="Enter Account Number" value="{{ $liquidation_cash_receipt_data->refund_to_bank ?? '' }}" >
                                    <span class="kt-form__help error refund_to_bank"></span>
                                </div>
                                
                                <div class="form-group col-sm-3">
                                    <label for="finance_remark">Finance Remark </label>
                                    <textarea name="finance_remark" id="finance_remark" class="form-control" placeholder="Enter Remarks" >{{ $liquidation_cash_receipt_data->finance_remark ?? '' }}</textarea>
                                    <span class="kt-form__help error finance_remark"></span>
                                </div>
                            </div>
                            
                            <div class="form-group">
                                <button type="submit" onclick="confirmAction()" class="btn waves-effect waves-light btn-rounded btn-outline-primary">Submit</button>
                                <a href="{{ route('liquidation') }}" class="btn waves-effect waves-light btn-rounded btn-outline-secondary">Back</a>
                            </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(){
            const $customer = $(".customer-refund-container");
            const $paymentType = $customer.find("#payment_type");
            const initialType = $paymentType.val();
        
            if (initialType && initialType !== "cash") {
                $customer.find(".bank_account_id").show();
                $customer.find("." + initialType).show();
            } else {
                $customer.find(".bank_account_id").hide();
            }
        
            $paymentType.change(function () {
                const selectedType = $(this).val();
                $customer.find(".box").hide();
        
                if (selectedType === "cash") {
                    $customer.find(".bank_account_id").hide();
                } else if (selectedType) {
                    $customer.find("." + selectedType).show();
                    $customer.find(".bank_account_id").show();
                }
            }).trigger('change');
        
        
            // Finance Section
            const $finance = $(".finance-refund-container");
            const $financeType = $finance.find("#finance_payment_type");
            const initialFinanceType = $financeType.val();
        
            if (initialFinanceType && initialFinanceType !== "cash") {
                $finance.find(".finance_bank_account_id").show();
                $finance.find("." + initialFinanceType).show();
            } else {
                $finance.find(".finance_bank_account_id").hide();
            }
        
            $financeType.change(function () {
                const selectedFinanceType = $(this).val();
                $finance.find(".finance_box").hide();
        
                if (selectedFinanceType === "cash") {
                    $finance.find(".finance_bank_account_id").hide();
                } else if (selectedFinanceType) {
                    $finance.find("." + selectedFinanceType).show();
                    $finance.find(".finance_bank_account_id").show();
                }
            }).trigger('change');
        });
   </script>
@endsection