<?php
$must_login            = true ;
$require_path          = '../../../' ;
$require_sub           = '../../' ;
require( $require_sub.'header.php' ) ;

if ( $boolean_login ){
    $status            = '201' ;

    $searchfilter      = $array['searchfilter'] ;
    $searchfilter      = ( $searchfilter != '' ? $searchfilter : 'current' ) ;

    $join_filter       = '' ;
    $join_query        = '' ;
    $query_sortable    = 'ORDER BY a.sortable ASC, a.created_at DESC' ;

    $search_query      = '' ;
    if ( $array['search'] != '' ){
        $search_query .= " AND b.title LIKE '%".$array['search']."%'" ;
    }
    if ( $array['category_id'] != '' ){
        $search_query .= " AND a.category_id = '".$array['category_id']."'" ;
    }

    if ( $searchfilter != '' ){
        switch ( $searchfilter ){
            case 'current' :
                $search_query  .= " AND a.branch LIKE '%/".$array['branch_id']."/%' AND a.status = 'active' AND ( ( a.association_type = 'all' ) OR ( a.association_type = 'date' AND a.date_start <= '".TODAYDAY."' AND a.date_end >= '".TODAYDAY."' ) )" ;
            break ; 
            case 'previous' :
                $join_filter    = ", c.view_id, c.status as association_status" ;
                $join_query     = "LEFT JOIN staff_association c ON ( a.association_id = c.association_id )" ;
                $search_query  .= " AND c.branch_id = '".$array['branch_id']."' AND c.staff_id = '".$staff_info['staff_id']."'" ;
                $query_sortable = "ORDER BY view_id DESC" ;
            break ; 
            case 'expired' :
                $search_query  .= " AND a.branch LIKE '%/".$array['branch_id']."/%' AND ( a.status = 'inactive' OR ( a.association_type = 'date' AND a.date_start <= '".TODAYDAY."' AND NOT ( a.date_end >= '".TODAYDAY."' ) ) )" ;
            break ; 
        }
    }

    $query             = "SELECT a.association_id, a.association_type, a.date_start, a.date_end, a.file, a.created_at, b.title ".$join_filter." FROM association a
                          LEFT JOIN association_translation b ON ( a.association_id = b.association_id )
                          ".$join_query."
                          WHERE a.deleted_at IS NULL AND b.lang = '".$array['lang']."' " . $search_query ;
    $mysqli_query      = $mysqli->query( $query . " ".$query_sortable." LIMIT " . getLimit( $current ) ) ;

    if ( $mysqli_query->num_rows > 0 ){

        $status        = '200' ;

        $list          = [] ;
        while ( $row   = $mysqli_query->fetch_assoc() ){
            $row['id']              = dataFilter( $row['association_id'] ) ;
            $row['title']           = dataFilter( $row['title'] ) ;
            $row['date_start']      = resetDateFormat( $row['date_start'] ) ;
            $row['date_end']        = resetDateFormat( $row['date_end'] ) ;
            $row['created_at']      = resetDateFormat( $row['created_at'] ) ;
            $row['association_status'] = ( checkExists( $row['association_status'] ) != '' ? $row['association_status'] : '' ) ;
            $row['file']            = ( $row['file'] != '' ? PATH.'uploads/Association/b/'.$row['file'] : '' ) ;
            $list[]                 = $row ;
        }

        $data['list']  = $list ;

    }
}

require( $require_sub.'footer.php' ) ;
?>