<?php

include_once 'rb.php';
//R::setup( 'mysql:host=localhost;dbname=survey','root', '' );
R::setup( 'mysql:host=localhost;dbname=genphar_pharma','genphar_pharma', 'pharma2018' );

function regions(){
    $regions = R::findAll('regions');
    foreach ($regions as $r)
        echo "<option id='$r->id'>$r->name</option>";
}

function selectedProvinceOfPharmacy($idPharmacy){

    $row = R::getAll("select province_id from pharmacies where id=$idPharmacy");
    $pId = $row[0]['province_id'];

    $row = R::getAll("select region_id from provinces where id=$pId");
    if(count($row)!=0) {
        $rId = $row[0]['region_id'];
        $provinces = R::find('provinces', 'region_id=?', [$rId]);
        foreach ($provinces as $p) {
            if ($pId == $p->id)
                echo "<option selected id='$p->id'>$p->name</option>";
            else
                echo "<option id='$p->id'>$p->name</option>";
        }
    }
}

function regionOfPharmacy($idPharmacy){
    $row = R::getAll("select province_id from pharmacies where id=$idPharmacy");
    $pId = $row[0]['province_id'];
    $row = R::getAll("select region_id from provinces where id=$pId");
    if(count($row)!=0)
        $rId = $row[0]['region_id'];
    else
        $rId=null;
    $regions = R::findAll('regions');
    foreach ($regions as $r){
        if($rId==$r->id)
            echo "<option selected id='$r->id'>$r->name</option>";
        else
            echo "<option id='$r->id'>$r->name</option>";
    }
}

function provinces(){
    $provinces = R::findAll('provinces');
    foreach ($provinces as $p)
        echo "<option id='$p->id'>$p->name</option>";
}

function pharmacies(){
    $pharmacies = R::findAll('pharmacies');
    foreach ($pharmacies as $p)
        echo "<option id='$p->id'>$p->name</option>";
}

function cities(){
    $cities = R::getAll('select distinct city from pharmacies where city !="" order by city');
    foreach ($cities as $c) {
        $x = $c['city'];
        echo "<option id='$x'>$x</option>";
    }
}

function questionRadio($id){

    $question = R::load('questions',$id);
    echo "<row class='col-xs-12' style='margin-bottom: 20px;'><label class=' text-green' id='q.$question->id'>$question->text</label><br>";
    $answers  = R::find('answers', 'question_id=?', [$question->id]);
    foreach ($answers as $a)
        echo "<div class='col-md-4 col-sm-4'>
                 <label class='containers'>$a->text
                    <input type='radio' name='q$question->id' value='$a->id'>
                    <span class=\"checkmarkradio\"></span>
                    </label>
              </div>";
    echo "</row>";
}

function questionCheck($id){

    $question = R::load('questions',$id);
    echo "<row class='col-xs-12' style='margin-bottom: 20px;'><label class=' text-green' id='q.$question->id'>$question->text</label><br>";
    $answers  = R::find('answers', 'question_id=?', [$question->id]);
    foreach ($answers as $a)
        echo "<div class='col-md-4 col-sm-4'>
                <label class='containers'>$a->text
                    <input type='checkbox' name='q$question->id[]' value='$a->id'> 
                    <span class=\"checkmark\"></span>
                </label>
              </div>";
        echo "</row>";


}

function questionText($id){

    $question = R::load('questions',$id);
    echo "<row class='col-xs-12' style='margin-bottom: 20px;'><label class=' text-green' id='q.$question->id'>$question->text</label><br>";
    echo "<div class='col-md-4 col-sm-4'><input type='text' name='q$question->id' value='0' q$question->id'> DHs</div>";
    echo "</row>";
}

function getUser($login,$pass){
    $user = R::getAll("select * from users where login='$login' and pass='$pass'");
    return count($user);
}
function getUserId($login)
{
    $result = R::getAll("select id from users where login='$login'");
    foreach ($result as $u) {
        return $u['id'];
    }
}

function getUserAdmin($login)
{
    $result = R::getAll("select admin from users where login='$login'");
    foreach ($result as $u) {
        return $u['admin'];
    }
}

//echo getUserId('VP.RABAT');

?>