@extends('layouts.web')

@section('css')
<link href="{{ asset('css/dataTables.bootstrap4.min.css') }}" rel="stylesheet" />
<link href="{{ asset('css/sweetalert2.min.css') }}" rel="stylesheet" />
<link href="{{ asset('css/bootstrap-datepicker.min.css') }}" rel="stylesheet" />
@endsection

@section('content')
<div class="container-fluid pt-3">
    <a href="{{ route('student.create') }}" class="btn btn-success rounded"><i class="fas fa-plus-circle"></i> เพิ่มนักเรียน</a>
    <div class="row mt-3">
        <div class="col">
            <div class="card card-outline card-success">
                <div class="card-body">
                    <table class="table student-table" id="student-table">
                        <thead>
                          <tr>
                            <th scope="col" width="2%">#</th>
                            <th scope="col" width="5%">เลขที่</th>
                            <th scope="col" width="7%">วันที่สมัคร</th>
                            <th scope="col" width="12%">ชื่อ-สกุล</th>
                            <th scope="col" width="7%">เลขที่บัตรประชาชน</th>
                            <th scope="col" width="28%">หลักสูตร</th>
                            <th scope="col" width="10%">วันสอบ</th>
                            {{-- <th scope="col" width="5%">ค่าเรียน</th>
                            <th scope="col" width="5%">ส่วนลด</th> --}}
                            <th scope="col" width="8%">ชำระแล้ว</th>
                            <th scope="col" width="8%">คงค้าง</th>
                            <th width="10%"></th>
                          </tr>
                        </thead>
                        <tbody>
                            @foreach ($students as $indexKey => $student)
                            <tr class="hand-hover">
                                <td class="align-middle">{{ $indexKey + 1 }}.</td>
                                <td class="align-middle"><a href="{{ route('student.show',$student->sid) }}" class="course-title-link">{{ $student->code_no."/".$student->code_month."/".$student->code_branch }}</a></td>
                                <td class="align-middle">{{ formatDateFromDB($student->register_date) }}</td>
                                <td class="align-middle">
                                    <a class="course-title-link @if(alertPayment($student->exam_date, ($student->course_cost - $student->discount - $student->paid))) {{ 'text-danger font-weight-bold' }} @endif"
                                        href="{{ route('student.show',$student->sid) }}"
                                    >
                                        {{ $student->firstname ." ". $student->lastname }}
                                    </a>
                                </td>
                                {{-- <td class="align-middle">{{ $student->phone_1 }} @if($student->phone_2 != null) , {{ $student->phone_2 }} @endif</td> --}}
                                <td class="align-middle">{{ $student->idcard }}</td>
                                <td class="align-middle">{{ "(".$student->type .") ". $student->name }}</td>
                                <td class="align-middle">{{ ($student->exam_date != "") ? formatDateFromDB($student->exam_date) : " " }}</td>
                                {{-- <td class="align-middle">{{ number_format($student->course_cost) }}</td>
                                <td class="align-middle">{{ ($student->discount != "" || $student->discount != "0" ) ? number_format($student->discount) : "-" }}</td> --}}
                                <td class="align-middle">{{ number_format($student->paid) }}</td>
                                <td class="align-middle @if( ($student->course_cost - $student->discount - $student->paid) > '0') text-danger" @else " @endif>{{ number_format( $student->course_cost - $student->discount - $student->paid) }}</td>
                                <td class="align-middle text-center">
                                    <a href="{{ route('student.edit',$student->sid) }}" class="edit-btn" title="แก้ไขตารางเรียน"><i class="far fa-edit"></i></a>
                                    {{-- <a href="#" class="edit-btn ml-1 text-info"><i class="fas fa-calendar-alt"></i> เลื่อนนัด</a>
                                    <br />      --}}
                                    <form id="graduate-frm{{ $indexKey }}" class="d-inline"
                                        method="POST"
                                        action="{{ route('student.graduate', $student->sid ) }}"
                                        onsubmit="confirmGraduateAlert(event, {{ $indexKey }}, 'นักเรียน')"
                                    >
                                    @csrf
                                    @method('PUT')
                                        <input type="hidden" name="gStu" value="{{ $student->firstname ." ". $student->lastname }}">
                                        <button type="submit" class="btn graduate-btn btn-sm ml-1" title="จบการเรียน">
                                            <i class="fas fa-graduation-cap"></i>
                                        </button>
                                    </form>
                                    <form id="del-frm{{ $indexKey }}" class="d-inline"
                                        method="POST"
                                        action="{{ route('student.destroy', $student->sid ) }}"
                                        onsubmit="confirmalert(event, {{ $indexKey }}, 'นักเรียน')"
                                    >
                                    @csrf
                                    @method('DELETE')
                                        <button type="submit" class="btn del-btn btn-sm ml-1" title="ลบข้อมูล">
                                            <i class="far fa-trash-alt"></i>
                                        </button>
                                    </form>
                                </td>
                            </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@section('footerscript')
<script src="{{ asset('js/sweetalert2.min.js') }}"></script>
<script src="{{ asset('js/jquery.dataTables.min.js') }}"></script>
<script src="{{ asset('js/dataTables.bootstrap4.min.js') }}"></script>
<script src="{{ asset('js/bootstrap-datepicker.min.js') }}"></script>
<script src="{{ asset('js/bootstrap-datepicker.th.min.js') }}"></script>
<script src="{{ asset('js/custom.js') }}"></script>
<script>
    $(document).ready(function() {
        $('.student-table').DataTable({
            "lengthChange": false,
            "info": false,
            "pageLength": 20,
            "language": {
                "emptyTable":   "ไม่พบข้อมูล",
                "paginate": {
                    "first":    "หน้าแรก",
                    "last":     "หน้าสุดท้าย",
                    "next":     "ถัดไป",
                    "previous": "ก่อนหน้า"
                },
                "search":       "ค้นหา:",
                "lengthMenu":   "จำนวนนักเรียนทั้งหมด _MENU_ คน",
                "info":         "ทั้งหมด _TOTAL_ คน",
                "infoEmpty":    "ไม่พบนักเรียน",
            }
        });

        $('#student-table tr td:not(:last-child)').click(function () {
            var href = $(this).closest('tr').find('a').attr('href');
            location.href = href;
        });

        $('.pay-date').datepicker({
            format: 'dd/mm/yyyy',
            autoclose: true,
            language: 'th',
            orientation: 'bottom',
            thaiyear: true,
        }).datepicker("setDate",'now');

    });
    function confirmGraduateAlert(e, f, m) {
    var form = 'graduate-frm' + f;
    var msg = m;
    e.preventDefault();
    Swal.fire({
            title: 'คุณต้องการเปลี่ยนสถานะนักเรียนเป็นเรียนจบแล้ว' + msg +' ?',
            icon: 'warning',
            showCancelButton: false,
            confirmButtonColor: '#62966C',
            denyButtonColor: '#CACACA',
            showDenyButton: true,
            confirmButtonText: 'ตกลง',
            denyButtonText: 'ยกเลิก'
        }).then((result) => {
            if (result.isConfirmed) {
                document.getElementById(form).submit();
            } else if (result.isDenied) {
                return false;
            }
        });
}
</script>
@if (session('feedback'))
<script>
    Swal.fire({
                text: '{{ session('feedback') }}',
                icon: 'success',
                confirmButtonText: 'ตกลง',
                confirmButtonColor: '#62966C'
    })
</script>
@endif
@endsection

