<?php
include_once("../public/config.php");
$do = $_GET['do'];
$today = date("Y-m-d H:i:s");
$date = date('Y-m-d');
$userid = $_SESSION['userid'];

if ($do == 'getselectjoin') {
  getselectjoin();
}else if ($do == 'gettabledata') {
  gettabledata();
}else if ($do == 'checkcolumn') {
  checkcolumn();
}else if ($do == 'removeteble') {
  removeteble();
}else if ($do == 'getselectdata') {
  getselectdata();
}else if ($do == 'getselectdatam') {
  getselectdatam();
}


function getselectdatam() {
    $table = $_POST['table'];
    $tableid = $_POST['tableid'];
    $tableval = $_POST['tableval'];
    $join = $_POST['join'];
    $tablesearch = $_POST['search'];
    $tableid = $table.".".$tableid;
    $getdata = R::getAll("SELECT $tableid as id, CONCAT($tablesearch) as text FROM $table $join WHERE $tableid in ($tableval)");
    echo json_encode($getdata);
}


function getselectjoin() {
    $searchterm = $_POST['searchTerms']; 
    $tablesearch = $_POST['tablesearch'];
    $table = $_POST['table'];
    $parentcol = $_POST['parentcol'];
    $parentid = $_POST['parentid'];
    $join = $_POST['join'];
    $filter = $_POST['filter'];
    $tableid = $_POST['tableid'];
    $search = '';

    if ($parentid) {$search .= " and $table.$parentcol =  $parentid "; }
    if ($filter) {$search .= $filter;}
    $tableid = $table.".".$tableid;

    $row_array = array();
    $return_arr = array();
    if ($table) {
        $allData = R::getAll("SELECT $tableid as id,CONCAT($tablesearch) as name FROM $table 
        $join WHERE  CONCAT($tablesearch) LIKE '%" . $searchterm . "%' $search limit 50");
        foreach ($allData as $data) {
            $row_array['id'] = $data['id'];
            $row_array['text'] = $data['name'];
            array_push($return_arr, $row_array);
        }
    }
    echo json_encode($return_arr);

}




function gettabledata() {
    $table = $_POST['table']; 
    $tableid = $_POST['tableid'];
    $tableval = $_POST['tableval'];
    $join = $_POST['join'];
    $tableid = $table.".".$tableid;
    $getdata = R::getRow("SELECT * FROM $table $join WHERE $tableid = ?",[$tableval]);
    echo json_encode($getdata);
}


function getselectdata() {
    $table = $_POST['table']; 
    $tableid = $_POST['tableid'];
    $tableval = $_POST['tableval'];
    $join = $_POST['join'];
    $tablesearch = $_POST['search'];
    $tableid = $table.".".$tableid;
    $getdata = R::getRow("SELECT *,CONCAT($tablesearch) as texts FROM $table $join WHERE $tableid = ?",[$tableval]);
    echo json_encode($getdata);
}

function checkcolumn() {
    $table = filter_input(INPUT_POST, 'table');
    $tablecolval = filter_input(INPUT_POST, 'tablecolval');
    $tableid = filter_input(INPUT_POST, 'tableid');
    $parentid = filter_input(INPUT_POST, 'parentid');
    $parentcol = filter_input(INPUT_POST, 'parentcol');
    $filter = filter_input(INPUT_POST, 'filter');

    if (!$tableid && !$parentid) {
    $checkinput = R::count($table, " $filter ", [$tablecolval]);
    }else if ($tableid && !$parentid) {
    $checkinput = R::count($table, " $filter ", [$tablecolval, $tableid]); 
    }else if (!$tableid && $parentid) {
    $checkinput = R::count($table, " $tablecol = ? and $parentcol = ? and $tabledelname < 2 ", [$tablecolval, $parentid]);
    }else if ($tableid && $parentid) {
    $checkinput = R::count($table, " $tablecol = ? and $parentcol = ? and $tableidname != ? and $tabledelname < 2", [$tablecolval, $parentid, $tableid]);
    }  
    if ($checkinput > 0) {
         echo 1;
    } else {
         echo 0;
    }
       
}


function removeteble() {
    $id = filter_input(INPUT_POST, 'id');
    $table = filter_input(INPUT_POST, 'table');
    $tableid = filter_input(INPUT_POST, 'tableid');
    try {
         R::exec("UPDATE $table SET `conditions`= 1 WHERE $tableid = $id");
        echo 1;
    } catch (Exception $e) {
        echo 0 ;
    }
}



?>