<?php $__env->startSection('styles'); ?>
<style>
    .no-caret::after { display: none !important; }
    .dropdown-item { font-size: 14px; padding: 8px 15px; cursor: pointer; }
    .dropdown-item i { width: 20px; text-align: center; }
    .status-badge { font-size: 0.85rem; padding: 5px 10px; }
</style>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('content'); ?>
<div class="mb-4 d-flex justify-content-between align-items-center">
    <div>
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb bg-transparent p-0 mb-2">
                <li class="breadcrumb-item"><a href="<?php echo e(route('documents.index')); ?>">সব প্রতিষ্ঠান</a></li>
                <li class="breadcrumb-item"><a href="<?php echo e(route('documents.company-folders', $company)); ?>"><?php echo e($company->name); ?></a></li>
                <li class="breadcrumb-item"><a href="<?php echo e(route('documents.financial-years', [$company, $documentType])); ?>"><?php echo e($documentType->name); ?></a></li>
                <?php if(isset($financialYear)): ?>
                    <li class="breadcrumb-item active"><?php echo e($financialYear->year_name); ?></li>
                <?php else: ?>
                    <li class="breadcrumb-item active">সাধারণ ডকুমেন্ট</li>
                <?php endif; ?>
            </ol>
        </nav>
        <h3 class="font-weight-bold">
            <i class="fas <?php echo e($documentType->icon ?? 'fa-file-alt'); ?> text-primary"></i> 
            <?php echo e($documentType->name); ?> 
            <?php if(isset($financialYear)): ?> - <?php echo e($financialYear->year_name); ?> <?php endif; ?>
        </h3>
    </div>
    <button type="button" class="btn btn-primary shadow-sm" data-toggle="modal" data-target="#uploadModal">
        <i class="fas fa-plus"></i> নতুন আপলোড
    </button>
</div>

<div class="card border-0 shadow-sm">
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-hover table-bordered" id="documentsTable">
                <thead class="bg-light">
                    <tr>
                        <th width="5%">ক্রমিক</th>
                        <th>ফাইলের নাম</th>
                        <th>ডকুমেন্ট নম্বর</th>
                        <th>মেয়াদ শুরু</th>
                        <th>মেয়াদ শেষ</th>
                        <th>বাকি দিন</th>
                        <th>স্ট্যাটাস</th>
                        <th width="12%" class="text-center">অ্যাকশন</th>
                    </tr>
                </thead>
                <tbody>
                    <?php $__currentLoopData = $documents; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $index => $doc): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                    <tr>
                        <td class="text-center"><?php echo e($index + 1); ?></td>
                        <td>
                            <?php 
                                $extension = strtolower(pathinfo($doc->file_path, PATHINFO_EXTENSION));
                                $isPdf = ($extension == 'pdf');
                            ?>
                            <i class="fas <?php echo e($isPdf ? 'fa-file-pdf text-danger' : 'fa-file-image text-primary'); ?> mr-2"></i>
                            <span class="font-weight-bold"><?php echo e($doc->title ?? 'বিনা নামের ফাইল'); ?></span>
                        </td>
                        <td><?php echo e($doc->document_number ?? '-'); ?></td>
                        <td><?php echo e($doc->issue_date ? \Carbon\Carbon::parse($doc->issue_date)->format('d/m/Y') : '-'); ?></td>
                        <td><?php echo e($doc->expiry_date ? \Carbon\Carbon::parse($doc->expiry_date)->format('d/m/Y') : '-'); ?></td>
                        <td>
                            <?php if($doc->is_expired): ?>
                                <span class="text-danger font-weight-bold small">মেয়াদ উত্তীর্ণ</span>
                            <?php else: ?>
                                <span class="small"><?php echo e($doc->days_left); ?> দিন বাকি</span>
                            <?php endif; ?>
                        </td>
                        <td class="text-center">
                            <span class="badge badge-<?php echo e($doc->validity_badge_class); ?> status-badge">
                                <?php echo e($doc->is_expired ? 'মেয়াদ উত্তীর্ণ' : ($doc->days_left <= 30 ? 'মেয়াদ শেষ হতে চলেছে' : 'বৈধ')); ?>

                            </span>
                        </td>
                        <td>
                            <div class="d-flex align-items-center justify-content-center">
                                <a href="<?php echo e(route('documents.download', $doc->id)); ?>" class="btn btn-sm btn-info mr-2" title="ডাউনলোড">
                                    <i class="fas fa-download"></i>
                                </a>

                                <div class="dropdown">
                                    <button class="btn btn-sm btn-light border dropdown-toggle no-caret" type="button" data-toggle="dropdown">
                                        <i class="fas fa-ellipsis-v text-muted"></i>
                                    </button>
                                    <div class="dropdown-menu dropdown-menu-right shadow-lg border-0">
                                        
                                        <?php $fileUrl = asset('storage/' . $doc->file_path); ?>
                                        <a class="dropdown-item text-primary" href="<?php echo e($fileUrl); ?>" target="_blank">
                                            <i class="fas fa-eye mr-2"></i> ফাইল প্রিভিউ
                                        </a>
                                        
                                        
                                        <button type="button" class="dropdown-item text-warning" data-toggle="modal" data-target="#editModal<?php echo e($doc->id); ?>">
                                            <i class="fas fa-edit mr-2"></i> এডিট করুন
                                        </button>
                                        
                                        <div class="dropdown-divider"></div>
                                        
                                        <button class="dropdown-item text-danger" onclick="deleteDocument(<?php echo e($doc->id); ?>)">
                                            <i class="fas fa-trash-alt mr-2"></i> ডিলিট করুন
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </td>
                    </tr>

                    
                    <div class="modal fade" id="editModal<?php echo e($doc->id); ?>" tabindex="-1" role="dialog" aria-hidden="true">
                        <div class="modal-dialog modal-lg" role="document">
                            <div class="modal-content">
                                <div class="modal-header bg-info text-white">
                                    <h5 class="modal-title"><i class="fas fa-edit"></i> ডকুমেন্ট তথ্য পরিবর্তন</h5>
                                    <button type="button" class="close text-white" data-dismiss="modal"><span>&times;</span></button>
                                </div>
                                <form action="<?php echo e(route('documents.update', $doc->id)); ?>" method="POST">
                                    <?php echo csrf_field(); ?>
                                    <?php echo method_field('PUT'); ?>
                                    <div class="modal-body text-left">
                                        <div class="form-group mb-4">
                                            <div class="custom-control custom-checkbox">
                                                <input type="checkbox" class="custom-control-input edit-gen-check" 
                                                       id="edit_is_general_<?php echo e($doc->id); ?>" name="is_general_doc" 
                                                       data-id="<?php echo e($doc->id); ?>" <?php echo e(is_null($doc->financial_year_id) ? 'checked' : ''); ?>>
                                                <label class="custom-control-label font-weight-bold text-primary" for="edit_is_general_<?php echo e($doc->id); ?>">
                                                    এটি কি সাধারণ ডকুমেন্ট?
                                                </label>
                                            </div>
                                        </div>

                                        <div id="edit_expiry_wrapper_<?php echo e($doc->id); ?>">
                                            <div class="row">
                                                <div class="col-md-12 form-group">
                                                    <label>অর্থবছর</label>
                                                    <select name="financial_year_id" class="form-control">
                                                        <option value="">নির্বাচন করুন</option>
                                                        <?php $__currentLoopData = \App\Models\FinancialYear::all(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $fy): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                            <option value="<?php echo e($fy->id); ?>" <?php echo e($doc->financial_year_id == $fy->id ? 'selected' : ''); ?>><?php echo e($fy->year_name); ?></option>
                                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                                    </select>
                                                </div>
                                                <div class="col-md-6 form-group">
                                                    <label>ইস্যু তারিখ</label>
                                                    <input type="date" name="issue_date" class="form-control" value="<?php echo e($doc->issue_date); ?>">
                                                </div>
                                                <div class="col-md-6 form-group">
                                                    <label>মেয়াদ শেষ তারিখ</label>
                                                    <input type="date" name="expiry_date" class="form-control" value="<?php echo e($doc->expiry_date); ?>">
                                                </div>
                                            </div>
                                        </div>

                                        <div class="form-group">
                                            <label>ডকুমেন্ট নাম/টাইটেল</label>
                                            <input type="text" name="title" class="form-control" value="<?php echo e($doc->title); ?>" required>
                                        </div>
                                    </div>
                                    <div class="modal-footer">
                                        <button type="button" class="btn btn-secondary" data-dismiss="modal">বাতিল</button>
                                        <button type="submit" class="btn btn-info">আপডেট করুন</button>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php echo $__env->make('documents.partials.upload-modal', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>


<form id="deleteForm" method="POST" style="display: none;">
    <?php echo csrf_field(); ?>
    <?php echo method_field('DELETE'); ?>
</form>

<?php $__env->startPush('scripts'); ?>
<script>
    function deleteDocument(id) {
        if(confirm('আপনি কি নিশ্চিত যে এই ডকুমেন্টটি ডিলিট করতে চান?')) {
            let form = document.getElementById('deleteForm');
            form.action = '/documents/' + id; // আপনার রাউট অনুযায়ী পরিবর্তন করুন
            form.submit();
        }
    }

    $(document).ready(function() {
        // এডিট মডাল টগল
        function handleEditToggle(checkbox) {
            let id = checkbox.data('id');
            if(checkbox.is(':checked')) {
                $('#edit_expiry_wrapper_' + id).slideUp();
            } else {
                $('#edit_expiry_wrapper_' + id).slideDown();
            }
        }

        $('.edit-gen-check').each(function() { handleEditToggle($(this)); });
        $(document).on('change', '.edit-gen-check', function() { handleEditToggle($(this)); });
    });
</script>
<?php $__env->stopPush(); ?>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH /home/naowarin/mydoc.naowarinternational.my.id/resources/views/documents/documents-list.blade.php ENDPATH**/ ?>