<?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 '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' => 'Products name',//商品名称
                    'headerOther' => ['width' => '200'],
                ],
                'GOODS_NO' => [
                    'header' => 'Products code',//商品编号
                    'headerOther' => ['width' => '200'],
                ],
                // 'TYPE' => [
                //     'header' => '商品来源',
                //     'value' => function($row){
                //         return ShopGoods::GOODS_TYPE[$row['TYPE']]['name'];
                //     },
                //     'headerOther' => ['width' => '150'],
                // ],
                'SELL_DISCOUNT' => [
                    'header' => 'Products discount',//商品折扣
                    'value' => function($row) {
                        $discount = 0;
                        if ($row['TYPE'] == 1) {
                            $discount = '0.5';
                        } else if($row['TYPE'] == 2) {
                            $discount = '0.6';
                        } else {
                            $discount = $row['SELL_DISCOUNT'];
                        }

                        return $discount;
                    },
                    'headerOther' => ['width' => '150'],
                ],
                'GIFT_TYPE' => [
                    'header' => 'Products type',//商品类型
                    'value' => function($row){
                        $giftType = 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'],
                ],
                'CATEGORY_TYPE' => [
                    'header' => 'Products Category',//商品分类
                    'value' => function($row) {
                        $categoryType = array_column(ShopGoods::CATEGORY_TYPE, NULL, 'id');
                        return $categoryType[$row['CATEGORY_TYPE']]['name'] ?? '';
                    },
                    'headerOther' => ['width' => '150'],
                ],
                'SELL_TYPE' => [
                    'header' => 'Purchase method',//购买方式
                    '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_STANDARD' => [
                    'header' => 'Standard price($)',//标准价格($)
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function ($row) {
                        return Tool::formatPrice($row['SELL_PRICE_STANDARD']);
                    },
                    'valueOther' => function ($row) {
                        return [
                            'tag' => ['type' => 'danger', 'size' => 'small', 'class' => 'no-border']
                        ];
                    },
                ],
                'SELL_PRICE' => [
                    'header' => 'Products Prices(₦)',//商品价格
                    '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' => 'Products BV',//产品BV
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function ($row) {
                        return Tool::formatPrice($row['PRICE_PV']);
                    },
                    'valueOther' => function ($row) {
                        return [
                            'tag' => ['type' => 'danger', 'size' => 'small', 'class' => 'no-border']
                        ];
                    },
                ],
                'TAX_RATE' => [
                    'header' => 'Tax Rate(%)',//税率
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function ($row) {
                        return Tool::formatPrice($row['TAX_RATE']);
                    },
                    'valueOther' => function ($row) {
                        return [
                            'tag' => ['type' => 'danger', 'size' => 'small', 'class' => 'no-border']
                        ];
                    },
                ],
                'TAX' => [
                    'header' => 'Tax(₦)',//税额
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'value' => function ($row) {
                        return Tool::formatPrice($row['SELL_PRICE'] - $row['SELL_PRICE'] / (1 + $row['TAX_RATE'] / 100));
                    },
                    'valueOther' => function ($row) {
                        return [
                            'tag' => ['type' => 'danger', 'size' => 'small', 'class' => 'no-border']
                        ];
                    },
                ],
                'STORE_NUMS' => [
                    'header' => 'Inventory',//库存
                    'headerOther' => [
                        'width' => '110',
                    ],
                ],
                'STATUS' => [
                    'header' => 'STATUS',//状态
                    'value' => function($row) {
                        return $row['STATUS'] == 1 ? 'On sale' : 'Sold out'; //已上架：已下架
                    },
                    'headerOther' => [
                        'width' => '110',
                    ],
                ],
                'UPDATED_AT' => [
                    'header' => 'Update time',//更新时间
                    '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' => '进口商品']]],
                //'GIFT_TYPE' => ['isUserTable' => false, 'name' => '商品类型', 'other' => 'select', 'selectData' => [['id' => 1, 'name' => '报单区'], ['id' => 2, 'name' => '复消区'], ['id' => 3, 'name' => '工作室报单'], ['id' => 4, 'name' => '工作室复消']]],
                'GIFT_TYPE' => ['isUserTable' => false, 'name' => 'Products type', 'other' => 'select', 'selectData' => [['id' => 1, 'name' => 'Entry area'], ['id' => 2, 'name' => 'Reselling area'], ['id' => 3, 'name' => 'Office Entry area'], ['id' => 4, 'name' => 'Office Reselling area']]],
                'STATUS'=> ['name'=> 'Status', 'other'=> 'select', 'selectData'=> [['id'=> 0, 'name'=> 'Sold out'],['id'=> 1, 'name'=> 'On sale']]],//状态，已下架，已上架
                'GOODS_NAME'=> ['name'=> 'Products name'],//商品名称
                'GOODS_NO'=> ['name'=> 'Products code'],//商品编号
//                'SELL_TYPE'=> ['name'=> '复消购买方式', 'other'=> 'select', 'selectData'=> [['id'=> 1, 'name'=> '余额购买'],['id'=> 2, 'name'=> '积分购买']]],
                'SELL_PRICE'=> ['name'=> 'Products Prices'],//销售价格
                'PRICE_PV'=> ['name'=> 'Products BV'],//销售PV
                'CATEGORY_TYPE' => ['name' => 'Products Category', 'other'=> 'select', 'selectData'=> ShopGoods::CATEGORY_TYPE],//商品分类
            ];
        }
        return $this->filterTypes;
    }
}