<?php
namespace backend\models;

use Yii;

class CommissionApi extends BaseApi
{
    public $apiUrl = 'commission';

    /**
     * @param array $data
     * @return array
     */
    public function getRule($data)
    {
        $data['member_id']  = Yii::$app->user->id;
        $result = $this->post($this->apiUrl . '/rule', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data']);
        } else {
            return $this->returnArray(0, [], $result['message']);
        }
    }

    /**
     * 批量设置返佣规则
     * @param array $data
     * @return array
     */
    public function batchSet($data)
    {
        $result = $this->post($this->apiUrl . '/batch-set', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data']);
        } else {
            return $this->returnArray(0, [], $result['message']);
        }
    }

    /**
     * 已返佣设置
     * @param int $login
     * @return array
     */
    public function setHadComm($login)
    {
        $data = [
            'login' => $login,
        ];
        $result = $this->post($this->apiUrl . '/set-had-comm', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data']);
        } else {
            return $this->returnArray(0, [], $result['message']);
        }
    }

    /**
     * 开启自动返佣
     * @param int $login
     * @param int $run
     * @return array
     */
    public function setIsCommissionRun($login, $run)
    {
        $data = [
            'login' => $login,
            'run' => $run,
        ];
        $result = $this->post($this->apiUrl . '/set-is-commission-run', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data']);
        } else {
            return $this->returnArray(0, [], $result['message']);
        }
    }

    /**
     * 取消上级代理
     * @param int $login
     * @return array
     */
    public function delIb($login)
    {
        $data = [
            'login' => $login,
        ];
        $result = $this->post($this->apiUrl . '/del-ib', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data']);
        } else {
            return $this->returnArray(0, [], $result['message']);
        }
    }

    /**
     * 设置返佣代理
     * @param int $login
     * @param int $memberId
     * @return array
     */
    public function setIb($login, $memberId)
    {
        $data = [
            'login' => $login,
            'memberId' => $memberId,
        ];
        $result = $this->post($this->apiUrl . '/set-ib', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data']);
        } else {
            return $this->returnArray(0, [], $result['message']);
        }
    }

    /**
     * 获取返佣规则
     * @param $login
     * @return array
     */
    public function getCommission($login)
    {
        $data = [
            'login' => $login,
        ];
        $result = $this->get($this->apiUrl . '/detail', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data']);
        } else {
            return $this->returnArray(0, [], $result['message']);
        }
    }

    /**
     * 设置返佣规则
     * @param int $login
     * @param array $post
     * @return array
     */
    public function setCommission($login, $post)
    {
        $userId = Yii::$app->user->identity->getId();
        $data = $post;
        $data['login'] = $login;
        $data['userId'] = $userId;
        $result = $this->post($this->apiUrl . '/set-commission', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data']);
        } else {
            return $this->returnArray(0, [], $result['message']);
        }
    }

    /**
     * 查看返佣规则记录log
     */
    public function getCommissionLog($data = [])
    {
        $result = $this->get($this->apiUrl . '/get-commission-log', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data']);
        } else {
            return $this->returnArray(0, [], $result['message']);
        }
    }
}