<?php

namespace common\models;

use backendApi\modules\v1\models\Admin;
use common\helpers\user\Info;
use Yii;
use common\libs\logging\operate\valueType\Config as ValueTypeConfig;

/**
 * This is the model class for table "{{%BALANCE_AUDIT}}".
 *
 * @property string $ID
 * @property string $USER_ID 会员ID
 * @property string $LAST_DEC_LV 申请时会员级别
 * @property string $LAST_EMP_LV 申请时会员聘级
 * @property string $LAST_DEC_ROLE_ID 申请时报单中心级别
 * @property string $TYPE 余额类型
 * @property string $DEAL_TYPE 交易类型
 * @property string $AMOUNT 金额
 * @property int $AUDIT_STATUS 状态
 * @property string $UPDATE_ADMIN_ID 变更管理员
 * @property string $AUDIT_ADMIN_ID 审核管理员
 * @property string $CREATE_REMARK 创建备注
 * @property string $AUDIT_REMARK 审核备注
 * @property int $REMARK_IS_SHOW 前台是否展示
 * @property int $CREATED_AT 创建时间
 * @property int $AUDITED_AT  审核时间
 */
class BalanceAudit extends \common\components\ActiveRecord
{
    const TYPE = [
        'bonus' => [
            'name' => 'bonus',
            'label' => '会员账户奖金',
        ],
        'reconsume_points' => [
            'name' => 'reconsume_points',
            'label' => '复消积分',
        ],
        'cash' => [
            'name' => 'cash',
            'label' => '会员账户余额',
        ],
//        'cf' => [
//            'name' => 'cf',
//            'label' => '车房养老奖余额',
//        ],
//        'lx' => [
//            'name' => 'lx',
//            'label' => '领袖分红奖余额',
//        ],
    ];

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%BALANCE_AUDIT}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['USER_ID', 'TYPE', 'DEAL_TYPE', 'CREATED_AT'], 'required'],
            [['AMOUNT'], 'number'],
            [['AUDIT_STATUS', 'REMARK_IS_SHOW', 'CREATED_AT', 'AUDITED_AT'], 'integer'],
            [['ID', 'USER_ID', 'LAST_DEC_LV', 'LAST_EMP_LV', 'LAST_DEC_ROLE_ID', 'DEAL_TYPE', 'UPDATE_ADMIN_ID', 'AUDIT_ADMIN_ID'], 'string', 'max' => 32],
            [['TYPE'], 'string', 'max' => 48],
            [['CREATE_REMARK', 'AUDIT_REMARK'], 'string', 'max' => 4000],
            [['ID'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'ID' => 'ID',
            'USER_ID' => '会员ID',
            'LAST_DEC_LV' => '申请时会员级别',
            'LAST_EMP_LV' => '申请时会员聘级',
            'LAST_DEC_ROLE_ID' => '申请时报单中心级别',
            'TYPE' => '余额类型',
            'DEAL_TYPE' => '交易类型',
            'AMOUNT' => '金额',
            'AUDIT_STATUS' => '状态',
            'UPDATE_ADMIN_ID' => '变更管理员',
            'AUDIT_ADMIN_ID' => '审核管理员',
            'CREATE_REMARK' => '创建备注',
            'AUDIT_REMARK' => '审核备注',
            'REMARK_IS_SHOW' => '前台是否展示',
            'CREATED_AT' => '创建时间',
            'AUDITED_AT' => ' 审核时间',
        ];
    }

    /**
     * 操作日志记录条件
     * @return array
     */
    public function attrLabelsWithLogType(){
        return [
            'USER_ID' => '会员ID',
            'TYPE' => [
                'label' => '余额类型',
                'type' => function($data){
                    $value = $data['value'];
                    return \Yii::$app->params['bonusWalletType'][$value]['label'];
                },
            ],
            'DEAL_TYPE' => [
                'label' => '交易类型',
                'type' => function($data){
                    $value = $data['value'];
                    $dealType = DealType::findOneAsArray('ID=:ID', [':ID'=>$value], 'TYPE_NAME');
                    return $dealType['TYPE_NAME'];
                },
            ],
            'AMOUNT' => '金额',
            'AUDIT_STATUS' => [
                'label' => '审核状态',
                'type' => ValueTypeConfig::AUDIT_STATUS_TYPE,
            ],
            'UPDATE_ADMIN_ID' => [
                'label' => '变更管理员',
                'type' => function($data){
                    $value = $data['value'];
                    $result = Admin::findOneAsArray('ID=:ID', [':ID'=>$value], 'ADMIN_NAME');
                    return $result['ADMIN_NAME'];
                },
            ],
            'AUDIT_ADMIN_ID' => [
                'label' => '审核管理员',
                'type' => function($data){
                    $value = $data['value'];
                    $result = Admin::findOneAsArray('ID=:ID', [':ID'=>$value], 'ADMIN_NAME');
                    return $result['ADMIN_NAME'];
                },
            ],
            'CREATE_REMARK' => '备注',
            'REMARK_IS_SHOW' => [
                'label' => '前台是否显示备注',
                'type' => ValueTypeConfig::YES_NO_TYPE,
            ],
            'CREATED_AT' => [
                'label' => '创建时间',
                'type' => ValueTypeConfig::DATE_TIME_TYPE,
            ],
            'AUDITED_AT' => [
                'label' => '审核时间',
                'type' => ValueTypeConfig::DATE_TIME_TYPE,
            ],
        ];
    }
}
