@extends('layouts.app_operasi')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-12">
            <div class="card">

                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb pb-3">
                      <li class="breadcrumb-item"><a href="{{ url('/') }}">Home</a></li>
                      <li class="breadcrumb-item active" aria-current="page">Kakitangan</li>
                    </ol>
                </nav>

                <div class="card-body text-center">
                    @if($auth_user->roles == 'operasi')
                    <form action="{{ route('kakitangan.baru') }}" method="post" onsubmit="return verifyregister()">
                    @elseif($auth_user->roles == 'admin')
                    <form action="{{ route('admin.kakitangan.baru') }}" method="post" onsubmit="return verifyregister()">
                    @endif
                        @csrf
                        <div class='row form-group'>
                            <div class='col-md-6'>
                                <div class='form-group row col-md-12'>
                                    <div class='col-md-5'>
                                        <b>Nama</b>
                                    </div>
                                    <div class='col-md-7'>
                                        <input type="text" name='nama' id='nama' value="" class='form-control'>
                                    </div>
                                </div>
                                <div class='form-group row col-md-12'>
                                    <div class='col-md-5'>
                                        <b>Email</b>
                                    </div>
                                    <div class='col-md-7'>
                                        <input type="text" name='email' id='email' value="" class='form-control'>
                                    </div>
                                </div>
                                <div class='form-group row col-md-12'>
                                    <div class='col-md-5'>
                                        <b>Cawangan</b>
                                    </div>
                                    <div class='col-md-7'>
                                        @php
                                            $cawangan_all = Http::withOptions(['verify' => config('app.api_verify')])->get($api_url . '/api/cawangan')->json();
                                        @endphp
                                        <select class="form-control" name="cawangan" data-placeholder="Pilih Cawangan" id="cawangan">
                                            <option disabled selected value> -- Pilih Cawangan -- </option>
                                            <option value="operasi">Operasi</option>
                                            <option value="callcenter">Call Center</option>
                                            @foreach($cawangan_all as $cawangan)
                                                <option value="{{ $cawangan['kodcaw'] }}">{{ $cawangan['details_caw'] }}</option>
                                            @endforeach
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <div class='col-md-6'>
                                <div class='form-group row col-md-12'>
                                    <div class='col-md-5'>
                                        <b>No KP</b>
                                    </div>
                                    <div class='col-md-7'>
                                        <input type="text" name='nokp' id='nokp' value="" class='form-control'>
                                    </div>
                                </div>
                                <div class='form-group row col-md-12'>
                                    <div class='col-md-5'>
                                        <b>No Telefon</b>
                                    </div>
                                    <div class='col-md-7'>
                                        <input type="text" name='telefon' id='telefon' value="" class='form-control'>
                                    </div>
                                </div>
                                <div class='form-group row col-md-12'>
                                    <div class='col-md-5'>
                                        <b>Jawatan</b>
                                    </div>
                                    <div class='col-md-7'>
                                        <select class="form-control" name="role" data-placeholder="Pilih Jawatan" id="role">
                                            <option disabled selected> -- Pilih Jawatan -- </option>
                                            @foreach($role as $roles)
                                                <option value="{{ $roles['role_code'] }}">{{ $roles['keterangan'] }}</option>
                                            @endforeach
                                        </select>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class='row justify-content-end'>
                            <div class='col-md-12'>
                                <input type="submit" class='btn btn-info float-right mr-5' value='Daftar'>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header">{{ __('Senarai Kakitangan') }}</div>
                <div class="card-body text-center">
                    <div class="row">
                        <div class="col-md-12">
                            <table id="rekod_kakitangan" class="table table-striped table-hover">
                                <thead class="thead-dark">
                                    <tr>
                                        <th>Bil</th>
                                        <th>Nama Staff</th>
                                        <th>Jawatan</th>
                                        <th>Cawangan</th>
                                        <th>Tindakan</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach($kakitangan as $index=>$staff)
                                        @if($staff->roles != 'admin')
                                        <tr>
                                            <td>{{ $loop->iteration }}</td>
                                            <td>{{ $staff->name }}</td>
                                            <td>{{ strtoupper($staff->roles) }}</td>
                                            <td>
                                                @if($staff->cawangan == 'operasi' || $staff->cawangan == 'callcenter' || $staff->cawangan == 'admin')
                                                    {{ ucwords($staff->cawangan) }}
                                                @else
                                                    @php
                                                    $cawangan_staff = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $staff->cawangan)->json();
                                                    echo  (isset($cawangan_staff['details_caw'])) ? ucwords($cawangan_staff['details_caw']) : 'test';
                                                    @endphp
                                                @endif
                                            </td>
                                            <td>
                                                @if($auth_user->roles == 'operasi')
                                                    <a href="{{ route('kakitangan.tukartaraf',['id'=>$staff->id]) }}" class="btn btn-warning" title="Tukar Taraf"><i class="material-icons">edit</i></a>
                                                    <a href="{{ route('kakitangan.tukarcawangan',['id'=>$staff->id]) }}" class="btn btn-info" title="Tukar Cawangan"><i class="material-icons">work</i></a>
                                                    <a href="{{ route('operasi.kakitangan.edit',['id'=>$staff->id]) }}" class="btn btn-success" title="Edit Maklumat"><i class="material-icons">settings</i></a>
                                                    <button type="button" class="btn btn-danger btn-delete" data-roles="{{ $auth_user->roles }}" data-staff-id="{{ $staff->id }}" data-token="{{ csrf_token() }}" title="Delete Kakitangan"><i class="material-icons">delete</i></button>
                                                @elseif($auth_user->roles == 'admin')
                                                {{-- <form method="post" action="{{ route('admin.kakitangan.delete') }}">
                                                    {{ @csrf_field() }} --}}
                                                    <input type="hidden" name="staff_id" value="{{ $staff->id }}"/>
                                                    <a href="{{ route('admin.kakitangan.tukartaraf',['id'=>$staff->id]) }}" class="btn btn-warning" title="Tukar Taraf"><i class="material-icons">edit</i></a>
                                                    <a href="{{ route('admin.kakitangan.tukarcawangan',['id'=>$staff->id]) }}" class="btn btn-info" title="Tukar Cawangan"><i class="material-icons">work</i></a>
                                                    <a href="{{ route('admin.kakitangan.edit',['id'=>$staff->id]) }}" class="btn btn-success" title="Edit Maklumat"><i class="material-icons">settings</i></a>
                                                    <a href="{{ route('admin.kakitangan.resetcredentials',['id'=>$staff->id]) }}" class="btn btn-primary" title="Reset Password"><i class="material-icons">restore</i></a>
                                                    <button type="button" class="btn btn-danger btn-delete" data-staff-id="{{ $staff->id }}" data-token="{{ csrf_token() }}" title="Delete Kakitangan"><i class="material-icons">delete</i></button>
                                                {{-- </form> --}}
                                                @endif
                                            </td>
                                        </tr>
                                        @endif
                                    @endforeach
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>

    var tablerekod = $('#rekod_kakitangan').DataTable();

    $("#rekod_kakitangan").on('click', ".btn-delete", function(){
    //$('.btn-delete').on('click', function(){
        // var form = $(this).closest('form');
        var roles = $(this).data('roles');
        var staff_id = $(this).data('staff-id');

        var url_redirected = (roles == 'operasi') ? "{{ route('operasi.kakitangan.delete') }}" : "{{ route('admin.kakitangan.delete') }}";

        Swal.fire({
                title: 'Padam Kakitangan Ini?',
                text: "Anda tidak boleh mengembalikan data ini balik selepas dipadamkan!",
                type: 'warning',
                showCancelButton: true,
                confirmButtonClass: 'btn btn-danger',
                cancelButtonClass: 'btn btn-warning',
                confirmButtonText: 'Padam!',
                cancelButtonText: 'Batal!',
                buttonsStyling: false
            }).then(function(result) {
            if (result.isConfirmed) {
                
                $.ajax({
                    method:'post',
                    url: url_redirected,
                    data: {
                        "_token": "{{ csrf_token() }}",
                        "staff_id" : staff_id
                    },
                    success:function(data){
                        if(data == 'success')
                        {
                            if(roles == 'operasi') 
                            window.location.href = "{{ route('daftar.kakitangan') }}";
                            else
                                window.location.href = "{{ route('admin.daftar.kakitangan') }}";

                            Swal.fire({
                                title: 'Memuatkan...',
                                allowEscapeKey : false,
                                allowOutsideClick: false,
                                didOpen: function () {
                                    Swal.showLoading();
                                }
                            });
                        }
                    },
                    error: function(xhr, status, error) {
                        var err = eval("(" + xhr.responseText + ")");
                        alert(xhr.responseText);
                    }
                });

            } 
        }).catch(swal.noop)
    });

    function verifyregister(){
        var cawangan = $('#cawangan').val();
        var role = $('#role').val();
        var nama = $('#nama').val();
        var email = $('#email').val();
        var nokp = $('#nokp').val();
        var telefon = $('#telefon').val();
        if (cawangan == null){
            Swal.fire({
                icon: 'error',
                title: 'Amaran!',
                text: 'Sila Pilih Cawangan Kakitangan'
            });
            event.preventDefault();
            return false;
        }
        if (role == null){
            Swal.fire({
                icon: 'error',
                title: 'Amaran!',
                text: 'Sila Pilih Jawatan Kakitangan'
            });
            event.preventDefault();
            return false;
        }
        if(cawangan == 'operasi'){
            if(role != 'operasi'){
                Swal.fire({
                    icon: 'error',
                    title: 'Amaran!',
                    text: 'Jawatan Tidak Sah'
                });
                event.preventDefault();
                return false;
            }
        }else{
            if(role == 'operasi'){
                Swal.fire({
                    icon: 'error',
                    title: 'Amaran!',
                    text: 'Jawatan Tidak Sah'
                });
                event.preventDefault();
                return false;
            }
        }
        if(nama == ''){
            Swal.fire({
                icon: 'error',
                title: 'Amaran!',
                text: 'Sila Masukan Nama Kakitangan'
            });
            event.preventDefault();
            return false;
        }else{
            if(nokp == ''){
                Swal.fire({
                    icon: 'error',
                    title: 'Amaran!',
                    text: 'Sila Masukan No KP Kakitangan'
                });
                event.preventDefault();
                return false;
            }else{
                if(isNaN(nokp) == true){
                    Swal.fire({
                        icon: 'error',
                        title: 'Amaran!',
                        text: 'Sila Masukan Hanya Nombor Pada No KP Kakitangan'
                    });
                    event.preventDefault();
                    return false;
                }else{
                    if(nokp.length != 12){
                        Swal.fire({
                            icon: 'error',
                            title: 'Amaran!',
                            text: 'Sila Periksa No KP Kakitangan'
                        });
                        event.preventDefault();
                        return false;
                    }else{
                        if(/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/.test(email) == false){
                            Swal.fire({
                                icon: 'error',
                                title: 'Amaran!',
                                text: 'Sila Periksa Email Kakitangan'
                            });
                            event.preventDefault();
                            return false;
                        }else{
                            if(telefon == ''){
                                Swal.fire({
                                    icon: 'error',
                                    title: 'Amaran!',
                                    text: 'Sila Masukan No Telefon Kakitangan'
                                });
                                event.preventDefault();
                                return false;
                            }else{
                                if(isNaN(telefon) == true){
                                    Swal.fire({
                                        icon: 'error',
                                        title: 'Amaran!',
                                        text: 'Sila Masukan Hanya Nombor Pada No Telefon Kakitangan'
                                    });
                                    event.preventDefault();
                                    return false;
                                }else{
                                    if(telefon.length <= 9 || telefon.length >= 12){
                                        Swal.fire({
                                            icon: 'error',
                                            title: 'Amaran!',
                                            text: 'Sila Periksa No Telefon Kakitangan'
                                        });
                                        event.preventDefault();
                                        return false;
                                    }else{
                                        if(cawangan == null){
                                            Swal.fire({
                                                icon: 'error',
                                                title: 'Amaran!',
                                                text: 'Sila Pilih Cawangan Kakitangan'
                                            });
                                        }else{
                                            return true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
</script>
@endsection
