<?php
require_once __DIR__ . '/../init.php';
require_once PROJECT_ROOT . '/backend/controllers/common_function.php';
require_once PROJECT_ROOT . '/backend/controllers/db_select_helper.php';

// include other controller
require_once PROJECT_ROOT . '/backend/controllers/ActivityController.php';
require_once PROJECT_ROOT . '/backend/controllers/UserAksesController.php';
require_once PROJECT_ROOT . '/backend/controllers/AttachmentController.php';

class TeamController
{
    private $conn;

    private $table = "
        team TM
        LEFT JOIN tournament T ON TM.IDTournament = T.IDTournament
        LEFT JOIN tournament_category_level TCL ON TM.IDTCL = TCL.IDTCL
        LEFT JOIN tournament_registration TR ON TM.IDTeam = TR.IDTeam
        LEFT JOIN (
            SELECT IDTeam, COUNT(*) AS TotalMember
            FROM team_member
            GROUP BY IDTeam
        ) M ON TM.IDTeam = M.IDTeam
    ";

    private $pk = "IDTeam";
    private $sv = "Team";

    protected $displayedColumn = [
        'TM.IDTeam',
        'TM.IDTournament',
        'T.NamaTournament',
        'T.NamaTournamentEN',
        'TM.IDTCL',
        'TCL.IDKategori',
        'TCL.IDLevel',
        'TCL.MaxSlot',
        'TCL.RegistrationFee',
        'TM.TeamName',
        'TM.CreateAt',

        // total member
        'M.TotalMember',

        // registration info
        'TR.IDRegistration',
        'TR.PaymentStatus',
        'TR.PaymentProof',
        'TR.RegistrationDate',
        'TR.Status'
    ];

    public function __construct($conn)
    {
        $this->conn = $conn;
        $this->activityController = new ActivityController($this->conn);
        $this->attachmentController = new AttachmentController($this->conn);
        $this->userAksesController = new UserAksesController($this->conn);
    }

    public function select($post)
    {
        $listExclude = [];

        $listSearch = [
            'TM.TeamName',
            'T.NamaTournament',
            'T.NamaTournamentEN'
        ];

        return runSelect(
            $this->conn,
            $this->table,
            $this->pk,
            $this->displayedColumn,
            $post,
            $listExclude,
            $listSearch
        );
    }
}
?>
