<?php

namespace app\admin\service\setting;

use app\admin\service\AdminBaseService;
use app\admin\validate\setting\PaymentValidate;
use app\admin\validate\setting\StockValidate;
use app\model\PaymentListModel;
use app\model\StockListModel;
use PHPMailer\PHPMailer\Exception;
use think\exception\ValidateException;
use think\facade\Validate;

class PaymentService extends AdminBaseService
{

    public function index($param)
    {
        try {
            validate(PaymentValidate::class)->scene('payment_index')->check($param);
            $list = PaymentListModel::order('id', 'desc')->page($param['page'], $param['limit'])->select();
            $total = PaymentListModel::order('id', 'desc')->count();
            $rows = [];
            if (!$list->isEmpty()) {
                $rows = $list->toArray();
            }
            return $this->toData('0', 'SUCCESS', ['list' => $rows, 'total' => $total, 'extend' => ['type_list' => PaymentListModel::TPYE_LIST]]);
        } catch (ValidateException $validateException) {
            // 参数校验失败
            $message = $validateException->getError();
            return $this->toData('1', $message);
        } catch (\Exception $exception) {
            return $this->toData('1', '系统繁忙', []);
        }
    }

    public function add($param)
    {
        try {
            validate(PaymentValidate::class)->scene('payment_add')->check($param);
            // 新增数据
            $payment = new PaymentListModel();
            $payment->country = $param['country'];
            $payment->channel = $param['channel'];
            $payment->pay_type = $param['pay_type'];
            $payment->service_rate = $param['service_rate'];
            $payment->type = $param['type'] ?? 1;
            $payment->status = $param['status'];
            $payment->channel_type = $param['channel_type'];
//            $payment->exchange_rate = $param['exchange_rate'];
            $payment->is_recharge = $param['is_recharge'];
            $payment->is_withdrawal = $param['is_withdrawal'];
            $payment->is_online = $param['is_online'];
            if (isset($param['bank_name'])) {
                $payment->bank_name = $param['bank_name'];
            }
            if (isset($param['bank_branch'])) {
                $payment->bank_branch = $param['bank_branch'];
            }
            if (isset($param['bank_user'])) {
                $payment->bank_user = $param['bank_user'];
            }
            if (isset($param['bank_account'])) {
                $payment->bank_account = $param['bank_account'];
            }
            if (isset($param['wallet_address'])) {
                $payment->wallet_address = $param['wallet_address'];
            }
            if (isset($param['unit'])) {
                $payment->unit = $param['unit'];
            }
            if (isset($param['account_type'])) {
                $payment->account_type = $param['account_type'];
            }
            if (isset($param['ifsc'])) {
                $payment->ifsc = $param['ifsc'];
            }
            if (isset($param['extra_name'])) {
                $payment->extra_name = $param['extra_name'];
            }
            if (isset($param['extra_list'])) {
                $payment->extra_list = $param['extra_list'];
            }
            $payment->save();
            return $this->toData('0', 'SUCCESS', []);

        } catch (ValidateException $validateException) {
            $message = $validateException->getError();
            return $this->toData('1', $message);
        } catch (\Exception $exception) {
            return $this->toData('1', '系统繁忙', [$exception->getMessage()]);
        }
    }


    public function edit($param)
    {
        try {
            validate(PaymentValidate::class)->scene('payment_edit')->check($param);

            $payment_info = PaymentListModel::where('id', $param['id'])->find();
            if (empty($payment_info)) {
                return $this->toData('1', '目标不存在', []);
            }
            $param['type'] = $param['type'] ?? 1;
            PaymentListModel::where('id', $param['id'])->update($param);
            return $this->toData('0', 'SUCCESS', []);

        } catch (ValidateException $validateException) {
            $message = $validateException->getError();
            return $this->toData('1', $message);
        } catch (\Exception $exception) {
            return $this->toData('1', '系统繁忙', [$exception->getMessage()]);
        }
    }

    public function info($param)
    {
        try {
            validate(PaymentValidate::class)->scene('payment_info')->check($param);
            $payment_info = PaymentListModel::where('id', $param['id'])->find();
            if (empty($payment_info)) {
                return $this->toData('1', '目标不存在', []);
            }
            return $this->toData('0', 'SUCCESS', $payment_info->toArray());

        } catch (ValidateException $validateException) {
            $message = $validateException->getError();
            return $this->toData('1', $message);
        } catch (\Exception $exception) {
            return $this->toData('1', '系统繁忙', []);
        }

    }

    public function del($param)
    {
        try {
            validate(PaymentValidate::class)->scene('payment_info')->check($param);
            $payment_info = PaymentListModel::where('id', $param['id'])->find();
            if (empty($payment_info)) {
                return $this->toData('1', '目标不存在', []);
            }
            PaymentListModel::where('id', $param['id'])->delete();
            return $this->toData('0', 'SUCCESS', []);
        } catch (ValidateException $validateException) {
            $message = $validateException->getError();
            return $this->toData('1', $message);
        } catch (\Exception $exception) {
            return $this->toData('1', '系统繁忙', []);
        }

    }
}