@extends('layouts.app')

@section('content')


<!-- Modal HistoryBayaran -->
<div class="modal fade" id="denominasiShowModal" tabindex="-1" role="dialog" aria-labelledby="ModalCenterTitle"
    aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered modal-xl" role="document">
        <div class="modal-content">
            <div class="modal-header">

                <ul class="nav nav-pills">
                    {{-- <li class="nav-item">
              <a class="nav-link" data-toggle="pill" href="#home" role="tab" aria-controls="pills-flamingo" aria-selected="true">Bundle</a>
            </li> --}}
                    <li class="nav-item">
                        <a class="nav-link active" data-toggle="pill" href="#profile" role="tab"
                            aria-controls="pills-cuckoo" aria-selected="false">Keping</a>
                    </li>
                </ul>

                <label class="mt-2 ml-3 hideelem d-none">Bundle:</label><input type="text"
                    class="form-control col-2 ml-3 hideelem d-none" id="amaunbundle" placeholder="0.00" readonly>
                <label class="mt-2 ml-3 hideelem d-none">Keping:</label><input type="text"
                    class="form-control col-2 ml-3 hideelem d-none" id="amaunkeping" placeholder="0.00" readonly>
                <label class="mt-2 ml-3">Amaun Deno:</label><input type="text" class="form-control col-2 ml-3"
                    data-amaun="" id="amaunkeseluruhan" placeholder="0.00" readonly>
                <label class="mt-2 ml-3">Peti Besi:</label><input type="text" class="form-control col-2 ml-3"
                    id="amaunvault" placeholder="0.00" readonly>

                <button type="button" class="btn btn-warning ml-4 hideelement d-none"
                    data-dismiss="modal">Tukar</button>

                <button type="button" class="close invisible" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>

            </div>
            <div class="modal-body">

                <div class="alert alert-warning text-center d-none" role="alert">
                    <i class='fas fa-exclamation-triangle text-danger mr-2'></i> is a primary alert—check it out!
                </div>
                <div class="tab-content">
                    <div class="tab-pane fade" id="home" role="tabpanel" aria-labelledby="flamingo-tab">

                        <table class="table">
                            <tr>
                                <th></th>
                                <th>Bil Keping </th>
                                <th>Jumlah (RM)</th>
                                <th></th>
                                <th>Bil Keping </th>
                                <th>Jumlah (RM)</th>
                            </tr>
                            <tr>
                                <td>RM 1.00</td>
                                <td><input type="text" name="ambilanb1" class="form-control" id="bundle1" readonly />
                                </td>
                                <td><input type="text" name="totalb1" class="form-control" id="totalb1" value="0.00"
                                        readonly /> </td>
                                <td> RM 20.00</td>
                                <td><input type="text" name="ambilanb20" class="form-control" id="bundle20" readonly />
                                </td>
                                <td><input type="text" name="totalb20" class="form-control" id="totalb20" value="0.00"
                                        readonly /> </td>
                            </tr>
                            <tr>
                                <td>RM 5.00</td>
                                <td><input type="text" name="ambilanb5" class="form-control" id="bundle5" readonly />
                                </td>
                                <td><input type="text" name="totalb5" class="form-control" id="totalb5" value="0.00"
                                        readonly /> </td>
                                <td>RM 50.00</td>
                                <td><input type="text" name="ambilanb50" class="form-control" id="bundle50" readonly />
                                </td>
                                <td><input type="text" name="totalb50" class="form-control" id="totalb50" value="0.00"
                                        readonly /> </td>
                            </tr>
                            <tr>
                                <td>RM 10.00</td>
                                <td><input type="text" name="ambilanb10" class="form-control" id="bundle10" readonly />
                                </td>
                                <td><input type="text" name="totalb10" class="form-control" id="totalb10" value="0.00"
                                        readonly /> </td>
                                <td>RM 100.00 </td>
                                <td><input type="text" name="ambilanb100" class="form-control" id="bundle100"
                                        readonly /> </td>
                                <td><input type="text" name="totalb100" class="form-control" id="totalb100" value="0.00"
                                        readonly /> </td>
                            </tr>
                        </table>

                    </div>
                    <div class="tab-pane fade  show active" id="profile" role="tabpanel" aria-labelledby="profile-tab">

                        <table class="table">
                            <tr class="bg-warning text-center">
                                <th width="20%"></th>
                                <th>Peti Besi</th>
                                <th>Jumlah (RM)</th>
                                <th width="20%"></th>
                                <th>Peti Besi</th>
                                <th>Jumlah (RM)</th>
                            </tr>
                            <tr>
                                <td class="text-center">RM 0.01</td>
                                <td><input type="text" data-deno="" name="ambilansen1" onchange="totaltukarandeno(this)"
                                        class="form-control text-center editfunc" id="sen1" readonly /> </td>
                                <td><input type="text" name="totalsen1" id="totalsen1" class="form-control text-center"
                                        value="0.00" readonly /> </td>
                                <td class="text-center">RM 1.00</td>
                                <td><input type="text" data-deno="" name="ambilanring1"
                                        class="form-control text-center editfunc" id="ringgit1"
                                        onchange="totaltukarandeno(this)" readonly /> </td>
                                <td><input type="text" name="totalring1" id="totalringgit1"
                                        class="form-control text-center" value="0.00" readonly /> </td>
                            </tr>
                            <tr>
                                <td class="text-center">RM 0.05</td>
                                <td><input type="text" data-deno="" name="ambilansen5"
                                        class="form-control text-center editfunc" id="sen5"
                                        onchange="totaltukarandeno(this)" readonly /> </td>
                                <td><input type="text" name="totalsen5" id="totalsen5" class="form-control text-center"
                                        value="0.00" readonly /> </td>
                                <td class="text-center">RM 5.00</td>
                                <td><input type="text" data-deno="" name="ambilanring5"
                                        class="form-control text-center editfunc" id="ringgit5"
                                        onchange="totaltukarandeno(this)" readonly /> </td>
                                <td><input type="text" name="totalring5" id="totalringgit5"
                                        class="form-control text-center" value="0.00" readonly /> </td>
                            </tr>
                            <tr>
                                <td class="text-center">RM 0.10</td>
                                <td><input type="text" data-deno="" name="ambilansen10"
                                        class="form-control text-center editfunc" id="sen10"
                                        onchange="totaltukarandeno(this)" readonly /> </td>
                                <td><input type="text" name="totalsen10" id="totalsen10"
                                        class="form-control text-center" value="0.00" readonly /> </td>
                                <td class="text-center">RM 10.00</td>
                                <td><input type="text" data-deno="" name="ambilanring10"
                                        class="form-control text-center editfunc" id="ringgit10"
                                        onchange="totaltukarandeno(this)" readonly /> </td>
                                <td><input type="text" name="totalring10" id="totalringgit10"
                                        class="form-control text-center" value="0.00" readonly /> </td>
                            </tr>
                            <tr>
                                <td class="text-center">RM 0.20</td>
                                <td><input type="text" data-deno="" name="ambilansen20"
                                        class="form-control text-center editfunc" id="sen20"
                                        onchange="totaltukarandeno(this)" readonly /> </td>
                                <td><input type="text" name="totalsen20" id="totalsen20"
                                        class="form-control text-center" value="0.00" readonly /> </td>
                                <td class="text-center"> RM 20.00</td>
                                <td><input type="text" data-deno="" name="ambilanring20"
                                        class="form-control text-center editfunc" id="ringgit20"
                                        onchange="totaltukarandeno(this)" readonly /> </td>
                                <td><input type="text" name="totalring20" id="totalringgit20"
                                        class="form-control text-center" value="0.00" readonly /> </td>
                            </tr>
                            <tr>
                                <td class="text-center">RM 0.50</td>
                                <td><input type="text" data-deno="" name="ambilansen50"
                                        class="form-control text-center editfunc" id="sen50"
                                        onchange="totaltukarandeno(this)" readonly /> </td>
                                <td><input type="text" name="totalsen50" id="totalsen50"
                                        class="form-control text-center" value="0.00" readonly /> </td>
                                <td class="text-center">RM 50.00</td>
                                <td><input type="text" data-deno="" name="ambilanring50"
                                        class="form-control text-center editfunc" id="ringgit50"
                                        onchange="totaltukarandeno(this)" readonly /> </td>
                                <td><input type="text" name="totalring50" id="totalringgit50"
                                        class="form-control text-center" value="0.00" readonly /> </td>
                            </tr>
                            <tr>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td class="text-center">RM 100.00 </td>
                                <td><input type="text" data-deno="" name="ambilanring100"
                                        class="form-control text-center editfunc" id="ringgit100"
                                        onchange="totaltukarandeno(this)" readonly /> </td>
                                <td><input type="text" name="totalring100" id="totalringgit100"
                                        class="form-control text-center" value="0.00" readonly /> </td>
                            </tr>
                        </table>

                    </div>
                </div>

                <input type="hidden" id="jenisjenis" />
                <input type="hidden" id="kodcawangan" />
                <input type="hidden" id="tellerla" />

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-warning btn-lg" id="editbtn" onclick="editfunc()">Ubah</button>
                <button type="button" class="btn btn-danger btn-lg" id="tutupbtn" data-dismiss="modal">Tutup</button>
                <button type="button" class="btn btn-success btn-lg d-none" id="simpanbtn"
                    onclick="simpanfunc()">Simpan</button>
                <button type="button" class="btn btn-danger btn-lg d-none" id="batalbtn"
                    onclick="batalfunc()">Batal</button>
            </div>
        </div>
    </div>
</div>

<div class="container">

    <div class="row justify-content-center">

        <div class="col-md-12">

            <div class="card">

                <div class="card-header bg-secondary text-center">{{ __('Paparan Utama') }}</div>

                <div class="card-body text-center">

                    <div class="row form-group justify-content-center">
                        <div class="col-3 mx-1">
                            <div class="row">
                                <a href="{{ url('dashboard') }}" disabled
                                    class="btn btn-info btn-lg btn-block mb-3">Dashboard</a>
                            </div>
                            <div class="row">
                                <a href="{{ url('kakitangan') }}"
                                    class="btn btn-info btn-lg btn-block mb-3">Kakitangan</a>
                            </div>
                            <div class="row">
                                <a href="{{ url('kelulusan') }}" id="btnkelulusan"
                                    class="btn btn-info btn-lg btn-block  mb-3">Kelulusan</a>
                            </div>
                            {{-- <div class="row">
                            <a href="{{ url('permohonan') }}" class="btn btn-danger btn-lg btn-block mb-3">Permohonan</a>
                            </div> --}}
                        <div class="row">
                            <a href="#" data-target="#denominasiShowModal" data-backdrop="static" data-keyboard="false"
                                data-toggle="modal" class="btn btn-info btn-lg btn-block mb-3">Denominasi</a>
                        </div>
                        <div class="row">
                            <a href="{{ route('khazanah.harga_emas') }}"
                                class="btn btn-info btn-lg btn-block mb-3">Harga Emas</a>
                        </div>
                    </div>

                    <div class="col-3 mx-1">
                        <div class="row">
                            <a href="{{ route('khazanah.pelanggan') }}"
                                class="btn btn-info btn-lg btn-block mb-3">Pelanggan</a>
                        </div>
                        <div class="row">
                            <a href="{{ route('laporan') }}" class="btn btn-info btn-lg btn-block mb-3">Laporan</a>
                        </div>
                        <div class="row">
                            <a href="{{ url('menu') }}"
                                id="btn-khazanah"
                                class="btn {{ $countkhazanah > 0 ? 'btn-danger' : 'btn-info' }} btn-lg btn-block mb-3">Khazanah</a>
                        </div>
                        <div class="row">
                            <a href="{{ url('pendahuluanserahan') }}"
                                class="btn btn-info btn-lg btn-block mb-3">Pendahuluan - Serahan</a>
                        </div>
                        <div class="row">
                            <a href="{{ route('khazanah.indexwakil') }}"
                                class="btn btn-info btn-lg btn-block mb-3">Surat Wakil</a>
                        </div>
                    </div>
                    <div class="col-3 mx-1">
                        {{-- <div class="row">
            <a href="#" class="btn btn-danger btn-lg btn-block mb-3">Capaian</a>
        </div> --}}
                        <div class="row">
                            <a href="{{ route('lelong') }}" class="btn btn-info btn-lg btn-block mb-3">Lelong</a>
                        </div>
                        <div class="row">
                            <a href="{{ url('panjar') }}" class="btn btn-info btn-lg btn-block mb-3">Perbelanjaan</a>
                        </div>
                        <div class="row">
                            <a href="{{ url('alirantunai') }}" class="btn btn-info btn-lg btn-block  mb-3">Aliran
                                Tunai</a>
                        </div>
                        <div class="row">
                            <a href="{{ route('khazanah.bajet') }}" class="btn btn-info btn-lg btn-block mb-3">Bajet</a>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>
</div>
</div>

<script>
    const formatter = new Intl.NumberFormat('en-US', {
        minimumFractionDigits: 2
    });

    var pusher = new Pusher("{{ env('PUSHER_APP_KEY') }}", {
        cluster: 'ap1',
        encrypted: true
    });

    var cawangankhazanah = '{{ Auth::user()->cawangan }}';

    var channel = pusher.subscribe('status-liked');
    channel.bind('my-event', function (data) {
        // let message = JSON.stringify(data);

        if (cawangankhazanah == data.cawangan) {
            $('#btnkelulusan').removeClass("btn-primary").addClass("btn-success");

        }
    });

    $(function () {
        getData();
    });


    let api = "{{ config('api.url') }}";
    let kodcaw = "{{ Auth::user()->cawangan }}";

    const initialambilan = [
        ["bundle1", 0, 0],
        ["bundle5", 0, 0],
        ["bundle10", 0, 0],
        ["bundle20", 0, 0],
        ["bundle50", 0, 0],
        ["bundle100", 0, 0],
        ["sen1", 0, 0],
        ["sen5", 0, 0],
        ["sen10", 0, 0],
        ["sen20", 0, 0],
        ["sen50", 0, 0],
        ["ringgit1", 0, 0],
        ["ringgit5", 0, 0],
        ["ringgit10", 0, 0],
        ["ringgit20", 0, 0],
        ["ringgit50", 0, 0],
        ["ringgit100", 0, 0]
    ];

    let arrayambilan = [
        ["bundle1", 0, 0],
        ["bundle5", 0, 0],
        ["bundle10", 0, 0],
        ["bundle20", 0, 0],
        ["bundle50", 0, 0],
        ["bundle100", 0, 0],
        ["sen1", 0, 0],
        ["sen5", 0, 0],
        ["sen10", 0, 0],
        ["sen20", 0, 0],
        ["sen50", 0, 0],
        ["ringgit1", 0, 0],
        ["ringgit5", 0, 0],
        ["ringgit10", 0, 0],
        ["ringgit20", 0, 0],
        ["ringgit50", 0, 0],
        ["ringgit100", 0, 0]
    ];


    function saveData() {
        let bundle1 = $('#bundle1').val();
        let bundle5 = $('#bundle5').val();
        let bundle10 = $('#bundle10').val();
        let bundle20 = $('#bundle20').val();
        let bundle50 = $('#bundle50').val();
        let bundle100 = $('#bundle100').val();
        let sen1 = $('#sen1').val();
        let sen5 = $('#sen5').val();
        let sen10 = $('#sen10').val();
        let sen20 = $('#sen20').val();
        let sen50 = $('#sen50').val();
        let ringgit1 = $('#ringgit1').val();
        let ringgit5 = $('#ringgit5').val();
        let ringgit10 = $('#ringgit10').val();
        let ringgit20 = $('#ringgit20').val();
        let ringgit50 = $('#ringgit50').val();
        let ringgit100 = $('#ringgit100').val();


        $.ajax({
            method: 'put',
            async: false,
            url: api + '/api/DenominasiUpdate/' + kodcaw,
            data: {
                "bd1": bundle1,
                "bd5": bundle5,
                "bd10": bundle10,
                "bd20": bundle20,
                "bd50": bundle50,
                "bd100": bundle100,
                "txtring1": ringgit1,
                "txtring5": ringgit5,
                "txtsen1": sen1,
                "txtsen5": sen5,
                "txtsen10": sen10,
                "txtsen20": sen20,
                "txtsen50": sen50,
                "txtring10": ringgit10,
                "txtring20": ringgit20,
                "txtring50": ringgit50,
                "txtring100": ringgit100,
                "txtring1000": "0",
                "jumlahbundle": "0",
                "txtjumlah": "0",
                "jumlahkeping": "0"
            },
            success: function (data) {

                if(data == 0){
                    Swal.fire({
                        icon: 'error',
                        title: 'Tidak Berjaya',
                        text: 'Tidak berjaya mengemaskini denominasi',
                        allowEscapeKey : false,
                        allowOutsideClick: false,
                    }).then((result) => {
                        if (result.isConfirmed) {
                            $('#editbtn').removeClass('d-none');
                            $('#tutupbtn').removeClass('d-none');
                            $('#simpanbtn').addClass('d-none');
                            $('#batalbtn').addClass('d-none');
                            $('.editfunc').prop('readonly', true);
                        }
                    });
                }else{
                    Swal.fire({
                        icon: 'success',
                        title: 'Berjaya',
                        text: 'Berjaya mengemaskini denominasi',
                        allowEscapeKey : false,
                        allowOutsideClick: false,
                    }).then((result) => {
                        if (result.isConfirmed) {
                            
                            $('#editbtn').removeClass('d-none');
                            $('#tutupbtn').removeClass('d-none');
                            $('#simpanbtn').addClass('d-none');
                            $('#batalbtn').addClass('d-none');
                            $('.editfunc').prop('readonly', true);
                            location.reload();
                        }
                    });
                }
            }
        });
    }

    function getData() {
        $.ajax({
            method: 'get',
            url: api + '/api/VaultController/' + kodcaw,
            success: function (data) {
                $('#amaunvault').val(formatter.format(data[0].bakiakhir));
            }
        });

        $.ajax({
            method: 'get',
            url: api + '/api/DenominasiController/' + kodcaw,
            success: function (data) {
                $.each(data, function (key, val) {

                    initialambilan[0][1] = val.bd1;
                    initialambilan[0][2] = parseFloat(Number(val.b1));
                    initialambilan[1][1] = val.bd5;
                    initialambilan[1][2] = parseFloat(Number(val.b5));
                    initialambilan[2][1] = val.bd10;
                    initialambilan[2][2] = parseFloat(Number(val.b10));
                    initialambilan[3][1] = val.bd20;
                    initialambilan[3][2] = parseFloat(Number(val.b20));
                    initialambilan[4][1] = val.bd50;
                    initialambilan[4][2] = parseFloat(Number(val.b50));
                    initialambilan[5][1] = val.bd100;
                    initialambilan[5][2] = parseFloat(Number(val.b100));
                    initialambilan[6][1] = val.txtsen1;
                    initialambilan[6][2] = parseFloat(Number(val.k01));
                    initialambilan[7][1] = val.txtsen5;
                    initialambilan[7][2] = parseFloat(Number(val.k05));
                    initialambilan[8][1] = val.txtsen10;
                    initialambilan[8][2] = parseFloat(Number(val.k010));
                    initialambilan[9][1] = val.txtsen20;
                    initialambilan[9][2] = parseFloat(Number(val.k020));
                    initialambilan[10][1] = val.txtsen50;
                    initialambilan[10][2] = parseFloat(Number(val.k050));
                    initialambilan[11][1] = val.txtring1;
                    initialambilan[11][2] = parseFloat(Number(val.k1));
                    initialambilan[12][1] = val.txtring5;
                    initialambilan[12][2] = parseFloat(Number(val.k5));
                    initialambilan[13][1] = val.txtring10;
                    initialambilan[13][2] = parseFloat(Number(val.k10));
                    initialambilan[14][1] = val.txtring20;
                    initialambilan[14][2] = parseFloat(Number(val.k20));
                    initialambilan[15][1] = val.txtring50;
                    initialambilan[15][2] = parseFloat(Number(val.k50));
                    initialambilan[16][1] = val.txtring100;
                    initialambilan[16][2] = parseFloat(Number(val.k100));


                    arrayambilan[0][1] = val.bd1;
                    arrayambilan[0][2] = parseFloat(Number(val.b1));
                    arrayambilan[1][1] = val.bd5;
                    arrayambilan[1][2] = parseFloat(Number(val.b5));
                    arrayambilan[2][1] = val.bd10;
                    arrayambilan[2][2] = parseFloat(Number(val.b10));
                    arrayambilan[3][1] = val.bd20;
                    arrayambilan[3][2] = parseFloat(Number(val.b20));
                    arrayambilan[4][1] = val.bd50;
                    arrayambilan[4][2] = parseFloat(Number(val.b50));
                    arrayambilan[5][1] = val.bd100;
                    arrayambilan[5][2] = parseFloat(Number(val.b100));
                    arrayambilan[6][1] = val.txtsen1;
                    arrayambilan[6][2] = parseFloat(Number(val.k01));
                    arrayambilan[7][1] = val.txtsen5;
                    arrayambilan[7][2] = parseFloat(Number(val.k05));
                    arrayambilan[8][1] = val.txtsen10;
                    arrayambilan[8][2] = parseFloat(Number(val.k010));
                    arrayambilan[9][1] = val.txtsen20;
                    arrayambilan[9][2] = parseFloat(Number(val.k020));
                    arrayambilan[10][1] = val.txtsen50;
                    arrayambilan[10][2] = parseFloat(Number(val.k050));
                    arrayambilan[11][1] = val.txtring1;
                    arrayambilan[11][2] = parseFloat(Number(val.k1));
                    arrayambilan[12][1] = val.txtring5;
                    arrayambilan[12][2] = parseFloat(Number(val.k5));
                    arrayambilan[13][1] = val.txtring10;
                    arrayambilan[13][2] = parseFloat(Number(val.k10));
                    arrayambilan[14][1] = val.txtring20;
                    arrayambilan[14][2] = parseFloat(Number(val.k20));
                    arrayambilan[15][1] = val.txtring50;
                    arrayambilan[15][2] = parseFloat(Number(val.k50));
                    arrayambilan[16][1] = val.txtring100;
                    arrayambilan[16][2] = parseFloat(Number(val.k100));

                    $('#bundle1').val(val.bd1);
                    $('#totalb1').val(formatter.format(val.b1));

                    $('#bundle20').val(val.bd20);
                    $('#totalb20').val(formatter.format(val.b20));

                    $('#bundle5').val(val.bd5);
                    $('#totalb5').val(formatter.format(val.b5));

                    $('#bundle50').val(val.bd50);
                    $('#totalb50').val(formatter.format(val.b50));

                    $('#bundle10').val(val.bd10);
                    $('#totalb10').val(formatter.format(val.b10));

                    $('#bundle100').val(val.bd100);
                    $('#totalb100').val(formatter.format(val.b100));

                    $('#sen1').val(val.txtsen1);
                    $('#sen1').data('deno', val.txtsen1);
                    $('#totalsen1').val(formatter.format(val.k01));

                    $('#sen5').val(val.txtsen5);
                    $('#sen5').data('deno', val.txtsen5);
                    $('#totalsen5').val(formatter.format(val.k05));

                    $('#sen10').val(val.txtsen10);
                    $('#sen10').data('deno', val.txtsen10);
                    $('#totalsen10').val(formatter.format(val.k010));

                    $('#sen20').val(val.txtsen20);
                    $('#sen20').data('deno', val.txtsen20);
                    $('#totalsen20').val(formatter.format(val.k020));

                    $('#sen50').val(val.txtsen50);
                    $('#sen50').data('deno', val.txtsen50);
                    $('#totalsen50').val(formatter.format(val.k050));

                    $('#ringgit1').val(val.txtring1);
                    $('#ringgit1').data('deno', val.txtring1);
                    $('#totalringgit1').val(formatter.format(val.k1));

                    $('#ringgit5').val(val.txtring5);
                    $('#ringgit5').data('deno', val.txtring5);
                    $('#totalringgit5').val(formatter.format(val.k5));

                    $('#ringgit10').val(val.txtring10);
                    $('#ringgit10').data('deno', val.txtring10);
                    $('#totalringgit10').val(formatter.format(val.k10));

                    $('#ringgit20').val(val.txtring20);
                    $('#ringgit20').data('deno', val.txtring20);
                    $('#totalringgit20').val(formatter.format(val.k20));

                    $('#ringgit50').val(val.txtring50);
                    $('#ringgit50').data('deno', val.txtring50);
                    $('#totalringgit50').val(formatter.format(val.k50));

                    $('#ringgit100').val(val.txtring100);
                    $('#ringgit100').data('deno', val.txtring100);
                    $('#totalringgit100').val(formatter.format(val.k100));

                    $('#amaunkeping').val(formatter.format(val.jumlahkeping));
                    $('#amaunbundle').val(formatter.format(val.jumlahbundle));
                    $('#amaunkeseluruhan').val(formatter.format(val.txtjumlah));
                    $('#amaunkeseluruhan').data('amaun', val.txtjumlah);

                });
            }

        });
    }


    function editfunc() {
        let amaundeno = $('#amaunkeseluruhan').val().replace(/[^0-9.-]+/g, "");
        let amaunvault = $('#amaunvault').val().replace(/[^0-9.-]+/g, "");

        $('#editbtn').addClass('d-none');
        $('#tutupbtn').addClass('d-none');
        $('#simpanbtn').removeClass('d-none');
        $('#batalbtn').removeClass('d-none');

        if (parseFloat(Number(amaundeno)) > parseFloat(Number(amaunvault))) {
            $('#amaunkeseluruhan').addClass('bg-success');
        } else if (parseFloat(Number(amaundeno)) < parseFloat(Number(amaunvault))) {
            $('#amaunkeseluruhan').addClass('bg-danger');
        } else {
            $('#amaunkeseluruhan').removeClass('bg-success');
            $('#amaunkeseluruhan').removeClass('bg-danger');
        }

        $('.editfunc').prop('readonly', false);
    }

    function simpanfunc() {
        // console.log('simpan');
        //Swal.fire({
        //      title: 'Adakah anda pasti untuk mengemaskini denominasi?',
        //      text: "Sila pastikan denominasi anda betul!",
        //      icon: 'warning',
        //      showCancelButton: true,
        //      confirmButtonColor: '#3085d6',
        //      cancelButtonColor: '#d33',
        //      confirmButtonText: 'Simpan'
        //    }).then((result) => {

        //      console.log(result);
        //      return false;
        //    });


        let amaundeno = $('#amaunkeseluruhan').val().replace(/[^0-9.-]+/g, "");
        let amaunvault = $('#amaunvault').val().replace(/[^0-9.-]+/g, "");
        let samacount = 0;

        if (amaundeno !== amaunvault) {
            $.each(initialambilan, function (index, initial) {
                $.each(arrayambilan, function (index1, arr) {

                    if (initial[0] === arr[0]) {
                        if ((initial[1] === arr[1]) && (initial[2] === arr[2])) {

                        } else {
                            samacount++;
                        }
                    }

                });
            });
        }

        if (samacount != 0) {
            if (amaundeno != amaunvault) {
                Swal.fire({
                    icon: 'error',
                    title: 'Denominasi dan peti besi masih tidak sama!',
                    text: "Sila pastikan denominasi anda betul!"
                });

                return false;
            }
        } else {

            Swal.fire({
                title: 'Adakah anda pasti untuk mengemaskini denominasi?',
                text: "Sila pastikan denominasi anda betul!",
                icon: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#3085d6',
                cancelButtonColor: '#d33',
                confirmButtonText: 'Simpan'
            }).then(async (result) => {

                if (result.isConfirmed) {
                    
                    let savedata = await saveData();

                } else if (result.isDismissed) {

                }
            })

        }

        $('#editbtn').removeClass('d-none');
        $('#tutupbtn').removeClass('d-none');
        $('#simpanbtn').addClass('d-none');
        $('#batalbtn').addClass('d-none');

        $('.editfunc').prop('readonly', true);

    }

    function batalfunc() {
        let totalamaun = 0;

        $.each(initialambilan, function (index, initial) {
            $('#' + initial[0]).val(Number(initial[1]));
            $('#total' + initial[0]).val(formatter.format(initial[2]));

            totalamaun += initial[2];


            $.each(arrayambilan, function (index1, arr) {

                if (arr[0] == initial[0]) {
                    arr[1] = initial[1];
                    arr[2] = initial[2];
                }

            });

        });

        $('#amaunkeseluruhan').val(formatter.format(totalamaun));

        $('#editbtn').removeClass('d-none');
        $('#tutupbtn').removeClass('d-none');
        $('#simpanbtn').addClass('d-none');
        $('#batalbtn').addClass('d-none');

        $('.editfunc').prop('readonly', true);
    }

    function totalamountdeno(idwang, bilangan) {
        // console.log(idwang + 'asdas');
        var amount = 0;
        switch (idwang) {
            case 'bundle1':
                amount += (bilangan * 100);
                break;
            case 'bundle20':
                amount += (bilangan * 1000);
                break;
            case 'bundle5':
                amount += (bilangan * 500);
                break;
            case 'bundle50':
                amount += (bilangan * 5000);
                break;
            case 'bundle10':
                amount += (bilangan * 1000);
                break;
            case 'bundle100':
                amount += (bilangan * 5000);
                break;
            case 'sen1':
                amount += (bilangan * 0.01);
                break;
            case 'sen5':
                amount += (bilangan * 0.05);
                break;
            case 'sen10':
                console.log(bilangan);
                amount += (bilangan * 0.10);
                break;
            case 'sen20':
                amount += (bilangan * 0.20);
                break;
            case 'sen50':
                amount += (bilangan * 0.50);
                break;
            case 'ringgit1':
                amount += (bilangan * 1);
                break;
            case 'ringgit5':
                amount += (bilangan * 5);
                break;
            case 'ringgit10':
                amount += (bilangan * 10);
                break;
            case 'ringgit20':
                amount += (bilangan * 20);
                break;
            case 'ringgit50':
                amount += (bilangan * 50);
                break;
            case 'ringgit100':
                amount += (bilangan * 100);
                break;
        }

        return amount;
    }



    function totaltukarandeno(datakeyin) {
        let amaundeno = $('#amaunkeseluruhan').val().replace(/[^0-9.-]+/g, "");
        let amaunvault = $('#amaunvault').val().replace(/[^0-9.-]+/g, "");
        var idkey = $(datakeyin).attr('id');
        var valkey = $(datakeyin).val();
        var deno = $(datakeyin).data('deno');
        var amount = 0;

        // if(parseFloat(Number(valkey)) > parseFloat(Number(deno)))
        // {
        //     $('#' + idkey).val(0);
        //   Swal.fire({
        //       icon: 'error',
        //       title: 'Amaun Kiraan lebih banyak daripada Amaun Sebenar!'
        //     });

        //     return ;
        // }

        var validate = $('#amaunvault').val().replace(/[^0-9.-]+/g, "");

        let indexarr = 0;
        $.each(arrayambilan, function (index, arr) {
            if (arr[0] == idkey) {
                arrayambilan[index][1] = Number(valkey);
                arrayambilan[index][2] = totalamountdeno(idkey, valkey);
                indexarr = index;
            }

            amount += parseFloat(Number(arr[2]));

        });

        // if(parseFloat(Number(amount)) > parseFloat(Number(validate)))
        // {
        //     // $('#' + idkey).val(0);
        //     Swal.fire({
        //       icon: 'error',
        //       title: 'Amaran',
        //       text: 'Jumlah deno melebihi peti besi',
        //     });
        // }
        // else
        // {
        //   $('#amaunkiraan').val(formatter.format(amount));
        // }

        $('#total' + idkey).val(formatter.format(arrayambilan[indexarr][2]));

        if (parseFloat(Number(amount)) > parseFloat(Number(amaunvault))) {
            $('#amaunkeseluruhan').addClass('bg-success');
        } else if (parseFloat(Number(amount)) < parseFloat(Number(amaunvault))) {
            $('#amaunkeseluruhan').addClass('bg-danger');
        } else {
            $('#amaunkeseluruhan').removeClass('bg-success');
            $('#amaunkeseluruhan').removeClass('bg-danger');
        }

        $('#amaunkeseluruhan').val(formatter.format(amount));

    }

</script>
@endsection
