<?php
if ( ! class_exists ( 'WP_List_Table' ) ) {
    require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
}
include dirname(__FILE__).'/../../jdf.php';
/**
 * List table class - نیکی ساز
 */
class class_webapp_install_apps_table extends \WP_List_Table {
    public $page_status = '2'; // افزودن property برای رفع اخطار
    public $counts = [];
    function __construct() {
        check_finished_apps();
        parent::__construct( array(
            'singular' => 'نیکی ساز',
            'plural'   => 'اپلیکیشن‌های نصب شده',
            'ajax'     => false
        ) );
    }
    function get_table_classes() {
        return array( 'widefat', 'fixed', 'striped roles-table', $this->_args['plural'] );
    }
    /**
     * Message to show if no designation found
     *
     * @return void
     */
    function no_items() {
        _e( 'هیچ اپلیکیشن نصب شده‌ای یافت نشد', 'niki-saz' );
    }
    /**
     * Default column values if no callback found
     *
     * @param  object  $item
     * @param  string  $column_name
     *
     * @return string
     */
    function column_default( $item, $column_name ) {
        switch ( $column_name ) {
            case 'app_name':
                return trim($item->app_name!='') ? $item->app_name:'-';
            case 'count_app':
                return trim($item->count_app!='') ? $item->count_app:'-';
            case 'app_version':
                return trim($item->app_version!='') ? $item->app_version:'-';
            case 'app_package_name':
                return trim($item->app_package_name!='') ? $item->app_package_name:'-';
            case 'app_icon':
                return trim($item->app_icon!='') ? "<img width=30 height=30 src='".$item->app_icon."'>":'-';
            default:
                return isset( $item->app_url ) ? $item->app_url : '';
        }
    }
    /**
     * Get the column names
     *
     * @return array
     */
    function get_columns() {
        $columns = array(
            'app_icon'      => __( 'آیکون اپلیکیشن', 'niki-saz' ),
            'app_name'      => __( 'نام اپلیکیشن', 'niki-saz' ),
            'count_app'      => __( 'تعداد نصب', 'niki-saz' ),
            'app_version'      => __( 'نسخه اپلیکیشن', 'niki-saz' ),
            'app_package_name'      => __( 'نام پکیج', 'niki-saz' ),
        );
        return $columns;
    }
    /**
     * Render the designation name column
     *
     * @param  object  $item
     *
     * @return string
     */
    function column_app_names( $item ) {
        $actions           = array();
        $actions['delete'] = sprintf( '<a href="admin.php?page=hd-lgs-create-roles&action=delete&id=%d" class="delete-role"  title="%s">%s</a>', $item->id, __( 'حذف', 'niki-saz' ), __( 'حذف', 'niki-saz' ) );
        return sprintf( '<a href="%1$s"><strong>%2$s</strong></a> %3$s', admin_url( 'admin.php?page=hd-lgs-create-roles&action=view&id=' . $item->id ),  trim($item->users!='') ? hd_show_array($item->users,'user'):'-', $this->row_actions( $actions ) );
    }
    /**
     * Get sortable columns
     *
     * @return array
     */
    function get_sortable_columns() {
        $sortable_columns = array(
            'app_name' => array( 'app_name', true ),
            'app_package_name' => array( 'app_package_name', true ),
            'app_version' => array( 'app_version', true ),
            'count_app' => array( 'count_app', true ),
            'app_icon' => array( 'app_icon', true ),
        );
        return $sortable_columns;
    }
    /**
     * Set the bulk actions
     *
     * @return array
     */
    /*    function get_bulk_actions() {
            $actions = array(
                'trash'  => __( 'Send To Trash', 'niki-saz' ),
            );
            return $actions;
        }*/
    /**
     * Render the checkbox column
     *
     * @param  object  $item
     *
     * @return string
     */
    function column_cb( $item ) {
        return sprintf(
            '<input type="checkbox" name="hotel_id[]" value="%d" />', $item->id
        );
    }
    /**
     * Set the views
     *
     * @return array
     */
    public function get_views_() {
        $status_links   = array();
        $base_link      = admin_url( 'admin.php?page=hd-awg-list' );
        foreach ($this->counts as $key => $value) {
            $class = ( $key == $this->page_status ) ? 'current' : 'status-' . $key;
            $status_links[ $key ] = sprintf( '<a href="%s" class="%s">%s <span class="count">(%s)</span></a>', add_query_arg( array( 'status' => $key ), $base_link ), $class, $value['label'], $value['count'] );
        }
        return $status_links;
    }
    /**
     * Prepare the class items
     *
     * @return void
     */
    function prepare_items() {
        $columns               = $this->get_columns();
        $hidden                = array( );
        $sortable              = $this->get_sortable_columns();
        $this->_column_headers = array( $columns, hidden, $sortable );
        $per_page              = 20;
        $current_page          = $this->get_pagenum();
        $offset                = ( $current_page -1 ) * $per_page;
        $this->page_status     = isset( $_GET['status'] ) ? sanitize_text_field( $_GET['status'] ) : '2';
        // only ncessary because we have sample data
        $args = array(
            'offset' => $offset,
            'number' => $per_page,
        );
        if ( isset( $_REQUEST['orderby'] ) && isset( $_REQUEST['order'] ) ) {
            $args['orderby'] = $_REQUEST['orderby'];
            $args['order']   = $_REQUEST['order'] ;
        }
        if(isset($_REQUEST["s"]))
            $this->items  = tu_get_installed( $args,$_REQUEST["s"] );
        else
            $this->items  = tu_get_installed( $args , null);
        $this->set_pagination_args( array(
            'total_items' => isset($_REQUEST["s"])? tu_get_installed_count($_REQUEST["s"]):tu_get_installed_count(null),
            'per_page'    => $per_page
        ) );
    }
}