<?php
    $entry_date = $_POST['entry_date'];
    $houre = $_POST['houre'];
    $minuet = $_POST['minuet'];

    $entry_time = strtotime($entry_date.' '.$houre.':'.$minuet);
    $doctor_id = intval($_POST['doctor_id']);
    $prescription = intval($_POST['prescription']);

    $finished_product = $_POST['finished_product']??[];
    $gift_product = $_POST['gift_product']??[];
    $note = $_POST['note']??'';
    $doctor = $db->get_rowData('doctor','id',$doctor_id);
    if($entry_time==false){$error=fl(); setMessage(63,'entry time');}
    elseif($entry_time<strtotime('-2 years')){$error=fl();setMessage(63,'entry time');}
    elseif($doctor_id<1){$error=fl();setMessage(36,'doctor');}
    elseif($prescription<0){$error=fl();setMessage(36,'prescription');}
    elseif(empty($doctor)){$error=fl();setMessage(63,'doctor');}
    else{
        $product_ids=[];
        if(!empty($finished_product)){
            foreach($finished_product as $data){
                $product_id = $data['product_id'];
                $product_ids[$product_id] = $product_id;
            }
        }
        if(!empty($gift_product)){
            foreach($gift_product as $data){
                $product_id = $data['product_id'];
                $product_ids[$product_id] = $product_id;
            }
        }
        if(!empty($product_ids)){
            $products = $db->selectAll('products','where isActive=1 and id in('.implode(',',$product_ids).')','id,title,type');
            $general->arrayIndexChange($products);
        }
        else{
            $products=[];
        }
        
        if(!empty($finished_product)){
            foreach($finished_product as $data){
                $product_id = $data['product_id'];
                if(!isset($products[$product_id])){$error=fl(); setMessage(63,'product'); break;}
                $product =  $products[$product_id];
                if($product['type']!=PRODUCT_TYPE_FINISHED){$error=fl(); setMessage(63,'product');break;}
                if($data['quaintity']<1){$error=fl(); setMessage(63,'quaintity for '.$product['title']);break;}
            }
        }
        if(!isset($error) && !empty($gift_product)){
            foreach($gift_product as $data){
                $product_id = $data['product_id'];
                if(!isset($products[$product_id])){$error=fl(); setMessage(63,'product'); break;}
                $product =  $products[$product_id];
                if($product['type']!=PRODUCT_TYPE_GIFT_ITEM){$error=fl(); setMessage(63,'product');break;}
                if($data['quaintity']<1){$error=fl(); setMessage(63,'quaintity for '.$product['title']);break;}
            }
        }
        if(!isset($error)){
            $db->transactionStart();
            $data=[
                'entry_time'=>$entry_time,
                'doctor_id'=>$doctor_id,
                'prescription'=>$prescription,
                'note'=>$note,
            ];
            $db->arrayUserInfoAdd($data);
            $id = $db->insert('doctor_visit',$data,true);
            if($id==false){$error=fl(); setMessage(66);}
            else{
                $code = $db->setAutoCode('doctor_visit',$id);
                if(!$code){$error=fl();setMessage(66);}
                else{
                    if(!empty($finished_product)){
                        foreach($finished_product as $data){
                            $data['doctor_visit_id'] = $id;
                            $insert = $db->insert('doctor_visit_finished_product',$data);
                            if(!$insert){$error=fl(); setMessage(66);break;}
                        }
                    }
                    if(!isset($error) && !empty($gift_product)){
                        foreach($gift_product as $data){
                            $data['doctor_visit_id'] = $id;
                            $insert = $db->insert('doctor_visit_gift_product',$data);
                            if(!$insert){$error=fl(); setMessage(66);break;}
                        }
                    }
                }
            }
            $ac = false; 
            if(!isset($error)){
                $jArray['status'] = 1;
                $ac = true;
                setMessage(2,'Doctor visit successfully');
            }
            $db->transactionStop($ac);
        }

    }




