@extends('layouts.master')

@section('title')
    Sync Noidentitas Dosen
@endsection

@section('header')
<div class="page-title">
    <h3>Dashboard</h3>
</div>
@endsection

@section('contents')
    <div class="page-title-box">
        <div class="row align-items-center">
            <div class="col-md-12">
                <h6 class="page-title">{!! $title !!}</h6>
                <ol class="breadcrumb m-0">
                    <li class="breadcrumb-item">Operator</li>
                    <li class="breadcrumb-item active">{!! $title !!}</li>
                </ol>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-body">
                    <p class="text-muted mb-3">
                        Dosen di bawah ini belum memiliki noidentitas. Klik <strong>Sync</strong> untuk mengambil NIDN/NUPTK dari API i-sdm ke kolom input, periksa atau ubah bila perlu, lalu klik <strong>Update</strong> untuk menyimpan ke biodata simpmw.
                    </p>
                    @if($users->isEmpty())
                        <p class="text-success mb-0">Tidak ada dosen dengan noidentitas kosong.</p>
                    @else
                        <div class="table-responsive">
                            <table class="table table-bordered table-hover">
                                <thead>
                                    <tr>
                                        <th width="50">No.</th>
                                        <th>Nama</th>
                                        <th>Email</th>
                                        <th>NIDN / NUPTK</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach ($users as $item)
                                        <tr>
                                            <td>{{ $loop->iteration }}</td>
                                            <td>{{ $item->name }}</td>
                                            <td>{{ $item->email }}</td>
                                            <td>
                                                <form action="{{ route('operator.sync-noidentitas.update') }}" method="POST" class="row-sync-form d-flex gap-1 align-items-center flex-wrap">
                                                    @csrf
                                                    <input type="hidden" name="biodata_id" value="{{ Crypt::encrypt($item->id) }}" class="row-biodata-id">
                                                    <input type="text" name="identifier" class="form-control form-control-sm row-identifier" placeholder="Klik Sync untuk ambil dari i-sdm" maxlength="50" required style="max-width: 180px;">
                                                    <input type="hidden" name="nip" class="row-nip" value="">
                                                    <input type="hidden" name="nidn" class="row-nidn" value="">
                                                    <button type="button" class="btn btn-sm btn-outline-primary btn-sync">Sync</button>
                                                    <button type="submit" class="btn btn-sm btn-primary">Update</button>
                                                </form>
                                            </td>
                                        </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
@endsection

@section('js')
<script>
$(function() {
    $('.btn-sync').on('click', function() {
        var $row = $(this).closest('.row-sync-form');
        var biodataId = $row.find('.row-biodata-id').val();
        var $input = $row.find('.row-identifier');
        var $btn = $(this);

        if (!biodataId) return;
        $btn.prop('disabled', true).text('...');

        $.ajax({
            url: '{{ route("operator.sync-noidentitas.fetch") }}',
            type: 'GET',
            data: { biodata_id: biodataId },
            dataType: 'json',
            success: function(res) {
                if (res.success) {
                    $input.val(res.nidn_or_nuptk || '');
                    $row.find('.row-nip').val(res.userid || '');
                    $row.find('.row-nidn').val(res.nidn || '');
                } else {
                    alert(res.message || 'Data tidak ditemukan di i-sdm.');
                }
            },
            error: function(xhr) {
                var msg = (xhr.responseJSON && xhr.responseJSON.message) ? xhr.responseJSON.message : 'Gagal mengambil data.';
                alert(msg);
            },
            complete: function() {
                $btn.prop('disabled', false).text('Sync');
            }
        });
    });
});
</script>
@endsection
