@extends('layout')


@section('content')

<style type="text/css">
    .table-form>tbody>tr>th, .table-form>tfoot>tr>th, .table-form>thead>tr>td, .table-form>tbody>tr>td, .table-form>tfoot>tr>td{
        padding: 5px;
        -- vertical-align: middle;
    }
    table.table-form tbody>tr.selected, table.table-form tbody>tr.selected td, table.table-form tbody>tr>.selected{
        color: white !important;
        background-color: #49b6d6 !important;
    }

    table, th, td, input, textarea, select {
        color:  black !important; 
    }
    .help-block {
        
        margin-top: 0px!important;
        margin-bottom: 0px!important;
    }
</style>

<div class="page-header page-header-default">
    <div class="breadcrumb-line">
        <ul class="breadcrumb">
            <li><i class="icon-home2 position-left"></i> Pembelian</li>
            <li class="active">Hutang Awal</li>
        </ul>
    </div>
</div>

<div class="content">
    <div class="panel panel-flat">
        <div class="panel-heading">
            <h5 class="panel-title">Form Input Hutang Awal</h5>
            <div class="heading-elements">
                <ul class="icons-list">
                    <li><a href="{{ url('hutang_awal') }}" class="text-warning"><i class="icon-circle-left2 warning"></i> Kembali</a></li>
                </ul>
            </div>
        </div>
        <div class="panel-body">
            <form class="form-horizontal" method="post" action="{{ url('hutang_awal').($data['data']['id']!='' ? '/'.$data['data']['id'] : '') }}" autocomplete="off" >
                <input type="hidden" name="_token" value="{{ csrf_token() }}">
                {{ method_field($data['method']) }}
                <input type="hidden" name="id" value="{{ $data['data']['id'] }}">

                <div class="col-sm-12 col-md-12">
                    <div class="row col-sm-6 col-md-6">
                        <div class="form-group">
                            {{ Form::label("Lokasi", "", ['class'=>'control-label col-sm-4']) }}
                            <div class="col-sm-8">
                                <select name="lokasi" class="form-control set_selected">
                                    @foreach($data['lokasi'] AS $key => $value)
                                    <option value="{{ $value->id_lokasi }}" {{ ($data['data']['lokasi']==$value->id_lokasi) ? "selected=''" : "" }}>{{ $value->nama_lokasi }}</option>
                                    @endforeach
                                </select>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="col-sm-12 col-md-12">
                    <div class="row col-sm-6 col-md-6">
                        <div class="form-group">
                            {{ Form::label("Label CV", "", ['class'=>'control-label col-sm-4']) }}
                            <div class="col-sm-8">
                                <div class="checkbox">
                                    <label>
                                        {{ Form::checkbox("cv", '1', $data['data']['cv'], ['class'=>'styled']) }}
                                    </label>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="control-label col-sm-4">Penyedia</label>
                        <div class="col-sm-8">
                            <select class="form-control select-search" name="penyedia">
                                <option value="">--- Pilih Penyedia ---</option>
                                @php foreach($data['supplier'] as $s){ 
                                $sel = $data['data']['id_penyedia'];
                                @endphp

                                <option value="@php echo $s->id_penyedia @endphp" {{ ($s->id_penyedia == $sel) ? "selected":"" }}>@php echo  $s->pic.' - '.$s->nama @endphp</option>

                                @php } @endphp
                            </select>
                        </div>
                    </div>
                <div class="form-group">
                    <label class="control-label col-sm-4">Tanggal Input</label>
                    <div class="col-sm-8">
                        <input class="form-control set_datepicker" type="text" name="tanggal" placeholder="Tanggal" value="{{ ($data['data']['tanggal'] == '') ? date('d-m-Y'):$data['data']['tanggal'] }}" required readonly />
                    </div>
                </div>
                    <div class="form-group">
                        <label class="control-label col-sm-4">Tanggal Transaksi</label>
                        <div class="col-sm-8">
                            <input class="form-control set_datepicker" type="text" name="tanggal_faktur" placeholder="Tanggal Faktur" value="{{ ($data['data']['tanggal_faktur'] == '') ? date('d/m/Y'):$data['data']['tanggal_faktur'] }}" onchange="get_nomor($(this)), set_jatuh_tempo($(this))" required readonly />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-sm-4">Tanggal Jatuh Tempo</label>
                        <div class="col-sm-8">
                            <input class="form-control set_datepicker" type="text" name="tanggal_tempo" placeholder="Tanggal" value="{{ ($data['data']['tanggal_tempo'] == '') ? date('d/m/Y'):$data['data']['tanggal_tempo'] }}" required readonly />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-sm-4">No Faktur</label>
                        <div class="col-sm-2 col-md-2">
                            {{ Form::text('no_faktur', $data['data']['no_faktur'], ['class'=>'form-control text-right']) }}
                        </div>
                        <div class="col-sm-6 col-md-6">
                            {{ Form::text("sufix_no_faktur", $data['data']['sufix_no_faktur'], ['class'=>'form-control']) }}
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-sm-4">Keterangan</label>
                        <div class="col-sm-8">
                            <textarea class="form-control" name="keterangan" rows="2">{{ $data['data']['keterangan'] }}</textarea>
                        </div>
                    </div>
                </div>

                <div class="col-sm-6 col-md-6">
                    <div class="form-group">
                        <label class="control-label col-sm-4 col-md-4">Jumlah</label>
                        <div class="col-sm-8 col-md-8">
                            <input class="form-control text-right" type="text" name="td_nominal" value="{{ $data['data']['nominal'] }}" onkeyup="setFormatNumber($(this), 'div'), get_jum()" min="0" required="" />
                            <p class="help-block pull-right">Rp 0</p>
                            {{ Form::hidden("temp_nominal", $data['data']['nominal']) }}
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-sm-4 col-md-4">&nbsp;</label>
                        <div class="col-sm-8 col-md-8 text-right">
                            <label class="checkbox-inline checkbox-right">
                                <input type="checkbox" name="termasuk_pajak" value="1" class="styled" onchange="termasuk_pajaks($(this))" onclick="termasuk_pajaks($(this))" {{ $data['data']['termasuk_pajak']==1 ? "checked='checked'" : "" }}> Sudah Termasuk PPN
                            </label>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-sm-4 col-md-4">PPN</label>
                        <div class="col-sm-8 col-md-8">
                            <select class="form-control select-search" name="pajak">
                                <option value="0" >Tidak</option>
                                <option value="1" >Iya</option>
                            </select>
                            <input type="hidden" name="td_pajak" class="form-control number-only text-right" value="0">
                            <p class="help-block pull-right" id="help_td_pajak">Rp 0</p>
                        </div>
                    </div>
                    <div class="form-group" style="font-weight: bold !important;">
                        <label class="control-label col-sm-4 col-md-4">Total</label>
                        <div class="col-sm-8 col-md-8">
                            <input type="hidden" name="td_total" class="form-control text-right" value="0">
                            <p class="form-control-static text-right" id="help_td_total">Rp0</p>
                        </div>
                    </div>
                </div>

                </div>

                <div class="panel-footer">
                    <div class="form-group col-md-12 col-sm-12">
                        <a href="{{ url('hutang_awal') }}" class="btn bg-warning-400">Kembali</a>
                        <div class="form-group pull-right">
                            <button class="btn btn-primary" type="submit">Simpan</button>
                        </div>
                    </div>
                </div>

                <input type="hidden" name="td_netto" class="form-control">
            </form>
        </div>
    </div>

    <script type="text/javascript">

    $(document).ready(function(){

        id = '{{ $data['data']['id']}}';
        if(id != ''){
            // get_edit(id);
        }

        $("select[name=pajak]").change();
        $(document).on("change","select[name=pajak]",function(e){
            if($(this).val()==1){
                $("input[name=td_pajak]").val(0);       
                $("input[name=td_pajak]").removeAttr('readonly');
            }else{
                $("input[name=td_pajak]").val(0);       
                $("input[name=td_pajak]").attr('readonly','readonly');
            }
            get_jum();
            netto();
        });

        $("select[name=pajak]").val("{{$data['data']['pajak']}}").change();
        $("input[name=termasuk_pajak]").trigger("change");
        $("input[name=tanggal_faktur]").trigger("change");
        $("input[name=td_nominal]").trigger("keyup");

        $("[name=td_pajak]").keyup(function(){
            netto();
        })

    })

    function get_nomor(this_){
        var id = $("input[name=id]").val();
        var date = this_.val();
        var s_date = date.split("/");
        var sufix_no_faktur = "";
        $.ajax({
            url : "{{ url('hutang_awal/get_nomor') }}",
            type : "get",
            data : {id:id, tanggal:date},
            dataType : "json",
            success : function(result){
                $("input[name=no_faktur]").val(result.no_faktur);
                $("input[name=sufix_no_faktur]").val(result.sufix_no_faktur);
            }
        });
    }

    function set_jatuh_tempo(this_) {
        var id = $("input[name=id]").val();
        var date = this_.val();
        var s_date = date.split("/");
        var _date = new Date(s_date[2], s_date[1], s_date[0]);
        _date.setMonth(_date.getMonth()+1);

        var dd = _date.getDate();
        var mm = _date.getMonth()+1;
        var yyyy = _date.getFullYear();
        if(mm < 10) mm = '0'+mm;
        if(dd < 10) dd = '0'+dd;

        var set = [dd, mm, yyyy];
        $("input[name=tanggal_tempo]").val(set.join("/"));
    }

    function termasuk_pajaks(this_) {
        var is_checked = this_.is(":checked");
        var temp_tagihan = $("input[name=td_nominal]").val();
        var temp_sub_total = 0;

        if(is_checked){
            temp_sub_total = (100/110)*temp_tagihan;
            // temp_sub_total = 0;

            $("input[name=temp_nominal]").val(temp_sub_total);
            $("select[name=pajak]").val(1).change();
            netto();
        }else{
            // temp_sub_total = $("input[name=td_netto]").val();
            // $("#td_total").text(accounting.formatMoney(temp_sub_total));
            // $("input[name=td_total]").val(temp_sub_total);

            // $("select[name=pajak]").val("{{ $data['data']['pajak'] }}").change();

            $("input[name=temp_nominal]").val(temp_tagihan);
            get_jum();

        }
    }

    function netto(){
        var is_checked = $("input[name=termasuk_pajak]").is(":checked");
        // var total = parseFloat($("[name=td_total]").val());
        var jumlah = parseFloat($("[name=td_nominal]").val());

        if(is_checked){
            jumlah = $("input[name=temp_nominal]").val();
        }

        var potongan = 0;
        var ppn = parseFloat($("[name=td_pajak]").val());

        var netto = parseFloat(0);
        var ppn = parseFloat(0);

        if($('select[name=pajak]').val()==1){
            ppn = parseFloat((jumlah-potongan)*(10/100));
        }
            
        netto = jumlah-potongan+ppn; //+bunga
        $("[name=td_pajak]").val(ppn);
        $("#help_td_pajak").text(accounting.formatMoney(ppn));

        $("[name=td_total]").val(netto);
        $("#help_td_total").text(accounting.formatMoney(netto));
    }

    function get_jum(){
        var is_checked = $("input[name=termasuk_pajak]").is(":checked");
        var jum = $("input[name=td_nominal]").val();

        if(is_checked){
            jum = jum*100/110;
            $("input[name=temp_nominal]").val(jum);
        }else{
            $("input[name=temp_nominal]").val(jum);
        }
        
        // $("#help_td_pajak").text(accounting.formatMoney($("[name=td_pajak]").val()));
        // $("#help_td_total").text(accounting.formatMoney(jum));
        // $("[name=td_total]").val(jum);
        netto();
    }

    function setFormatNumber(this_, p) {
        var parentz = this_.parent(p);
        var data = this_.val();
        parentz.find(".help-block").text(accounting.formatMoney(data));
    }
</script>

@endsection