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

$title="GESTIONE CARDS";

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


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



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

if(request_ifset('card_name')) {
   $prep = new stdClass();
                
                if($id !== 'new') $prep->id = $id;
                    if(is_admin()){
                    
                        $prep->card_name                    = request_ifset('card_name');
                        $prep->card_type                  = request_ifset('card_type');
                        $prep->target_id                   = request_ifset('target_id');
                        $prep->owner_id                   = $User->id;

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

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 {



    
    $CardData = GetListWhere('cards', "id = '{$id}'", false);
    $CardData = $CardData[0];

    if(!$CardData && $id !== 'new') die('NON PUOI ACCEDERE A QUESTA CARD.');
    
  

    
    $card_type_options  = map_values_to_options("cards","card_type",$CardData->card_type);
    $targets  = $BCard->GetTargets($User->id, 'obj');

    $targets_options = '';
    foreach($targets as $target){
        if($CardData->target_id == $target->id) $CardData->is_selected="selected";
        $targets_options .= "<option {$CardData->is_selected} value='{$target->id}'>{$target->target_name}</option>";
    }

    $encoded_card = $BCard->CreateCardToken($id);

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

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

                            </div>
                            
                            <div class="form-group col-md-4">
                                    <label>Tipo Card</label>
                                    <select name="card_type" class="form-control " $disabled_check>
                                        $card_type_options
                                    </select>
                            </div>

                            <div class="form-group col-md-12">
                                    <label>Destinazione Card</label>
                                    <select name="target_id" class="form-control" id="single-select-field" $disabled_check>
                                        $targets_options
                                    </select>
                            </div>

                        </div>
                </div><!-- /.box-body -->
            </div><!-- /.card -->
            <a target="_blank" href="{$Page->BASE_URL_HTTPS}/open/{$encoded_card}">Preview Destinazione Card</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
$(function() {
    "use strict";


    $( '#single-select-field' ).select2( {
        theme: "bootstrap-5",
        width: $( this ).data( 'width' ) ? $( this ).data( 'width' ) : $( this ).hasClass( 'w-100' ) ? '100%' : 'style',
        placeholder: $( this ).data( 'placeholder' ),
    } );

});
EOD;

} else {


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

}

if(is_admin()){

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


}

$Body = <<<EOD

<div class="row">

<div class="col-md-12">
        <div class="card">
                <div class="card-header" data-background-color="purple">
                      ELENCO CARDS
                </div>
                
            <div class="card-body">
            
             
            $new_button
            
                <table class="table table-responsive" id="cards_table" >
                    <thead>
                        <tr>
                            <th>ID
                            <th>Nome Carta
                            <th>Target
                        </tr>
                    </thead>
                        
                    $cards_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() {

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


</script>

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

?>
