<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
 * 重写laravel-admin Grid 类
+-----------------------------------------------------------------------------------------------------------------------
 *
 * PHP version 7
 *
 * @category  App\Admin\Rewrite
 * @package   App\Admin\Rewrite
 * @author    Richer <yangzi1028@163.com>
 * @date      2020年11月12日13:49:31
 * @copyright 2020-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
namespace App\Admin\Rewrite;

use App\Admin\Rewrite\Grid\Tools\CreateButton;
use App\Admin\Rewrite\Grid\Tools\ImportButton;
use Closure;
use App\Admin\Rewrite\Grid\Concerns;
use App\Admin\Rewrite\Grid\Tools\TrashedButton;
use Encore\Admin\Grid as EncoreGrid;
use App\Admin\Rewrite\Grid\Model;
use Illuminate\Database\Eloquent\Model as Eloquent;

/**
 * Class Grid
 *
 * @category  App\Admin\Rewrite
 * @package   App\Admin\Rewrite
 * @author    Richer <yangzi1028@163.com>
 * @date      2020年11月12日13:49:31
 * @copyright 2020-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
class Grid extends EncoreGrid
{
    use Concerns\HasFilter,
        Concerns\HasActions,
        Concerns\HasTools;

    /**
     * Options for grid.
     *
     * @var array
     */
    protected $options = [
        'show_pagination'        => true,
        'show_tools'             => true,
        'show_filter'            => true,
        'show_exporter'          => true,
        'show_actions'           => true,
        'show_row_selector'      => true,
        'show_create_btn'        => true,
        'show_column_selector'   => true,
        'show_define_empty_page' => true,
        'show_perpage_selector'  => true,
        'show_trashed_btn'  => true,
        'show_import_btn'  => true,
    ];

    /**
     * Create a new grid instance.
     *
     * @param Eloquent $model
     * @param Closure|null $builder
     */
    public function __construct(Eloquent $model, Closure $builder = null)
    {
        $this->model = new Model($model, $this);
        $this->keyName = $model->getKeyName();
        $this->builder = $builder;

        $this->initialize();

        $this->callInitCallbacks();
    }

    /**
     * Render create button for grid.
     *
     * @return string
     */
    public function renderCreateButton()
    {
        return (new CreateButton($this))->render();
    }

    /**
     * Alias for method `disableTrashedButton`.
     *
     * @return \Encore\Admin\Grid
     *
     * @deprecated
     */
    public function disableTrashed()
    {
        return $this->disableTrashedButton();
    }

    /**
     * Remove create button on grid.
     *
     * @return $this
     */
    public function disableTrashedButton(bool $disable = true)
    {
        return $this->option('show_trashed_btn', !$disable);
    }

    /**
     * If allow creation.
     *
     * @return bool
     */
    public function showTrashedBtn()
    {
        return $this->option('show_trashed_btn');
    }

    /**
     * Render create button for grid.
     *
     * @return string
     */
    public function renderTrashedButton()
    {
        return (new TrashedButton($this))->render();
    }

    /**
     * Get create url.
     *
     * @return string
     */
    public function getTrashedUrl()
    {
        $queryString = '';

        if ($constraints = $this->model()->getConstraints()) {
            $queryString = http_build_query($constraints);
        }

        return sprintf(
            '%s?trashed=1%s',
            $this->resource(),
            $queryString ? ('?'.$queryString) : ''
        );
    }

    /**
     * Alias for method `disableTrashedButton`.
     *
     * @return \Encore\Admin\Grid
     *
     * @deprecated
     */
    public function disableImport()
    {
        return $this->disableImportButton();
    }

    /**
     * Remove create button on grid.
     *
     * @param bool $disable
     * @return $this
     */
    public function disableImportButton(bool $disable = true)
    {
        return $this->option('show_import_btn', !$disable);
    }

    /**
     * If allow Import.
     *
     * @return bool
     */
    public function showImportBtn()
    {
        return $this->option('show_import_btn');
    }

    /**
     * Render create button for grid.
     *
     * @return string
     */
    public function renderImportButton()
    {
        return (new ImportButton($this))->render();
    }

    /**
     * Get create url.
     *
     * @return string
     */
    public function getImportUrl()
    {
        $queryString = '';

        if ($constraints = $this->model()->getConstraints()) {
            $queryString = http_build_query($constraints);
        }

        return sprintf(
            '%s/import%s',
            $this->resource(),
            $queryString ? ('?'.$queryString) : ''
        );

        return sprintf(
            '%s?=1%s',
            $this->resource(),
            $queryString ? ('?'.$queryString) : ''
        );
    }

    /**
     * Get create url.
     *
     * @return string
     */
    public function getCommonUrl()
    {
        $queryString = '';

        if ($constraints = $this->model()->getConstraints()) {
            $queryString = http_build_query($constraints);
        }

        return sprintf(
            '%s%s',
            $this->resource(),
            $queryString ? ('?'.$queryString) : ''
        );
    }
}