<?php
require_once __DIR__.'/../include/autoload.php';
require_once __DIR__.'/../include/Page.Class.php';
require_once __DIR__.'/../include/App.Class.php';

$title="GESTIONE TARGET";

$Page = new Page();
$Page->Title = $title;
$BCard = new BCard();
$Page->SetActivePage('/my-targets');


$param          = array_to_obj($param);
$id             = $param->id;



if($id){
$Page->Title = "DETTAGLI TARGET #{$id}";

if(request_ifset('target_name')) {
   $prep = new stdClass();
                
                if($id !== 'new') $prep->id = $id;
                    if(is_admin()){
                    
                        $prep->target_name                    = request_ifset('target_name');
                        $prep->target_type                  = request_ifset('target_type');
                        $prep->external_target                   = request_ifset('external_target');
                        $prep->user_id                   = $User->id;

                    } 
                  
                $values = PrepSQLValues($prep);
                $Save = $BCard->InsertOrUpdate($values, "targets");
       

if($Save) $msg = "Modifiche salvate correttamente.";
else $msg = "ERRORE GENERICO.";

$Body = <<<EOD
        <!-- Main content -->
        <section class="content">
	    <div class="row">
		<div class="card">
		  <div class="card-header" data-background-color="red">
		      <h4 class="title">Card</h4>
		  </div>
		  <div class="card-body">
			     $msg
			  </div><!-- /.box-body -->
			</div><!-- /.box -->
		</div>
	    </div>
	</section><!-- /.content -->
EOD;
} else {



    
    $TargetData = GetListWhere('targets', "user_id = '{$User->id}'", false);
    $TargetData = $TargetData[0];

    if(!$TargetData && $id !== 'new') die('NON PUOI ACCEDERE A QUESTO TARGET.');
    
    
    $targets  = $BCard->GetTargets($User->id, 'obj');
    $target_type_options  = map_values_to_options("targets","target_type",$TargetData->target_type);


  
    if(is_admin()){
   
    
    } else {
    
    
        $disabled_check = 'disabled';
    
    }
    
$Body = <<<EOD
<!-- Main content -->
        <section class="content">
        		<a href="{$Page->BASE_URL_HTTPS}/my-cards">← Vai ai miei target </a><br/><br/>

	    <div class="row">
	    
		<div class="col-md-6">
			<div class="card">
                <div class="card-header" data-background-color="red">
                    INSERISCI NUOVO TARGET
                </div>
                <div class="card-body">
                        <form method="POST" action="">
                        <div class="input-group">
                        
                            <div class="form-group col-md-4">
                                    <label>Nome Target</label>
                                    <input placeholder="" $disabled_check required type="text" class="form-control" name="target_name" value="{$TargetData->target_name}"/>

                            </div>
                            
                            <div class="form-group col-md-4">
                                    <label>Tipo Target</label>
                                    <select name="target_type" class="form-control " $disabled_check>
                                        $target_type_options
                                    </select>
                            </div>

                           

                        </div>
                </div><!-- /.box-body -->
            </div><!-- /.card -->
            <a target="_blank" href="{$Page->BASE_URL_HTTPS}/open/{$encoded_card}">Prova destinazione</a>
            
        </div>
       
        <div class="col-md-6">
        
	
         <button class="btn btn-primary btn-block" type="submit">Salva modifiche</button>
                {$delete_btn}
            </form>
			

    </div>
        
            
    </div>
    </section>
		
EOD;
}

$Page->AddToFooter = <<<EOD

EOD;

} else {


$Targets = GetListWhere("targets", "user_id = '{$User->id}'", false, 'id DESC');
foreach($Targets as $Target){
    
    $line_color = '';
    switch($Target->status){
        case 1:
        $line_color = 'background: #00800033';
        break;
        case 0:
        $line_color = 'background: #ff000036';
        break;
    }
    
    $Target->status_text                   = map_get_value("cards", 'status', $Card->status);
   
    $targets_list_table .= <<<EOD
    
    <tr style="{$line_color}">
        
        <td>#{$Target->id}
        <a target='_blank' href='{$BASE_URL}/my-targets/{$Target->id}/edit'><button type='button' class='btn btn-primary btn-xs'><i class='material-icons' style="color:#fff">edit</i></button></a>
        </td>
        <td>{$Target->target_name}</td>
        <td>{$Target->target_type}</td>       
    </tr>
    
EOD;

}

if(is_admin()){

$new_button = <<<EOD
   <a href="{$Page->BASE_URL_HTTPS}/admin/targets/new" class="btn btn-primary btn-lg"><i class="fa-regular fa-square-plus pr-2" aria-hidden="true"></i> Nuovo Target</a>
EOD;


}

$Body = <<<EOD

<div class="row">

<div class="col-md-12">
        <div class="card">
                <div class="card-header" data-background-color="purple">
                      ELENCO TARGET
                </div>
                
            <div class="card-body">
            
             
            $new_button
            
                <table class="table table-responsive" id="targets_table" >
                    <thead>
                        <tr>
                            <th>ID
                            <th>Nome Target
                            <th>Tipo
                        </tr>
                    </thead>
                        
                    $targets_list_table
                    
                </table>
            </div>
        </div>
    </div>
</div>

<style>
table tr td {
color:#000;
}
</style>
EOD;

}
$Page->AddToFooter = <<<EOD

<script type="text/javascript">
$(document).ready(function() {

    $('#targets_table').DataTable( {
        
         dom: 'Pflrtip',
         "pageLength": 50,
         "order": [[ 1, "desc" ]],
        buttons: [
            'copy', 'csv', 'excel', 'pdf', 'print'
        ]
    } );
} );


</script>

EOD;
echo $Page->Output($Body);

?>
