<?php
    if(isset($_GET['edit'])){
        $edit = intval($_GET['edit']);
        $u = $db->get_rowData('additional_expense','id',$edit);
        $general->arrayContentShow($u);
        if(empty($u)){$general->redirect($pUrl,[37,$rModule['title']]);}
        if(isset($_POST['edit'])){
            $title= $_POST["title"]; 
            if(empty($title)){SetMessage(36,'Title');$error=1;}
                if(!isset($error)){ 
                $data = [
                    'title'        => $title
                ];
                $db->arrayUserInfoEdit($data);
                $where = ['id'=>$edit];
                $update = $db->update('additional_expense',$data,$where);  
                if($update){
                    $general->redirect($pUrl,30,$rModule['title']);
                }
                else{$error=fl();SetMessage(66);}
            }
        }
        
        $data = [$pUrl=>$rModule['title'],'javascript:void()'=>$u['title'],'1'=>'Edit'];
        $general->pageHeader('Edit '.$rModule['title'],$data);
    ?>
    <div class="row">
        <div class="col-lg-12">
            <div class="white-box border-box">
                <div class="row">
                    <div class="col-lg-12"><?php show_msg();?></div>
                    <div class="col-xs-12 col-sm-12 col-md-12">
                        <form method="post" action="">
                            <div class="col-xs-6 col-sm-4 col-md-4">
                                   <?php $general->inputBoxText('title','Title',$u['title'],'y','','');?>
                            </div>
                            <div class="clearfix visible-xs"></div>
                            <div class="col-xs-6 col-sm-4 col-md-4">
                                <div class="form-group m-b-0">
                                    <div class="pull-right m-t-5">
                                        <input type="submit" name="edit" value="Update" class="btn btn-info waves-effect waves-light">
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <?php
    }
    else{
        if(isset($_POST['add'])){
            $title= $_POST['title'];
            if(empty($title)){setMessage(36,'Title');$error=1;}
            
            if(!isset($error)){
                $data = [
                    'title'    => $title
                ];
                $db->arrayUserInfoAdd($data);
                $insert = $db->insert('additional_expense',$data);
                if($insert){$general->redirect($pUrl,29,$rModule['title']);}
                else{
                    $error=fl();SetMessage(66);
                }
            }
        }
        $data = [$pUrl=>$rModule['title']];
        $general->pageHeader($rModule['title'],$data);
    ?>
    <div class="row">
        <div class="col-lg-12">
            <div class="white-box border-box">
                <div class="row">
                    <div class="col-lg-12"><?php show_msg();?></div>
                    <div class="col-xs-12 col-sm-12 col-md-12">
                        <form method="post" action="">
                            <div class="col-xs-6 col-sm-4 col-md-4">
                                <?php $general->inputBoxText('title','Title',@$_POST['title'],'y','','');?>  
                                <div class="form-group m-b-0">
                                    <div class="pull-right m-t-5">
                                        <input type="submit" name="add" value="Add" class="btn btn-info waves-effect waves-light">
                                    </div>
                                </div>
                            </div>
                            <div class="col-xs-6 col-sm-4 col-md-4">
                            </div>
                            <div class="clearfix visible-xs"></div>
                            <div class="col-xs-6 col-sm-4 col-md-4">
                                
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <?php
    }
?>
<div class="row">
    <div class="col-sm-12">
        <div class="white-box">
            <?php
                show_msg();
               
                    $units=$db->selectAll('additional_expense','order by title asc');  
            ?>
            <div class="col-md-5">
            <table class="table table-striped table-bordered table-hover">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Title</th>
                        <th>Edit</th>
                        <th>Status</th>
                    </tr>
                </thead> 
                <tbody>
                    <?php
                        $total=1;;
                        foreach($units as $u){
                        ?>
                        <tr>
                            <td><?=$total++?></td>
                            <td><?=$u['title']?></td>
                            <td><a href="<?=$pUrl?>&edit=<?=$u['id']?>" class="btn btn-info">Edit</a>
                            </td>
                            <td><?php $general->onclickChangeBTN($u['id'],$general->checked($u['isActive']));?></td>
                        </tr>
                        <?php
                        }
                    ?>
                </tbody>
            </table>
            </div>
        </div>
    </div>
</div>