<?php
namespace backend\models;

use Yii;
use common\helpers\Utils;

class RemitApi extends BaseApi
{
    public $apiUrl = 'remit';

    /**
     * 获取财务打款审核列表
     * @param array $data
     * @return array
     */
    public function getFinanceRemitList($data = [])
    {
        $result = $this->get($this->apiUrl . '/get-finance-remit-list', $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 financeRemitDetail($data = [])
    {
        $result = $this->get($this->apiUrl . '/finance-remit-detail', $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 memo($data = [])
    {
        $result = $this->get($this->apiUrl . '/memo', $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 memoUpdate($data = [])
    {
        $result = $this->post($this->apiUrl.'/memo-update', $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 importRemitOrder($data = [])
    {
        $result = $this->post($this->apiUrl.'/import-remit-order', $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 getBatchRemitLog($data = [])
    {
        $result = $this->get($this->apiUrl.'/get-batch-remit-log', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data'], '成功');
        } else {
            return $this->returnArray(0, [], $result['message']);
        }
    }

    /**
     * 批次初审
     * @param int $batchNo
     * @param int $auditStatus
     * @param $auditReason
     * @param array $extData
     * @return array
     */
    public function batchAudit($batchNo, $auditStatus, $auditReason, $extData = [])
    {
        $data = [
            'batch_no' => $batchNo,
            'audit_status' => $auditStatus,
            'audit_reason' => $auditReason,
        ];
        isset($extData['audit_admin_id']) && $data['audit_admin_id'] = trim($extData['audit_admin_id']);
        isset($extData['audit_name']) && $data['audit_name'] = trim($extData['audit_name']);
        $data['audit_ip'] = isset($extData['audit_ip']) ? trim($extData['audit_ip']) : Utils::getClientIp();
        $result = $this->post($this->apiUrl . '/batch-audit', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data'], '操作成功');
        } else {
            return $this->returnArray(0, [], $result['message']);
        }
    }

    /**
     * 批次复审
     * @param $batchNo
     * @param $auditStatus
     * @param $auditReason
     * @param array $extData
     * @return array
     */
    public function batchReAudit($batchNo, $auditStatus, $auditReason, $extData = [])
    {
        $data = [
            'batch_no' => $batchNo,
            'audit_status' => $auditStatus,
            'reaudit_reason' => $auditReason,
        ];
        isset($extData['reaudit_admin_id']) && $data['reaudit_admin_id'] = trim($extData['reaudit_admin_id']);
        isset($extData['reaudit_name']) && $data['reaudit_name'] = trim($extData['reaudit_name']);
        $data['reaudit_ip'] = isset($extData['reaudit_ip']) ? trim($extData['reaudit_ip']) : Utils::getClientIp();
        $result = $this->post($this->apiUrl . '/batch-re-audit', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data'], '操作成功');
        } else {
            return $this->returnArray(0, [], $result['message']);
        }
    }

    /**
     * 检查批次号是否存在
     * @param $batch_no
     * @return array
     */
    public function checkBatchNo($batch_no)
    {
        $result = $this->get($this->apiUrl.'/check-batch-no', ['batchNo' => $batch_no]);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data'], '成功');
        } else {
            return $this->returnArray(0, [], $result['message']);
        }
    }

    /**
     * 创建打款单
     * @param array $data
     * @return array
     */
    public function create($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 $remitNo
     * @param int $auditStatus
     * @param array $extData
     * @return array
     */
    public function audit($remitNo, $auditStatus, $extData = [])
    {
        $data = [
            'remit_no' => $remitNo,
            'audit_status' => $auditStatus,
        ];
        isset($extData['audit_admin_id']) && $data['audit_admin_id'] = trim($extData['audit_admin_id']);
        isset($extData['audit_name']) && $data['audit_name'] = trim($extData['audit_name']);
        $data['audit_ip'] = isset($extData['audit_ip']) ? trim($extData['audit_ip']) : Utils::getClientIp();
        $result = $this->post($this->apiUrl . '/audit', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data'], '操作成功');
        } else {
            return $this->returnArray(0, [], $result['message']);
        }
    }

    /**
     * 复审
     * @param int $remitNo
     * @param int $auditStatus
     * @param array $extData
     * @return array
     */
    public function reAudit($remitNo, $auditStatus, $extData = [])
    {
        $data = [
            'remit_no' => $remitNo,
            'audit_status' => $auditStatus,
        ];
        isset($extData['reaudit_admin_id']) && $data['reaudit_admin_id'] = trim($extData['reaudit_admin_id']);
        isset($extData['reaudit_name']) && $data['reaudit_name'] = trim($extData['reaudit_name']);
        $data['reaudit_ip'] = isset($extData['reaudit_ip']) ? trim($extData['reaudit_ip']) : Utils::getClientIp();
        $result = $this->post($this->apiUrl . '/reaudit', $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 outRemit($data)
    {
        $result = $this->post($this->apiUrl . '/outremit', $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 getRemitList($data)
    {
        $result = $this->post($this->apiUrl . '/get-remit-list', $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 updatePayStatus($data)
    {
        $data['admin_id'] = Yii::$app->user->id;
        $data['admin_name'] = Yii::$app->getUser()->getIdentity(false)->name;
        $data['admin_ip'] = (string) Yii::$app->request->getUserIP();
        $result = $this->post($this->apiUrl . '/update-pay-status', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data'], $result['message']);
        } else {
            return $this->returnArray(0, [], $result['message']);
        }
    }
    
    /**
     * 操作日志
     * @param array $data
     * @return array
     */
    public function getRemitLog($data)
    {
        $result = $this->post($this->apiUrl . '/get-remit-log', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data'], $result['message']);
        } else {
            return $this->returnArray(0, [], $result['message']);
        }
    }
}

