<?php if(isset($_REQUEST['FUNCTION']) || isset($_REQUEST['LOADER']) || isset($_REQUEST['FCN'])){require('../connection.php');}


if($_REQUEST['FCN']=="genNewLists"){
    $stmt = $GLOBALS['con']->prepare("SELECT * FROM `dd_plant` ORDER BY `code` ASC")or die(mysqli_error($GLOBALS['con']));
    $stmt->execute();$cc=$stmt->get_result();$stmt->close();	
    while($pd=mysqli_fetch_array($cc)){$ca[]=$pd;}
    
    $ret=array();
    $dat='';
    foreach($ca as $px){
        if($c==$px['code']){$s="SELECTED";}else{$s="";}
        $dat.='<option value="'.$px['code'].'" title="'.$px['description'].'" '.$s.'>'.$px['code'].'</option>';//.PHP_EOL;
    }
    $ret[]=$dat;
    $dat='';
    foreach($ca as $px){
        if($c==$px['code']){$s="SELECTED";}else{$s="";}
        $dat.='<option value="'.$px['description'].'" title="'.$px['code'].'" '.$s.'>'.$px['description'].'&nbsp;&nbsp;&nbsp;&nbsp;('.$px['frequency'].' months)</option>';//.PHP_EOL;
    }
    $ret[]=$dat;

    $return=json_encode($ret);
    echo $ret;
    exit();
}

if($_REQUEST['FUNCTION']=="save"){
    if($_REQUEST['mode']=="payee"){
    $_REQUEST['name']=str_replace("'",'&#39;',$_REQUEST['name']);
    $check=mysqli_query($GLOBALS['con'],"SELECT `id` FROM `dd_payee` WHERE `name`='".$_REQUEST['name']."'");
    $cnum=mysqli_num_rows($check);
    if($cnum==0){
        $_REQUEST['desc']=str_replace("'",'&#39;',$_REQUEST['desc']);
        $stmt = $GLOBALS['con']->prepare("INSERT INTO `dd_payee` VALUES (NULL,'".$_REQUEST['name']."','".$_REQUEST['type']."')")or die($e=mysqli_error($GLOBALS['con']));
        $stmt->execute();$rslt=$stmt->get_result();
        echo "s";
    }
    else{echo "e";}
    }

    if($_REQUEST['mode']=="expenses"){      
        $check=mysqli_query($GLOBALS['con'],"SELECT `id` FROM `dd_expenses` WHERE `code`='".$_REQUEST['code']."'");
        $cnum=mysqli_num_rows($check);
        if($cnum==0){
            $_REQUEST['phrase']=str_replace("'",'&#39;',$_REQUEST['desc']);
            $stmt = $GLOBALS['con']->prepare("INSERT INTO `dd_expenses` VALUES (NULL,'".$_REQUEST['code']."','".$_REQUEST['desc']."')")or die($e=mysqli_error($GLOBALS['con']));
            $stmt->execute();
            echo "s";
        }
        else{echo "e";}
        }

    exit();
}

if($_REQUEST['FUNCTION']=="remove"){
    $stmt = $GLOBALS['con']->prepare("DELETE FROM `dd_".$_REQUEST['table']."` WHERE `id`='".$_REQUEST['id']."'")or die($e=mysqli_error($GLOBALS['con']));
    $stmt->execute();
    echo "s".$e;
    exit();
}


if($_REQUEST['FUNCTION']=="update"){
    if($_REQUEST['mode']=="payee"){
    $_REQUEST['name']=str_replace("'",'&#39;',$_REQUEST['name']);
    $stmt = $GLOBALS['con']->prepare("UPDATE `dd_payee` SET 
    `name`='".$_REQUEST['name']."',
    `type`='".$_REQUEST['type']."'
    WHERE `id`='".$_REQUEST['id']."'
    ")or die($e=mysqli_error($GLOBALS['con']));
    $stmt->execute();$rslt=$stmt->get_result();

    }

    if($_REQUEST['mode']=="expenses"){
        $_REQUEST['phrase']=str_replace("'",'&#39;',$_REQUEST['desc']);
        $stmt = $GLOBALS['con']->prepare("UPDATE `dd_expenses` SET 
        `code`='".$_REQUEST['code']."',
        `desc`='".$_REQUEST['desc']."'
        WHERE `id`='".$_REQUEST['id']."'
        ")or die($e=mysqli_error($GLOBALS['con']));
        $stmt->execute();$rslt=$stmt->get_result();
    }

    echo "s";
    exit();
}

function getItems($type,$format,$sub){
    $stmt = $GLOBALS['con']->prepare("SELECT * FROM `dd_".$type."`")or die(mysqli_error("getItems: ".$GLOBALS['con']));
    $stmt->execute();$rslt=$stmt->get_result();
    switch ($type){
        default:break;

        case "expenses":
            if($format=="list"){?>
            <div class="col-md-12 pt-4">
            <table id="phraseTable" class="table table-striped table-hover responsive" style="width:100%">
            <thead>
            <tr><th width="10%">Code</th><th>Desc</th><th class="no-sort"></th></tr>
            </thead>
            <tbody>
            <?php while($d=mysqli_fetch_array($rslt)){?>

            <tr id="P<?php echo $d['id']?>">
            <td><span><?php echo $d['code']?></span></td>
            <td><span><?php echo $d['desc']?></span></td>

            <td class="text-right"><button class="btn btn-sm btn-secondary mr-3" onClick="edit('<?php echo $d['id']?>','<?php echo $type?>','p3','expenses')" type="button"><i class="fad fa-pencil" aria-hidden="true"></i></button><button class="btn btn-sm btn-outline-danger" type="button" onClick="remove('expenses','<?php echo $d['id']?>','p3','expenses')"><i class="fad fa-trash" aria-hidden="true"></i></button>
            </td>

            </tr>


            <?php }?>
            </tbody>
            </table>

            </div>
            <?php $table="phraseTable";
            require('../light_table.engine');?>
            <?php }

      
        break;

        case "payee":
            if($sub=="receipts"){?>
            <div class="col-md-12 pt-4">
                <table id="receiptsTable" class="table table-striped table-hover table-sm responsive" style="width:100%">
                    <thead>
                    <tr><th>Name</th><th>Type</th><th class="no-sort"></th></tr>
                    </thead>
                    <tbody>
                    <?php while($d=mysqli_fetch_array($rslt)){
                        switch ($d['type']){
                            case "in":
                                ?>
                                <tr id="R<?php echo $d['id']?>">
                                    <td><span><?php echo $d['name']?></span></td>
                                    <td><span>Receipts</span></td>
                                    <td class="text-right"><button class="btn btn-sm btn-secondary mr-3" onClick="edit('<?php echo $d['id']?>','<?php echo $type?>','p1','receipts')" type="button"><i class="fad fa-pencil" aria-hidden="true"></i></button><button class="btn btn-sm btn-outline-danger" type="button" onClick="remove('payee','<?php echo $d['id']?>','p1','receipts')"><i class="fad fa-trash" aria-hidden="true"></i></button>
                                    </td>
                                </tr>
                            <?php } ?>
                    <?php }?>
                    </tbody>
                </table>
            </div>
            <?php $table="receiptsTable";
            require('../light_table.engine');?>
            <?php }

            if($sub=="outgoings"){?>
            <div class="col-md-12 pt-4">
                <table id="outgoingsTable" class="table table-striped table-hover table-sm responsive" style="width:100%">
                    <thead>
                    <tr><th>Name</th><th>Type</th><th class="no-sort"></th></tr>
                    </thead>
                    <tbody>
                    <?php while($d=mysqli_fetch_array($rslt)){
                        switch ($d['type']){
                            case "out":
                                ?>
                                <tr id="R<?php echo $d['id']?>">
                                    <td><span><?php echo $d['name']?></span></td>
                                    <td><span>Outgoings</span></td>
                                    <td class="text-right"><button class="btn btn-sm btn-secondary mr-3" onClick="edit('<?php echo $d['id']?>','<?php echo $type?>','p2','outgoings')" type="button"><i class="fad fa-pencil" aria-hidden="true"></i></button><button class="btn btn-sm btn-outline-danger" type="button" onClick="remove('payee','<?php echo $d['id']?>','p2','outgoings')"><i class="fad fa-trash" aria-hidden="true"></i></button>
                                    </td>
                                </tr>
                            <?php } ?>
                    <?php }?>
                    </tbody>
                </table>
            </div>
            <?php $table="outgoingsTable";
            require('../light_table.engine');?>
            <?php }

        break;
        
        
    }	
    $stmt->free_result();$stmt->close();
}


if(isset($_REQUEST['LOADER'])){	
echo getItems($_REQUEST['type'],$_REQUEST['format'],$_REQUEST['sub']);
}


?>
