<?php 
namespace backend\models;

class WithdrawApi extends BaseApi
{
    public $apiUrl = 'withdraw';

    /**
     * 出金列表
     * @param array $data
     * @return array
     */
    public function getWithdrawList($data = [])
    {
        $result = $this->get($this->apiUrl . '/index', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data'], '获取出金记录成功');
        } else {
            return $this->returnArray(0, [], '获取出金记录失败');
        }
    }

    /**
     * 出金申请
     * @param array $data
     * @return array
     */
    public function addWithdraw($data)
    {
        $result = $this->post($this->apiUrl . '/create', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data'], '出金申请成功');
        } else {
            return $this->returnArray(0, [], $result['message']);
        }
    }

    /**
     * 当月出金记录
     * @param int $memberId
     * @return array
     */
    public function getMonthWithdraw($memberId)
    {
        $data = ['memberId' => $memberId];
        $result = $this->get($this->apiUrl . '/get-month-withdraw', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data'], '获取出金记录成功');
        } else {
            return $this->returnArray(0, [], $result['message']);
        }
    }

    /**
     * 统计出金金额 amount+fee
     * @param int $type
     * @return array
     */
    public function sumWithdrawByType($type = null)
    {
        $data['type'] = $type;
        $result = $this->get($this->apiUrl . '/sum-withdraw', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data'], 'SUCCESS');
        } else {
            return $this->returnArray(0, [], 'FAILED');
        }
    }

    /**
     * 统计出金金额 amount
     * @param int $type
     * @return array
     */
    public function sumNotFeeByType($type)
    {
        $data['type'] = $type;
        $result = $this->get($this->apiUrl . '/sum-not-fee', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data'], 'SUCCESS');
        } else {
            return $this->returnArray(0, [], 'FAILED');
        }
    }

    /**
     * 统计出金金额 人民币 amount*rate
     * @param int $type
     * @return array
     */
    public function sumRmbByType($type)
    {
        $data['type'] = $type;
        $result = $this->get($this->apiUrl . '/sum-rmb', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data'], 'SUCCESS');
        } else {
            return $this->returnArray(0, [], 'FAILED');
        }
    }

    /**
     * 出金详情
     * @param int $id
     * @return array
     */
    public function detail($id)
    {
        $data['id'] = $id;
        $result = $this->get($this->apiUrl . '/detail', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data'], 'SUCCESS');
        } else {
            return $this->returnArray(0, [], 'FAILED');
        }
    }

    /**
     * 修改出金记录
     * @param int $id
     * @param array $post
     * @return array
     */
    public function updateWithdraw($id, $post)
    {
        $data = $post;
        $data['id'] = $id;
        $result = $this->post($this->apiUrl . '/update', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data'], 'SUCCESS');
        } else {
            return $this->returnArray(0, [], 'FAILED');
        }
    }

    /**
     * 出金备注
     * @param int $id
     * @param string $memo
     * @return array
     */
    public function updateWithdrawMemo($id, $memo)
    {
        return static::updateWithdraw($id, ['memo' => $memo]);
    }

    /**
     * 出金审核
     * @param int $id
     * @param int $type
     * @param string $admin_name
     * @return array
     */
    public function updateWithdrawType($id, $type, $admin_name = '')
    {
        $data = [
            'type' => $type,
            'admin_name' => $admin_name,
        ];
        return static::updateWithdraw($id, $data);
    }
}