<?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 == 'checkcolumn') {
    checkcolumn();
} else if ($do == 'getselectjoin') {
    getselectjoin();
} else if ($do == 'removeteble') {
    removeteble();
} else if ($do == 'getselect') {
    getselect();
} else if ($do == 'getselectparent') {
    getselectparent();
} else if ($do == 'getselectmultiple') {
    getselectmultiple();
} else if ($do == 'receivedata') {
    receivedata();
} else if ($do == 'getsetting') {
    getsetting();
} else if ($do == 'detailcharity') {

    $charityid = filter_input(INPUT_GET, 'charityid');
    $first_date = date('Y-m-d', strtotime('first day of this month'));

    $charitys = R::findOne('charitys', 'id = ? and del < 2 ', [$charityid]);
    $smarty->assign('charitys', $charitys);
    $charityabsences = R::findAll('charityabsences', "charityid = $charityid and todaysend >= '$first_date' and del < 2");
    $smarty->assign('charityabsences', $charityabsences);

    $charityspay = R::findOne('charityspays', 'charityid = ? and del < 2 order by id desc', [$charityid]);
    $smarty->assign('charityspay', $charityspay);


    $smarty->display("charityview/charityparcode.html");
}
function checkcolumn()
{
    $table = filter_input(INPUT_POST, 'table');
    $tablecol = filter_input(INPUT_POST, 'tablecol');
    $tablecolval = filter_input(INPUT_POST, 'tablecolval');
    $tableid = filter_input(INPUT_POST, 'tableid');
    $parentid = filter_input(INPUT_POST, 'parentid');
    $parentcol = filter_input(INPUT_POST, 'parentcol');

    if (!$tableid && !$parentid) {
        $checkinput = R::count($table, " $tablecol = ? and del < 2 ", [$tablecolval]);
    } else if ($tableid && !$parentid) {
        $checkinput = R::count($table, " $tablecol = ? and id != ? and del < 2 ", [$tablecolval, $tableid]);
    } else if (!$tableid && $parentid) {
        $checkinput = R::count($table, " $tablecol = ? and $parentcol = ? and del < 2 ", [$tablecolval, $parentid]);
    } else if ($tableid && $parentid) {
        $checkinput = R::count($table, " $tablecol = ? and $parentcol = ? and id != ? and del < 2", [$tablecolval, $parentid, $tableid]);
    }
    if ($checkinput > 0) {
        if ($tablecol == "idnumber" || $tablecol == "phone_no") {
            $ben = R::findOne($table, " $tablecol = ? and del < 2 ", [$tablecolval]);
            $charity = R::findOne('charities', 'id = ?', [$ben->charity_id]);
            echo $charity->charityname . ' - رقم التليفون: ' . $charity->charityphone;
        } else
            echo 1;
    } else {
        echo 0;
    }
}

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

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

    if ($filter) {
        $search .= $filter;
    }
    if(!$tableid ){
       $tableid  = $table.'id';
    }

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

function removeteble()
{
    $id = filter_input(INPUT_POST, 'id');
    $table = filter_input(INPUT_POST, 'table');
    $tables = R::load($table, $id);
    $tables->del = 2;
    $tables->deltoday = date("Y-m-d H:i:s");
    $tables->deluserid = $_SESSION['userid'];
    try {
        R::store($tables);
        echo 1;
    } catch (Exception $e) {
        echo 0;
    }
}

function getselect()
{
    $searchterm = $_POST['searchTerms'];
    $tablesearch = $_POST['tablesearch'];
    $table = $_POST['table'];
    $del = $_POST['del'];
    $filter = $_POST['filter'];
    $parentid = $_POST['parentid'];
    $parentcol = $_POST['parentcol'];

    $search = '';
    // if ($parentid) {
    $search .= " and $table.del < 2";
    // }
    if ($parentid) {
        $search .= " and $parentcol = $parentid ";
    }

    if ($filter) {
        $search .= $filter;
    }

    $row_array = array();
    $return_arr = array();

    $allData = R::getAll("SELECT $table.id,CONCAT($tablesearch) as name
        FROM $table 
        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 getselectmultiple()
{
    $searchterm = $_POST['searchTerms'];
    $tablesearch = $_POST['tablesearch'];
    $table = $_POST['table'];
    $del = $_POST['del'];
    $charitysubjectid = $_POST['charitysubjectid'];

    $row_array = array();
    $return_arr = array();
    $search = '';
    if ($del) {
        $search .= " and $table.del < 2";
    }
    $charityid = '0';
    $charitystosubjects = R::findAll('charityaddsubjectdetails', 'charitysubjectid = ? and del < 2', [$charitysubjectid]);
    foreach ($charitystosubjects as $data) {
        $charityid .= ',' . $data->charityid;
    }
    $search .= " and $table.id not in ($charityid) ";
    $allData = R::getAll("SELECT $table.id,CONCAT($tablesearch) as name
        FROM $table 
        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 getselectparent()
{
    $searchterm = $_POST['searchTerms'];
    $tablesearch = $_POST['tablesearch'];
    $table = $_POST['table'];
    $parentcol = $_POST['parentcol'];
    $parentid = $_POST['parentid'];
    $filter = $_POST['filter'];
    $search = '';

    $search .= " and $table.del < 2";
    if ($parentid) {
        $search .= " and $parentcol =  $parentid ";
    }

    if ($filter) {
        $search .= $filter;
    }

    $row_array = array();
    $return_arr = array();

    $allData = R::getAll("SELECT $table.id,CONCAT($tablesearch) as name
        FROM $table 
        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 getsetting()
{
    $getdata = R::getRow("SELECT * FROM programsettings");
    echo json_encode($getdata);
}
