<?php

// echo "<pre>";
// print_r($data);

/*foreach ($data as $value) {
}*/

/*  var_dump($data);

if(!empty($data)){
    echo "found";

    var_dump(count($data));

}else{
    echo "not found";
}

 die('fd');
*/


?>


@extends('template.app')

@section('meta')
@endsection

@section('title')
    Cash Receipt Generate
@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>
                        @csrf
                        @method('POST')

                        @if(count($data) > 0)
                        @foreach($data as $data)
                        <div class="row" id="print">
                            <div class="col-md-12">
                                <h1 class="text-center">Payment Receipt</h1>
                            </div>
                            <div class="form-group col-sm-3">
                                <label for="receipt_no"><b>Receipt No:</b></label>
                                <span class="form-lable">{{ $data->receipt_no ?? 'N/A' }}</span>
                            </div>
                            <div class="form-group col-sm-3">
                                <label for="customer_name"><b>Customer Name:</b></label>
                                <span class="form-lable">{{ str_replace(['Bill To: ', 'Ship To: '], '', $data->customer_nm) }}</span>
                            </div>
                            
                            <div class="form-group col-sm-3">
                                <label for="model_name"><b>Model Name:</b></label>
                                <span class="">{{ $data->car_model_name ?? 'N/A' }}</span>
                            </div>
                            
                            <div class="form-group col-sm-3">
                                <label for="model_name"><b>Variant:</b></label>
                                <span class="">{{ $data->car_varient_name ?? 'N/A' }}</span>
                            </div>
                            
                            <div class="form-group col-sm-3">
                                <label for="model_name"><b>Exterior Color:</b></label>
                                <span class="">{{ $data->exteriorcolor_name ?? 'N/A' }}</span>
                            </div>
                            
                            <div class="form-group col-sm-3">
                                <label for="model_name"><b>Interior Color:</b></label>
                                <span class="">{{ $data->interiorcolor_name ?? 'N/A' }}</span>
                            </div>
                            
                            <!--<div class="form-group col-sm-3">
                                <label for="model_name"><b>Ex showroom Price:</b></label>
                                <span class="">{{ $data->ex_showroom_price ?? 'N/A' }}</span>
                            </div>
                            
                            <div class="form-group col-sm-3">
                                <label for="model_name"><b>Extended Warrnaty years:</b></label>
                                <span class="">{{ $data->extand_warranties_years ?? 'N/A' }}</span>
                            </div>
                            
                            <div class="form-group col-sm-3">
                                <label for="model_name"><b>registration Tax:</b></label>
                                <span class="">{{ $data->registration_tax ?? 'N/A' }}%</span>
                            </div>-->
                        
                            <div class="form-group col-sm-3">
                                <label for="booking_amount"><b>Amount:</b></label>
                                <span class="">{{ $data->amount ?? 'N/A' }}</span>
                            </div>
                            <div class="form-group col-sm-3">
                                <label for="booking_amount"><b>Finance Name:</b></label>
                                <span class="">{{ $data->finance_name ?? 'N/A' }}</span>
                            </div>
                            
                            <!--<div class="form-group col-sm-3">-->
                            <!--    <label for="remark"><b>Remark:</b></label>-->
                            <!--    <span class="">{{ $data->reason ?? 'N/A' }}</span>-->
                            <!--</div>-->
                            
                            <div class="form-group col-sm-3">
                                <label for="remark"><b>Remark:</b></label>
                                <span class="">{{ $data->cash_remark ?? 'N/A' }}</span>
                            </div>

                            <div class="form-group col-sm-3">
                                <label for="remark"><b>Payment Type:</b></label>
                                <?php
                                    $payment_type = '';
                                    if($data->cash_receipt_payment_type == 'online_transfer'){
                                        $payment_type = 'Online Transfer';
                                    }elseif ($data->cash_receipt_payment_type == 'cash') {
                                        $payment_type = 'Cash';
                                    }elseif ($data->cash_receipt_payment_type == 'cheque') {
                                        $payment_type = 'Cheque';
                                    }elseif ($data->cash_receipt_payment_type == 'vallet_payment') {
                                        $payment_type = 'Vallet Payment';
                                    }
                                ?> 
                                <span class="">{{ $payment_type ?? 'N/A' }}</span>
                            </div>
                        </div>
                        <div class="form-group">
                            <!-- <span onclick="printDiv('#print')" class="btn waves-effect waves-light btn-rounded btn-outline-primary">Print</span> -->
                            <a title="Recipt" href="<?php echo route('cash_receipt.recipt_pdf', ['id' => base64_encode($data->id),'cash_receipt_id' => base64_encode($data->cash_receipt_id),'page' => $page]); ?>" class="btn waves-effect waves-light btn-rounded btn-outline-secondary" target="_blank"> Print </a>
                            <?php $url=route('cash_receipt.recipt_pdf_share', ['cash_receipt_id' => base64_encode($data->cash_receipt_id)]);  ?>
                        @if($data->contact_number)
                            <a href="https://api.whatsapp.com/send?phone=+91{{$data->contact_number}}&text=Use This link to download your cash recipt  :   {{$url}}" target="_blank"><i class="fa fa-share-alt"></i></a>
                        @endif   
                        @if(auth()->user()->roles[0]['name'] == 'Accounts Manager' || auth()->user()->roles[0]['name'] == 'admin')
                            <a title="Add Cash" href="<?php echo route('cash_receipt.create', ['id' => base64_encode($data->id),'cash_receipt_id' => base64_encode($data->cash_receipt_id),'page' => $page]); ?>" class="btn waves-effect waves-light btn-rounded btn-outline-secondary"> Edit receipt </a> 

                            <a title="Add Cash" href="javascript:void(0)" class="btn waves-effect waves-light btn-rounded btn-outline-secondary" onclick="change_status('<?php echo base64_encode($data->cash_receipt_id); ?>')"> Delete receipt </a> 
                        @endif
                            @if($page == 'cash_receipt')
                            <a href="{{ route('cash_receipt') }}" class="btn waves-effect waves-light btn-rounded btn-outline-secondary">Back</a>
                            @else
                            <a href="{{ route('order') }}" class="btn waves-effect waves-light btn-rounded btn-outline-secondary">Back</a>
                            @endif
                        </div>
                        @endforeach
                        @else
                        <div class="row">
                            Cash receipt not found
                        </div>

                        @endif

                    </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 printDiv(elem){
        renderMe($('<div/>').append($(elem).clone()).html());
    }
    function renderMe(data) {
        var link = "{{ asset('assets/css/sb-admin-2.min.css') }}";
        var mywindow = window.open('', 'invoice-box', 'height=1000,width=1000');
        mywindow.document.write('<html><head><title>invoice-box</title>');
        mywindow.document.write('<link rel="stylesheet" href="'+link+'" type="text/css" />');
        mywindow.document.write('</head><body > <h1>Cash Receipt</h1> ');
        mywindow.document.write(data);
        mywindow.document.write('</body></html>');


        setTimeout(function () {
        mywindow.print();
        mywindow.close();
        }, 1000)
        return true;
    }

    function change_status(id){
        var id = id;

        var status = 'rejected';

        if (confirm('Are you sure you want to delete?')) {
            $.ajax({
                "url": "{!! route('cash_receipt.change_status_delete') !!}",
                "dataType": "json",
                "type": "POST",
                "data":{
                    id: id,
                    status: status,
                    _token: "{{ csrf_token() }}"
                },
                success: function (response){
                    if (response.code == 200){
                        // datatable.ajax.reload();
                        window.location.reload();    

                        toastr.success('Status chagned successfully', 'Success');
                    }else{
                        toastr.error('Failed to change status', 'Error');
                    }
                }
            });
        }
    }
</script>
@endsection