<?php

namespace common\models;

/**
 * This is the model class for table "{{%SHOP_GOODS}}".
 *
 * @property string $ID
 * @property string $GOODS_NAME 商品名称
 * @property string $CATE_ID 所属分类ID
 * @property string $TYPE 商品来源
 * @property string $GIFT_TYPE 商品类型
 * @property string $CATEGORY_TYPE 商品分类
 * @property string $SELL_TYPE 允许购买方式
 * @property int $GOODS_NO 商品编号
 * @property string $UNIT 单位
 * @property string $COVER 商品封面
 * @property int $IMAGES 商品图片
 * @property string $CONTENT 商品内容
 * @property string $SELL_PRICE_STANDARD 标准价格
 * @property int $PRICE_PV 价格PV
 * @property int $POINT 兑换积分
 * @property int $STORE_NUMS 库存
 * @property int $IS_DEL 是否删除
 * @property int $CREATED_AT 创建时间
 * @property int $UPDATED_AT 更新时间
 * @property int $DELETED_AT 删除时间
 * @property int $STATUS 状态
 * @property string $SORT 排序
 * @property int $AUTO_MAINTENANCE AMP
 */
class ShopGoods extends \common\components\ActiveRecord
{
    const SALE_TYPE = [
        1 => [
            'id' => 1,
            'name' => 'Account Balance', //余额购买
            'label' => 'cash',
            'language' => 'shopListAccountBalance',
        ],
//        2 => [
//            'id' => 2,
//            'name' => '复消兑换',
//        ],
//        3 => [
//            'id' => 3,
//            'name' => '兑换点数',
//            'label' => 'exchange',
//        ],
        7 => [
            'id' => 7,
            'name' => 'PayStack',
            'label' => 'pay_stack',
            'language' => 'saleTypePayStack',
        ],
        8 => [
            'id' => 8,
            'name' => 'Performance Bonus',
            'label' => 'prp',
            'language' => 'shopPayTypeUserPerformance',
        ],
    ];

    const GOODS_TYPE = [
        1 =>[
            'name'=>'国内商品',//国内商品
            'discount'=>'50',
            'language' => 'shopGoodTypeDomestic',
        ],
        2 =>[
            'name'=>'进口商品',//进口商品
            'discount'=>'60',
            'language' => 'shopGoodTypeImport',
        ],
    ];
    const GIFT_TYPE = [
        1 =>[
            'name'=>'1st Purchase',//报单区
        ],
        2 =>[
            'name'=>'Repeat Purchase',//复消区
        ],
//        3 =>[
//            'name'=>'Office Entry area',//工作室报单
//        ],
//        4 =>[
//            'name'=>'Office Reselling area',//工作室复消
//        ],
    ];
    const STATUS_NAME = [
        1 => 'On sale',//已上架
        0 => 'Sold out',//已下架
    ];
    const CATEGORY_TYPE = [
        [
            'id' => 1,
            'name' => 'Standard Products',//普通商品
            'sell_type' => [
                self::SALE_TYPE[1],
                self::SALE_TYPE[8],
                self::SALE_TYPE[7],
            ]
        ],
    ];
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%SHOP_GOODS}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['SELL_DISCOUNT','GOODS_NAME', 'CATE_ID','GIFT_TYPE', 'GOODS_NO', 'PRICE_PV','STORE_NUMS', 'SELL_TYPE', 'CATEGORY_TYPE', 'SELL_PRICE_STANDARD', 'AUTO_MAINTENANCE'], 'required'],
            [['STORE_NUMS', 'AUTO_MAINTENANCE'], 'integer'],
            [['PRICE_PV','POINT', 'CATEGORY_TYPE' ,'SELL_PRICE_STANDARD'], 'number'],
            [['ID','CATE_ID', 'GOODS_NO','GIFT_TYPE'], 'string', 'max' => 32],
            [['UNIT'], 'string', 'max' => 16],
            [['COVER', 'GOODS_NAME'], 'string', 'max' => 255],
            [['IMAGES','CONTENT'], 'string', 'max' => 4000],
            [['GOODS_NAME'], 'unique'],
            [['ID'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'ID' => 'ID',
            'GOODS_NAME' => '商品名称',
            'CATE_ID' => '所属分类ID',
            'SELL_DISCOUNT' => '会员折扣',
            'GIFT_TYPE' => '商品类型',
            'CATEGORY_TYPE' => '商品分类',
            'SELL_TYPE' => '出售方式',
            'GOODS_NO' => '商品编号',
            'UNIT' => '商品单位',
            'COVER' => '商品封面',
            'IMAGES' => '商品图片',
            'CONTENT' => '商品内容',
            'SELL_PRICE_STANDARD' => 'US price($)',
            'PRICE_PV' => '价格PV',
            'STORE_NUMS' => '库存',
            'STATUS' => '是否上架',
            'IS_DEL' => '是否删除',
            'CREATED_AT' => '创建时间',
            'UPDATED_AT' => '更新时间',
            'DELETED_AT' => '更新时间',
            'SORT' => '排序',
            'AUTO_MAINTENANCE' => 'AMP'
        ];
    }

    /**
     * 判断提现状态
     * @param $nowStatus
     * @param $toStatus
     * @return string
     */
    public static function chkAuditStatus($nowStatus, $toStatus) {
        $statusName = self::STATUS_NAME;
        $msg = \Yii::t('ctx', 'shopSetProductStatusErrorNotice', [
            'nowStatus' => $statusName[$nowStatus],
            'newStatus' => $statusName[$toStatus]
        ]);
        switch ($toStatus) {
            // 下架
            case 0:
                if ($nowStatus == 1) {
                    $msg = '';
                }
                break;
            //上架
            case 1:
                if ($nowStatus == 0) {
                    $msg = '';
                }
                break;
            default:
        }
        return $msg;
    }

    /**
     * 支付方式
     * @return array
     */
    public static function payTypes(){
        return [
            'cash'=>[
                'name'=> \Yii::t('ctx', 'shopPayTypeCash')//余额支付
            ],
            'prp' => [
                'name' => \Yii::t('ctx', 'shopPayTypeUserPerformance')
            ],
            'pay_stack' => [
                'name' => 'PayStack'
            ],
        ];
    }

    // 获取商品类型
    public static function getGiftType() {
        return [
            1 =>[
                'name'=>\Yii::t('ctx', 'shopList1stPurchase'),//报单区
            ],
            2 =>[
                'name'=>\Yii::t('ctx', 'shopListRepeatPurchase'),//复消区
            ],
//            3 =>[
//                'name'=>\Yii::t('ctx', 'shopListEntryArea'),//工作室报单
//            ],
//            4 =>[
//                'name'=>\Yii::t('ctx', 'shopListResellingArea'),//工作室复消
//            ],
        ];
    }

    // 获取分类类型
    public static function getCategoryType() {
        return [
            [
                'id' => 1,
                'name' => \Yii::t('ctx', 'shopListStandardProducts'),//普通商品
                'sell_type' => [
                    self::SALE_TYPE[1],
                    self::SALE_TYPE[8],
                    self::SALE_TYPE[7],
                ]
            ],
        ];
    }

    public static function getSaleType(): array
    {
        return array_map(function ($item) {
            $item['name'] = \Yii::t('ctx', $item['language']);
            return $item;
        }, self::SALE_TYPE);
    }

    public static function getGoodType(): array
    {
        return array_map(function ($item) {
            $item['name'] = \Yii::t('ctx', $item['language']);
            return $item;
        }, self::GOODS_TYPE);
    }
}
