<?php

namespace app\controller\manager;

use app\controller\BaseController;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use think\response\Redirect;

/**
 * 控制器基础类
 */
class Base extends BaseController
{
    // excel 导出格式配置
    protected $excelStyle = [
        'font'      => [
            'name' => '宋体',
        ],
        'alignment' => [
            'horizontal' => Alignment::HORIZONTAL_CENTER,  // 水平居中
            'vertical'   => Alignment::VERTICAL_CENTER,  // 垂直居中
            'wrapText'   => true,
        ],
        'borders'   => [
            'allBorders' => [
                'borderStyle' => Border::BORDER_THIN,
                'color'       => ['rgb' => 'eeeeee'],
            ]
        ],
    ];

    protected $defaultSetting = [
        'cell_width' => 30,
        'font_size'  => 12
    ];

    protected $data = [];


    protected function initialize()
    {
        $this->middleware     = [
            //'csrf',
            'auth' => [
                'except' => ['manager.login/index']
            ]
        ];
        $auth                 = session('auth');
        $this->data['member'] = $auth;
        if (session('?__token__')) {
            $this->data['_token'] = session('__token__');
        } else {
            $this->data['_token'] = $this->request->buildToken();
        }
        $this->data['groupId'] = $auth['groupId'] ?? 0;
    }

    //变量赋值到模板
    protected function view()
    {
        return view()->assign($this->data);
    }

    /**
     * @param  string  $msg
     * @param  string|null  $url
     * @param  string  $data
     * @param  int  $wait
     * @return Redirect
     */
    protected function error($msg = '', string $url = null, $data = '', int $wait = 3): Redirect
    {
        if (is_null($url)) {
            $url = $this->request->isAjax() ? '' : 'javascript:history.back(-1);';
        } elseif ($url) {
            $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : $this->app->route->buildUrl($url);
        }
        $result = [
            'code' => 0,
            'msg'  => $msg,
            'data' => $data,
            'url'  => $url,
            'wait' => $wait,
        ];

        return $this->redirect(url('manager.error/jump', $result));
    }
}
