<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\api\controller\dealer;

use app\api\controller\Controller;
use app\api\model\dealer\Apply as DealerApplyModel;
use app\api\model\dealer\User as DealerUserModel;
use app\api\service\User as UserService;
use cores\exception\BaseException;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\response\Json;

/**
 * 分销商申请
 * Class Apply
 * @package app\api\controller\user\dealer
 */
class Apply extends Controller
{
    /**
     * 分销商申请状态
     * @return Json
     * @throws BaseException
     */
    public function status(): Json
    {
        return $this->renderSuccess([
            // 当前是否为分销商
            'isDealer' => DealerUserModel::isDealerUser(UserService::getCurrentLoginUserId()),
            // 当前是否在申请中
            'isApplying' => DealerApplyModel::isApplying(UserService::getCurrentLoginUserId()),
            // 推荐人昵称
            'refereeName' => DealerUserModel::getRefereeName(),
        ]);
    }

    /**
     * 提交分销商申请
     * @return Json
     * @throws BaseException
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function submit(): Json
    {
        $model = new DealerApplyModel;
        if ($model->submit($this->postForm())) {
            return $this->renderSuccess('分销商申请已提交');
        }
        return $this->renderError($model->getError() ?: '提交失败');
    }

    /**
     * @notes:申请服务商
     * @return Json
     * @throws BaseException
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @author: wanghousheng
     */
    public function applyProviders(): Json
    {
        $real_name = $this->request->post('real_name');
        if (!$real_name) {
            return $this->renderError('联系人不能为空');
        }
        $mobile = $this->request->post('mobile');
        if (empty($mobile)) {
            return $this->renderError('联系电话不能为空');
        }
        $shop_type = intval($this->request->post('shop_type', 10));
        $shop_name = $this->request->post('shop_name');
        if (empty($shop_name)) {
            return $this->renderError('店铺名称不能为空');
        }
        $business = $this->request->post('business');
        if (empty($business)) {
            return $this->renderError('经营类型不能为空');
        }
        $city = $this->request->post('city');
        if (empty($city)) {
            return $this->renderError('所在城市不能为空');
        }
        $data = compact('business', 'city', 'real_name', 'mobile', 'shop_type', 'shop_name', 'business', 'city');
        $model = new DealerApplyModel;
        if ($model->applyServiceProviders($data)) {
            return $this->renderSuccess('提交成功');
        }
        return $this->renderError($model->getError() ?: '提交失败');
    }


    /**
     * @notes:更新申请服务商
     * @return Json
     * @throws BaseException
     * @author: wanghousheng
     */
    public function editApplyProviders(): Json
    {
        $real_name = $this->request->post('real_name');
        if (!$real_name) {
            return $this->renderError('联系人不能为空');
        }
        $mobile = $this->request->post('mobile');
        if (empty($mobile)) {
            return $this->renderError('联系电话不能为空');
        }
        $shop_type = intval($this->request->post('shop_type', 10));
        $shop_name = $this->request->post('shop_name');
        if (empty($shop_name)) {
            return $this->renderError('店铺名称不能为空');
        }
        $business = $this->request->post('business');
        if (empty($business)) {
            return $this->renderError('经营类型不能为空');
        }
        $city = $this->request->post('city');
        if (empty($city)) {
            return $this->renderError('所在城市不能为空');
        }
        $data = compact('business', 'city', 'real_name', 'mobile', 'shop_type', 'shop_name', 'business', 'city');
        $model = new DealerApplyModel;
        if ($model->editApplyServiceProviders($data)) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * @notes:申请记录详情
     * @return Json
     * @throws BaseException
     * @author: wanghousheng
     */
    public function getApplyInfo(): Json
    {
        $model = new DealerApplyModel;
        $info = $model->getApplyInfo();
        return $this->renderSuccess(compact('info'));
    }
}