<?php

namespace app\api\controller;

use app\api\model\wholesaler\Set;
use cores\exception\BaseException;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\response\Json;

class Wholesaler extends Controller
{
    /**
     * @notes:
     * @return Json
     * @throws BaseException
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @author: wanghousheng
     */
    public function price(): Json
    {
        $client = $this->request->post('client');
        if (!$client) {
            return $this->renderError('客户端不能为空');
        }
        $model = new \app\api\service\wholesaler\Wholesaler();
        $list = $model->setClient($client)->center();
        return $this->renderSuccess($list);
    }

    /**
     * @notes:创建订单并支付
     * @return Json
     * @throws BaseException
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @author: wanghousheng
     */
    public function submit(): Json
    {
        $method = $this->request->post('method');
        if (!$method) {
            return $this->renderError('支付方式不能为空');
        }
        $client = $this->request->post('client');
        if (!$client) {
            return $this->renderError('客户端不能为空');
        }
        $priceInfo = Set::detail();
        if ($priceInfo->isEmpty()) {
            return $this->renderError('后台价格未设置');
        }
        $priceInfo = $priceInfo->toArray();
        $username = $this->request->post('username');
        if (!$username) {
            return $this->renderError('姓名不能为空');
        }
        $mobile = $this->request->post('mobile');
        if (!$mobile) {
            return $this->renderError('手机号不能为空');
        }
        $mobile_code = $this->request->post('mobile_code');
        if (!$mobile_code) {
            return $this->renderError('手机号验证码不能为空');
        }
        // 验证短信验证码是否匹配
//        try {
//            CaptchaApi::checkSms($mobile_code, $mobile);
//        } catch (Exception $e) {
//            return $this->renderError($e->getMessage() ?: '短信验证码不正确');
//        }
        $card_front_img_id = intval($this->request->post('card_front_img_id'));
        if (!$card_front_img_id) {
            return $this->renderError('身份证正面不能为空');
        }
        $card_back_img_id = intval($this->request->post('card_back_img_id'));
        if (!$card_back_img_id) {
            return $this->renderError('身份证反面不能为空');
        }
        $license_img_id = intval($this->request->post('license_img_id'));
        if (!$license_img_id) {
            return $this->renderError('营业执照不能为空');
        }
        $card_no = $this->request->post('card_no');
        if (!$card_no) {
            return $this->renderError('身份证号不能为空');
        }
        $door_img_id = intval($this->request->post('door_img_id'));
        if (!$door_img_id) {
            return $this->renderError('门口照片不能为空');
        }
        $province_id = intval($this->request->post('province_id'));
        if (!$province_id) {
            return $this->renderError('省份不能为空');
        }
        $city_id = intval($this->request->post('city_id'));
        if (!$city_id) {
            return $this->renderError('城市不能为空');
        }
        $company_name = $this->request->post('company_name');
        if (!$company_name) {
            return $this->renderError('公司名称不能为空');
        }
        $credit_code = $this->request->post('credit_code');
        if (!$credit_code) {
            return $this->renderError('社会信用代码不能为空');
        }
        $business = $this->request->post('business');
        if (!$business) {
            return $this->renderError('经营类目不能为空');
        }
        $data = compact('company_name', 'credit_code', 'city_id', 'province_id', 'door_img_id', 'business', 'card_no');
        $data = array_merge($data, compact('username', 'mobile', 'card_back_img_id', 'card_front_img_id', 'license_img_id'));
        $data['total_price'] = $priceInfo['price'];
        $data['pay_price'] = $priceInfo['price'];
        $data['year'] = $priceInfo['year'];
        $service = new \app\api\service\wholesaler\Wholesaler();
        $data = $service->setMethod($method)
            ->setClient($client)
            ->orderPay($data);
        return $this->renderSuccess($data, $service->getMessage() ?: '申请成功');
    }

}