<?php  
    $id = intval($_GET['id']);
    $vl = $db->get_rowData('doctor_visit','id',$id);
    if(empty($vl)){$error=fl();setMessage(63,'doctor visit');}
    elseif($vl['createdBy']!=USER_ID){$error=fl();setMessage(63,'doctor visit');}
    else{
        $doctor  = $db->get_rowData('doctor','id',$vl['doctor_id']);
        $finished_product = $db->selectAll('doctor_visit_finished_product','where doctor_visit_id='.$id);
        $product_ids=[];
        if(!empty($finished_product)){
            foreach($finished_product as $fp){
                $product_ids[$fp['product_id']]=$fp['product_id'];
            }
        }
        $gift_product = $db->selectAll('doctor_visit_gift_product','where doctor_visit_id='.$id);
        if(!empty($gift_product)){
            foreach($gift_product as $gp){
                $product_ids[$gp['product_id']]=$gp['product_id'];
            }
        }
        $products=[];
        $units=[];
        if(!empty($product_ids)){
            $products = $db->selectAll('products','where id in('.implode(',',$product_ids).')','id,title,code,unit_id');
            $general->arrayIndexChange($products);
            $unit_ids=[];
            foreach($products as $p){
                $unit_ids[$p['unit_id']] = $p['unit_id'];
            }
            $units = $db->selectAll('unit','where id in('.implode(',',$unit_ids).')');
            $general->arrayIndexChange($units);
        }
        $vl['entry_time'] = $general->make_date($vl['entry_time'],'time');
        $vl['note'] = $general->content_show($vl['note']);
    }
    if(isset($error)){$general->redirect(APP_URL.'/?mdl=doctor-visit-list');}
    
?>

<section class="mb-4" id="sale_area">
    <div class="card">
        <div class="card-header text-center py-3"> 
            <h5 class="mb-0 text-center">
                <strong>Doctor visit Details</strong>
            </h5>
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-12" id="reportArea">
                    <div id="details_top">
                        <table class="table table-borderd">
                            <tr><td>Entry date time</td><td><?=$vl['entry_time']?></td></tr>
                            <tr><td>Dctor</td><td><?=$doctor['name']?></td></tr>
                            <tr><td>Prescription</td><td><?=$vl['prescription']?></td></tr>
                            <tr><td>Note</td><td><?=$vl['note']?></td></tr>
                        </table>
                    </div>
                    <h4>Finished product</h4>
                    <table class="table table-borderd">
                        <thead>
                            <tr>
                                <td>#</td>
                                <td>Product</td>
                                <td>Unit</td>

                                <td>Qty</td>
                            </tr>
                        </thead>
                        <tbody class="details_products">
                            <?php
                                $sr=1;
                                foreach($finished_product as $po){
                                    $p = $products[$po['product_id']];
                                ?>
                                <tr>
                                    <td><?=$sr++?></td>
                                    <td><?=$p['code'].' - '.$p['title']?></td>
                                    <td><?=$units[$p['unit_id']]['title']?></td>
                                    <td><?=$po['quaintity']?></td>
                                </tr>

                                <?php 
                                }
                            ?>
                        </tbody>

                    </table>
                    <h4>Gift product</h4>
                    <table class="table table-borderd">
                        <thead>
                            <tr>
                                <td>#</td>
                                <td>Product</td>
                                <td>Unit</td>

                                <td>Qty</td>
                            </tr>
                        </thead>
                        <tbody class="details_products">
                            <?php
                                $sr=1;
                                foreach($gift_product as $po){
                                    $p = $products[$po['product_id']];
                                ?>
                                <tr>
                                    <td><?=$sr++?></td>
                                    <td><?=$p['code'].' - '.$p['title']?></td>
                                    <td><?=$units[$p['unit_id']]['title']?></td>
                                    <td><?=$po['quaintity']?></td>
                                </tr>

                                <?php 
                                }
                            ?>
                        </tbody>

                    </table>
                </div>
            </div>
        </div>
    </div>
</section>
