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

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

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

    /**
     * 列表筛选到的数据
     */
    public function dataHandle()
    {
        $this->listData = ShopGoods::lists($this->condition, $this->params, [
            'orderBy' => 'CREATED_AT DESC, ID DESC',
            'from' => ShopGoods::tableName(),
            'page' => $this->page,
            'pageSize' => $this->pageSize,
        ]);
    }

    /**
     * 要展示和导出的所有字段
     * @return array
     */
    public function getColumn(){
        if(!$this->columns){
            $this->columns = [
                'ID' => null,
                'GOODS_NAME' => [
                    'header' => '商品名称',
                    'headerOther' => ['width' => '200'],
                ],
                'GOODS_NO' => [
                    'header' => '商品编号',
                    'headerOther' => ['width' => '200'],
                ],
                'TYPE' => [
                    'header' => '商品类型',
                    'value' => function($row){
                        return ShopGoods::GOODS_TYPE[$row['TYPE']]['name'];
                    },
                    'headerOther' => ['width' => '150'],
                ],
                'SELL_TYPE' => [
                    'header' => '出售方式',
                    'value' => function($row){
                        $sellType = ShopGoods::SALE_TYPE;
                        $sel = explode(',',$row['SELL_TYPE']);
                        $value = '';
                        foreach ($sel as $v){
                            $value.= $sellType[$v]['name'].',';
                        }
                        return $value;
                    },
                    'headerOther' => ['width' => '150'],
                ],
                'SELL_PRICE' => [
                    'header' => '销售价格',
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function ($row) {
                        return Tool::formatPrice($row['SELL_PRICE']);
                    },
                    'valueOther' => function ($row) {
                        return [
                            'tag' => ['type' => 'danger', 'size' => 'small', 'class' => 'no-border']
                        ];
                    },
                ],
                'PRICE_PV' => [
                    'header' => '销售PV',
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function ($row) {
                        return Tool::formatPrice($row['PRICE_PV']);
                    },
                    'valueOther' => function ($row) {
                        return [
                            'tag' => ['type' => 'danger', 'size' => 'small', 'class' => 'no-border']
                        ];
                    },
                ],
                'STORE_NUMS' => [
                    'header' => '库存',
                    'headerOther' => [
                        'width' => '110',
                    ],
                ],
                'STATUS' => [
                    'header' => '状态',
                    'value' => function($row) {
                        return $row['STATUS'] == 1 ? '已上架' : '已下架';
                    },
                    'headerOther' => [
                        'width' => '110',
                    ],
                ],
                'UPDATED_AT' => [
                    'header' => '更新时间',
                    'value' => function ($row) {
                        return (new DateTime([
                            'value' => $row['UPDATED_AT'],
                        ]))->result();
                    },
                    'headerOther' => ['width' => '190'],
                ],
            ];
        }
        return $this->columns;
    }

    /**
     * 前台用于筛选的类型集合
     * @return mixed
     */
    public function getFilterTypes()
    {
        if(!$this->filterTypes){
            $this->filterTypes = [
                'TYPE' => ['isUserTable' => false, 'name' => '商品类型', 'other' => 'select', 'selectData' => [['id' => 1, 'name' => '国内商品'], ['id' => 2, 'name' => '进口商品']]],
                'STATUS'=> ['name'=> '状态', 'other'=> 'select', 'selectData'=> [['id'=> 0, 'name'=> '已下架'],['id'=> 1, 'name'=> '已上架']]],
                'GOODS_NAME'=> ['name'=> '商品名称'],
                'GOODS_NO'=> ['name'=> '商品编号'],
                'SELL_TYPE'=> ['name'=> '出售方式', 'other'=> 'select', 'selectData'=> [['id'=> 1, 'name'=> '余额购买'],['id'=> 2, 'name'=> '积分购买']]],
                'SELL_PRICE'=> ['name'=> '销售价格'],
                'PRICE_PV'=> ['name'=> '销售PV'],
            ];
        }
        return $this->filterTypes;
    }
}