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

use common\libs\dataList\DataListInterface;
use common\models\Countries;
use common\models\Order;
use common\models\OrderPeriodAdjust;
use common\libs\dataList\column\DateTime;
use Yii;

class OrderPeriodAdjustList extends \common\libs\dataList\DataList implements DataListInterface
{
    /**
     * 列表名称
     * @return string
     */
    public function getListName(){
        return '订单期数调整列表';
    }

    /**
     * 列表筛选到的数据
     */
    public function dataHandle()
    {
        $this->listData = OrderPeriodAdjust::lists($this->condition, $this->params, [
            'select' => 'P.*,O.ORDER_TYPE,O.COUNTRY_ID,C.NAME AS COUNTRY_NAME',
            'orderBy' => 'P.CREATED_AT DESC, P.ID DESC',
            'from' => OrderPeriodAdjust::tableName() . ' AS P',
            'join' => [
                ['LEFT JOIN', Order::tableName() . ' AS O', 'P.ORDER_SN=O.SN'],
                ['LEFT JOIN', Countries::tableName() . ' AS C', 'O.COUNTRY_ID=C.ID'],
            ],
            'page' => $this->page,
            'pageSize' => $this->pageSize,
        ]);
    }

    /**
     * 要展示和导出的所有字段
     * @return array
     */
    public function getColumn(){
        if (!$this->columns) {
            $this->columns = [
                'ID' => null,
                'ORDER_SN' => [
                    'header' => Yii::t('ctx', 'shopOrderListOrderCode'),
                ],
                'ORDER_TYPE' => [
                    'header' => Yii::t('ctx', 'shopOrderListOrderType'),
                    'value' => function ($row) {
                        return $row['ORDER_TYPE'] == 'ZC' ? Yii::t('ctx', 'shopOrderListOrderTypeZc') : Yii::t('ctx', 'shopOrderListOrderTypeFx');
                    },
                ],
                'COUNTRY_NAME' => [
                    'header' => Yii::t('ctx', 'country'),
                ],
                'ORIGIN_PERIOD' => [
                    'header' => Yii::t('ctx', 'beforeAdjustmentPcNo'),
                ],
                'MODERN_PERIOD' => [
                    'header' => Yii::t('ctx', 'afterAdjustmentPcNo'),
                ],
                'CREATED_AT' => [
                    'header' => Yii::t('ctx', 'createAt'),
                    'value' => function ($row) {
                        return (new DateTime([
                            'value' => $row['CREATED_AT'],
                        ]))->result();
                    },
                ],
                'ADMIN_ID' => [
                    'header' => Yii::t('ctx', 'operationAdministrator'),
                ],
            ];
        }
        return $this->columns;
    }

    /**
     * 前台用于筛选的类型集合
     * @return mixed
     */
    public function getFilterTypes()
    {
        if (!$this->filterTypes) {
            $this->filterTypes = [
                'ORDER_SN' => ['name'=> Yii::t('ctx', 'shopOrderListOrderCode')],
                'ORIGIN_PERIOD'=> ['name'=> Yii::t('ctx', 'beforeAdjustmentPcNo')],
                'MODERN_PERIOD'=> ['name'=> Yii::t('ctx', 'afterAdjustmentPcNo')],
                'CREATED_AT'=> ['name'=> Yii::t('ctx', 'createAt'), 'other'=>'date'],
            ];
        }
        return $this->filterTypes;
    }
}