@extends('adminlte::page') @section('content')
<?php Fun::lang() ?>
<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="#">Setting</a></li>
            <li><a href="#">Payment Policy Detail</a></li>
            <li><a href="#">Edit</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-page">
                    <div class="pull-left">
                        <h3>Edit Payment Policy Detail</h3>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-sm-12 col-xs-12">
            <div class="form-wrapper">
                {!! Form::model($data, ['method' => 'PATCH','route' => ['payment-policy-detail.update', $data->id],'enctype'=>'multipart/form-data','autocomplete'=>'off']) !!}
                @csrf
                <div class="row">
                    <div class="form-group col-sm-4 col-xs-12">
                        <label class="required">Payment Policy </label>
                        {!! Form::select('payment_policy',$payment_policy, $data->policy_id, ['class' => 'form-control select2','required']) !!}
                    </div>
                    <div class="form-group col-sm-4 col-xs-12">
                        <label>Category Item</label>
                        {!! Form::select('category_id[]',$categories,$categories_opt, ['class' => 'form-control select2','multiple']) !!}
                    </div>
                    <div class="form-group col-sm-4 col-xs-12">
                        <label>Sub Category Item</label>
                        {!! Form::select('sub_category_id[]',$sub_categories,$sub_categorys_opt, ['class' => 'form-control select2','multiple']) !!}
                    </div>
                    <div class="col-sm-12">&nbsp;</div>
                    <div class="form-group col-sm-4 col-xs-12">
                        <label class="required">Payment Policy Detail Name</label>
                        {!! Form::text('name', $data->name, ['class' => 'form-control','required']) !!}
                    </div>
                    <div class="form-group col-sm-4 col-xs-12">
                        <label class="required">Loan Generate Type</label>
                        {!! Form::select('loan_generate_type',['0'=>'Please Select','1'=>'Loan without interest','2'=>'Loan with interest','3'=>'Loan with interest (interest is changeable)','4'=>'Loan with interest and without interest ','5'=>'Loan without interest and with interest'], $data->loan_generate_type, ['class' => 'form-control select2','required','id'=>'loan_generate_type']) !!}
                    </div>
                    <div class="form-group col-sm-4 col-xs-12" id="number_month_generate">
                        @if($data->loan_generate_type==2||$data->loan_generate_type==3)
                        <?php $data_details = DB::table('nso007_payment_policy_detail_loan_with_interest')->where("payment_policy_detail_id",$data->id)->first(); ?>
                        <label class="required">Loan Type</label>
                        {!! Form::select('loan_type',['1'=>'បង់រំលោះថេរ','2'=>'បង់រំលោះថយ'],$data_details->loan_type, ['class' => 'form-control select2','required','id'=>'loan_type-id']) !!}
                        @endif
                        @if($data->loan_generate_type==1)
                        <?php  $data_details = DB::table("nso007_payment_policy_detail_loan_without_interest")->where("payment_policy_detail_id",$data->id)->orderBy("id","ASC")->get(); ?>
                        <label class="required">Number of Payments</label>
                        {!! Form::text('months_number',count($data_details), ['class' => 'form-control','required','id'=>'months_number-id']) !!}
                        @endif
                        @if($data->loan_generate_type == 5)
                        <?php  
                            $details_no_interest = DB::table("nso007_payment_policy_detail_loan_without_interest")
                                                    ->where("payment_policy_detail_id",$data->id)
                                                    ->orderBy("id","ASC")
                                                    ->get(); 
                            $details_with_interest = DB::table('nso007_payment_policy_detail_loan_with_interest')
                                                    ->where("payment_policy_detail_id",$data->id)
                                                    ->first();
                        ?>
                        <label class="required">Number of months payment without interest</label>
                        {!! Form::text('months_number',count($details_no_interest), ['class' => 'form-control','required','id'=>'months_number-id']) !!}
                        @endif
                    </div>
                </div>
                <div class="row" id="interest_not_interest">
                    @if($data->loan_generate_type==4)
                    <?php 
                        $details_no_interest = DB::table("nso007_payment_policy_detail_loan_without_interest")
                                                    ->where("payment_policy_detail_id",$data->id)
                                                    ->orderBy("id","ASC")
                                                    ->get(); 
                        $details_with_interest = DB::table('nso007_payment_policy_detail_loan_with_interest')
                                                    ->where("payment_policy_detail_id",$data->id)
                                                    ->first();
                    ?>
                        @if(!empty($details_with_interest))
                            <div class="form-group col-sm-12 col-xs-12" style="border-top:1px solid black;margin-top:25px;margin-bottom:25px;"></div>
                            <div class="form-group col-sm-12 col-xs-12">
                                <h3> Loan with interest </h3>
                            </div>
                            <div class="form-group col-sm-6 col-xs-12">
                                <label class="required">Loan Type</label>
                                {!! Form::select('loan_type',['1'=>'បង់រំលោះថេរ','2'=>'បង់រំលោះថយ'],$details_with_interest->loan_type, ['class' => 'form-control select2','required','id'=>'loan_type-id']) !!}
                            </div>
                            <div class="form-group col-sm-6 col-xs-12">
                                <label class="required">Term</label>
                                {!! Form::text('term','Month', ['class' => 'form-control','required','readonly']) !!}
                            </div>
                            <div class="form-group col-sm-6 col-xs-12">
                                <label class="required">Duration</label>
                                {!! Form::text('duration',$details_with_interest->duration, ['class' => 'form-control','required','id'=>'duration-id']) !!}
                            </div>
                            <div class="form-group col-sm-6 col-xs-12">
                                <label class="required">Interest</label>
                                {!! Form::text('interest',$details_with_interest->interest, ['class' => 'form-control','required','id'=>'interest-id']) !!}
                            </div>
                        @endif

                        @if(!empty($details_no_interest)&&count($details_no_interest)>0)
                            <div class="form-group col-sm-12 col-xs-12" style="border-top:1px solid black;margin-top:25px;margin-bottom:25px;"></div>
                            <div class="form-group col-sm-12 col-xs-12">
                                <h3> Loan without interest </h3>
                            </div>
                            <div class="form-group col-sm-12 col-xs-12">
                                <label class="required">Number of months payment without interest</label>
                                {!! Form::text('months_number',count($details_no_interest), ['class' => 'form-control','required','id'=>'months_number-id']) !!}
                            </div>
                            @foreach ($details_no_interest as $de_no=>$detail)
                                <table class="table table-striped table-bordered">
                                    <tbody class="rate-condition-appending">
                                    <tr>
                                        <td width="10%">
                                        <label class="control-label required"><strong>ការបង់លើកទី{{ $de_no+1 }} (%)</strong></label>
                                        </td>
                                        <td>
                                        <input type="text" required class="form-control number-only" name="percentage_of_amount[]" placeholder="ភាគរយនៃទឹកប្រាក់" value="{{ $detail->percentage_of_amount }}">
                                        </td>
                                    </tr>
                                    </tbody>
                                </table>
                            @endforeach
                        @endif
                    @endif
                </div>
                <div class="row" id="number_month_generate_row">
                    @if($data->loan_generate_type==2||$data->loan_generate_type==3)
                        <div class="form-group col-sm-4 col-xs-12">
                            <label class="required">Term</label>
                            {!! Form::text('term','Month', ['class' => 'form-control','required','readonly']) !!}
                        </div>
                        <div class="form-group col-sm-4 col-xs-12">
                            <label class="required">Duration</label>
                            {!! Form::text('duration',$data_details->duration, ['class' => 'form-control','required','id'=>'duration-id']) !!}
                        </div>
                        <div class="form-group col-sm-4 col-xs-12">
                            <label class="required">Interest</label>
                            {!! Form::text('interest',$data_details->interest, ['class' => 'form-control','required','id'=>'interest-id']) !!}
                        </div>
                    @endif
                </div>
                <div class="row" id="change_rate_condition" style="padding: 15px;">
                    <?php 
                        if($data->loan_generate_type==1){
                           
                            foreach($data_details as $key=>$value){
                                echo ' <table class="table table-striped table-bordered">
                                            <tbody class="rate-condition-appending">
                                            <tr>
                                                <td width="10%">
                                                <label class="control-label required"><strong>ការបង់លើកទី'.($key+1).' (%)</strong></label>
                                                </td>
                                                <td>
                                                <input type="text" required class="form-control number-only" name="percentage_of_amount[]" placeholder="ភាគរយនៃទឹកប្រាក់" value="'.$value->percentage_of_amount.'">
                                                </td>
                                            </tr>
                                            </tbody>
                                        </table>';
                            }
                        } 
                    ?>
                    @if($data->loan_generate_type==3)
                        <?php $change_interests = DB::table('nso007_payment_policy_detail_loan_change_interest')->where("payment_policy_detail_id",$data->id)->orderBy("id","ASC")->get(); ?>
                        <div style="border-bottom: 2px solid #4CAF50">Change Rate Conditions<span class="pull-right button_primary btn-sm plus_ens fa fa-plus add-change-rate-option"></span></div>
                        <br />
                        
                        <table class="table table-striped table-bordered">
                        <tbody class="rate-condition-appending">
                            @foreach($change_interests as $i=>$change_interest)
                            <tr>
                                <td width="8%">
                                    <label class="control-label required"><strong> From Month: </strong></label>
                                </td>
                                <td>
                                    <input type="text" required class="form-control number-only change_rate_from  number-only" name="change_rate_from[]" placeholder="Rank of Month" value="{{ $change_interest->from_month }}">
                                </td>
                                <td width="9%">
                                    <label class="control-label required"><strong> To Month: </strong></label>
                                </td>
                                <td>
                                    <input type="text" value="{{ $change_interest->to_month }}" required name="change_rate_to[]" class="number-only form-control change_rate_to  number-only" placeholder="Rank of Month">
                                </td>
                                <td width="7%">
                                    <label class="control-label required"><strong> Rate : </strong></label>
                                </td>
                                <td>
                                    <input type="text" value="{{ $change_interest->interest }}" required name="rate_condition[]" class="number-only form-control rate_condition" placeholder="Interest">
                                </td>
                                @if($i==0)
                                <td><button type="button" class="btn btn-sm btn-danger" disabled><i class="fa fa-trash-o"></i></button></td>
                                @else
                                <td><button type="button" class="btn btn-sm btn-danger" onclick="$(this).closest('tr').remove();"><i class="fa fa-trash-o"></i></button></td>
                                @endif
                            </tr>
                            @endforeach
                        </tbody>
                        </table><br><br />
                        
                    @endif
                </div>
                <div id="bwi-append">
                    <br/><br/>
                    <h3 class="loan-generate-info"></h3>
                    <div class="row loan-generate-info-for-append" style="padding: 15px;">
                        
                    </div>
                </div>
                <div class="row" id="not_interest_interest">
                    @if($data->loan_generate_type==5)
                        @if(!empty($details_no_interest)&&count($details_no_interest)>0)
                            @foreach ($details_no_interest as $de_no=>$detail)
                                <table class="table table-striped table-bordered">
                                    <tbody class="rate-condition-appending">
                                    <tr>
                                        <td width="10%">
                                        <label class="control-label required"><strong>ការបង់លើកទី{{ $de_no+1 }} (%)</strong></label>
                                        </td>
                                        <td>
                                        <input type="text" required class="form-control number-only" name="percentage_of_amount[]" placeholder="ភាគរយនៃទឹកប្រាក់" value="{{ $detail->percentage_of_amount }}">
                                        </td>
                                    </tr>
                                    </tbody>
                                </table>
                            @endforeach
                        @endif
                        @if(!empty($details_with_interest))
                            <div class="form-group col-sm-12 col-xs-12" style="border-top:1px solid black;margin-top:25px;margin-bottom:25px;"></div>
                            <div class="form-group col-sm-12 col-xs-12">
                                <h3> Loan with interest </h3>
                            </div>
                            <div class="form-group col-sm-6 col-xs-12">
                                <label class="required">Loan Type</label>
                                {!! Form::select('loan_type',['1'=>'បង់រំលោះថេរ','2'=>'បង់រំលោះថយ'],$details_with_interest->loan_type, ['class' => 'form-control select2','required','id'=>'loan_type-id']) !!}
                            </div>
                            <div class="form-group col-sm-6 col-xs-12">
                                <label class="required">Term</label>
                                {!! Form::text('term','Month', ['class' => 'form-control','required','readonly']) !!}
                            </div>
                            <div class="form-group col-sm-6 col-xs-12">
                                <label class="required">Duration</label>
                                {!! Form::text('duration',$details_with_interest->duration, ['class' => 'form-control','required','id'=>'duration-id']) !!}
                            </div>
                            <div class="form-group col-sm-6 col-xs-12">
                                <label class="required">Interest</label>
                                {!! Form::text('interest',$details_with_interest->interest, ['class' => 'form-control','required','id'=>'interest-id']) !!}
                            </div>
                        @endif
                    @endif
                </div>
                <div class="row">
                    <div class="col-sm-12 col-xs-12">
                        <div class="form-group">
                            <button type="submit" class="button_sm button_primary pull-right"><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>
                <br/>
                {!! Form::close() !!}
            </div>
        </div>
    </div>
</div>
@endsection @section('js')
<script>
    $(document).on("keyup","#months_number-id",function(){
        var $_value = $(this).val();
        var div = ``;
        for(var i = 0 ; i<$_value;i++){
            div += `<table class="table table-striped table-bordered">
                        <tbody class="rate-condition-appending">
                        <tr>
                            <td width="10%">
                            <label class="control-label required"><strong>ការបង់លើកទី${i+1} (%)</strong></label>
                            </td>
                            <td>
                            <input type="text" required class="form-control number-only" name="percentage_of_amount[]" placeholder="ភាគរយនៃទឹកប្រាក់">
                            </td>
                        </tr>
                        </tbody>
                    </table>`;
        }
        $("#number_month_generate_row").html('');
        $("#change_rate_condition").html(div);
    });
    $(document).on("change","#loan_generate_type",function(){
        var $_this_value =$(this).val();
        $("#not_interest_interest").html(``);
        $("#interest_not_interest").html(``);
        if($_this_value==1){
            $("#number_month_generate").html(`<label class="required">Number of Payments</label>
                        {!! Form::text('months_number',null, ['class' => 'form-control','required','id'=>'months_number-id']) !!}`);
            $("#number_month_generate_row").html(``);
            $("#change_rate_condition").html(``);
        }
        else{
            $("#number_month_generate").html(``);
            if($_this_value==2){
                $("#number_month_generate").html(`
                    <label class="required">Loan Type</label>
                    {!! Form::select('loan_type',['1'=>'បង់រំលោះថេរ','2'=>'បង់រំលោះថយ'],null, ['class' => 'form-control select2','required','id'=>'loan_type-id']) !!}
                `);
                $("#number_month_generate_row").html(`
                    <div class="form-group col-sm-4 col-xs-12">
                        <label class="required">Term</label>
                        {!! Form::text('term','Month', ['class' => 'form-control','required','readonly']) !!}
                    </div>
                    <div class="form-group col-sm-4 col-xs-12">
                        <label class="required">Duration</label>
                        {!! Form::text('duration',null, ['class' => 'form-control','required','id'=>'duration-id']) !!}
                    </div>
                    <div class="form-group col-sm-4 col-xs-12">
                        <label class="required">Interest</label>
                        {!! Form::text('interest',null, ['class' => 'form-control','required','id'=>'interest-id']) !!}
                    </div>
                `);
                $("#change_rate_condition").html(``);
            }
            else if($_this_value==3){
                $("#number_month_generate").html(`
                    <label class="required">Loan Type</label>
                    {!! Form::select('loan_type',['1'=>'បង់រំលោះថេរ','2'=>'បង់រំលោះថយ'],null, ['class' => 'form-control select2','required','id'=>'loan_type-id']) !!}
                `);
                $("#number_month_generate_row").html(`
                    <div class="form-group col-sm-4 col-xs-12">
                        <label class="required">Term</label>
                        {!! Form::text('term','Month', ['class' => 'form-control','required','readonly']) !!}
                    </div>
                    <div class="form-group col-sm-4 col-xs-12">
                        <label class="required">Duration</label>
                        {!! Form::text('duration',null, ['class' => 'form-control','required','id'=>'duration-id']) !!}
                    </div>
                    <div class="form-group col-sm-4 col-xs-12">
                        <label class="required">Interest</label>
                        {!! Form::text('interest',null, ['class' => 'form-control','required','id'=>'interest-id']) !!}
                    </div>
                `);
                $("#change_rate_condition").html(`
                            <div style="border-bottom: 2px solid #4CAF50">Change Rate Conditions<span class="pull-right button_primary btn-sm plus_ens fa fa-plus add-change-rate-option"></span></div>
                            <br />
                            <table class="table table-striped table-bordered">
                              <tbody class="rate-condition-appending">
                                <tr>
                                  <td width="8%">
                                    <label class="control-label required"><strong> From Month: </strong></label>
                                  </td>
                                  <td>
                                    <input type="text" required class="form-control number-only change_rate_from  number-only" name="change_rate_from[]" placeholder="Rank of Month">
                                  </td>
                                  <td width="9%">
                                    <label class="control-label required"><strong> To Month: </strong></label>
                                  </td>
                                  <td>
                                    <input type="text" required name="change_rate_to[]" class="number-only form-control change_rate_to  number-only" placeholder="Rank of Month">
                                  </td>
                                  <td width="7%">
                                    <label class="control-label required"><strong> Rate : </strong></label>
                                  </td>
                                  <td>
                                    <input type="text" required name="rate_condition[]" class="number-only form-control rate_condition" placeholder="Interest">
                                  </td>
                                  <td><button type="button" class="btn btn-sm btn-danger" disabled><i class="fa fa-trash-o"></i></button></td>
                                </tr>
                              </tbody>
                            </table><br><br />
                `);
            }
            else if($_this_value==4){
                $("#change_rate_condition").html(``);
                $("#number_month_generate_row").html(``);
                $("#interest_not_interest").html(``);
                $("#interest_not_interest").html(`
                    <div class="form-group col-sm-12 col-xs-12" style="border-top:1px solid black;margin-top:25px;margin-bottom:25px;"></div>
                    <div class="form-group col-sm-12 col-xs-12">
                        <h3> Loan with interest </h3>
                    </div>
                    <div class="form-group col-sm-6 col-xs-12">
                        <label class="required">Loan Type</label>
                        {!! Form::select('loan_type',['1'=>'បង់រំលោះថេរ','2'=>'បង់រំលោះថយ'],null, ['class' => 'form-control select2','required','id'=>'loan_type-id']) !!}
                    </div>
                    <div class="form-group col-sm-6 col-xs-12">
                        <label class="required">Term</label>
                        {!! Form::text('term','Month', ['class' => 'form-control','required','readonly']) !!}
                    </div>
                    <div class="form-group col-sm-6 col-xs-12">
                        <label class="required">Duration</label>
                        {!! Form::text('duration',null, ['class' => 'form-control','required','id'=>'duration-id']) !!}
                    </div>
                    <div class="form-group col-sm-6 col-xs-12">
                        <label class="required">Interest</label>
                        {!! Form::text('interest',null, ['class' => 'form-control','required','id'=>'interest-id']) !!}
                    </div>
                    <div class="form-group col-sm-12 col-xs-12" style="border-top:1px solid black;margin-top:25px;margin-bottom:25px;"></div>
                    <div class="form-group col-sm-12 col-xs-12">
                        <h3> Loan without interest </h3>
                    </div>
                    <div class="form-group col-sm-12 col-xs-12">
                        <label class="required">Number of payments</label>
                        {!! Form::text('months_number',null, ['class' => 'form-control','required','id'=>'months_number-id']) !!}
                    </div>
                `);
            }else if($_this_value==5){
                
                $("#change_rate_condition").html(``);
                $("#number_month_generate_row").html(``);
                $("#not_interest_interest").html(``);
                $("#number_month_generate").html(`
                        <label class="required">Number of months payment without interest</label>
                        {!! Form::text('months_number',null, ['class' => 'form-control','required','id'=>'months_number-id']) !!}`);
                $("#not_interest_interest").html(`
                    <div class="form-group col-sm-12 col-xs-12" style="border-top:1px solid black;margin-top:25px;margin-bottom:25px;"></div>
                    <div class="form-group col-sm-12 col-xs-12">
                        <h3> Loan with interest </h3>
                    </div>
                    <div class="form-group col-sm-6 col-xs-12">
                        <label class="required">Loan Type</label>
                        {!! Form::select('loan_type',['1'=>'បង់រំលោះថេរ','2'=>'បង់រំលោះថយ'],null, ['class' => 'form-control select2','required','id'=>'loan_type-id']) !!}
                    </div>
                    <div class="form-group col-sm-6 col-xs-12">
                        <label class="required">Term</label>
                        {!! Form::text('term','Month', ['class' => 'form-control','required','readonly']) !!}
                    </div>
                    <div class="form-group col-sm-6 col-xs-12">
                        <label class="required">Duration</label>
                        {!! Form::text('duration',null, ['class' => 'form-control','required','id'=>'duration-id']) !!}
                    </div>
                    <div class="form-group col-sm-6 col-xs-12">
                        <label class="required">Interest</label>
                        {!! Form::text('interest',null, ['class' => 'form-control','required','id'=>'interest-id']) !!}
                    </div>
                `);
            }
            else{}
        }
        $(".select2").select2();
    });
    $(document).on("click",".add-change-rate-option",function()
    {
        $(".rate-condition-appending").append(`<tr>
                <td width="8%">
                <label class="control-label"><strong> From Month: </strong></label>
                </td>
                <td>
                <input type="text" class="form-control number-only change_rate_from" name="change_rate_from[]" placeholder="Rank of Month">
                </td>
                <td width="9%">
                <label class="control-label"><strong> To Month: </strong></label>
                </td>
                <td>
                <input type="text" name="change_rate_to[]" class="number-only form-control change_rate_to" placeholder="Rank of Month">
                </td>
                <td width="7%">
                <label class="control-label"><strong> Rate : </strong></label>
                </td>
                <td>
                <input type="text" name="rate_condition[]" class="number-only form-control rate_condition" placeholder="Interest">
                </td>
                <td><button type="button" class="btn btn-sm btn-danger" onclick="$(this).closest('tr').remove();"><i class="fa fa-trash-o"></i></button></td>
            </tr>`);
    });
</script>
@endsection