@extends('layouts.master')
@section('title','Pengeluaran')
@section('content')
        
        <div class="breadcrumbs">
            <div class="breadcrumbs-inner">
                <div class="row m-0">
                    <div class="col-sm-4">
                        <div class="page-header float-left">
                            <div class="page-title">
                                <h1>Pengeluaran</h1>
                            </div>
                        </div>
                    </div>
                    <div class="col-sm-8">
                        <div class="page-header float-right">
                            <div class="page-title">
                                <ol class="breadcrumb text-right">
                                    <li><a href="#">Master</a></li>
                                    <li><a href="#">Pengeluaran</a></li>
                                    <li class="active">Ubah Pengeluaran</li>
                                </ol>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!-- Content -->
        <div class="content">
            <div class="animated fadeIn">
                <div class="row">

                    <div class="col-md-12">
                        <div class="card">
                            <div class="card-header">
                                <strong class="card-title">Ubah Pengeluaran</strong>
                            </div>
                            <div class="card-body card-block">
                                <form id="form-pengeluaran" action="{{ route('pengeluaran.update', ['pengeluaran' => $data->id]) }}" method="post" enctype="multipart/form-data" class="form-horizontal">
                                    @csrf
                                    @method('PUT')
                                    <div class="row form-group">
                                        <div class="col col-md-3"><label for="text-input" class=" form-control-label">Tanggal</label></div>
                                        <div class="col-12 col-md-4">
                                           <input type="date" class="form-control"  name="tanggal" value="{{ date('Y-m-d') }}">
                                        </div>
                                    </div>
                                   
                                    <hr>
                                    <h5>Kredit</h5>
                                    <hr>
                                    <div class="row form-group">
                                        <div class="col col-md-3"><label for="text-input" class=" form-control-label">Nama Akun</label></div>
                                        <div class="col-12 col-md-6">
                                            
                                            <select name="no_akun_kredit" class="form-control select2" required>
                                                <option value="">--Pilih Akun--</option>
                                                @foreach ($header_coa as $header)
                                                    <optgroup label="{{ $header->no_header_akun }} - {{ $header->nama_header_akun }}">
                                                        @foreach ($header->masterCoa as $item)
                                                            <option {{ $data->no_akun_kredit == $item->no_akun?'selected':'' }} value="{{ $item->no_akun }}">
                                                                {{ $item->no_akun }} - {{ $item->nama_akun }}
                                                            </option>
                                                        @endforeach
                                                    </optgroup>
                                                @endforeach
                                            </select>
                                        </div>
                                    </div>
                                    <div class="row form-group">
                                        <div class="col col-md-3"><label for="text-input" class=" form-control-label">Jumlah</label></div>
                                        <div class="col-12 col-md-6">
                                            
                                             <input type="text" class="form-control" onkeyup="hitungTotal()" id="kredit" name="kredit" value="{{ number_format($data->kredit, 0, ',', '.') }}">
                                        </div>
                                    </div>

                                    <hr>
                                    <h5>Debit</h5>
                                    <hr>
                                    <div class="row form-group">
                                        <div class="col col-md-3"><label for="text-input" class=" form-control-label">Nama Akun</label></div>
                                        <div class="col-12 col-md-6">
                                            
                                            <select name="no_akun_debet" class="form-control select2" required>
                                                <option value="">--Pilih Akun--</option>
                                                @foreach ($header_coa as $header)
                                                    <optgroup label="{{ $header->no_header_akun }} - {{ $header->nama_header_akun }}">
                                                        @foreach ($header->masterCoa as $item)
                                                            <option {{ $data->no_akun_debet == $item->no_akun?'selected':'' }} value="{{ $item->no_akun }}">
                                                                {{ $item->no_akun }} - {{ $item->nama_akun }}
                                                            </option>
                                                        @endforeach
                                                    </optgroup>
                                                @endforeach
                                            </select>
                                        </div>
                                    </div>

                                    <div class="row form-group">
                                        <div class="col col-md-3"><label for="text-input" class=" form-control-label">Jumlah</label></div>
                                        <div class="col-12 col-md-6">
                                            
                                             <input type="text" class="form-control" onkeyup="hitungTotal()" id="debet" name="debet" value="{{ number_format($data->debet, 0, ',', '.') }}" readonly>
                                        </div>
                                    </div>

                                    <div class="row form-group">
                                        <div class="col col-md-3"><label for="text-input" class=" form-control-label">Keterangan</label></div>
                                        <div class="col-12 col-md-6">
                                            
                                             <input type="text" class="form-control" id="keterangan" name="keterangan" value="{{ $data->keterangan }}">
                                        </div>
                                    </div>

                                    

                                    
                                    <button type="submit" class="btn btn-primary">
                                        Simpan
                                    </button>
                                    <a href="{{ route('pengeluaran.index') }}" class="btn btn-default">
                                        Batal
                                    </button>
                                </form>
                            </div>
                           
                        </div>
                    </div>


                </div>
            </div><!-- .animated -->
        </div><!-- .content -->
        <!-- /.content -->
@section('js')


<script>
    

    function formatRupiah(angka) {
        let number_string = angka.replace(/[^,\d]/g, "").toString(),
            split = number_string.split(","),
            sisa  = split[0].length % 3,
            rupiah  = split[0].substr(0, sisa),
            ribuan  = split[0].substr(sisa).match(/\d{3}/gi);

        if (ribuan) {
            let separator = sisa ? "." : "";
            rupiah += separator + ribuan.join(".");
        }
        return split[1] !== undefined ? rupiah + "," + split[1] : rupiah;
    }

    function parseRupiah(nilai) {
        return parseInt(nilai.replace(/\./g, "").replace(/[^0-9]/g, '')) || 0;
    }

    function hitungTotal() {
        let kredit = parseRupiah(document.getElementById('kredit').value);
        
        // Format input saat ketik
        document.getElementById('kredit').value = formatRupiah(kredit.toString());
        document.getElementById('debet').value = formatRupiah(kredit.toString());
        
    }

    $(document).ready(function () {
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

        $('#form-pengeluaran').on('submit', function (e) {
            e.preventDefault();
            var debetValue = $('input[name="debet"]').val().replace(/\./g, '');
            var kreditValue = $('input[name="kredit"]').val().replace(/\./g, '');
            $.ajax({
                url: "{{ route('pengeluaran.cekDuplikat') }}",
                method: 'POST',
                data: {
                    id: $('input[name="id"]').val(),
                    tanggal: $('input[name="tanggal"]').val(),
                    id_coa_debet: $('select[name="id_coa_debet"]').val(),
                    id_coa_kredit: $('select[name="id_coa_kredit"]').val(),
                    debet: debetValue,
                    kredit: kreditValue
                },
                success: function (response) {
                    if (response.duplikat) {
                        alert('Data duplikat! Tidak bisa disimpan.');
                        e.preventDefault();
                    } else {
                        e.currentTarget.submit(); // lanjut submit
                    }
                }
            });
        });
    });


    // function cekDataDuplikat() {
    //     $.ajax({
    //         url: "{{ route('penerimaan.cekDuplikat') }}", // ganti sesuai route kamu
    //         method: 'POST',
    //         data: {
    //             _token: $('meta[name="csrf-token"]').attr('content'),
    //             id: $('input[name="id"]').val(), // kosongkan jika insert
    //             tanggal: $('input[name="tanggal"]').val(),
    //             id_coa_debet: $('select[name="id_coa_debet"]').val(),
    //             id_coa_kredit: $('select[name="id_coa_kredit"]').val(),
    //             debet: $('input[name="debet"]').val(),
    //             kredit: $('input[name="kredit"]').val()
    //         },
    //         success: function (response) {
    //             console.log(response);
                
    //             if (response.duplikat) {
    //                 alert('Data sudah pernah diinput sebelumnya.');
    //             }
    //         }
    //     });
    // }
</script>
@endsection       
@endsection