<?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;
use common\models\ShopGoodsNature;

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

    /**
     * 列表筛选到的数据
     */
    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' => \Yii::t('ctx', 'shopListProductName'), // 商品名称
                    'headerOther' => ['width' => '200'],
                ],
                'GOODS_NO' => [
                    'header' => \Yii::t('ctx', 'shopListProductsCode'), // 商品编号
                    'headerOther' => ['width' => '200'],
                ],
                'GIFT_TYPE' => [
                    'header' => \Yii::t('ctx', 'shopListProductsType'), // 商品类型
                    'value' => function($row){
                        $giftType = $this->getGiftType();//ShopGoods::GIFT_TYPE;
                        $gift = explode(',',$row['GIFT_TYPE']);
                        $value = '';
                        foreach ($gift as $v){
                            if($v==0) continue;
                            $value.= $giftType[$v]['name'].',';
                        }
                        return $value;
                    },
                    'headerOther' => ['width' => '150'],
                ],
                'SELL_PRICE_STANDARD' => [
                    'header' => \Yii::t('ctx', 'shopListPriceStandard'), // 标准价格($)
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function ($row) {
                        return Tool::formatPrice($row['SELL_PRICE_STANDARD']);
                    },
                ],
                'PRICE_PV' => [
                    'header' => \Yii::t('ctx', 'shopListProductsBv'), // 产品BV
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function ($row) {
                        return Tool::formatPrice($row['PRICE_PV']);
                    },
                ],
                'STORE_NUMS' => [
                    'header' => \Yii::t('ctx', 'shopListInventory'), // 库存
                    'headerOther' => [
                        'width' => '110',
                    ],
                ],
                'STATUS' => [
                    'header' => \Yii::t('ctx', 'shopListStatus'), // 产品BV
                    'headerOther' => [
                        'width' => '120',
                    ],
                    'value' => function ($row) {
                        return $row['STATUS'] == 1 ? \Yii::t('ctx', 'shopListOnSale') : \Yii::t('ctx', 'shopListSoldOut');
                    },
                    'valueOther' => function ($row) {
                        $tag = ($row['STATUS'] == 1) ? 'success' : 'danger';
                        return [
                            'tag' => ['type' => $tag, 'size' => 'small', 'class' => 'no-border']
                        ];
                    },
                ],
                'AUTO_MAINTENANCE' => [
                    'header' => \Yii::t('ctx', 'shopListAutoMaintenance'),
                    'value' => function($row) {
                        return $row['AUTO_MAINTENANCE'] == 1 ? \Yii::t('ctx', 'yes') : \Yii::t('ctx', 'no');
                    },
                    'headerOther' => [
                        'width' => '110',
                    ],
                ],
            ];
        }
        return $this->columns;
    }

    /**
     * 前台用于筛选的类型集合
     * @return mixed
     */
    public function getFilterTypes()
    {
        if(!$this->filterTypes){
            $this->filterTypes = [
                'GIFT_TYPE' => [
                    'isUserTable' => false, 
                    'name' => \Yii::t('ctx', 'shopListProductsType'), 
                    'other' => 'select', 
                    'selectData' => [
                        ['id' => 1, 'name' => \Yii::t('ctx', 'shopList1stPurchase')], 
                        ['id' => 2, 'name' => \Yii::t('ctx', 'shopListRepeatPurchase')], 
                    ]
                ],
                'STATUS'=> [
                    'name'=> \Yii::t('ctx', 'shopListStatus'), 'other'=> 'select', 
                    'selectData'=> [
                        ['id'=> 0, 'name'=> \Yii::t('ctx', 'shopListSoldOut')],
                        ['id'=> 1, 'name'=> \Yii::t('ctx', 'shopListOnSale')]
                    ]
                ], // 状态，已下架，已上架
                'GOODS_NAME'=> ['name'=> \Yii::t('ctx', 'shopListProductName')], // 商品名称
                'GOODS_NO'=> ['name'=> \Yii::t('ctx', 'shopListProductsCode')], // 商品编号
                'PRICE_PV'=> ['name'=> \Yii::t('ctx', 'shopListProductsBv')], // 销售PV
                'CATEGORY_TYPE' => [
                    'name' => \Yii::t('ctx', 'shopListProductCategory'), 
                    'other'=> 'select', 
                    'selectData'=> $this->getCategoryType()
                ], // 商品分类
            ];
        }
        return $this->filterTypes;
    }

    public function getCategoryType() {
        $saleType = $this->getSaleType();
        return [
            [
                'id' => 1,
                'name' => \Yii::t('ctx', 'shopListStandardProducts'),//普通商品
                'sell_type' => [
                    $saleType[1],
    //                self::SALE_TYPE[3],
                    $saleType[7],
                ]
            ],
    //        [
    //            'id' => 4,
    //            'name' => 'Travel bonus products',//旅游积分商品
    //            'sell_type' => [
    //                self::SALE_TYPE[4]
    //            ]
    //        ],
            [
                'id' => 5,
                'name' => \Yii::t('ctx', 'shopListCarFundProducts'),
                'sell_type' => [
                    $saleType[5]
                ]
            ],
            [
                'id' => 6,
                'name' => \Yii::t('ctx', 'shopListVillaFundProducts'),
                'sell_type' => [
                    $saleType[6]
                ]
            ],
        ];
    }

    public function getSaleType() {
        return [
            1 => [
                'id' => 1,
                'name' => \Yii::t('ctx', 'shopListAccountBalance'), //余额购买
                'label' => 'cash',
            ],
    //        2 => [
    //            'id' => 2,
    //            'name' => '复消兑换',
    //        ],
    //        3 => [
    //            'id' => 3,
    //            'name' => '兑换点数',
    //            'label' => 'exchange',
    //        ],
            4 => [
                'id' => 4,
                'name' => \Yii::t('ctx', 'shopListPayTravelBonus'), // 旅游积分
                'label' => 'tourism_points',
            ],
            5 => [
                'id' => 5,
                'name' => \Yii::t('ctx', 'shopListPayCarFund'), // 车奖积分
                'label' => 'garage_points',
            ],
            6 => [
                'id' => 6,
                'name' => \Yii::t('ctx', 'shopListPayVillaFund'), // 房奖积分
                'label' => 'villa_points',
            ],
            7 => [
                'id' => 7,
                'name' => 'PayStack', // PayStack
                'label' => 'pay_stack',
            ],
            8 => [
                'id' => 7,
                'name' => 'Performance Bonus', // Performance Bonus
                'label' => 'prp',
            ],
        ];
    }

    public function getGiftType() {
        return [
            1 =>[
                'name'=>\Yii::t('ctx', 'shopList1stPurchase'),//报单区
            ],
            2 =>[
                'name'=>\Yii::t('ctx', 'shopListRepeatPurchase'),//复消区
            ],
//            3 =>[
//                'name'=>\Yii::t('ctx', 'shopListEntryArea')//'Office Entry area',//工作室报单
//            ],
//            4 =>[
//                'name'=>\Yii::t('ctx', 'shopListResellingArea')//'Office Reselling area',//工作室复消
//            ],
        ];
    }
}