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

use common\helpers\Tool;
use common\libs\dataList\column\DateTime;
use common\libs\dataList\DataListInterface;
use common\models\DeclarationLevel;
use common\models\DeclarationPackage;

class PackageList extends \common\libs\dataList\DataList implements DataListInterface
{
    /**
     * 列表名称
     * @return string
     */
    public function getListName(){
        return '套餐列表';
    }

    /**
     * 列表筛选到的数据
     */
    public function dataHandle()
    {
        $this->listData = DeclarationPackage::lists($this->condition, $this->params, [
            'select' => 'DP.*,DL.LEVEL_NAME',
            'orderBy' => 'DP.CREATED_AT DESC, DP.ID DESC',
            'from' => DeclarationPackage::tableName(). ' AS DP',
            'join' => [
                ['LEFT JOIN', DeclarationLevel::tableName() . ' AS DL', 'DP.LEVEL_ID=DL.ID'],
            ],
            'page' => $this->page,
            'pageSize' => $this->pageSize,
        ]);
    }

    /**
     * 要展示和导出的所有字段
     * @return array
     */
    public function getColumn(){
        if(!$this->columns){
            $this->columns = [
                'ID' => null,
                'PACKAGE_NAME' => [
                    'header' => '套餐名称',
                    'headerOther' => ['width' => '200'],
                ],
                'PACKAGE_NO' => [
                    'header' => '套餐编号',
                    'headerOther' => ['width' => '200'],
                ],
                'LEVEL_NAME' => [
                    'header' => '所属报单级别',
                    'headerOther' => ['width' => '200'],
                ],
                'AMOUNT' => [
                    'header' => '套餐金额',
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function ($row) {
                        return Tool::formatPrice($row['AMOUNT']);
                    },
                    'valueOther' => function ($row) {
                        return [
                            'tag' => ['type' => 'danger', 'size' => 'small', 'class' => 'no-border']
                        ];
                    },
                ],
                'PV' => [
                    'header' => '套餐PV',
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function ($row) {
                        return Tool::formatPrice($row['PV']);
                    },
                    'valueOther' => function ($row) {
                        return [
                            'tag' => ['type' => 'danger', 'size' => 'small', 'class' => 'no-border']
                        ];
                    },
                ],
                'STATUS' => [
                    'header' => '状态',
                    'value' => function($row) {
                        return $row['STATUS'] == 1 ? '已上架' : '已下架';
                    },
                    'headerOther' => [
                        'width' => '110',
                    ],
                ],
                'PACKAGE_CONTENT' => [
                    'header' => '套餐内容',
                    'headerOther' => ['width' => '290'],
                ],
                'CREATED_AT' => [
                    'header' => '创建时间',
                    'value' => function ($row) {
                        return (new DateTime([
                            'value' => $row['CREATED_AT'],
                        ]))->result();
                    },
                    'headerOther' => ['width' => '190'],
                ],
            ];
        }
        return $this->columns;
    }

    /**
     * 前台用于筛选的类型集合
     * @return mixed
     */
    public function getFilterTypes()
    {
        if(!$this->filterTypes){
            $this->filterTypes = [
                'LEVEL_NAME'=> ['name'=> '所属报单级别', 'other'=> 'decLevel'],
                'PACKAGE_NAME'=> ['name'=> '套餐名称'],
                'AMOUNT'=> ['name'=> '套餐金额'],
                'PV'=> ['name'=> '套餐PV'],
                'STATUS'=> ['name'=> '状态', 'other'=> 'select', 'selectData'=> [['id'=> 1, 'name'=> '已上架'],['id'=> 0, 'name'=> '已下架']]],
            ];
        }
        return $this->filterTypes;
    }
}