<?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));
    }

    // 根据活动
    protected function getImgSize($categoryId)
    {
        $category      = \app\model\Category::field('id,img_width,img_height,img_width_mobile,img_height_mobile,width,height,width_mobile,height_mobile')->find($categoryId);
        $pcImgSize     = '';//PC组图
        $mobileImgSize = '';//手机组图
        //PC封面图
        $coverSize = $category['img_width'].'*'.$category['img_height'].'px';
        //手机封面图
        $mobileCoverSize = $category['img_width_mobile'].'*'.$category['img_height_mobile'].'px';
        switch ($categoryId) {
            // 品牌活动
            case 39:
                $pcImgSize       = '1360*680px';
                $mobileImgSize   = '365*185px';
                $coverSize       = '340*225px';
                $mobileCoverSize = '185*120px';
                break;
            case 9:
            case 10:
            case 23:
            case 24:
            case 25:
                $pcImgSize     = '1360*680px';
                $mobileImgSize = '365*185px';
                break;
        }

        $this->data['pcImgSize']       = $pcImgSize;
        $this->data['mobileImgSize']   = $mobileImgSize;
        $this->data['coverSize']       = $coverSize ?? '';
        $this->data['mobileCoverSize'] = $mobileCoverSize ?? '';
    }
}
