<?php
if(isset($_GET['details'])){
    include(__DIR__."/draftDetails.php");   
}
else{

    $q[]=' isActive=1';
    $draftData=$db->selectAll('sale_draft','where '.implode(' and ',$q). " ORDER BY sDate ASC");
    $employees=$db->selectAll('employees',' order by eName asc');
    $general->arrayIndexChange($employees,'eID');
    $routs=$db->selectAll('route','where isActive=1');
    $general->arrayIndexChange($routs,'rtID');
    $users= $db->selectAll('system_user','','uID,uFullName');
    $general->arrayIndexChange($users,'uID');
    $rData=[];
    $serial=1;
    if(!empty($draftData)){
        foreach($draftData as $drD){
            $suppliersname=$smt->supplyerInfoByID($drD['supID']); 
            $sr=$employees[$drD['sSr']]['eName'];
            $dsr='';
            if(isset($employees[$drD['sDsr']])){
                $dsr=$employees[$drD['sDsr']]['eName'];
            }
            $rout=$routs[$drD['rtID']]['rtTitle'];
            $sData=$general->getJsonFromString($drD['sData']);
            $rData[]=[
                's' => $serial++,
                'i'=>$drD['sID'],
                //'sdt'=>$sData,
                'su'=> $suppliersname['supName'],
                'sr' => $sr,
                'dsr' => $dsr,
                'r'=> $rout,
                'd'=> date('d-m-Y',$drD['sDate']),
                'u'=>@$users[$drD['createdBy']]['uFullName'],
                'n'=>$drD['sNote'],  
            ];
        }


    }
//
//    $extraData=[
//        'module'=> 'dealerDraftList',
//    ];
//    $report=$general->getReportFromDealer($extraData);
//    if($report!=false){
//        $report=$general->getJsonFromString($report);
//        //$general->printArray($report);
//    }
?>
    <section class="mb-4">
        <div class="card">
            <div class="card-header text-center py-3">
                <h5 class="mb-0 text-center">
                    <strong>Dealer Draft list</strong>
                </h5>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    
                            <table class="table table-striped" id="reportTable">
                                <thead>
                                    <tr>
                                        <th>#</th>
                                        <th>Date</th>
                                        <th>Supplier</th>
                                        <th>SR</th>
                                        <th>DSR</th>
                                        <th>Route</th>
                                        <th>User</th>
                                        <th>Details</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $serial=1;
                                    foreach($rData as $s){
                                    ?>
                                        <tr>
                                            <td><?php echo $serial++?></td>
                                            <td><?php echo $s['d']?></td>
                                            <td><?php echo $s['su']?></td>
                                            <td><?php echo $s['sr']?></td>
                                            <td><?php echo $s['dsr']?></td>
                                            <td><?php echo $s['r']?></td>
                                            <td><?php echo $s['u']?></td>
                                            <td><a href="<?php echo $pUrl?>&details=<?php echo $s['i']?>">Details</a></td>
                                        </tr>
                                    <?php
                                    }
                                    ?>
                                </tbody>
                            </table>
                            <script>
                                $(document).ready(function () {
                                    $('#reportTable thead th').each(function () {
                                        var title = $(this).text();
                                        //$(this).html(title+'<br><input type="text" style="width:70px"/>');
                                    });
                                    var table = $('#reportTable').DataTable({
                                        /*"columnDefs": [
                                        {
                                        className: "amount_td",
                                        targets: [1,2]
                                        }
                                        ],*/
                                        paging: false,
                                        initComplete: function (){
                                            this.api()
                                            .columns()
                                            .every(function () {
                                                var that = this;
                                                $('input', this.header()).on('keyup change clear', function () {
                                                    if (that.search() !== this.value) {
                                                        that.search(this.value).draw();
                                                    }
                                                });
                                            });
                                        },
                                    });
                                });
                            </script>
                    
                </div>
            </div>
        </div>
    </section>
<?php
}
?>