<?php Fun::lang()?>
@extends('adminlte::page')
@section('content')
    <div class="title-page">
        <div class="page-bar">
            <ul class="page-breadcrumb">
                <li><a href="#"><i class="fa fa-dashboard"></i> Dashboard</a></li>
                <li><a href="#">Accounting</a></li>
                <li><a href="#">Create Income</a></li>
            </ul>
        </div>
    </div>
    <div class="page">
        <div class="top-content">
		    <div class="row">
		        <div class="col-sm-12 col-xs-12">
		            <div class="title-add">
                        <div class="pull-left">
                            <h3>Create Income</h3>
                        </div>
                    </div>
		        </div>
		    </div>
		</div>
        <div class="row">
            <div class="col-sm-12 col-xs-12">
               <div class="form-wrapper">
                    {!! Form::open(array('route' => 'accouting-income.store','method'=>'POST')) !!}

                    <div class="row">
                        <div class="form-group col-sm-6 col-xs-12">
                            <label class="required">Project Name</label>
                            {!! Form::select('pay_for', $projects, null, ['class' => 'form-control select2','required', 'id'=>'project-id']) !!}
                        </div>
                        <div class="form-group col-sm-6 col-xs-12">
                            <label class="required">Account Type</label>
                            {!! Form::select('journal_acccode', $account_chats, null, ['class' => 'form-control select2','required','id'=>'account-code']) !!}
                        </div>
                        <div class="form-group col-sm-6 col-xs-12" id="available-box" style="display: none;">
                            <label>Available Amount</label>
                            {!! Form::text('available_amount', null, array('placeholder' => 'Availbe Amount','class' => 'form-control available_amount number-only', 'id' => 'available-amount','readonly')) !!}
                        </div>
                        <div class="form-group col-sm-6 col-xs-12">
                            <label class="required">Receive Amount </label>
                            <span class="message_check_amount label label-danger" style="display: none;" ></span>
                            {!! Form::text('amount', null, array('placeholder' => 'Amount','class' => 'form-control number-only amount', 'id' => 'receive-amount','required')) !!}
                        </div>
                        <div class="form-group col-sm-6 col-xs-12">
                            <label class="required">Cash Flow Type</label>
                            {!! Form::select('cash_flow_id', $cash_flow, null, ['id' => 'cash-flow', 'class' => 'form-control select2','required']) !!}
                        </div>
                        <div class="form-group col-sm-6 col-xs-12">

                            <label class="required">Date</label>
                            {!! Form::text('journal_paydate', null, array('placeholder' => 'Date','class' => 'form-control date_picker','autocomplete'=>'off','required')) !!}

                        </div>
                        <div class="form-group col-sm-6 col-xs-12">
                            <label>Invoice</label>
                            {!! Form::text('journal_invoice', null, array('placeholder' => 'Invoice','class' => 'form-control')) !!}
                        </div>
                        <div class="form-group col-sm-6">
                            <label>Description</label>
                            {!! Form::textarea('journal_des', null, array('placeholder' => 'Description','class' => 'form-control','rows'=>'3')) !!}
                        </div>
                        <div class="form-group col-sm-12 col-xs-12">
                            <div class="form-group">
                                <button type="submit" class="button_sm button_primary pull-right" onclick="save_income(this)" id="btn-submit"><span class="fa fa-save"></span> Save</button>
                                <button type="button"  onClick="javascript:history.go(-1)" class="button_sm button_warning pull-right">
                                    <span class=" fa fa-arrow-circle-left"></span> Back</button>
                            </div>
                        </div>
                    </div>
                    {!! Form::close() !!}
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade bs-modal-lg" id="myModal" tabindex="-1" role="dialog" aria-hidden="true">
        <div class="modal-dialog modal-dm">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                    <h3 class="modal-title text-danger">Warning...!</h3>
                </div>
                <br/>
                <div class="row">
                    <div class="col-md-12">
                        <div class="col-md-10">
                            <h4>Receivable amount must be smaller than or equal available amount.</h4>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" data-dismiss="modal" class="button_primary"><i class="fa fa-close"></i> Close</button>
                </div>
            </div>
            <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
    </div>

@endsection

@section('js')
    <script type="text/javascript">
        $( document ).ready(function() {
        
            $("#account-code").change(function(){
                var account_code = $(this).val();
                if (account_code == 40005) {
                    $("#available-box").css({"display":"block"});
                    $(document).on('keyup','.amount',function(){
                        var amount = parseFloat($(this).val());
                        var available_amount = parseFloat($('.available_amount').val());
                        if (amount > available_amount){
                           $('.message_check_amount').html("Commission must be smaller or equal available commission amount");
                            $('.message_check_amount').show();
                            $('.amount').val('');
                        } else {
                            $('.message_check_amount').html("");
                            $('.message_check_amount').hide();
                        }
                    });
                        
                } else {
                    $("#available-box").css({"display":"none"});
                }
            });
            $("#project-id").change(function(){
                var project_id = $(this).val();
                if (project_id !== "") {
                    get_available_commission_by_project(project_id);
                } else {
                    $("#available-amount").val("");
                }
            });
        });
        function get_available_commission_by_project($project_id){
            var id = $project_id;
            $.ajax({
                url: "/realty/available-commission-receivable/"+id,
                type: "get",
                success: function(data){
                    var d = $.parseJSON(data);
                    $("#pid").val(d.id);
                    $("#available-amount").val(d[0].commission_available - d[0].commission_received);
                }
            });
        }
    

    </script>
@endsection
