@extends('layouts.app_operasi')
@section('content')
@include('modal.hargaemas')


<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-12">
            <div class="card">
               
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb pb-3">
                      <li class="breadcrumb-item"><a href="{{ url('/') }}">Home</a></li>
                      <li class="breadcrumb-item"><a href="{{ url('operasi/parameter') }}">Parameter</a></li>
                      <li class="breadcrumb-item active" aria-current="page">Harga Emas</li>
                    </ol>
                </nav>


                <div class="card-body text-center">
                    
                    <div class="mt-3">
                        
                        <table class='table table-striped table-hover' id='table-emas' style="width:100%;">
                            <thead class="thead-dark">
                                <tr>
                                    <th>Bil</th>
                                    <th>Mutu Emas</th>
                                    <th>Karat</th>
                                    <th>Harga/Gram</th>
                                    <th>Tarikh Ubah</th>
                                    <th>Tindakan</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach ($harga_emas as $index=>$he)
                                <tr>
                                <td>{{ $loop->iteration }}</td>
                                <td>{{ $he['keterangan'] }}</td>
                                <td>{{ $he['karat'] }}</td>
                                <td>{{ $he['harga'] }}</td>
                                <td>{{ $he['tarikhupdate'] }}</td>
                                <td class="text-center">
                                    <a href="#" onclick="editharga(this)" data-recno="{{ $he['recno'] }}" class="btn btn-primary"><i class="fas fa-edit"></i></a>
                                    <a href="#" id="modal_history_es" data-toggle="modal" data-target="#history_emas" data-karat="{{ $he['karat'] }}" class="btn btn-info"><i class="fas fa-info"></i></a>
                                </td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>

                        
                    <div class="mt-3 col-12 d-flex justify-content-end pr-1">
                
                        <a href="#" data-toggle="modal" data-target="#createHargaModal" data-backdrop="static" data-keyboard="false" class="btn btn-info ">Tambah Harga Emas</a>
                    </div>

                </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="history_emas" tabindex="-1" data-backdrop="static"  aria-labelledby="ModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="ModalLabel">Rekod Emas Sandaran</h5>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-12">
                        <table id="history_es">
                            <thead>
                                <tr>
                                    <td></td>
                                    <td>Karat</td>
                                    <td>Harga/Gram</td>
                                    <td>Tarikh Efektif</td>
                                    <td>Masa Efektif</td>
                                </tr>
                            </thead>
                        </table>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <div class="form-group row">
                    <button type="button" class="btn btn-danger" name="tutup_komuditi" id="tutup_komuditi" data-dismiss="modal">Tutup</button>
                </div>
            </div>
            </form>
        </div>
    </div>
</div>
<script>

    let api_url = "{{ $api_url }}";

    var tableemas = $('#table-emas').DataTable({
        "scrollX": true
    });

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

    var history_es = $('#history_es').DataTable();

    $('#terima_emas').on('click',function(){
        $('#kemaskiniHargaModal').modal('hide');
        var recno = $('#recno').val();
        var harga = $('#harga').val();

        $.ajax({
            method:'put',
            datatype:'json',
            url: api_url + '/api/harga_emas/update',
            data:{
                'recno': recno,
                'harga': harga
            },
            success:function(data){
                if(data == 1){
                    Swal.fire(
                        'Berjaya!',
                        'Harga Emas Berjaya Dikemaskini',
                        'success'
                    ).then(function(){
                        location.reload();
                    });
                }
            }
        });
    });

    $('#terima_emas_baru').on('click',function(){
        $('#createHargaModal').modal('hide');
        var karat = $('#karat_baru').val();
        var harga = $('#harga_baru').val();
        var keterangan = $('#keterangan_baru').val();

        $.ajax({
            method:'post',
            datatype:'json',
            url: api_url + '/api/harga_emas/create',
            data:{
                'karat' : karat,
                'keterangan': keterangan,
                'harga' : harga,
                'harga2' : harga
            },
            success:function(data){
                if(data == 1){
                    Swal.fire(
                        'Berjaya!',
                        'Emas Baru Berjaya Ditambah',
                        'success'
                    ).then(function(){
                        location.reload();
                    });
                }else{
                    Swal.fire(
                        'Tidak Berjaya',
                        'Emas Baru Tidak Berjaya Ditambah',
                        'error'
                    )
                }
            }
        });
    });
    
    $('#history_emas').on('show.bs.modal', function(event) {
        var button = $(event.relatedTarget);
        var karat = button.data('karat');
        history_es.clear().draw();
        $.ajax({
            method:'get',
            url: api_url + '/api/harga_emas/rekod/sandaran',
            datatype:'json',
            data:{
                'karat':karat
            },
            success:function(rekodsandaran){
                var bilcount = 0;
                $.each(rekodsandaran,function(index,emassandaran){
                    bilcount ++
                    history_es.row.add([
                        bilcount,
                        emassandaran['karat'],
                        formatter.format(emassandaran['harga']),
                        emassandaran['t_efektif'],
                        emassandaran['m_efektif']
                    ]).draw();
                });
            }
        });
    });
 

    function editharga(e)
    {
        let rec_no = $(e).data('recno');
        let keterangan = $(e).closest('tr').find('td:eq(1)').text();
        let karat = $(e).closest('tr').find('td:eq(2)').text();
        let harga = $(e).closest('tr').find('td:eq(3)').text();

        $('#recno').val(rec_no);
        $('#keterangan').val(keterangan);
        $('#karat').val(karat);
        $('#harga').val(harga);

        $('#kemaskiniHargaModal').modal();

    }

</script>

@endsection
