@extends('layouts.print')

@section('content')
<style>
    table.table-bordered tr th,table.table-bordered tr td{
        border:1px rgb(133, 129, 129) solid !important;
    }
    table tr td.vertical,table tr th.vertical {
        vertical-align:middle !important;
        text-align: center !important;  
    }
    table tbody tr.first-change td{
        background-color: rgb(145, 204, 238) ;
    }
    table tbody tr.last-change td{
        background-color: rgb(169, 207, 159);
    }
    @media print  {
        table tbody tr.first-change td{
            background-color: rgb(145, 204, 238) !important;
            -webkit-print-color-adjust: exact; 
        }
        table tbody tr.last-change td{
            background-color: rgb(169, 207, 159) !important;
            -webkit-print-color-adjust: exact; 
        }
    }
</style>
<?php

use App\VillageModel;

$village = VillageModel::find($load[0]->village_id);
$address = $village->address();
?>
<div class="content-wrapper">
    <div class="box">
        <div class="content">
            <div class="container">
                <div class="page">
                    <div class="row">
                        <div>
                            <button class="btn btn-primary hidden-print pull-right" onclick="myFunction()"><span class="glyphicon glyphicon-print" aria-hidden="true"></span> Print</button>
                        </div>
                        <div class="col-sm-2">
                            
                            {{--<div>--}}
                            {{--<img src="{{asset('assets/images/resize')}}/{{$branch[0]->branch_img}}" width="130px">--}}
                            {{--</div>--}}

                        </div>
                        <div class="col-sm-8">
                            <br>
                            
                            <center>
                                <p style="font-family: 'Khmer OS Muol Light';font-size:150%;"> ឧបសម្ព័ន្ធ១</p>
                                <p style="font-family: 'Khmer OS Muol Light';font-size:120%;">ព័ត៌មាន និង តារាងបង់ប្រាក់ ថ្លៃទិញផ្ទះ</p>
                            </center>
                        </div>
                        <div class="col-sm-2 branch-info">
                            <!-- <label>{{$load[0]->project_name}}</label>
                            <label>{{\Globals::getProjectLocationBySaleID($load[0]->id)}}</label>
                            <label>{{$load[0]->project_phone}}</label> -->
                        </div>
                    </div>
                    <br/>
                    <div style="border: 1px solid gray;padding: 15px;padding-bottom:-0px;">
                        <p style="color: #045FB4 !important;font-size: 90%;margin-top: -25px;background-color: white !important;width: 130px !important;text-align: center;font-family: 'Khmer OS Muol Light'">ព័ត៌មានអតិថិជន</p>
                        <table width="100%" class="table table-bordered">
                            <tbody>
                            <tr>
                                <td>លេខកិច្ចសន្យា</td>
                                <td></td>
                                <td>ប្រភេទផ្ទះ</td>
                                <td>{{$load[0]->sub_category_name}}</td>
                                <td>បុរី ថៃ ឈុនគ្រី គម្រោង</td>
                                <td>{{$load[0]->project_name}}</td>
                            </tr>
                            <tr>
                                <td>ឈ្មោះអតិថិជន</td>
                                <td>{{$load[0]->client_fullname}}</td>
                                <td>លេខទូរស័ព្ទ</td>
                                <td>{{$load[0]->client_phone1}}</td>
                                <td>លេខផ្ទះ</td>
                                <td>{{$load[0]->item_name}}</td>

                            </tr>
                            <tr>
                                <td>លេខអត្តសញ្ញាណប័ណ្ណ</td>
                                <td>{{$load[0]->client_code}}</td>
                                <td>កូដផ្ទះ</td>
                                <td colspan="3">{{$load[0]->item_code}}</td>
                            </tr>
                            </tbody>
                        </table>
                    </div>
                    <br/>
                    <div style="border: 1px solid gray;padding: 15px;padding-bottom:-0px;">
                        <p style="color: #045FB4 !important;font-size: 80%;margin-top: -25px;background-color: white !important;width: 130px !important;text-align: center;font-family: 'Khmer OS Muol Light'">ព័ត៌មានបង់ប្រាក់</p>
                        <table width="100%" class="table table-bordered">
                            <tbody>
                            <tr>
                                <td>ប្រភេទកិច្ចសន្យា</td>
                                <td></td>
                                <td>គោលការណ៍</td>
                                <td>{{$payment_policy}}</td>
                                <td>តម្លៃសរុប</td>
                                <td>$ {{number_format($load[0]->loan_amount,2)}}</td>
                            </tr>
                            <tr>
                                <td>ថ្ងៃកក់ប្រាក់</td>
                                <td>{{date('d/m/Y',strtotime($load[0]->sale_date))}} {{ Globals::date_khmer_get_word($load[0]->sale_date,"d") }}</td>
                                <td>ប្រាក់កក់ទ្រនាប់ដៃ</td>
                                <td>$ {{number_format($deposit_amount,2)}}</td>
                                <td>តម្លៃលក់</td>
                                <td>$ {{number_format($load[0]->loan_amount-$load[0]->loan_discount-$load[0]->discount_commis-$load[0]->discounttype_amount,2)}}</td>
                            </tr>
                            <tr>
                                <td>រយ:ពេល​</td>
                                <td>{{$load[0]->loan_duration}} ខែ</td>
                                <td>អត្រាការប្រាក់​</td>
                                <?php 
                                    $loan_interest = "";
                                    $str_interest = explode("//",$load[0]->loan_interest);
                                    $first_change = array();
                                    $last_change = array();
                                    if(!empty($str_interest))
                                    {
                                        if(count($str_interest)>1){
                                            $loan_interest = "";
                                            for($i= 0;$i<count($str_interest);$i++)
                                            {
                                                $loan_interest .= $str_interest[$i];
                                                if($i<count($str_interest)-1) $loan_interest .= ", ";

                                                $substr = explode("%",$str_interest[$i]);
                                                if(!empty($substr))
                                                {
                                                    if(count($substr)>1)
                                                    {
                                                        $month_str = explode("to",$substr[1]);
                                                        if(!empty($month_str))
                                                        {
                                                            if(count($month_str)>1)
                                                            {
                                                                preg_match_all('!\d+!', $month_str[0], $matches);
                                                                array_push($first_change,$matches[0][0]);

                                                                preg_match_all('!\d+!', $month_str[1], $matches1);
                                                                array_push($last_change,$matches1[0][0]);
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        else{
                                            $loan_interest = $load[0]->loan_interest;
                                        }
                                    }
                                    else{
                                        $loan_interest = $load[0]->loan_interest;
                                    }
                                    // dd($first_change);
                                ?>
                                <td colspan="3">{{$loan_interest}}</td>
                            </tr>
                            </tbody>
                        </table>
                    </div>
                    <br/>

                    <table class="table table-bordered schedul"  cellpadding="0" cellspacing="0" width="100%">
                        <tbody>
                            <tr>
                                <td colspan="7" class="text-center" style="font-size:150% !important;"><h4>តារាងរំលស់ប្រាក់</h4></td>
                            </tr>
                            <tr>
                                <th class="vertical">លរ</th>
                                <th class="vertical">ថ្ងៃបង់ប្រាក់</th>
                                <th class="vertical">បរិយាយ</th>
                                <th class="vertical">ប្រាក់ដើមត្រូវបង់</th>
                                <th class="vertical">ការប្រាក់ត្រូវបង់</th>
                                <th class="vertical">សរុបប្រាក់ត្រូវបង់</th>
                                {{-- <th class="vertical">ប្រាក់ដើមនៅសល់</th> --}}
                                <th class="vertical">ផ្សេងៗ</th>
                            </tr>
                            <?php $count = 0; $first_month_count = 0;
                                if($deposit_amount>0){
                                    $deposit_schedule = DB::table('nso007_deposit_schedule')->where("ds_saleid",$load[0]->loan_saleid)->select("*")->get();
                                    ?>
                                    @if(!empty($deposit_schedule)&&count($deposit_schedule)>0)
                                        @foreach ($deposit_schedule as $d=>$deposit_v)
                                            <?php $count++; ?>
                                            <tr align="center">
                                                <td class="vertical">{{$d+1}}</td>
                                                <td class="vertical">{{date('d-m-Y',strtotime($deposit_v->ds_date))}} {{ Globals::date_khmer_get_word($deposit_v->ds_date,"d") }}</td>
                                                @if($deposit_v->type==0)
                                                <td class="vertical">ប្រាក់កក់លើកដំបូង</td>
                                                @elseif($deposit_v->type==1)
                                                <td class="vertical">ប្រាក់បង់បង្គ្រប់ខែទី1</td>
                                                <?php $first_month_count = 1; ?>
                                                @elseif ($deposit_v->type==2)
                                                <td class="vertical">ប្រាក់បង់ខែទី1</td>
                                                <?php $first_month_count = 1; ?>
                                                @endif
                                                <td class="vertical">${{number_format($deposit_v->ds_amount,2)}}</td>
                                                <td class="vertical">${{number_format(0,2)}}</td>
                                                <td class="vertical">${{number_format($deposit_v->ds_amount,2)}}</td>
                                                {{-- <td class="vertical">${{number_format($deposit_v->loanprocess_principleos,2)}}</td> --}}
                                                <td></td>
                                            </tr>
                                        @endforeach 
                                    @endif
                                    <?php
                                }
                            ?>
                            <?php $total_Principle=0;$total_Interest=0;$total_Payment_amount=0;?>
                            @foreach($load_proccess as $key => $value)
                                <?php $check = 0; ?>
                                <?php $count++; ?>
                                @foreach($first_change as $ii=>$v)
                                    @if(($key==$v-1)&&$ii>0)
                                        <tr align="center" class="first-change">
                                            <td class="vertical">{{$count}}</td>
                                            <td class="vertical">{{date('d-m-Y',strtotime($value->loanprocess_datepayment))}} {{ Globals::date_khmer_get_word($value->loanprocess_datepayment,"d") }}</td>
                                            <td class="vertical">ប្រាក់បង់ខែទី{{ $key+1+$first_month_count }}</td>
                                            <td class="vertical">${{number_format($value->loanprocess_principle,2)}}</td>
                                            <td class="vertical">${{number_format($value->loanprocess_interest,2)}}</td>
                                            <td class="vertical">${{number_format($value->loanprocess_owe,2)}}</td>
                                            {{-- <td class="vertical">${{number_format($value->loanprocess_principleos,2)}}</td> --}}
                                            <td></td>
                                        </tr>   
                                        <?php $check = 1; ?>
                                    @else
                                         
                                    @endif
                                @endforeach
                                @foreach($last_change as $i=>$vl)
                                    @if(($key==$vl-1)&&($i!=count($last_change)-1))
                                        <tr align="center" class="last-change">
                                            <td>{{$count}}</td>
                                            <td>{{date('d-m-Y',strtotime($value->loanprocess_datepayment))}} {{ Globals::date_khmer_get_word($value->loanprocess_datepayment,"d") }}</td>
                                            <td class="vertical">ប្រាក់បង់ខែទី{{ $key+1+$first_month_count }}</td>
                                            <td>${{number_format($value->loanprocess_principle,2)}}</td>
                                            <td>${{number_format($value->loanprocess_interest,2)}}</td>
                                            <td>${{number_format($value->loanprocess_owe,2)}}</td>
                                            {{-- <td>${{number_format($value->loanprocess_principleos,2)}}</td> --}}
                                            <td></td>
                                        </tr>   
                                        <?php $check = 1; ?>
                                    @else
                                    @endif
                                @endforeach
                                @if($check == 0)
                                    <tr align="center">
                                        <td class="vertical">{{$count}}</td>
                                        <td class="vertical">{{date('d-m-Y',strtotime($value->loanprocess_datepayment))}} {{ Globals::date_khmer_get_word($value->loanprocess_datepayment,"d") }}</td>
                                        <td class="vertical">ប្រាក់បង់ខែទី{{ $key+1+$first_month_count }}</td>
                                        <td class="vertical">${{number_format($value->loanprocess_principle,2)}}</td>
                                        <td class="vertical">${{number_format($value->loanprocess_interest,2)}}</td>
                                        <td class="vertical">${{number_format($value->loanprocess_owe,2)}}</td>
                                        {{-- <td class="vertical">${{number_format($value->loanprocess_principleos,2)}}</td> --}}
                                        <td></td>
                                    </tr>   
                                @endif
                            <?php $total_Principle+=$value->loanprocess_principle; $total_Interest+=$value->loanprocess_interest;$total_Payment_amount+= $value->loanprocess_owe; ?>
                            @endforeach()
                            <tr>
                                <td colspan="3" style="text-align: center;height: 30px;vertical-align: middle;padding-left: 15px;">សរុប</td>
                                <td style="text-align: center;height: 30px;vertical-align: middle;padding-left: 15px;">{{$total_Principle}}</td>
                                <td style="text-align: center;height: 30px;vertical-align: middle;padding-left: 15px;">${{$total_Interest}}</td>
                                <td style="text-align: center;height: 30px;vertical-align: middle;padding-left: 15px;">${{$total_Payment_amount}}</td>
                                <td colspan="2"></td>
                            </tr>
                        </tbody>
                    </table>

                    <br>

                    <table width="100%" border="0">

                        <tr align="center">

                            <td align="center" class="letter-bold">ស្នាមមេដៃនិងឈ្មោះ ភាគី <span class="letter-bold colorsd">(ខ)</span></td>

{{--                            <td align="center">បានឃើញនិងបញ្ជាក់ថា:<br>ស្នាមមេដៃស្តាំនេះ ពិតជាស្នាមមេដៃរបស់គូរភាគី ពិតប្រាកដមែន។<br><span class="letter-bold">ហត្ថលេខា និង ត្រា</span></td>--}}

                            <td align="center" class="letter-bold">​ស្នាមមេដៃនិងឈ្មោះ តំណាងភាគី<span class="letter-bold colorsd">​ (ក) ​</span></td>

                        </tr>

                        <tr align="center">

                            <td align="center"><br /><br /><br /><br /> {{$load[0]->client_fullname}} {{ $client_relationship_name }}</td>
{{--                            <td>&nbsp;</td>--}}

                            <td align="center"><br /><br /><br /><br />.....................</td>

                        </tr>

                    </table>


                </div>
            </div>
        </div>
    </div>
</div>
@endsection
<script>
    function myFunction() {
        window.print();
    }
</script>