<?php
namespace backendApi\modules\v1\models\lists\bonus;

use common\helpers\Cache;
use common\models\PerfMonth;
use common\models\ScoreMonth;
use common\models\User as modelUser;
use common\libs\dataList\column\Price;
use common\libs\dataList\DataListInterface;
use common\libs\dataList\column\DateTime;
use Yii;

class ScoreMonthList extends \common\libs\dataList\DataList implements DataListInterface
{
    /**
     * 列表名称
     * @return string
     */
    public function getListName(){
        return '月积分列表';
    }

    /**
     * 列表筛选到的数据
     * @throws \yii\base\Exception
     */
    public function dataHandle()
    {
        $data = ScoreMonth::lists($this->condition, $this->params, [
            'select'=>'SM.*, U.USER_NAME, U.REAL_NAME',
            'from' => ScoreMonth::tableName().' AS SM',
            'join' => [
                ['LEFT JOIN', modelUser::tableName() . ' AS U', 'SM.USER_ID=U.ID'],
            ],
            'orderBy' => 'SM.CREATED_AT DESC, SM.ID DESC',
            'page' => $this->page,
            'pageSize' => $this->pageSize,
        ]);
        $this->listData = $data;
    }

    /**
     * 要展示和导出的所有字段
     * @return array
     */
    public function getColumn(){
        if(!$this->columns){
            $this->columns = [
                'CALC_MONTH' => [
                    'header' => 'Bonus Month',//结算月
                    'headerOther' => [
                        'width' => '100',
                    ],
                ],
                'USER_NAME' => [
                    'header' => 'Member Code',//会员编号
                    'headerOther' => [
                        'width' => '110',
                    ],
                ],
                'REAL_NAME' => [
                    'header' => 'Member Name',//会员名字
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                'BASE_SCORE' => [
                    'header' => 'Basic Integral',//基础积分
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['BASE_SCORE'],
                        ]))->result();
                    },
                ],
                'LEVEL_SCORE' => [
                    'header' => 'Level Integral',//级别积分
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['LEVEL_SCORE'],
                        ]))->result();
                    },
                ],
                'UPGRADE_SCORE' => [
                    'header' => 'Upgrade Integral',//升级积分
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['UPGRADE_SCORE'],
                        ]))->result();
                    },
                ],
                'TOTAL_SCORE' => [
                    'header' => 'Total Points',//总积分
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['TOTAL_SCORE'],
                        ]))->result();
                    },
                ],
                'CREATED_AT' => [
                    'header' => 'Creation Time', // 创建时间
                    'value' => function($row) {
                        return (new DateTime([
                            'value' => $row['CREATED_AT'],
                        ]))->result();
                    },
                    'headerOther' => ['width' => '170'],
                ],
            ];
        }
        return $this->columns;
    }

    /**
     * 前台用于筛选的类型集合
     * @return mixed
     */
    public function getFilterTypes()
    {
        if(!$this->filterTypes){
            $this->filterTypes = [
                'CALC_MONTH'=> ['isUserTable'=> false, 'name'=> 'Bonus Month'],//结算月
                'USER_NAME'=> ['isUserTable'=> false, 'name'=> 'Member Code'],//会员编号
                'CREATED_AT'=> ['isUserTable'=> false, 'name'=> 'Creation Time', 'other'=> 'date'],//创建时间
            ];
        }
        return $this->filterTypes;
    }
}